<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>vigoo&#39;s software development blog</title>
    <subtitle>Blog posts about projects, ideas and experiments of Daniel Vigovszky (vigoo)</subtitle>
    <link rel="self" type="application/atom+xml" href="https://blog.vigoo.dev/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://blog.vigoo.dev"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-09-29T18:00:00+00:00</updated>
    <id>https://blog.vigoo.dev/atom.xml</id>
    <entry xml:lang="en">
        <title>Making decisions with Jev in Golem</title>
        <published>2026-09-29T18:00:00+00:00</published>
        <updated>2026-09-29T18:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/jev-and-golem/"/>
        <id>https://blog.vigoo.dev/posts/jev-and-golem/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/jev-and-golem/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Two weeks ago &lt;a rel=&quot;external&quot; href=&quot;https://typesafe.ai/blog/introducing-system-one-models-and-jev&quot;&gt;TypeSafe AI introduced Jev&lt;/a&gt; and for a while everyone seemed to talk about it, showing more and more interesting use cases. In this post I&#39;m not trying to find the most exciting new application for this AI model; instead I&#39;m going to show how easy it is to introduce it to a &lt;a rel=&quot;external&quot; href=&quot;https://golem.cloud&quot;&gt;Golem&lt;/a&gt; application (just like into any other TypeScript project), and use this opportunity to talk more about Golem. Why would you use Jev in a Golem application? Why would you use Golem for something like the demo we will look into?&lt;/p&gt;
&lt;h2 id=&quot;the-use-case&quot;&gt;The use case&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Jev&lt;/strong&gt; is basically a fast general purpose classifier - and it can be used for arbitrary tasks without training it first. &lt;strong&gt;Golem&lt;/strong&gt; is a platform for running agents - where agents are stateful, can communicate with each other, and can use LLMs or other AI (or non-AI) services in a fault tolerant and very safe, sandboxed environment.&lt;/p&gt;
&lt;p&gt;A simple use case I came up with for this experiment is to use &lt;strong&gt;Jev&lt;/strong&gt; as a filter and router among Golem agents. We are going to build a log ingress that maintains a sliding window of logs, and for each log line it asks Jev to classify it based on a configurable set of categories (for example security incident, service outage, data loss, performance degradation or none of these). Then we wait a bit more to acquire the surroundings of a matched log, and we forward it, together with a configurable amount of surrounding logs, to one or more other agents corresponding to the categories the classifier selected. This per-category agent then summarizes the event using an LLM and stores it in an event log.&lt;/p&gt;
&lt;img src=&quot;/images/jev-golem-concept.svg&quot; alt=&quot;Conceptual flow from HTTP log ingress through Jev classification to per-category incident registries&quot; width=&quot;1600&quot; height=&quot;600&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;h2 id=&quot;building-it&quot;&gt;Building it&lt;/h2&gt;
&lt;p&gt;In the past I really enjoyed explaining step by step in blog posts like this how to build something using a given technology. I don&#39;t think it makes too much sense anymore - nobody is going to follow it step by step, and coding agents can learn the &lt;em&gt;how&lt;/em&gt; from the source code (Golem itself is open-source) and the &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/posts/golem15-part9-skills/&quot;&gt;provided skills&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In this particular case I just took the last pre-release version of Golem (&lt;strong&gt;Golem 1.6&lt;/strong&gt; is to be released within a few weeks!) and created a new Golem application using the &lt;a rel=&quot;external&quot; href=&quot;https://effect.website&quot;&gt;Effect.ts&lt;/a&gt; template provided by the &lt;code&gt;golem&lt;/code&gt; CLI:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ golem new jev-demo&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;? Select languages:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  TypeScript&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt; Effect&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  Rust&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  Scala&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  MoonBit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;? Select templates:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt; [x] effect: A simple Effect agent implementing a durable counter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  [ ] effect/streaming: A runnable Effect Durable Streams agent with HTTP routes, external streams, and forks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Finished applying template(s) [OK]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then I just gave a prompt similar to the following to a coding agent:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Let&#39;s remove the counter example and build a demo. First I want you to understand how Jev can be used (https://typesafe.ai) and in addition to that, how to use it from within Effect.ts.&lt;/p&gt;
&lt;p&gt;What I want to build is the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We will have a durable ingress agent that continuously receives log lines (structured json documents). The agent&#39;s state will be a circular buffer holding always just the last 100 lines (configurable through agent config).&lt;/li&gt;
&lt;li&gt;We will also have an ephemeral facade agent that takes a plain-text log and parses timestamp / content out of it and forwards to the durable ingress&lt;/li&gt;
&lt;li&gt;Every time the durable ingress gets a new line, we will call Jev to classify it among a number of watched preconfigured categories (+ none of them). If there is a match, we remember it and whenever it gets to the middle of the circular buffer OR a given TTL expired, we forward it and the whole surrounding circular buffer to an inner durable agent that&#39;s keyed by the category name.&lt;/li&gt;
&lt;li&gt;Maybe we should use a second round of jev-based filtering on the buffer before we send it and keep only the ones it flags. Not sure&lt;/li&gt;
&lt;li&gt;The per-category actors receive one &quot;incident&quot; with the surrounding logs, and run an LLM (using effect.ai) to summarize the incident based on the logs, then stores it in its own state keyed by timestamp. It should also have a method to query the stored incidents. As these queries cannot interleave with the LLM generation, we need some trick here, maybe use the read-only method feature to cache results, or spawn a sub-agent. Let&#39;s discover what&#39;s best.&lt;/li&gt;
&lt;li&gt;All features must be exposed via HTTP&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&#39;s not use stream types / durable streams yet - we want to demonstrate that capability separately.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I gave some pointers to it about how I imagine our architecture but intentionally did not give it too much detail - I was curious what kind of solution it would come up with for the interleaving problem I mentioned, for example. Using read-only methods, that I hinted in the prompt, is actually not a good solution in this case but I wanted to see how it answers.&lt;/p&gt;
&lt;p&gt;From this the agent created, deployed and tested a fully working Golem application implementing my use case correctly, with basically no further interaction needed. To test it I asked the agent to download some example log archives, run Golem locally, send sections of this downloaded archive to the ingress and then examine the various incident categories through our application&#39;s HTTP API.&lt;/p&gt;
&lt;h2 id=&quot;architecture&quot;&gt;Architecture&lt;/h2&gt;
&lt;p&gt;So instead of focusing on how it built it, let&#39;s start from the end result. The resulting architecture looks like this:&lt;/p&gt;
&lt;img src=&quot;/images/jev-golem-architecture.svg&quot; alt=&quot;Architecture of the Jev and Golem log incident processing demo&quot; width=&quot;1600&quot; height=&quot;880&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;h3 id=&quot;understanding-the-components&quot;&gt;Understanding the components&lt;/h3&gt;
&lt;p&gt;The left section contains three categories of the public HTTP API we implemented:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It is possible to post a plain text log line&lt;/li&gt;
&lt;li&gt;Or post a structured log JSON&lt;/li&gt;
&lt;li&gt;A few GET endpoints provide access to the collected incident summaries&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In Golem we don&#39;t have to write HTTP endpoint definitions and routing separately (although we can for custom cases) - they can be directly defined on the &lt;strong&gt;agents&lt;/strong&gt; that are going to be serving the requests.&lt;/p&gt;
&lt;p&gt;The middle section contains our agent types, with arrows representing the message flow between them.&lt;/p&gt;
&lt;p&gt;The right section represents the 3rd party AI providers we are going to connect to - in this example it&#39;s TypeSafe AI&#39;s Jev and OpenAI.&lt;/p&gt;
&lt;h4 id=&quot;agent-types&quot;&gt;Agent types&lt;/h4&gt;
&lt;p&gt;Before talking about all the 4 agent types on the above diagram, take a note of the labels on them: &lt;strong&gt;ephemeral&lt;/strong&gt; and &lt;strong&gt;durable&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ephemeral agents&lt;/strong&gt; are stateless, one-shot entities, perfect for example for request handlers of an HTTP API. They are fast and an arbitrary number of them can run in parallel, but they are not fault tolerant - they are designed for short-living, retry-able units of work.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Durable agents&lt;/strong&gt; are the default, and they represent one of Golem&#39;s main powers. Durable agents can run arbitrarily long and they survive crashes, redeployments, scaling and so on. Durable agents support &lt;em&gt;idempotency keys&lt;/em&gt; and automatically provide agent-to-agent communication with &lt;em&gt;exactly-once&lt;/em&gt; calling semantics - if an agent A calls an agent B, you can be sure that it is going to happen, and the call is not going to reach B twice even during outages. Another nice property of durable agents - which enables the fault tolerance we&#39;ve just talked about - is that they can be &lt;em&gt;suspended&lt;/em&gt; when they are idle. The runtime can remove them completely from memory, and still they are ready to be restored (by a new invocation, by a timer, etc) and they just continue running from where they were.&lt;/p&gt;
&lt;p&gt;An interesting feature emerges from the above properties - if it can be suspended and resumed, and &quot;survives anything&quot; (crashes, redeployments, etc...), we can simply store arbitrary state in an agent&#39;s memory (in regular variables) and there is no need to synchronize data into a database. This is something we are going to take advantage of in our demo!&lt;/p&gt;
&lt;p&gt;Another important property of durable agents is that their invocations are processed &lt;em&gt;sequentially&lt;/em&gt; and they cannot overlap. Maybe surprisingly this no-overlap constraint also includes promises. The invocation is only considered done when all promises have been resolved (or cancelled). This is a limitation that we probably will resolve in future versions. I actually like it though, as it also makes it much easier to understand how the whole system is executing. As we will see, this limitation requires some attention in some cases to make sure things are not blocked in the invocation queues.&lt;/p&gt;
&lt;h4 id=&quot;categoryincidents-agent&quot;&gt;CategoryIncidents agent&lt;/h4&gt;
&lt;p&gt;Let&#39;s start with the &lt;strong&gt;CategoryIncidents&lt;/strong&gt; agent. One &lt;strong&gt;instance&lt;/strong&gt; of this agent will represent the incidents of &lt;strong&gt;a given category&lt;/strong&gt; for &lt;strong&gt;a given log source&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;For example if we have a log source &lt;code&gt;payment-service&lt;/code&gt; and three categories (&lt;code&gt;security-incident&lt;/code&gt;, &lt;code&gt;data-loss&lt;/code&gt;, &lt;code&gt;service-outage&lt;/code&gt;), then the following &lt;strong&gt;agent IDs&lt;/strong&gt; are going to identify different instances of the same type:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;CategoryIncidents(&quot;payment-service&quot;, &quot;data-loss&quot;)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CategoryIncidents(&quot;payment-service&quot;, &quot;service-outage&quot;)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CategoryIncidents(&quot;account-service&quot;, &quot;service-outage&quot;)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The agent itself is going to be a simple agent, not having any AI enabled functionalities - just storing a list of incidents in its own state, and providing HTTP bindings to query that. When using the Effect.ts SDK for Golem, we define our data types used in our agents (whether it&#39;s on the public API or to store the state, etc.) using &lt;strong&gt;Effect Schema&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; .. more domain data types defined with Effect Schema ..&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; Incident&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Struct&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  context&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; IncidentContext&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  status&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Literals&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;pending&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;summarized&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;failed&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  summary&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;NullOr&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  error&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;NullOr&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Incident&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-7&quot;&gt; typeof&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Incident&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Type&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; CategoryState&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Struct&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  source&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  category&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  incidents&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Array&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;Incident&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CategoryState&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-7&quot;&gt; typeof&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; CategoryState&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Type&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our agent&#39;s state will store the &lt;code&gt;source&lt;/code&gt; and &lt;code&gt;category&lt;/code&gt; (the pair that identifies the agent) and an array of &lt;code&gt;Incident&lt;/code&gt;s, each storing the log lines belonging to the incident, and the LLM-based summarization (to be explained later).&lt;/p&gt;
&lt;p&gt;The agent itself defines not only its name, identity and invokable methods, but also the public HTTP API it is bound to:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; CategoryIncidents&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; defineAgent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  name&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;CategoryIncidents&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  description&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Durable incident store for one source and watched category&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  mode&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;durable&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; source&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; category&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  http&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Http&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;mount&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/categories/{source}/{category}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  snapshotting&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Snapshot&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;define&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    schema&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; CategoryState&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    policy&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Snapshot&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;policy&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;everyN&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;10&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  methods&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    list&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; method&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      input&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      success&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Array&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;Incident&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      http&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Http&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/incidents&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    receive&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; method&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      input&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; incident&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; IncidentContext&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      success&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Boolean&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    complete&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; method&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      input&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; incidentId&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; summary&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      success&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Boolean&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    fail&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; method&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      input&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; incidentId&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; error&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      success&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Boolean&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is just the &lt;em&gt;definition&lt;/em&gt; of our agent - we need to actually &lt;em&gt;implement&lt;/em&gt; each method as well. It&#39;s pretty simple, just storing data in our agent&#39;s state, backed by an Effect.ts &lt;code&gt;Ref&lt;/code&gt;, for example:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;CategoryIncidents&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;implement&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-10&quot;&gt;Ref&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Ref&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;CategoryState&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  init&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; source&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; category&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Ref&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;make&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;CategoryState&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; source&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; category&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; incidents&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  methods&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;state&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    list&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Ref&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;state&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;pipe&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Effect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; incidents&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; incidents&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  snapshot&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Snapshot&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;ref&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;CategoryState&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This agent demonstrates how the durability feature can be used to build agents corresponding to domain entities, simply storing their data in their own state. The &lt;em&gt;snapshotting&lt;/em&gt; feature (appears in both the agent definition and the implementation) is optional - Golem can preserve the agent&#39;s state even without it, but our agent was right to use it as it makes recovery from suspended state much faster.&lt;/p&gt;
&lt;h4 id=&quot;summarizationjob-agent&quot;&gt;SummarizationJob agent&lt;/h4&gt;
&lt;p&gt;The behavior we want from &lt;code&gt;CategoryIncidents&lt;/code&gt; is that when it &lt;code&gt;receive&lt;/code&gt;s a new incident, it uses an LLM to summarize it before storing the final incident in the incident list. (We can still store the non-summarized version in the incident list temporarily, so there is no delay in incident queries). The summarization, however, is a slow operation involving sending one or more requests to a 3rd party AI API. This is one of the cases where the &lt;strong&gt;sequential, non-overlapping invocation processing&lt;/strong&gt; has to be dealt with. If we simply start the (async!) summarization in the &lt;code&gt;receive&lt;/code&gt; call, our agent is not going to process any other invocations until it is fully done, including the summarization. For subsequent incoming &lt;code&gt;receive&lt;/code&gt; calls this is not a problem - we want to process them one by one anyway. But it also means our agent cannot create responses for the incident listing endpoint as it&#39;s bound to invoking the &lt;code&gt;list&lt;/code&gt; method!&lt;/p&gt;
&lt;p&gt;Our coding agent used a simple trick to avoid this problem - spawning another agent for each &lt;code&gt;receive&lt;/code&gt;ed incident, that runs in parallel to our &lt;code&gt;CategoryIncidents &lt;/code&gt;agent and just sends back the summarization result to it when it is done.&lt;/p&gt;
&lt;p&gt;This agent shows us a couple of new features of Golem.&lt;/p&gt;
&lt;p&gt;First of all, the LLM call requires an API key (and an API URL and model name). We can define this required configuration using Golem&#39;s &lt;code&gt;defineConfig&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SummarizationConfig&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; defineConfig&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;SummarizationJob.Config&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  openAiApiUrl&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  openAiModel&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  openAiApiKey&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Redacted&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then in the &lt;em&gt;agent definition&lt;/em&gt; we just add it as a property:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;defineAgent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    config&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; SummarizationConfig&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and in the &lt;em&gt;agent implementation&lt;/em&gt; we can just get it from our Effect&#39;s dependencies:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; config&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; SummarizationConfig&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then the implementation of our summarization agent&#39;s &lt;code&gt;run&lt;/code&gt; method just uses the &lt;code&gt;@effect/ai-openai&lt;/code&gt; package to call OpenAI with the configured URL, token and model, with a prompt constructed from the log lines passed to &lt;code&gt;run&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;run&lt;/code&gt; method is defined like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;defineAgent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    methods&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      run&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; method&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        input&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; incident&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; IncidentContext&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        success&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Void&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And the interesting feature that &lt;code&gt;defineAgent&lt;/code&gt; provides is a fully type-safe way to &lt;strong&gt;invoke an agent from another agent&lt;/strong&gt;. Here is how &lt;code&gt;receive&lt;/code&gt; in the &lt;code&gt;CategoryIncidents&lt;/code&gt; agent&#39;s implementation triggers the summarization:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; job&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; SummarizationJob&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;client&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  source&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; current&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;source&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  category&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; current&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;category&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  incidentId&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; incident&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;id&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; job&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;run&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;trigger&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; incident&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;get&lt;/code&gt; means &quot;get or create&quot; - the parameters passed are the constructor parameters for our &lt;code&gt;SummarizationJob&lt;/code&gt; agent, and they uniquely identify an agent instance. This means there is going to be &lt;strong&gt;one summarization job per incident&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Then we &lt;strong&gt;trigger&lt;/strong&gt; the run method, passing the incident. Triggering means we put the invocation in the other agent&#39;s invocation queue (with guaranteed delivery) and then continue - so we are not blocking our sender agent with the summarization job.&lt;/p&gt;
&lt;p&gt;When the summarization is done, the other agent uses the same technique to send back the summarization result:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; categoryStore&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; CategoryIncidents&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;client&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  source&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; current&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;source&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  category&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; current&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;category&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; categoryStore&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;complete&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  incidentId&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; current&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;incidentId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  summary&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; result&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;text&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here our agent wrote &lt;code&gt;.complete&lt;/code&gt; and not &lt;code&gt;.complete.trigger&lt;/code&gt; - this is the variant that awaits the remote invocation&#39;s completion. In this particular case it is not making a lot of difference, but helps a little with debugging our application&#39;s state - the summarization job&#39;s &lt;strong&gt;status&lt;/strong&gt; field (in its own state) only becomes &lt;code&gt;completed&lt;/code&gt; when it knows the &lt;code&gt;CategoryIncidents&lt;/code&gt; agent already received the summary.&lt;/p&gt;
&lt;h5 id=&quot;read-only-methods&quot;&gt;Read-only methods&lt;/h5&gt;
&lt;p&gt;In the agent prompt I mentioned read-only methods to the agent as a possible way to deal with the problem of not being able to return an incident list while summarization is running. Read-only methods cache their values (with some configurable expiration) and if the cache is hot, it requires no actual invocation to serve the HTTP request bound to them.
This is a useful optimisation technique but it would not solve our problem reliably here - it is not guaranteed that the cache is valid, so from time to time external requests trying to query the incident list would block on ongoing summarizations, if we would not have this separate &lt;code&gt;SummarizationJob&lt;/code&gt; agent.&lt;/p&gt;
&lt;h4 id=&quot;logingress-agent&quot;&gt;LogIngress agent&lt;/h4&gt;
&lt;p&gt;The &lt;code&gt;LogIngress&lt;/code&gt; agent is where we integrate &lt;strong&gt;Jev&lt;/strong&gt; in our application. It is a durable agent, with one instance per log source. The agent&#39;s state is a sliding window of observed structured log lines, and observed incidents. When we detect an incident with &lt;strong&gt;Jev&lt;/strong&gt; we are not immediately sending it to our &lt;code&gt;CategoryIncidents&lt;/code&gt; agent. First we wait a bit for follow-up log lines. We send it either when the classified log line is in the middle of our buffer, or after a configurable TTL (to make sure we handle the matches even if no follow-up log lines are arriving).&lt;/p&gt;
&lt;p&gt;This is an example of having a more complicated in-memory state, where Golem&#39;s &lt;strong&gt;durable execution&lt;/strong&gt; guarantees make this very easy to implement. Imagine writing this on a traditional runtime. We would have to persist the log lines, deal with throwing away old ones (or choose a database that supports such a construct), we would separately have to persist the candidates until we are ready to send them to the incident store agents, and so on. With Golem we don&#39;t have to do any of that - just simple in-memory state, guaranteed to never get lost:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; PendingMatch&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Struct&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  category&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  probability&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Number&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  matchedAt&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  matchedSequence&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Number&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; LogIngressState&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Struct&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  source&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  nextSequence&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Number&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  lines&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Array&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;SequencedLogLine&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  pending&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Array&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;PendingMatch&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LogIngressState&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-7&quot;&gt; typeof&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; LogIngressState&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Type&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Sending a pending incident to the other agent works with the same generated type-safe client interface as the one we&#39;ve already seen. The interesting part of this agent is how we use &lt;strong&gt;Jev&lt;/strong&gt; for routing among the agents, and how we deal with the TTL.&lt;/p&gt;
&lt;p&gt;To ask &lt;strong&gt;Jev&lt;/strong&gt;, we can use the official Effect.ts library &lt;code&gt;&quot;@effect/ai-typesafe&quot;&lt;/code&gt;. We store the classifier configuration itself in the agent&#39;s configuration:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LogIngressConfig&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; defineConfig&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;LogIngress.Config&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  bufferSize&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; WitTypes&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Int32&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  contextTtlSeconds&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; WitTypes&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Int32&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  jevApiUrl&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  jevModel&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  categories&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Array&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;WatchedCategory&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  typesafeApiKey&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Redacted&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We&#39;ve already seen this where we specified the OpenAI API keys, model and URL. The nice thing about Golem&#39;s configuration support is not only that it provides these values through the Effect.ts SDK as a layer; it also &lt;strong&gt;ensures&lt;/strong&gt; that all the configuration is present at deploy-time, and guarantees that it is type safe. In addition to that, secrets (&lt;code&gt;Schema.Redacted(...)&lt;/code&gt; in Effect.ts) are treated as secrets on the Golem deployment level too. They are redacted everywhere, and Golem provides special APIs and CLI commands to &lt;strong&gt;rotate them&lt;/strong&gt; without redeploying your application.&lt;/p&gt;
&lt;p&gt;In the &lt;code&gt;golem.yaml&lt;/code&gt; (our application&#39;s Golem specific manifest) we can specify our agent&#39;s default configuration and the initial values for the secrets (&lt;em&gt;initial&lt;/em&gt;, because they can be rotated later):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;gents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  L&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ogIngress&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;onfig&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      b&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ufferSize&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 100&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ontextTtlSeconds&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 30&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      j&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;evApiUrl&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;https://api.typesafe.ai/v1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      j&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;evModel&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;jev-latest&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ategories&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ame&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ecurity-incident&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;escription&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Authentication attacks, unauthorized access, suspicious activity, or security policy violations&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;hreshold&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0.65&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ame&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ervice-outage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;escription&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;A service is unavailable, unhealthy, repeatedly crashing, or failing requests&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;hreshold&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0.65&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ame&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ata-loss&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;escription&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Data was deleted, corrupted, lost, or cannot be recovered&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;hreshold&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0.65&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ame&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;erformance-degradation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;escription&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Latency, saturation, resource pressure, or throughput is materially worse than normal&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;hreshold&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0.65&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ecretDefaults&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ocal&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ypesafeApiKey&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{{ TYPESAFE_API_KEY }}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    o&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;penAiApiKey&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{{ OPENAI_API_KEY }}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The above secret syntax tells Golem to use the &lt;code&gt;TYPESAFE_API_KEY&lt;/code&gt; and &lt;code&gt;OPENAI_API_KEY&lt;/code&gt; environment variables from the machine we are deploying from.&lt;/p&gt;
&lt;p&gt;Getting the above config from our agent&#39;s dependencies and using the &lt;strong&gt;Jev&lt;/strong&gt; API is straightforward:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; config&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; LogIngressConfig&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ... extract decisions and other properties from config&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; definition&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Decision&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;make&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; LogDecisionInput&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; decisions&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; clientLayer&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; TypeSafeClient&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;layer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; apiKey&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; apiUrl&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;pipe&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;  Layer&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;provide&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;FetchHttpClient&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;layer&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; modelLayer&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; TypeSafeDecisionModel&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;layer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; model&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;pipe&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;  Layer&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;provide&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;clientLayer&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; response&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; DecisionModel&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;decide&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;definition&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  input&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    timestamp&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; line&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;timestamp&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    content&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; line&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;content&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    attributes&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Record&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;fromIterableWith&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;line&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;attributes&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; key&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; value&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;key&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;pipe&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;  Effect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;provide&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;modelLayer&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can call &lt;code&gt;decide&lt;/code&gt; for each incoming log-line and get back probabilities of matching each configured category. We convert this to a list of category matches - if the list is empty, it did not match &lt;em&gt;any&lt;/em&gt; of our classifier categories. Then we store the matches in our agent&#39;s in-memory state, and check if any previously pending items are now ready to be sent (when they are in the &quot;middle&quot; of our sliding window). Sending them is trivial - just one more agent-to-agent call using the typed client that comes free from &lt;code&gt;defineAgent&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The only new thing here is how to deal with the TTL; what if we have a match, but then nobody sends a new log line ever again - we would have the pending match pending forever. Spawning a timer purely in the JavaScript (Effect.ts) world is not an option because of the requirement that invocations cannot overlap - if we still had a pending promise at the end of &lt;code&gt;receive&lt;/code&gt;, new log lines would never arrive. But we also use the &lt;em&gt;clients&lt;/em&gt; defined by &lt;code&gt;defineAgent&lt;/code&gt; to &lt;strong&gt;schedule&lt;/strong&gt; an invocation in the future, and we can even do that on ourselves!&lt;/p&gt;
&lt;p&gt;Here is what this looks like in code:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; LogIngress&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;client&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; source&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; match&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; of&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; newPending&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;flushPending&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;schedule&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    scheduledAt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; ttlSeconds&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1_000&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; matchId&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; match&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;id&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;plaintextlogfacade-agent&quot;&gt;PlainTextLogFacade agent&lt;/h4&gt;
&lt;p&gt;The last agent from our diagram is an &lt;strong&gt;ephemeral agent&lt;/strong&gt;. It has no state, and Golem spawns a fresh instance for each invocation - in our case, for each incoming POST request on the plain-text log ingress endpoint.
In our demo there is no serious reason to have this log parsing in its own ephemeral agent - the reason I wanted to have it is to demonstrate how ephemeral agents can be used as the implementation of HTTP APIs while they can still communicate with the underlying durable agents.
We can argue for using ephemeral agents as the plain-text parsers to avoid the computation cost of parsing the log lines on the &lt;code&gt;LogIngress&lt;/code&gt; agent itself.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Adding &lt;strong&gt;Jev&lt;/strong&gt; based decisions to a Golem application is trivial. You can use it to control agent loops, make routing decisions between multiple agents, and so on.
This post used the &lt;em&gt;Effect.ts SDK&lt;/em&gt; for Golem, but you can also write Golem applications in plain TypeScript, in Rust, Scala and MoonBit.&lt;/p&gt;
&lt;p&gt;The above example was built using a pre-release version of &lt;strong&gt;Golem 1.6&lt;/strong&gt;. The final version is coming out soon. We did not really use any 1.6 features in it though - everything described here is possible using the current stable Golem 1.5 release, except that its Effect.ts SDK depends on an &lt;em&gt;older&lt;/em&gt; version of Effect.ts with no &lt;code&gt;@effect/ai-typesafe&lt;/code&gt; package, so we would have had to write our Jev client for ourselves using Effect&#39;s HTTP client API.&lt;/p&gt;
&lt;p&gt;The whole demo is available &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/jev-demo&quot;&gt;on GitHub&lt;/a&gt;.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Code review in the Agentic world</title>
        <published>2026-09-15T15:00:00+00:00</published>
        <updated>2026-09-15T15:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/code-review-in-the-agentic-world/"/>
        <id>https://blog.vigoo.dev/posts/code-review-in-the-agentic-world/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/code-review-in-the-agentic-world/">&lt;h2 id=&quot;bottleneck&quot;&gt;Bottleneck&lt;/h2&gt;
&lt;p&gt;As everyone nowadays, I am experiencing our usual code review process to be more and more a bottleneck. As several parallel tasks are being developed even by a single developer, it is impossible to keep up with the large number of opened PRs with manual review - even though it is helped by agents - and the non-merged pull requests are often hurting productivity on the follow-up work; some of those unmerged changes are prerequisites to follow-up tasks, or just change some common code that is going to be an extra work to be rebased later. Sometimes the changes in unmerged PRs are relevant for even just &lt;em&gt;planning&lt;/em&gt; some of the next steps. One thing you can do then is to show the open PRs to the agent working on the planning session, convincing it to treat them as &lt;em&gt;done&lt;/em&gt; and available and not something that&#39;s coming later. All this friction is reducing the performance gain we otherwise get from using coding agents.&lt;/p&gt;
&lt;p&gt;Things like stacking PRs can help a bit in some long-running epics where follow-up tasks are strictly depending on each other, and where it does not really matter if they get merged one by one or all together - but it&#39;s not helping with many relatively independent small tasks, and they can also leads to a lot of pain if more than one such big long-running stack is going on simultaneously. And that seems to be inevitable even with such a small team as ours.&lt;/p&gt;
&lt;h2 id=&quot;why-we-ask-for-code-reviews&quot;&gt;Why we ask for code reviews?&lt;/h2&gt;
&lt;p&gt;Why did we make code reviews a mandatory part of our development process? There are a few reasons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Most importantly to increase the &lt;strong&gt;quality and correctness of the code&lt;/strong&gt; being written. By having additional developers checking what was written, they could often point out details that were missed, edge cases, architectural and styling issues and so on.&lt;/li&gt;
&lt;li&gt;Even if not explicitly defined, &lt;strong&gt;code ownership&lt;/strong&gt; exists. Often there are modules primarily written by one developer - this person knows the most of it and asking for their review keeps this ownership alive and also makes sure the change is aligned with their original vision.&lt;/li&gt;
&lt;li&gt;Similar to the previous one, the developed feature may have a &lt;strong&gt;product owner&lt;/strong&gt; who wrote the original specification, and any divergence from the original specs may need the their review and approval.&lt;/li&gt;
&lt;li&gt;Regardless of ownership or prior knowledge, reviewing each other&#39;s work makes people &lt;strong&gt;up-to-date&lt;/strong&gt; about code changes, features and the overall progress of the project.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Submitting a pull-request alone only partially satisfied these requirements. A well written summary in the PR description, &lt;em&gt;self review comments&lt;/em&gt; by the author pointing out interesting details, explicitly asking specific team members in comments about some parts; these were all part of the best practices.&lt;/p&gt;
&lt;h3 id=&quot;how-did-it-change&quot;&gt;How did it change?&lt;/h3&gt;
&lt;p&gt;These practices are all either not making sense or increasing becoming the bottleneck in today&#39;s world of &lt;em&gt;agentic coding&lt;/em&gt;. You don&#39;t have time to write a detailed description yourself, and because the code was written by the agent anyway, so you generate the description as well. The generated PR description is much harder to understand for humans than a hand-written one, in worst case it even requires another agent to just make any sense of it.&lt;/p&gt;
&lt;p&gt;Others will review the PRs using agents too; it&#39;s inevitable as the tools are there, and the amount of changes to review is just growing. Depending on how overloaded each participant is, it may end up just copy-pasting agent output to each other in an interface originally designed for human interaction. Even without that concern, if the review is mostly done by an agent, how different it is from just asking more &lt;em&gt;agentic reviews&lt;/em&gt; locally?&lt;/p&gt;
&lt;p&gt;If you take your time to interpret and rephrase all the findings yourself, you just get even more behind and frustrated - as well as the PR author, waiting for feedback to unlock the next phases, as I explained in the first section.&lt;/p&gt;
&lt;h2 id=&quot;reducing-the-load&quot;&gt;Reducing the load&lt;/h2&gt;
&lt;p&gt;I propose a system with the overall goal to &lt;strong&gt;reduce the review load&lt;/strong&gt; on developers while trying to keep all the above identified original goals of why the review system is in place. The system assumes that every member of the team uses coding agents for implementation, verification and review.&lt;/p&gt;
&lt;h3 id=&quot;self-review-and-ci&quot;&gt;Self review and CI&lt;/h3&gt;
&lt;p&gt;A piece of work is done when the developer is confident enough that it&#39;s a good enough implementation of the task. This &lt;strong&gt;confidence&lt;/strong&gt; must come from a mix of &lt;em&gt;self-reviewing&lt;/em&gt; the agent&#39;s work, integrating &lt;em&gt;adversary reviews&lt;/em&gt;, &lt;em&gt;bug-finder loops&lt;/em&gt; and similar techniques to the &lt;em&gt;agentic coding&lt;/em&gt; session, making sure all the newly written code is tested and testable, and finally, a &lt;em&gt;draft PR&lt;/em&gt; is open and all CI checks are green. Details of these techniques are out of scope for this proposal.&lt;/p&gt;
&lt;p&gt;The part of making the CI green can (and should) also be part of the &lt;em&gt;agentic coding&lt;/em&gt; sessions. Telling the agent to periodically check CI and make all the necessary fixes is OK - but &lt;strong&gt;marking the PR ready&lt;/strong&gt; (non-draft) must mean the &lt;strong&gt;human author&lt;/strong&gt; is 100% sure the change is ready for that (not necessarily for merging yet, as I will describe below).&lt;/p&gt;
&lt;p&gt;Owning your changes is no different from the pre-AI era - everything the agent does in your name is your responsibility.&lt;/p&gt;
&lt;p&gt;At this point traditionally we would broadcast the PR link to our team immediately, and then wait for review. The first diversion from the old ways is that at this point the changeset was already checked by multiple &lt;em&gt;entities&lt;/em&gt;, one being human, others agents. So the quality and correctness point &lt;em&gt;may be&lt;/em&gt; already satisfied.&lt;/p&gt;
&lt;p&gt;This is a decision point where the developer can &lt;strong&gt;choose to not ask for any further review&lt;/strong&gt; and just merge the PR without causing any friction for the others. More precisely, there is a choice to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Just merge the PR, no further action needed&lt;/li&gt;
&lt;li&gt;Ask one or more team members for a (partial or full) review&lt;/li&gt;
&lt;li&gt;Either merging or waiting for review, additional &lt;strong&gt;async communication&lt;/strong&gt; can be triggered about the changeset, advertising new features or interesting code changes and so on.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;asking-for-a-review&quot;&gt;Asking for a review&lt;/h3&gt;
&lt;p&gt;When should you ask for a review, instead of just merging it?&lt;/p&gt;
&lt;p&gt;There are several possible reasons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Not being confident enough about the implementation&lt;/li&gt;
&lt;li&gt;The changeset touches areas which are (implicitly or explicitly) having an &lt;em&gt;owner&lt;/em&gt; who needs to know about and approve the changes&lt;/li&gt;
&lt;li&gt;The result diverges from the original plan significantly. Note that changes to the &lt;em&gt;chunking&lt;/em&gt; of work does not belong here. If a follow-up changeset will come soon after (in the same milestone), it is not a good enough reason for asking for a review.&lt;/li&gt;
&lt;li&gt;Some (mostly user-facing) behavior needs validation (by multiple team members or the product owner, etc.)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If any of the above is true, the PR (already green, non-draft, passing all the requirements of the previous section) should be sent to the team member(s) you need review from, preferable with an explicit explanation of why the review is needed. The asked review may be &lt;strong&gt;partial&lt;/strong&gt; - for example just approve changes to a given module out of the many that has been changed, the author taking responsibility for the correctness of the rest.&lt;/p&gt;
&lt;h3 id=&quot;interesting-parts&quot;&gt;Interesting parts&lt;/h3&gt;
&lt;p&gt;It is frequent that you &lt;em&gt;don&#39;t&lt;/em&gt; need a review, or at least not from all team members, but there are still some details about the changeset that you want others to know about. These can be collected and communicated asynchronously, there is no need for the PR to be kept open for this. Everyone can schedule their own time to catch up on this &lt;strong&gt;stream of interesting parts&lt;/strong&gt; notifications. They should be pointing to concrete code in the repository with some description of what to look for.&lt;/p&gt;
&lt;p&gt;This stream of updates, selected by the authors, is what could keep people &lt;strong&gt;up-to-date&lt;/strong&gt; with important changes of the codebase without getting lost in the sea of irrelevant details.&lt;/p&gt;
&lt;h3 id=&quot;walkthroughs&quot;&gt;Walkthroughs&lt;/h3&gt;
&lt;p&gt;The agents are good in generating &lt;strong&gt;walkthroughs&lt;/strong&gt; about a changeset - these are user-friendly, easily readable, good looking and often interactive pages summarizing the changes, focusing on the important aspects. Walkthroughs are useful both when an actual &lt;em&gt;code review is needed&lt;/em&gt;, and also can be part of the &lt;em&gt;interesting parts stream&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Especially with user-facing changes, demonstrating the UX of a new feature as part of the walkthrough helps the reviewers to know what to look for in the changeset, and the other team members to keep up-to-date with the evolution of the product.&lt;/p&gt;
&lt;h2 id=&quot;responsibility&quot;&gt;Responsibility&lt;/h2&gt;
&lt;p&gt;This of course puts a bigger responsibility on the person who decides to &lt;strong&gt;not&lt;/strong&gt; ask for a review. In a small team where members are having high trust towards each other, it should not be a problem. What if the system gets abused and people start merging everything without review (and/or without advertising the interesting parts and so on)?&lt;/p&gt;
&lt;p&gt;I believe this does not require any defined process - as all changes are still done through commits and PRs linking to issues etc, all information can be retrospectively gathered and whenever someone identifies a merged changeset that shouldn&#39;t have been merged, it can be discussed and resolved.&lt;/p&gt;
&lt;p&gt;Other options could be to tie the freedom of choosing to merge your own work without further review to some measurable factors, such as the developer&#39;s level, time spent in the team, or some way of demonstrating knowledge and skill of working with agents on the project.&lt;/p&gt;
&lt;p&gt;In the end the goal is to reduce the bottleneck caused by code reviews while keeping the code quality and team awareness high.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.5 features - Part 17: Semantic retry policies</title>
        <published>2026-04-24T16:00:00+00:00</published>
        <updated>2026-04-24T16:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem15-part17-semantic-retry-policies/"/>
        <id>https://blog.vigoo.dev/posts/golem15-part17-semantic-retry-policies/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem15-part17-semantic-retry-policies/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I am writing a series of &lt;em&gt;short&lt;/em&gt; posts showcasing the new features of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, to be released at the end of April, 2026. The episodes of this series will be short and assume the reader knows what Golem is. Check my &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem/&quot;&gt;other Golem-related posts&lt;/a&gt; for more information!&lt;/p&gt;
&lt;p&gt;Parts released so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;Part 1: Code-first routes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part2-webhooks&quot;&gt;Part 2: Webhooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part3-mcp&quot;&gt;Part 3: MCP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part4-nodejs&quot;&gt;Part 4: Node.js compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part5-scala&quot;&gt;Part 5: Scala support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part6-user-defined-snapshotting&quot;&gt;Part 6: User-defined snapshotting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;Part 7: Configuration and Secrets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part8-template-simplifications&quot;&gt;Part 8: Template simplifications and automatic updates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;Part 9: Agent skills&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part10-websocket&quot;&gt;Part 10: WebSocket client&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;Part 11: Bridge libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;Part 12: REPL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part13-per-agent-config&quot;&gt;Part 13: Per-agent configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part14-otlp&quot;&gt;Part 14: OpenTelemetry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part15-moonbit&quot;&gt;Part 15: MoonBit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part16-quotas&quot;&gt;Part 16: Quotas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part17-semantic-retry-policies&quot;&gt;Part 17: Semantic retry policies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;retry-policies&quot;&gt;Retry policies&lt;/h2&gt;
&lt;p&gt;Previous Golem versions had a simple, global retry policy describing a few retries with exponential backoff. This retry policy applied to everything - in case a Golem application failed with an error (for example a Rust panic) it recreated the instance according to this policy a few times before it marked the agent to be permanently failed. This in theory could make transient errors auto-resolved but it did not allow much control to the user (although the parameters of this global policy could be overridden) and the exact behavior depended heavily on how exactly (and &lt;em&gt;when&lt;/em&gt;) the application crashes with an error.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Golem 1.5&lt;/strong&gt; improves this in two ways:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;introducing automatic inline retries in many scenarios&lt;/li&gt;
&lt;li&gt;a fully redesigned, customizable and composable retry policy model&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;inline-retries&quot;&gt;Inline retries&lt;/h3&gt;
&lt;p&gt;In many cases throwing away the agent instance, and recreating its state from scratch is not necessary for a retry. Golem now transparently retries all these transient issues with HTTP requests etc. immediately, providing a much faster and reliable retry mechanism.&lt;/p&gt;
&lt;p&gt;Another part of this change is better classification of what &lt;em&gt;can&lt;/em&gt; be retried and what not. We no longer retry things that are known to be deterministically fail again.&lt;/p&gt;
&lt;h3 id=&quot;retry-policies-1&quot;&gt;Retry policies&lt;/h3&gt;
&lt;p&gt;The new retry policies are much more flexible and customizable than the single global configuration we had before. Just like with &lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;secrets&lt;/a&gt; and &lt;a href=&quot;/posts/golem15-part17-semantic-retry-policies&quot;&gt;resource quotas&lt;/a&gt;, retry policies are also defined &lt;strong&gt;per-environment&lt;/strong&gt;. An environment can have an arbitrary number of &lt;strong&gt;named&lt;/strong&gt; retry policies defined. Each has a &lt;strong&gt;predicate&lt;/strong&gt; and a &lt;strong&gt;policy&lt;/strong&gt; - the predicate is an expression that decides whether the policy should be applied for a given failure case or not. If it gets chosen (evaluation order can be controlled with a priority field), the applied retry strategy is described by the &lt;strong&gt;policy&lt;/strong&gt;.&lt;/p&gt;
&lt;h4 id=&quot;policies&quot;&gt;Policies&lt;/h4&gt;
&lt;p&gt;The policy is a highly composable structure, with the following base nodes controlling &lt;em&gt;delays&lt;/em&gt;:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Policy&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;periodic&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Fixed delay between each attempt&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;exponential&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;baseDelay × factor^attempt&lt;/code&gt; — exponentially growing delays&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;fibonacci&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Delays follow the Fibonacci sequence starting from &lt;code&gt;first&lt;/code&gt; and &lt;code&gt;second&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;immediate&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Retry immediately (zero delay)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;never&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Never retry — give up on first failure&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;These can be combined with various combinators:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Combinator&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;countBox&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Limits the total number of retry attempts&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;timeBox&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Limits retries to a wall-clock duration&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;clamp&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Clamps computed delay to a &lt;code&gt;[minDelay, maxDelay]&lt;/code&gt; range&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;addDelay&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Adds a constant offset on top of the computed delay&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;jitter&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Adds random noise (±factor × delay) to avoid thundering herds&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;filteredOn&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Apply the inner policy only when a predicate matches; otherwise give up&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;andThen&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Run the first policy until it gives up, then switch to the second&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;union&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Retry if &lt;em&gt;either&lt;/em&gt; sub-policy wants to; pick the shorter delay&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;intersect&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Retry only while &lt;em&gt;both&lt;/em&gt; sub-policies want to; pick the longer delay&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h4 id=&quot;predicates&quot;&gt;Predicates&lt;/h4&gt;
&lt;p&gt;Predicates are boolean expressions evaluated against the &lt;strong&gt;error context properties&lt;/strong&gt;. They can be composed with &lt;code&gt;and&lt;/code&gt;, &lt;code&gt;or&lt;/code&gt;, and &lt;code&gt;not&lt;/code&gt;:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Predicate&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;propEq&lt;/code&gt; / &lt;code&gt;propNeq&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Equality / inequality comparison&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;propGt&lt;/code&gt; / &lt;code&gt;propGte&lt;/code&gt; / &lt;code&gt;propLt&lt;/code&gt; / &lt;code&gt;propLte&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Numeric comparisons&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;propExists&lt;/code&gt;&lt;/td&gt;&lt;td&gt;True if the named property exists in the context&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;propIn&lt;/code&gt;&lt;/td&gt;&lt;td&gt;True if the property value is in a given set&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;propMatches&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Glob pattern matching on the text representation&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;propStartsWith&lt;/code&gt; / &lt;code&gt;propContains&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Prefix / substring matching&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;true&lt;/code&gt; / &lt;code&gt;false&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Constant predicates&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h4 id=&quot;available-properties&quot;&gt;Available properties&lt;/h4&gt;
&lt;p&gt;The &lt;strong&gt;properties&lt;/strong&gt; are what predicates can refer to to evaluate if a policy applies to a failure case. Golem defines the following ones:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Property&lt;/th&gt;&lt;th&gt;Set by&lt;/th&gt;&lt;th&gt;Type&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;verb&lt;/code&gt;&lt;/td&gt;&lt;td&gt;All&lt;/td&gt;&lt;td&gt;text&lt;/td&gt;&lt;td&gt;HTTP method, &lt;code&gt;&quot;invoke&quot;&lt;/code&gt;, &lt;code&gt;&quot;resolve&quot;&lt;/code&gt;, &lt;code&gt;&quot;trap&quot;&lt;/code&gt;, etc.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;noun-uri&lt;/code&gt;&lt;/td&gt;&lt;td&gt;All&lt;/td&gt;&lt;td&gt;text&lt;/td&gt;&lt;td&gt;Target URI (e.g. &lt;code&gt;https://…&lt;/code&gt;, &lt;code&gt;worker://…&lt;/code&gt;, &lt;code&gt;kv://…&lt;/code&gt;)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;uri-scheme&lt;/code&gt;&lt;/td&gt;&lt;td&gt;All with URI&lt;/td&gt;&lt;td&gt;text&lt;/td&gt;&lt;td&gt;Scheme part of the URI&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;uri-host&lt;/code&gt;&lt;/td&gt;&lt;td&gt;All with URI&lt;/td&gt;&lt;td&gt;text&lt;/td&gt;&lt;td&gt;Hostname&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;uri-port&lt;/code&gt;&lt;/td&gt;&lt;td&gt;When port present&lt;/td&gt;&lt;td&gt;integer&lt;/td&gt;&lt;td&gt;Port number&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;uri-path&lt;/code&gt;&lt;/td&gt;&lt;td&gt;All with URI&lt;/td&gt;&lt;td&gt;text&lt;/td&gt;&lt;td&gt;Path portion&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;status-code&lt;/code&gt;&lt;/td&gt;&lt;td&gt;HTTP responses&lt;/td&gt;&lt;td&gt;integer&lt;/td&gt;&lt;td&gt;HTTP response status code&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;error-type&lt;/code&gt;&lt;/td&gt;&lt;td&gt;HTTP errors&lt;/td&gt;&lt;td&gt;text&lt;/td&gt;&lt;td&gt;Error classification string&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;function&lt;/code&gt;&lt;/td&gt;&lt;td&gt;RPC&lt;/td&gt;&lt;td&gt;text&lt;/td&gt;&lt;td&gt;Target function name&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;target-component-id&lt;/code&gt;&lt;/td&gt;&lt;td&gt;RPC&lt;/td&gt;&lt;td&gt;text&lt;/td&gt;&lt;td&gt;Component ID of the target worker&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;target-agent-type&lt;/code&gt;&lt;/td&gt;&lt;td&gt;RPC&lt;/td&gt;&lt;td&gt;text&lt;/td&gt;&lt;td&gt;Agent type name of the target&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;db-type&lt;/code&gt;&lt;/td&gt;&lt;td&gt;RDBMS&lt;/td&gt;&lt;td&gt;text&lt;/td&gt;&lt;td&gt;Database type (from URI scheme)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;trap-type&lt;/code&gt;&lt;/td&gt;&lt;td&gt;WASM traps&lt;/td&gt;&lt;td&gt;text&lt;/td&gt;&lt;td&gt;&lt;code&gt;&quot;deterministic&quot;&lt;/code&gt; or &lt;code&gt;&quot;transient&quot;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;defining-policies&quot;&gt;Defining policies&lt;/h3&gt;
&lt;p&gt;Retry policies can be defined in the application manifest under the &lt;code&gt;retryPolicyDefaults&lt;/code&gt; section, keyed by environment name. These retry policies are default values set when the application is deployed, and they can be manipulated live while agents are already using them.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;etryPolicyDefaults&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;y-environment&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Never retry client errors (4xx)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    n&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;o-retry-4xx&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;riority&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 20&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;redicate&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nd&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ropGte&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;roperty&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;tatus-code&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; v&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;alue&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 400&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ropLt&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;roperty&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;tatus-code&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; v&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;alue&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 500&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;olicy&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;never&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Aggressive retry for known-transient HTTP errors&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    h&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ttp-transient&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;riority&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;redicate&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ropIn&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;roperty&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;tatus-code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          v&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;alues&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;502&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 503&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 504&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;olicy&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ountBox&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;axRetries&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          i&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nner&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            j&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;itter&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;              f&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;actor&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0.15&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;              i&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nner&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;lamp&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                  m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;inDelay&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;100ms&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                  m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;axDelay&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;5s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                  i&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nner&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                    e&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;xponential&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                      b&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;aseDelay&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;200ms&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                      f&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;actor&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 2.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Catch-all: moderate retry for everything else&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;atch-all&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;riority&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;redicate&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;olicy&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ountBox&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;axRetries&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          i&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nner&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            e&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;xponential&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;              b&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;aseDelay&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;100ms&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;              f&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;actor&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 3.0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When the agent encounters an error, policies are evaluated in &lt;strong&gt;descending priority order&lt;/strong&gt;. In this example:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;First check &lt;code&gt;no-retry-4xx&lt;/code&gt; (priority 20) — if it&#39;s a 4xx error, give up immediately&lt;/li&gt;
&lt;li&gt;Then check &lt;code&gt;http-transient&lt;/code&gt; (priority 10) — if it&#39;s a 502/503/504, retry aggressively&lt;/li&gt;
&lt;li&gt;Fall through to &lt;code&gt;catch-all&lt;/code&gt; (priority 0) — retry moderately for anything else&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;default-retry-policy&quot;&gt;Default retry policy&lt;/h3&gt;
&lt;p&gt;When no user-defined retry policies are set, a default catch-all policy is activated which behaves the same way as the old global retry policy did.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Name&lt;/strong&gt;: &lt;code&gt;&quot;default&quot;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Priority&lt;/strong&gt;: &lt;code&gt;0&lt;/code&gt; (lowest — any user-defined policy with priority ≥ 1 overrides it)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Predicate&lt;/strong&gt;: &lt;code&gt;true&lt;/code&gt; (matches everything)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Policy&lt;/strong&gt;: Up to &lt;strong&gt;3 retries&lt;/strong&gt;, exponential backoff with factor 3.0, delays clamped to [100ms, 1s], with 15% jitter&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In the new policy system, this would be defined like the following:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ame&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;efault&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;riority&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;redicate&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;olicy&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ountBox&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;axRetries&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    i&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nner&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      j&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;itter&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        f&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;actor&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0.15&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        i&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nner&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;lamp&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;inDelay&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;100ms&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;axDelay&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            i&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nner&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;              e&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;xponential&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                b&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;aseDelay&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;100ms&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                f&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;actor&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 3.0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;live-editing-policies&quot;&gt;Live-editing policies&lt;/h3&gt;
&lt;p&gt;As mentioned above, the default policies created by &lt;code&gt;golem deploy&lt;/code&gt; can be modified on the fly using the CLI (or the REST API). This can be useful to react to production issues by tweaking retry behaviors without having to redeploy anything.&lt;/p&gt;
&lt;p&gt;The following examples show how the CLI can be used to manipulate these policies:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Create a new policy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; retry-policy&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; create&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; http-transient&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-priority&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 10&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-predicate&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{ propIn: { property: &amp;quot;status-code&amp;quot;, values: [502, 503, 504] } }&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-policy&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{ countBox: { maxRetries: 5, inner: { exponential: { baseDelay: &amp;quot;200ms&amp;quot;, factor: 2.0 } } } }&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; List all policies in the current environment&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; retry-policy&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; list&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Get a specific policy by name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; retry-policy&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; get&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; http-transient&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Update an existing policy (e.g. raise its priority)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; retry-policy&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; update&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; http-transient&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-priority&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 15&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Delete a policy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; retry-policy&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; delete&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; http-transient&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;sdk-support&quot;&gt;SDK support&lt;/h4&gt;
&lt;p&gt;The Golem SDK provide the same runtime query and modification capabilities. We can query retry policies, modify or create new ones, and have temporary overrides to them that are only affecting the running agent.&lt;/p&gt;
&lt;p&gt;The following example uses the Golem SDK to define and use a custom retry policy for a given HTTP request:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  Policy&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; Predicate&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; NamedPolicy&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; Props&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; Duration&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  withRetryPolicy&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; from&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;@golemcloud/golem-ts-sdk&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; policy&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; NamedPolicy&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;named&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;http-transient&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;  Policy&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;exponential&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Duration&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;milliseconds&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;200&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clamp&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Duration&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;milliseconds&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;100&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Duration&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;seconds&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;withJitter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;15&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;onlyWhen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Predicate&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;oneOf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Props&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;statusCode&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;502&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 503&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 504&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;maxRetries&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;priority&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;10&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;appliesWhen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Predicate&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;eq&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Props&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;uriScheme&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;https&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Scoped usage — policy is restored when the block exits&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;withRetryPolicy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;policy&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; HTTP calls in this block use the custom retry policy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  makeHttpRequest&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; golem_rust&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;retry&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;time&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Duration&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; policy&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; NamedPolicy&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;named&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;http-transient&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;    Policy&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;exponential&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Duration&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;from_millis&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;200&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clamp&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Duration&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;from_millis&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;100&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Duration&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;from_secs&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;with_jitter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;15&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;only_when&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Predicate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;one_of&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Props&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;STATUS_CODE&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;502_&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;u16&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 503&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 504&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;max_retries&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;priority&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;10&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;applies_when&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Predicate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;eq&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Props&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;URI_SCHEME&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;https&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Scoped usage — policy is restored when the block exits&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;with_named_policy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;policy&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; HTTP calls in this block use the custom retry policy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    make_http_request&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span&gt; golem&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Guards&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span&gt; golem&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;host&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Retry&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span&gt; scala&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;concurrent&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;duration&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; policy&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; named&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;http-transient&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Policy&lt;/span&gt;&lt;span&gt;.exponential&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;200&lt;/span&gt;&lt;span&gt;.millis, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2.0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .clamp&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;100&lt;/span&gt;&lt;span&gt;.millis, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;5&lt;/span&gt;&lt;span&gt;.seconds&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .withJitter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0.15&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .onlyWhen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Props&lt;/span&gt;&lt;span&gt;.statusCode.oneOf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;502&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;503&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;504&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .maxRetries&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.priority&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;10&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; .appliesWhen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Props&lt;/span&gt;&lt;span&gt;.uriScheme.eq&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;https&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Scoped usage — policy is restored when the Future completes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;withRetryPolicy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;policy&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Future&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; HTTP calls in this block use the custom retry policy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    makeHttpRequest&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; policy&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  NamedPolicy&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;named&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;http-transient&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Policy&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;exponential&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Duration&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;millis&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;200&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 2.0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clamp&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Duration&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;millis&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;100&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Duration&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;seconds&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;with_jitter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0.15&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;only_when&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;        Predicate&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;one_of&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;          Props&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;status_code&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          [&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;int&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;502&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;int&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;503&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;int&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;504&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;max_retries&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;priority&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;10&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;applies_when&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Predicate&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;eq&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Props&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;uri_scheme&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;text&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;https&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;// Scoped usage — policy is restored when the block exits&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;with_named_policy&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;policy&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  // HTTP calls in this block use the custom retry policy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  make_http_request&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;extensionability&quot;&gt;Extensionability&lt;/h3&gt;
&lt;p&gt;Any future retry-capable host functionality we add to Golem can be integrated into this retry policy system, and with the ability of querying the policies runtime, third party, user-level retry functionalities can also be built on top of it.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.5 features - Part 16: Quotas</title>
        <published>2026-04-24T00:00:00+00:00</published>
        <updated>2026-04-24T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem15-part16-quotas/"/>
        <id>https://blog.vigoo.dev/posts/golem15-part16-quotas/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem15-part16-quotas/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I am writing a series of &lt;em&gt;short&lt;/em&gt; posts showcasing the new features of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, to be released at the end of April, 2026. The episodes of this series will be short and assume the reader knows what Golem is. Check my &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem/&quot;&gt;other Golem-related posts&lt;/a&gt; for more information!&lt;/p&gt;
&lt;p&gt;Parts released so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;Part 1: Code-first routes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part2-webhooks&quot;&gt;Part 2: Webhooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part3-mcp&quot;&gt;Part 3: MCP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part4-nodejs&quot;&gt;Part 4: Node.js compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part5-scala&quot;&gt;Part 5: Scala support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part6-user-defined-snapshotting&quot;&gt;Part 6: User-defined snapshotting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;Part 7: Configuration and Secrets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part8-template-simplifications&quot;&gt;Part 8: Template simplifications and automatic updates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;Part 9: Agent skills&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part10-websocket&quot;&gt;Part 10: WebSocket client&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;Part 11: Bridge libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;Part 12: REPL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part13-per-agent-config&quot;&gt;Part 13: Per-agent configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part14-otlp&quot;&gt;Part 14: OpenTelemetry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part15-moonbit&quot;&gt;Part 15: MoonBit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part16-quotas&quot;&gt;Part 16: Quotas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part17-semantic-retry-policies&quot;&gt;Part 17: Semantic retry policies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;quotas&quot;&gt;Quotas&lt;/h2&gt;
&lt;p&gt;A modern application usually depends on various third party services. This is even more true today with AI agents - an agent is going to make requests to various external systems as well as LLM providers and other AI infrastructure. Most of these have costs and limits. It does matter how many and how big requests you make to your chosen model, and many APIs have built-in quotas and rate limits affecting the callers.&lt;/p&gt;
&lt;p&gt;In &lt;strong&gt;Golem 1.5&lt;/strong&gt; we introduced a new, general purpose feature that tracks resource quotas in a distributed Golem application. The idea is that we can define an arbitrary set of &lt;strong&gt;resources&lt;/strong&gt; with a limited availability, and enforce reserving some of this pool of available resources through &lt;strong&gt;quota tokens&lt;/strong&gt;. With this token-granting we can control the parallel running agents and make sure we don&#39;t over-use the limited resources.&lt;/p&gt;
&lt;p&gt;It is possible to acquire some quota tokens and &lt;strong&gt;split them&lt;/strong&gt;, passing a part of them to another Golem agent through agent-to-agent RPC calls. This is a very powerful tool to have a structured control over arbitrary external resource consumption.&lt;/p&gt;
&lt;h3 id=&quot;integration&quot;&gt;Integration&lt;/h3&gt;
&lt;p&gt;In this release we just introduce this as a general tool, available through the Golem SDKs, but it is not integrated with any libraries yet. In the future we expect to have dedicated support for quota tokens in various client libraries, but for now they have to be used as a manually implemented layer on top of the clients.&lt;/p&gt;
&lt;h3 id=&quot;setting-it-up&quot;&gt;Setting it up&lt;/h3&gt;
&lt;p&gt;Let&#39;s see some concrete examples. Just like with &lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;secrets&lt;/a&gt;, quota &lt;strong&gt;resources&lt;/strong&gt; are also defined &lt;strong&gt;per environment&lt;/strong&gt;, and we can define them with their initial limits in the &lt;strong&gt;application manifest&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The following manifest snippet defines three different types of resources:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;esourceDefaults&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;rod&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;pi-calls&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;imit&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ype&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; R&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        v&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;alue&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 100&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;eriod&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;inute&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ax&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      e&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nforcementAction&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;eject&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      u&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nit&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;equest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      u&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nits&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;equests&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;torage&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;imit&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ype&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; C&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;apacity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        v&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;alue&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1073741824&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;       #&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; 1 GB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      e&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nforcementAction&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;eject&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      u&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nit&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;yte&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      u&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nits&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ytes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;onnections&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;imit&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ype&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; C&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;oncurrency&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        v&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;alue&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 50&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      e&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nforcementAction&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;hrottle&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      u&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nit&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;onnection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      u&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nits&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;onnections&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;prod&lt;/code&gt; is just an example environment. For each environment we can define an arbitrary number of resources, identified by a unique name. The most important in their configuration are &lt;code&gt;limit&lt;/code&gt; and &lt;code&gt;enforcementAction&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We have three different &lt;strong&gt;limit&lt;/strong&gt; types:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Rate&lt;/code&gt; defines a limited pool that refills by &lt;code&gt;value&lt;/code&gt; in every &lt;code&gt;period&lt;/code&gt;, with a maximum of &lt;code&gt;max&lt;/code&gt;. This is great for example for rate-limiting API calls.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Capacity&lt;/code&gt; defines a fixed number of &lt;code&gt;value&lt;/code&gt; tokens. It never gets refilled - if an agent takes some of it, it remains taken forever.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Concurrency&lt;/code&gt; defines a fixed pool of &lt;code&gt;value&lt;/code&gt; tokens. The agents can never allocate more than this value at the same time, but once they stop using the tokens they get back to the pool. This is useful for limiting concurrency.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In addition to the limits, we can select what to do when the token request cannot be satisfied:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;reject&lt;/code&gt; means we return an error (with an optional hint of how much to wait). It is the agent&#39;s responsibility to handle this&lt;/li&gt;
&lt;li&gt;&lt;code&gt;throttle&lt;/code&gt; means Golem will suspend the agent until the resource becomes available. This is fully automatic and does not require any further logic in the user code!&lt;/li&gt;
&lt;li&gt;&lt;code&gt;terminate&lt;/code&gt; kills the agent with a specific failure message&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;dynamic-changes&quot;&gt;Dynamic changes&lt;/h3&gt;
&lt;p&gt;The resource definitions in the application manifest are only &lt;strong&gt;defaults&lt;/strong&gt; applied at deployment. With CLI commands (or REST API calls) we can modify them any time, and the changes will affect running agents immediately.&lt;/p&gt;
&lt;p&gt;For example we can increase the above defined &lt;code&gt;connections&lt;/code&gt; resource&#39;s limit to 100 if we need more concurrency:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; resource&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; update&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; connections&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-limit&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&amp;quot;type&amp;quot;:&amp;quot;concurrency&amp;quot;,&amp;quot;value&amp;quot;:100}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-environment&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; prod&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;code-example&quot;&gt;Code example&lt;/h3&gt;
&lt;p&gt;Let&#39;s see how we can use these resources from code!&lt;/p&gt;
&lt;h4 id=&quot;initialization&quot;&gt;Initialization&lt;/h4&gt;
&lt;p&gt;The first step is to acquire a &lt;strong&gt;quota token interface&lt;/strong&gt; for every resource our agent is going to need. This can be done in the agent&#39;s constructor, or the first time the token is needed, but should be done only once:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; acquireQuotaToken&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; from&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;golem-ts-sdk&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; token&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; acquireQuotaToken&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;api-calls&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;n&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; golem_rust&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;quota&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;QuotaToken&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; token&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; QuotaToken&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;api-calls&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span&gt; golem&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;host&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;QuotaApi&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; token&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; QuotaToken&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;api-calls&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;BigInt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; token&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @quota&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;QuotaToken&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;api-calls&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1UL&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The parameter (&lt;code&gt;1&lt;/code&gt;) is the &lt;strong&gt;expected amount asked per reservation&lt;/strong&gt;. For a simple rate limiting use case, where we associate 1 API call with 1 token, this can be 1.&lt;/p&gt;
&lt;h4 id=&quot;simple-rate-limiting&quot;&gt;Simple rate limiting&lt;/h4&gt;
&lt;p&gt;For a simple rate limiting case, we can &lt;strong&gt;reserve&lt;/strong&gt; one token for each API call (of course we could also weight different API calls differently, by associating different token counts to them):&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; withReservation&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; from&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;golem-ts-sdk&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; withReservation&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;token&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;n&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; async&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;reservation&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; response&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; callSimpleApi&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  return&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; used&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; BigInt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; response&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; golem_rust&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;quota&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;with_reservation&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; with_reservation&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;token&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;_reservation&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; response&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; call_simple_api&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; response&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; withReservation&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;token, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;BigInt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; reservation &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  callSimpleApi&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;.map &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; response &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;BigInt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, response&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @quota&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;with_reservation&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;token&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1UL&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;reservation&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; response&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; callSimpleApi&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; response&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4 id=&quot;rate-limiting-llms&quot;&gt;Rate-limiting LLMs&lt;/h4&gt;
&lt;p&gt;The same mechanism can be used to rate-limit for example LLMs, but not based on just the requests, but on the actual tokens consumed. Instead of reserving just one token, we reserve the number of maximum tokens we expect the request will consume (and in most LLM APIs we can enforce this). Then in the &lt;code&gt;response&lt;/code&gt; we read how much actual tokens our request used, and &lt;strong&gt;commit&lt;/strong&gt; that (returning the final number the &lt;code&gt;withReservation&lt;/code&gt; helper is a way to commit, but there is also an explicit &lt;code&gt;commit&lt;/code&gt; call we can use).&lt;/p&gt;
&lt;h4 id=&quot;splitting-and-merging&quot;&gt;Splitting and merging&lt;/h4&gt;
&lt;p&gt;Quota tokens can be &lt;strong&gt;split&lt;/strong&gt;, &lt;strong&gt;merged&lt;/strong&gt; and &lt;strong&gt;transformed&lt;/strong&gt; between agents. The following example splits off 200 units from our agent&#39;s available tokens for a given resource, and sends it to another agent.&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; childToken&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; QuotaToken&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; token&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;split&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;n&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; childAgent&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; SummarizerAgent&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;newPhantom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; summary&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; childAgent&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;summarize&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;text&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; childToken&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; child_token&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; QuotaToken&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; token&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;split&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;200&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; child_agent&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; SummarizerAgent&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new_phantom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;await&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; summary&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; child_agent&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;summarize&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;text&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; child_token&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;await&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; childToken&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; QuotaToken&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; token.split&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;BigInt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;200&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  childAgent &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SummarizerAgent&lt;/span&gt;&lt;span&gt;.newPhantom&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  summary &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; childAgent.summarize&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;text, childToken&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span&gt; summary&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; child_token&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; QuotaToken&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;token&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;split&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;200UL&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; child_agent&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SummarizerAgent&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new_phantom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;child_agent&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;summarize&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;text&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; child_token&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In addition to this, we could return the split tokens after the remote call, and merge them back into the original agent&#39;s tokens:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;token&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;merge&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;returnedToken&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;token&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;merge&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;returned_token&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;token.merge&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;returnedToken&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;token&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;merge&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;returned_token&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.5 features - Part 15: MoonBit</title>
        <published>2026-04-23T00:00:00+00:00</published>
        <updated>2026-04-23T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem15-part15-moonbit/"/>
        <id>https://blog.vigoo.dev/posts/golem15-part15-moonbit/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem15-part15-moonbit/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I am writing a series of &lt;em&gt;short&lt;/em&gt; posts showcasing the new features of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, to be released at the end of April, 2026. The episodes of this series will be short and assume the reader knows what Golem is. Check my &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem/&quot;&gt;other Golem-related posts&lt;/a&gt; for more information!&lt;/p&gt;
&lt;p&gt;Parts released so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;Part 1: Code-first routes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part2-webhooks&quot;&gt;Part 2: Webhooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part3-mcp&quot;&gt;Part 3: MCP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part4-nodejs&quot;&gt;Part 4: Node.js compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part5-scala&quot;&gt;Part 5: Scala support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part6-user-defined-snapshotting&quot;&gt;Part 6: User-defined snapshotting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;Part 7: Configuration and Secrets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part8-template-simplifications&quot;&gt;Part 8: Template simplifications and automatic updates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;Part 9: Agent skills&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part10-websocket&quot;&gt;Part 10: WebSocket client&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;Part 11: Bridge libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;Part 12: REPL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part13-per-agent-config&quot;&gt;Part 13: Per-agent configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part14-otlp&quot;&gt;Part 14: OpenTelemetry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part15-moonbit&quot;&gt;Part 15: MoonBit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part16-quotas&quot;&gt;Part 16: Quotas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part17-semantic-retry-policies&quot;&gt;Part 17: Semantic retry policies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;moonbit-support&quot;&gt;MoonBit support&lt;/h2&gt;
&lt;p&gt;The new &lt;strong&gt;Golem 1.5&lt;/strong&gt; release compes with two new supported languages: &lt;a href=&quot;/posts/golem15-part5-scala&quot;&gt;Scala&lt;/a&gt; and &lt;strong&gt;MoonBit&lt;/strong&gt;. An experimental Golem MoonBit SDK has already been released as a separate project for the previous Golem version, but now it becomes a first-class supported language, integrated with our CLI and build flows.&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://www.moonbitlang.com&quot;&gt;MoonBit&lt;/a&gt; is an interesting new language with many nice features. One property that distinguishes it from the other languages we support is that it can compile to very small WASM binaries (and very quickly). This makes a difference in Golem, as the smallest the actual compiled component is, the faster we can instantiate an agent.&lt;/p&gt;
&lt;p&gt;As with Scala, we don&#39;t have &lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;bridge generators&lt;/a&gt; and a MoonBit &lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;REPL&lt;/a&gt; yet, but the TypeScript REPL is fully usable with MoonBit agents as well.&lt;/p&gt;
&lt;h3 id=&quot;how-does-it-look-like&quot;&gt;How does it look like?&lt;/h3&gt;
&lt;p&gt;Let&#39;s see how the simplest &lt;em&gt;counter agent&lt;/em&gt; example looks like in MooBit:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///|&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;/// Counter agent in MoonBit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.agent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Counter&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  name&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UInt64&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///|&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;/// Creates a new counter with the given name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Counter&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Counter&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///|&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;/// Increments the counter and returns the new value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Counter&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;increment&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UInt64&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; +=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///|&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;/// Returns the current value of the counter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Counter&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get_value&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UInt64&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Agents are structs annotated with &lt;code&gt;#derive.agent&lt;/code&gt;, and all public methods are becoming &lt;strong&gt;agent methods&lt;/strong&gt;. The type needs a &lt;code&gt;new&lt;/code&gt; constructor that is going to the agent&#39;s &lt;strong&gt;identity&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Custom data types are supported by annotating them with &lt;code&gt;#derive.golem_schema&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.golem_schema&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MyData&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  field1&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  field2&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UInt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.golem_schema&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;enum&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Status&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Active&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Inactive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Every type used in the constructor or agent methods (either as a parameter or return type) must have a derived Golem schema.&lt;/p&gt;
&lt;h4 id=&quot;rpc&quot;&gt;RPC&lt;/h4&gt;
&lt;p&gt;Agent to agent communication works just like with the other languages - for each agent we have a &lt;strong&gt;client type&lt;/strong&gt; generated:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;CounterClient&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;scoped&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;my-counter&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;counter&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; raise&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @common&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AgentError&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  counter&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;increment&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  counter&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;increment&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; counter&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get_value&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;scoped&lt;/code&gt; function is equivalent to calling the &lt;code&gt;get&lt;/code&gt; constructor (that in Golem means &quot;get or create if not existing&quot; an agent), and dropping the remote connection in the end.&lt;/p&gt;
&lt;h4 id=&quot;exposing-http-endpoints&quot;&gt;Exposing HTTP endpoints&lt;/h4&gt;
&lt;p&gt;As we&#39;ve seen, Golem 1.5 comes with &lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;code-first routes&lt;/a&gt; and MoonBit also fully supports this. For example we could expose our counter via a POST endpoint by just adding a few more annotations:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.agent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.mount&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/moonbit-counters/{name}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Counter&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; // ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.endpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/increment&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Counter&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;increment&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UInt64&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  // ..&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;other-features&quot;&gt;Other features&lt;/h3&gt;
&lt;p&gt;Every other feature we mentioned in &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem/&quot;&gt;this series&lt;/a&gt; is available for MoonBit. The new version of our &lt;a rel=&quot;external&quot; href=&quot;https://learn.golem.cloud&quot;&gt;documentation&lt;/a&gt; will have every code snippet presented in all supported languages including MoonBit, and we our &lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;skill catalog&lt;/a&gt; also have a large number of MoonBit specific agent skills. Until the new documentation site is published, they can be checked in &lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/golem/tree/main/golem-skills/skills/moonbit&quot;&gt;the repo&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;implementation-details&quot;&gt;Implementation details&lt;/h3&gt;
&lt;p&gt;The MoonBit SDK consists of two major parts: a code-level transformation tool implemented in MoonBit using the &lt;a rel=&quot;external&quot; href=&quot;https://mooncakes.io/docs/moonbitlang/parser&quot;&gt;moonbitlang/parser&lt;/a&gt; and &lt;a rel=&quot;external&quot; href=&quot;https://mooncakes.io/docs/moonbitlang/formatter&quot;&gt;moonbitlang/formatter&lt;/a&gt; packages, and a MoonBit library every Golem application must depend on.&lt;/p&gt;
&lt;p&gt;The tool parses the user&#39;s source code and finds all the Golem-specific derive attributes, and generates code (typeclass implementations for annotated custom types, agent registration code, RPC clients etc). The SDK encapsulates the generated WASM bindings (using &lt;code&gt;wit-bindgen-moonbit&lt;/code&gt;) and presents them as a higher level MoonBit library for the Golem developers.&lt;/p&gt;
&lt;p&gt;The whole multi-step build flow is &lt;em&gt;hidden&lt;/em&gt; in a &lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/golem/blob/main/cli/golem-cli/templates/moonbit/common-on-demand/golem.yaml&quot;&gt;golem-managed build template&lt;/a&gt;.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.5 features - Part 14: OpenTelemetry</title>
        <published>2026-04-22T00:00:00+00:00</published>
        <updated>2026-04-22T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem15-part14-otlp/"/>
        <id>https://blog.vigoo.dev/posts/golem15-part14-otlp/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem15-part14-otlp/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I am writing a series of &lt;em&gt;short&lt;/em&gt; posts showcasing the new features of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, to be released at the end of April, 2026. The episodes of this series will be short and assume the reader knows what Golem is. Check my &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem/&quot;&gt;other Golem-related posts&lt;/a&gt; for more information!&lt;/p&gt;
&lt;p&gt;Parts released so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;Part 1: Code-first routes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part2-webhooks&quot;&gt;Part 2: Webhooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part3-mcp&quot;&gt;Part 3: MCP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part4-nodejs&quot;&gt;Part 4: Node.js compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part5-scala&quot;&gt;Part 5: Scala support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part6-user-defined-snapshotting&quot;&gt;Part 6: User-defined snapshotting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;Part 7: Configuration and Secrets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part8-template-simplifications&quot;&gt;Part 8: Template simplifications and automatic updates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;Part 9: Agent skills&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part10-websocket&quot;&gt;Part 10: WebSocket client&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;Part 11: Bridge libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;Part 12: REPL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part13-per-agent-config&quot;&gt;Part 13: Per-agent configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part14-otlp&quot;&gt;Part 14: OpenTelemetry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part15-moonbit&quot;&gt;Part 15: MoonBit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part16-quotas&quot;&gt;Part 16: Quotas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part17-semantic-retry-policies&quot;&gt;Part 17: Semantic retry policies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;oplog-processor-plugins&quot;&gt;Oplog processor plugins&lt;/h2&gt;
&lt;p&gt;Previous Golem versions had an unfinished feature called &lt;strong&gt;oplog processor plugins&lt;/strong&gt;; the basics were already there but we did not have &lt;em&gt;exactly-once delivery semantics&lt;/em&gt; for it yet, and there were no actual use cases built on top of it.&lt;/p&gt;
&lt;p&gt;With &lt;strong&gt;Golem 1.5&lt;/strong&gt; the oplog processor plugins are now mature enough for real use. But what is an oplog processor?&lt;/p&gt;
&lt;p&gt;Oplog processors are special Golem agents implementing the following interface (currently we only have support for building oplog processors in Rust):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Example&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; OplogProcessorGuest&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Example&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; process&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        account_info&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AccountInfo&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        config&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        component_id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; ComponentId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        agent_id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AgentId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        metadata&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AgentMetadata&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        first_entry_index&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; OplogIndex&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        entries&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;OplogEntry&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Process the batch of oplog entries here&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;        Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;export_oplog_processor!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Example&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    with_types_in&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; golem_rust&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;oplog_processor&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For agents where a given oplog processor is enabled, the oplog processor instance will periodically receive &lt;code&gt;process&lt;/code&gt; calls with the following information:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;account information about the source agent (owner account, environment etc)&lt;/li&gt;
&lt;li&gt;the oplog processor&#39;s agent-specific configuration&lt;/li&gt;
&lt;li&gt;the source agent&#39;s component ID&lt;/li&gt;
&lt;li&gt;the source agent&#39;s ID (agent type and parameters, optionally with a phantom ID)&lt;/li&gt;
&lt;li&gt;detailed agent metadata&lt;/li&gt;
&lt;li&gt;a set of previously unseen &lt;strong&gt;oplog entries&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Golem guarantees that every oplog entry is sent exactly once to a given oplog processor. It does not have any guarantees though about how many oplog processor instances there are. By default it tries to keep an instance in every executor node, so the oplog delivery is always in-process without any network calls.&lt;/p&gt;
&lt;p&gt;Oplog processor plugins are real Golem agents with everything that involves, including &lt;strong&gt;durable execution&lt;/strong&gt;.&lt;/p&gt;
&lt;h3 id=&quot;creating-and-installing-plugins&quot;&gt;Creating and installing plugins&lt;/h3&gt;
&lt;p&gt;Plugins are WASM files with some metadata; it is possible to create new ones using the Rust Golem SDK and build them with the &lt;code&gt;golem&lt;/code&gt; CLI.&lt;/p&gt;
&lt;p&gt;The first step is to install the plugin with the CLI:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; plugin&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; register&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; my-plugin.yaml&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Where the YAML file is the plugin metadata:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ame&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;y-oplog-processor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ersion&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;escription&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; M&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;y custom oplog processor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;con&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;con.svg&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omepage&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ttps://example.com&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;pecs&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ype&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; O&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;plogProcessor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omponent&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/path/to/my-oplog-processor.wasm&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once it is registered, it can be referred to by its name and version in the &lt;strong&gt;application manifest&lt;/strong&gt;, and enable it for a component:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omponents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;y:component&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;lugins&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ame&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;y-oplog-processor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        v&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ersion&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;arameters&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          b&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;atch_size&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;arget_endpoint&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;https://logs.example.com&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It is possible to pass plugin-specific key-value pairs as configuration for each plugin installation. Once a plugin is enabled for a component and the change is deployed, every agent created from this component will have its oplog entries transferred to the plugin. This can be temporarily turned off using the CLI commands &lt;code&gt;golem agent activate-plugin&lt;/code&gt; and &lt;code&gt;golem agent deactivate-plugin&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;built-in-plugins&quot;&gt;Built-in plugins&lt;/h3&gt;
&lt;p&gt;Starting from &lt;strong&gt;Golem 1.5&lt;/strong&gt;, each release comes with a set of &lt;strong&gt;built-in plugins&lt;/strong&gt;. These are available for any application without explicitly installing anything. In this release we only have one built-in plugin for exporting agent information via &lt;strong&gt;OpenTelemetry&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 id=&quot;opentelemetry-plugin&quot;&gt;OpenTelemetry plugin&lt;/h2&gt;
&lt;p&gt;The OpenTelemetry plugin is a built-in plugin called &lt;code&gt;golem-otlp-exporter&lt;/code&gt; that exports agent behavior to &lt;strong&gt;OpenTelemetry&lt;/strong&gt; collectors via &lt;strong&gt;OTLP/HTTP&lt;/strong&gt;, including traces, logs and metrics.
The plugin takes the following configuration keys:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;endpoint&lt;/code&gt; the OTLP base URL&lt;/li&gt;
&lt;li&gt;&lt;code&gt;signals&lt;/code&gt; to enable a combination of &lt;code&gt;traces&lt;/code&gt;, &lt;code&gt;logs&lt;/code&gt; and &lt;code&gt;metrics&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;headers&lt;/code&gt; for custom HTTP headers to include in the OTLP requests&lt;/li&gt;
&lt;li&gt;&lt;code&gt;service-name-mode&lt;/code&gt; determines what the OTLP service name is going to be: either &lt;code&gt;agent-id&lt;/code&gt; or &lt;code&gt;agent-type&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&#39;s see what exactly gets exported by the plugin for the three different signals!&lt;/p&gt;
&lt;h3 id=&quot;traces&quot;&gt;Traces&lt;/h3&gt;
&lt;p&gt;OpenTelemetry &lt;strong&gt;spans&lt;/strong&gt; are created for a few built-in operations, such as agent invocation, RPC calls, outgoing HTTP requests. Users can also define custom spans using the SDK&#39;s:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; startSpan&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; currentContext&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; from&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;golem:api/context@1.5.0&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; span&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; startSpan&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;my-operation&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;span&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;setAttribute&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; tag&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;production&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;span&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;setAttributes&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; key&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;service&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; tag&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;my-service&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; key&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; tag&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ... do work ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; ctx&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; currentContext&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;console&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;trace_id: &lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;z-l-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;traceId&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;span&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;finish&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; golem_rust&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;bindings&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;api&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;context&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Start a span and set attributes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; span&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; context&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;start_span&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;my-operation&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;span&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;set_attribute&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;context&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;AttributeValue&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;String&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;production&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;span&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;set_attributes&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;    context&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Attribute&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; key&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;service&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; context&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;AttributeValue&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;String&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;my-service&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;    context&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Attribute&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; key&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; context&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;AttributeValue&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;String&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ... do work ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Read back context&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ctx&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; context&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;current_context&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;println!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;trace_id: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ctx&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;trace_id&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;span&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;finish&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span&gt; golem&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;host&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ContextApi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; span&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextApi&lt;/span&gt;&lt;span&gt;.startSpan&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;my-operation&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;span.setAttribute&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ContextApi&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AttributeValue&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;StringValue&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;production&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;span.setAttributes&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;List&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  ContextApi&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Attribute&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;service&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ContextApi&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AttributeValue&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;StringValue&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;my-service&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  ContextApi&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Attribute&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ContextApi&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AttributeValue&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;StringValue&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ... do work ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ctx&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextApi&lt;/span&gt;&lt;span&gt;.currentContext&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;println&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; ${&lt;/span&gt;&lt;span&gt;ctx.traceId&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;span.finish&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;// Using with_span for automatic lifecycle management:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;@context&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;with_span&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;my-operation&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  attributes&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;production&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;service&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;my-service&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;span&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    // ... do work ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    // Add more attributes dynamically if needed:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    span&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;set_attribute&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;processing&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ctx&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @context&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;current_context&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    println&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;trace_id: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ctx&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;trace_id&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note that trace and span IDs are properly propagated from inbound HTTP requests through the &lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;code-first routes&lt;/a&gt;, and outgoing HTTP requests are also having the standard trace headers. This is a feature independent from the OTLP plugin.&lt;/p&gt;
&lt;p&gt;For &lt;strong&gt;TypeScript&lt;/strong&gt; there is an alternative - we wired the invocation context feature to our runtime&#39;s Node.js &lt;code&gt;node:diagnostics_channel&lt;/code&gt; module:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; tracingChannel&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; from&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;node:diagnostics_channel&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Now any traceSync/tracePromise call automatically creates a Golem span&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; with attributes from the context object&amp;#39;s properties&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; dc&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; tracingChannel&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;my-operation&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; dc&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;traceSync&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ... do work ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 42&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; method&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;GET&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; url&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/api/data&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; env&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;production&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; these become span attributes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;logs&quot;&gt;Logs&lt;/h3&gt;
&lt;p&gt;When log exporting is enabled, any log entry (standard output/err or using the dedicated log interfaces) are being forwarded to the OTLP collector.&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;console&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Hello from TypeScript!&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;console&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;debug&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;This is a debug log entry&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; log&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;debug&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;println!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Hello from Rust!&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;debug!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;This is a debug log entry&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;println&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Hello from Scala!&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Logging&lt;/span&gt;&lt;span&gt;.log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LogLevel&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Debug&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;This is a debug log entry&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;println&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Hello from MoonBit!&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;@log&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;debug&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;This is a debug log entry&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;metrics&quot;&gt;Metrics&lt;/h3&gt;
&lt;p&gt;When metrics are enabled, the plugin will send the following metrics to the OTLP collector:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Metric Name&lt;/th&gt;&lt;th&gt;Type&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_invocation_count&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Counter&lt;/td&gt;&lt;td&gt;Number of agent method invocations&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_invocation_duration_ns&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Counter&lt;/td&gt;&lt;td&gt;Invocation duration&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_invocation_fuel_consumed&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Counter&lt;/td&gt;&lt;td&gt;Fuel consumed by invocations&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_invocation_pending_count&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Counter&lt;/td&gt;&lt;td&gt;Number of pending invocations&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_host_call_count&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Counter&lt;/td&gt;&lt;td&gt;Number of internal host calls&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_log_count&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Counter&lt;/td&gt;&lt;td&gt;Number of log entries emitted&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_memory_initial_bytes&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Gauge&lt;/td&gt;&lt;td&gt;Initially allocated memory&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_memory_total_bytes&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Gauge&lt;/td&gt;&lt;td&gt;Total allocated memory&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_memory_growth_bytes&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Counter&lt;/td&gt;&lt;td&gt;Memory growth since start&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_component_size_bytes&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Gauge&lt;/td&gt;&lt;td&gt;Component size in bytes&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_error_count&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Counter&lt;/td&gt;&lt;td&gt;Number of recorded errors&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_interruption_count&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Counter&lt;/td&gt;&lt;td&gt;Number of interrupt requests&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_exit_count&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Counter&lt;/td&gt;&lt;td&gt;Number of process exit signals&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_restart_count&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Counter&lt;/td&gt;&lt;td&gt;Number of times a fresh state was created&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_resources_created&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Counter&lt;/td&gt;&lt;td&gt;Number of internal resources created&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_resources_dropped&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Counter&lt;/td&gt;&lt;td&gt;Number of internal resources dropped&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_resources_active&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Gauge&lt;/td&gt;&lt;td&gt;Number of active internal resources&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_update_success_count&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Counter&lt;/td&gt;&lt;td&gt;Number of successful updates&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_update_failure_count&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Counter&lt;/td&gt;&lt;td&gt;Number of failed updates&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_transaction_committed&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Counter&lt;/td&gt;&lt;td&gt;Number of committed database transactions&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_transaction_rolled_back&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Counter&lt;/td&gt;&lt;td&gt;Number of rolled back database transactions&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_snapshot_size_bytes&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Counter&lt;/td&gt;&lt;td&gt;Snapshot size in bytes&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;golem_oplog_processor_lag&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Gauge&lt;/td&gt;&lt;td&gt;Oplog processor delivery lag&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Each metric is annotated with the standard &lt;code&gt;service.name&lt;/code&gt; that identifies the agent type, with &lt;code&gt;golem.agent.id&lt;/code&gt; identifying the instance, and with &lt;code&gt;golem.component.id&lt;/code&gt; and &lt;code&gt;golem.component.version&lt;/code&gt; to identify the deployment.&lt;/p&gt;
&lt;h3 id=&quot;setting-it-up&quot;&gt;Setting it up&lt;/h3&gt;
&lt;p&gt;As it is a built-in plugin, the only thing necessary is enabling it in the &lt;strong&gt;application manifest&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omponents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  o&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;tlp-demo:ts-main&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;emplates&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;lugins&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ame&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; g&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;olem-otlp-exporter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        v&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ersion&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1.5.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;arameters&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          e&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ndpoint&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;http://localhost:4318&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ignals&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;traces,logs,metrics&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After &lt;code&gt;golem deploy&lt;/code&gt;, newly created agents defined in this component are going to send their information to the OTLP exporter plugin.&lt;/p&gt;
&lt;p&gt;For example we can launch a local stack of &lt;code&gt;opentelemetry-collector&lt;/code&gt; / &lt;code&gt;grafana&lt;/code&gt; / &lt;code&gt;loki&lt;/code&gt; / &lt;code&gt;prometheus&lt;/code&gt; / &lt;code&gt;jaeger&lt;/code&gt; to get support for all three signals. The golem repository contains an example docker compose setup for this, as well as an &lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;agent skill&lt;/a&gt; describing how to set it up.&lt;/p&gt;
&lt;h3 id=&quot;example&quot;&gt;Example&lt;/h3&gt;
&lt;p&gt;On the following screenshots we can see how this default stack shows traces, logs and metrics from a simple &lt;em&gt;counter agent example&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Traces&lt;/strong&gt;:&lt;/p&gt;
&lt;img src=&quot;/images/golem15-otlp-jaeger.webp&quot; alt=&quot;Jaeger trace view of a Golem agent invocation showing its two spans&quot; width=&quot;1500&quot; height=&quot;310&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;&lt;strong&gt;Metrics&lt;/strong&gt;:&lt;/p&gt;
&lt;img src=&quot;/images/golem15-otlp-prometheus.webp&quot; alt=&quot;Grafana graph of the golem_invocation_count_total Prometheus metric per agent&quot; width=&quot;788&quot; height=&quot;649&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;&lt;strong&gt;Logs&lt;/strong&gt;:&lt;/p&gt;
&lt;img src=&quot;/images/golem15-otlp-loki.webp&quot; alt=&quot;Grafana Loki showing OTLP-exported Golem agent logs with the agent ID as service name&quot; width=&quot;1600&quot; height=&quot;896&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.5 features - Part 13: Per-agent configuration</title>
        <published>2026-04-21T00:00:00+00:00</published>
        <updated>2026-04-21T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem15-part13-per-agent-config/"/>
        <id>https://blog.vigoo.dev/posts/golem15-part13-per-agent-config/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem15-part13-per-agent-config/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I am writing a series of &lt;em&gt;short&lt;/em&gt; posts showcasing the new features of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, to be released at the end of April, 2026. The episodes of this series will be short and assume the reader knows what Golem is. Check my &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem/&quot;&gt;other Golem-related posts&lt;/a&gt; for more information!&lt;/p&gt;
&lt;p&gt;Parts released so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;Part 1: Code-first routes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part2-webhooks&quot;&gt;Part 2: Webhooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part3-mcp&quot;&gt;Part 3: MCP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part4-nodejs&quot;&gt;Part 4: Node.js compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part5-scala&quot;&gt;Part 5: Scala support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part6-user-defined-snapshotting&quot;&gt;Part 6: User-defined snapshotting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;Part 7: Configuration and Secrets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part8-template-simplifications&quot;&gt;Part 8: Template simplifications and automatic updates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;Part 9: Agent skills&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part10-websocket&quot;&gt;Part 10: WebSocket client&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;Part 11: Bridge libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;Part 12: REPL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part13-per-agent-config&quot;&gt;Part 13: Per-agent configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part14-otlp&quot;&gt;Part 14: OpenTelemetry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part15-moonbit&quot;&gt;Part 15: MoonBit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part16-quotas&quot;&gt;Part 16: Quotas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part17-semantic-retry-policies&quot;&gt;Part 17: Semantic retry policies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;components-vs-agents&quot;&gt;Components vs agents&lt;/h2&gt;
&lt;p&gt;Previously, in Golem &lt;strong&gt;components&lt;/strong&gt; were the most important user-defined entities. Components were the unit of compilation, each transformed to be a &lt;strong&gt;WebAssembly component&lt;/strong&gt; and all the customization such as environment variables, initial file system and so on were configurable &lt;strong&gt;per component&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;In &lt;strong&gt;Golem 1.5&lt;/strong&gt; the primary entity is an &lt;strong&gt;agent&lt;/strong&gt;. Agents are still organized into components - these are the unit of compilation and deployment, but each component can contain an arbitrary number of agents. These agents have to have unique type names in the whole application (that can consist of multiple components) so whenever we refer to a specific agent instance, for example to invoke them or query its metadata or oplog, we no longer need to deal with &lt;strong&gt;component ids&lt;/strong&gt; like before. We can identify the agents just by their type name and constructor parameters.&lt;/p&gt;
&lt;p&gt;To further continue our journey towards being &lt;em&gt;agent first&lt;/em&gt;, in the new release all the customization is &lt;strong&gt;per agent&lt;/strong&gt;. We can configure everything on the agent level:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;environment variables&lt;/li&gt;
&lt;li&gt;initial file system&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;bridge generation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With this small detail application manifests became much more in sync with how we can configure some other aspects (such as &lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;http routes&lt;/a&gt; and &lt;a href=&quot;/posts/golem15-part6-user-defined-snapshotting&quot;&gt;snapshotting&lt;/a&gt;) per agent from the code itself.&lt;/p&gt;
&lt;h3 id=&quot;example&quot;&gt;Example&lt;/h3&gt;
&lt;p&gt;The following example shows how we can configure different settings for three agents defined in the same component.&lt;/p&gt;
&lt;p&gt;We start with the application&#39;s shared runtime settings. We define them as a &lt;strong&gt;component template&lt;/strong&gt; so we can apply them to any component in our application, and every agent in those components will get them:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;anifestVersion&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1.5.0&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;pp&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;upportdesk&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omponentTemplates&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;hared-runtime&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    e&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nv&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      R&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;UST_LOG&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;nfo&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      T&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Z&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; U&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;TC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    f&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;iles&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ourcePath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; .&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/shared/ca-certificates.pem&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;argetPath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;certs/ca-certificates.pem&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ermissions&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ead-only&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;resets&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ocal&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;efault&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        e&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nv&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          G&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;OLEM_ENV&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ocal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;loud&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        e&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nv&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          G&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;OLEM_ENV&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;loud&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omponents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;upportdesk:agents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ir&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; .&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;emplates&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;hared-runtime&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we can provide additional configuration &lt;strong&gt;per agent&lt;/strong&gt;. For &lt;code&gt;InboxAgent&lt;/code&gt; we override the environment, config and initial file system:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;gents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  I&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nboxAgent&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    e&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nv&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      O&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;PENAI_API_KEY&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{{ OPENAI_API_KEY }}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      M&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ODEL&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; g&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;pt-4.1-mini&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    f&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;iles&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ourcePath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; .&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/prompts/inbox-system.md&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;argetPath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;prompts/system.md&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ermissions&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ead-only&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ourcePath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; .&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/data/routing-rules.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;argetPath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;data/routing-rules.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ermissions&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ead-only&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;onfig&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;efaultQueue&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; g&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;eneral&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ummarizeReplies&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;lassification&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;onfidenceThreshold&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0.75&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;abels&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;illing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;utage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;roduct&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can define a completely different set of overrides for the other agents. Note that each agent has its own typed configuration, and we only have to satisfy each agent&#39;s own requirements.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;gents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  E&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;scalationAgent&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    e&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nv&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      J&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;IRA_BASE_URL&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ttps://acme.atlassian.net&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      J&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;IRA_TOKEN&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{{ JIRA_TOKEN }}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      M&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ODEL&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;laude-3-7-sonnet&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    f&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;iles&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ourcePath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; .&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/prompts/escalation-system.md&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;argetPath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;prompts/system.md&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ermissions&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ead-only&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ourcePath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; .&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/playbooks/p1-outage.md&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;argetPath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;playbooks/p1-outage.md&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ermissions&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ead-only&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ourcePath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ttps://example.com/runbooks/severity-guide.md&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;argetPath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;playbooks/severity-guide.md&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ermissions&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ead-only&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;onfig&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;rojectKey&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; O&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;PS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;efaultPriority&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;igh&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;agerduty&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;erviceId&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; P&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;123456&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;utoPageAfterMinutes&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  A&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;uditAgent&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    e&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nv&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      S&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;3_BUCKET&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;upportdesk-audit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    f&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;iles&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ourcePath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; .&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/schemas/audit-event.schema.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;argetPath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;schemas/audit-event.schema.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ermissions&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ead-only&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;onfig&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      r&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;edactFields&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;mail&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;hone&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;aymentToken&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      r&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;etentionDays&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 90&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, we can configure the &lt;strong&gt;bridge generator&lt;/strong&gt; also per agent:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ridge&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;s&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;gents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; I&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;nboxAgent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; E&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;scalationAgent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  r&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ust&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;gents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;uditAgent&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When we define our &lt;strong&gt;environments&lt;/strong&gt; - these are the targets where we can deploy our application to - we can define &lt;code&gt;componentPresets&lt;/code&gt;. This selects one section from the &lt;code&gt;presets&lt;/code&gt; map we defined in the first snippet. This feature allows us to have different environment variables / configuration per deployment target!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nvironments&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ocal&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;efault&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;erver&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ocal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omponentPresets&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ocal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;rod&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;erver&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;loud&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omponentPresets&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;loud&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As demonstrated, the Golem application manifest is very powerful and allows quite complicated setup, even though in its default form it is just a few lines long. To help working on these files, we are publishing a full &lt;strong&gt;JSON schema&lt;/strong&gt; for it as well as a dedicated &lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;agent skill&lt;/a&gt;.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.5 features - Part 12: REPL</title>
        <published>2026-04-20T20:00:00+00:00</published>
        <updated>2026-04-20T20:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem15-part12-repl/"/>
        <id>https://blog.vigoo.dev/posts/golem15-part12-repl/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem15-part12-repl/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I am writing a series of &lt;em&gt;short&lt;/em&gt; posts showcasing the new features of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, to be released at the end of April, 2026. The episodes of this series will be short and assume the reader knows what Golem is. Check my &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem/&quot;&gt;other Golem-related posts&lt;/a&gt; for more information!&lt;/p&gt;
&lt;p&gt;Parts released so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;Part 1: Code-first routes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part2-webhooks&quot;&gt;Part 2: Webhooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part3-mcp&quot;&gt;Part 3: MCP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part4-nodejs&quot;&gt;Part 4: Node.js compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part5-scala&quot;&gt;Part 5: Scala support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part6-user-defined-snapshotting&quot;&gt;Part 6: User-defined snapshotting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;Part 7: Configuration and Secrets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part8-template-simplifications&quot;&gt;Part 8: Template simplifications and automatic updates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;Part 9: Agent skills&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part10-websocket&quot;&gt;Part 10: WebSocket client&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;Part 11: Bridge libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;Part 12: REPL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part13-per-agent-config&quot;&gt;Part 13: Per-agent configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part14-otlp&quot;&gt;Part 14: OpenTelemetry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part15-moonbit&quot;&gt;Part 15: MoonBit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part16-quotas&quot;&gt;Part 16: Quotas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part17-semantic-retry-policies&quot;&gt;Part 17: Semantic retry policies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;testing-agents-with-a-repl&quot;&gt;Testing agents with a REPL&lt;/h2&gt;
&lt;p&gt;The Golem REPL was an important element of &lt;strong&gt;testing agents&lt;/strong&gt; during development. The REPL allows you to get or create agent instances, invoke methods on them, and see their logs streamed during these invocations.&lt;/p&gt;
&lt;p&gt;In previous Golem versions, the REPL used our own scripting language called &lt;a rel=&quot;external&quot; href=&quot;https://golemcloud.github.io/rib/guide.html&quot;&gt;Rib&lt;/a&gt;. The same language was used for implementing HTTP APIs, but &lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;we changed that to be directly expressed in the agent&#39;s code&lt;/a&gt;. It did not make much sense to keep a custom scripting language just for interacting with agent instances, because it required learning a completely new syntax and mapping between its data types and the &quot;real&quot; data types in the agent&#39;s source code.&lt;/p&gt;
&lt;h3 id=&quot;typescript-repl&quot;&gt;TypeScript REPL&lt;/h3&gt;
&lt;p&gt;We decided to completely remove Rib from Golem, make it available as a standalone tool, and &lt;a rel=&quot;external&quot; href=&quot;https://github.com/bytecodealliance/wasmtime/issues/13136&quot;&gt;offer it&lt;/a&gt; for integration with &lt;code&gt;wasmtime&lt;/code&gt;. We no longer need to use Rib for anything in Golem, and our primary REPL is now a real &lt;strong&gt;TypeScript REPL&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The TypeScript REPL behaves just like you would expect, and it has all the agent client classes from &lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;the bridge libraries&lt;/a&gt; in the global scope, preconfigured. The following example shows how we can invoke the default counter example through it:&lt;/p&gt;
&lt;div id=&quot;demo1&quot;&gt;&lt;/div&gt;
&lt;p&gt;Note that the TypeScript REPL is not restricted to only agents &lt;em&gt;written&lt;/em&gt; in TypeScript. We can call any agent from it in the same, type-safe way, even if they are Rust, Scala or MoonBit agents.&lt;/p&gt;
&lt;h4 id=&quot;cli-commands-from-the-repl&quot;&gt;CLI commands from the REPL&lt;/h4&gt;
&lt;p&gt;In addition to the TypeScript classes for calling the agents, the REPL also provides a large set of &lt;strong&gt;built-in commands&lt;/strong&gt;. These accept both &lt;code&gt;.&lt;/code&gt; and &lt;code&gt;:&lt;/code&gt; prefixes, and many of them match CLI commands directly. This means we can run all the usual commands such as build, deploy, or inspecting agent logs without leaving the REPL:&lt;/p&gt;
&lt;div id=&quot;demo2&quot;&gt;&lt;/div&gt;
&lt;h4 id=&quot;running-scripts&quot;&gt;Running scripts&lt;/h4&gt;
&lt;p&gt;It is also possible to run whole TypeScript scripts through the REPL in a non-interactive way.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; cat&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; test.ts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; c2&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; CounterAgent.get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;c2&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; c2.increment&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; c2.increment&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; repl&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-script-file&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; test.ts&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-yes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;rust-repl&quot;&gt;Rust REPL&lt;/h3&gt;
&lt;p&gt;We wanted to bring a first-class REPL experience to every supported language, but with Golem 1.5 we are not there yet. We &lt;em&gt;do&lt;/em&gt; have a Rust REPL, but, as expected, it is very slow because it has to continuously recompile things. We kept it mainly because it can also run Rust &quot;scripts&quot;. In that mode the slowness is much less of an issue, and it lets these test scripts (potentially generated by a coding agent on the fly) use the same language as the agents themselves.&lt;/p&gt;
&lt;p&gt;The following example demonstrates this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; cat&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; test.rs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; c3&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; CounterAgent::get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;c3&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.await.unwrap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;c3.increment&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;.await&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;c3.increment&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;.await&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; repl&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-script-file&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; test.rs&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-language&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; rust&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-yes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;what-about-scala-and-moonbit&quot;&gt;What about Scala and MoonBit?&lt;/h3&gt;
&lt;p&gt;Unfortunately we don&#39;t have any REPL implementation for Scala and MoonBit in &lt;strong&gt;Golem 1.5&lt;/strong&gt;. This means we cannot use Scala or MoonBit syntax in the interactive REPL or in test scripts, but the TypeScript and Rust REPLs have full support for any agent, even if implemented in another language.&lt;/p&gt;
&lt;link rel=&quot;preconnect&quot; href=&quot;https://cdn.jsdelivr.net&quot; crossorigin&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://cdn.jsdelivr.net/npm/asciinema-player@3.9.0/dist/bundle/asciinema-player.css&quot; /&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/asciinema-player@3.9.0/dist/bundle/asciinema-player.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
  function mountAsciinema(id, url, cols, rows) {
    const host = document.getElementById(id)

    const syncHeight = () =&gt; {
      const player = host.querySelector(&#39;.ap-player&#39;)
      if (player) {
        host.style.height = `${player.getBoundingClientRect().height}px`
      }
    }

    AsciinemaPlayer.create(url, host, { theme: &#39;monokai&#39;, fit: &#39;width&#39;, cols, rows })

    requestAnimationFrame(() =&gt; requestAnimationFrame(syncHeight))
    setTimeout(syncHeight, 250)
    setTimeout(syncHeight, 1000)
    window.addEventListener(&#39;resize&#39;, syncHeight)
  }

  mountAsciinema(&#39;demo1&#39;, &#39;/images/repl-demo2.cast&#39;, 237, 48)
  mountAsciinema(&#39;demo2&#39;, &#39;/images/repl-demo3.cast&#39;, 237, 48)
&lt;/script&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.5 features - Part 11: Bridge libraries</title>
        <published>2026-04-20T00:00:00+00:00</published>
        <updated>2026-04-20T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem15-part11-bridges/"/>
        <id>https://blog.vigoo.dev/posts/golem15-part11-bridges/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem15-part11-bridges/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I am writing a series of &lt;em&gt;short&lt;/em&gt; posts showcasing the new features of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, to be released at the end of April, 2026. The episodes of this series will be short and assume the reader knows what Golem is. Check my &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem/&quot;&gt;other Golem-related posts&lt;/a&gt; for more information!&lt;/p&gt;
&lt;p&gt;Parts released so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;Part 1: Code-first routes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part2-webhooks&quot;&gt;Part 2: Webhooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part3-mcp&quot;&gt;Part 3: MCP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part4-nodejs&quot;&gt;Part 4: Node.js compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part5-scala&quot;&gt;Part 5: Scala support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part6-user-defined-snapshotting&quot;&gt;Part 6: User-defined snapshotting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;Part 7: Configuration and Secrets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part8-template-simplifications&quot;&gt;Part 8: Template simplifications and automatic updates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;Part 9: Agent skills&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part10-websocket&quot;&gt;Part 10: WebSocket client&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;Part 11: Bridge libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;Part 12: REPL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part13-per-agent-config&quot;&gt;Part 13: Per-agent configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part14-otlp&quot;&gt;Part 14: OpenTelemetry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part15-moonbit&quot;&gt;Part 15: MoonBit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part16-quotas&quot;&gt;Part 16: Quotas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part17-semantic-retry-policies&quot;&gt;Part 17: Semantic retry policies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;calling-agents-from-outside-of-golem&quot;&gt;Calling agents from outside of Golem&lt;/h2&gt;
&lt;p&gt;There are multiple ways to create and invoke &lt;strong&gt;Golem agents&lt;/strong&gt; from &lt;em&gt;outside&lt;/em&gt; of Golem. It is possible to &lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;expose agents through HTTP&lt;/a&gt; or &lt;a href=&quot;/posts/golem15-part3-mcp&quot;&gt;MCP&lt;/a&gt;, to use the CLI or the new &lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;REPL&lt;/a&gt;, or just use Golem&#39;s own REST API directly.&lt;/p&gt;
&lt;p&gt;With &lt;strong&gt;Golem 1.5&lt;/strong&gt; we added one more way to this list: generating &lt;strong&gt;bridge libraries&lt;/strong&gt;. A bridge library is a self-contained Rust crate or TypeScript npm package implementing a fully &lt;strong&gt;type-safe&lt;/strong&gt; client for a specific agent, to be used in non-golem applications. The Rust crate is built on &lt;code&gt;reqwest&lt;/code&gt; for making requests to Golem&#39;s REST API under the hood, while the TypeScript one uses &lt;code&gt;fetch&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Note that due to time constraints, Golem 1.5 will not have bridge generators for Scala and MoonBit as a target language, but we will add them in a future release. This means that it is possible to generate a Rust or TypeScript library to work with agents written in Scala or MoonBit, but it is not possible to generate a Scala or MoonBit library yet.&lt;/p&gt;
&lt;h3 id=&quot;enabling-the-bridge-generator&quot;&gt;Enabling the bridge generator&lt;/h3&gt;
&lt;p&gt;Generating these bridge libraries can be enabled &lt;strong&gt;per-agent&lt;/strong&gt; and &lt;strong&gt;per language&lt;/strong&gt;. The following example enables generating a TypeScript package to call the &lt;code&gt;CounterAgent&lt;/code&gt;, and one Rust crate for &lt;em&gt;every agent&lt;/em&gt; in the project:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ridge&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;s&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;gents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; C&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ounterAgent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  r&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ust&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;gents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After running &lt;code&gt;golem build&lt;/code&gt;, the generated bridges are in the &lt;code&gt;golem-temp&lt;/code&gt; directory:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;golem-temp/bridge-sdk/ts/counter-agent-client&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;golem-temp/bridge-sdk/rust/counter-agent-client&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;using-the-bridge-libraries&quot;&gt;Using the bridge libraries&lt;/h3&gt;
&lt;p&gt;The generated libraries follow the same conventions as our &lt;strong&gt;agent-to-agent&lt;/strong&gt; communication implementation: there is a type matching the agent type&#39;s name with static constructor methods such as &lt;code&gt;get&lt;/code&gt; (upserts a Golem agent identified by its constructor parameters), and &lt;code&gt;getPhantom&lt;/code&gt;/&lt;code&gt;newPhantom&lt;/code&gt; for phantom agents. There are also variants for overriding &lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;configuration&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The following example demonstrates how to call the default template&#39;s simple &lt;em&gt;counter agent&lt;/em&gt; from arbitrary Rust and TypeScript applications using the generated bridges:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  CounterAgent&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  configure&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; from&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;counter-agent-client/counter-agent-client.js&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;configure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  server&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; type&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;local&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  application&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;bridgetest&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  environment&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;local&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; c1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; c1&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;increment&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; counter_agent_client&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;CounterAgent&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; golem_client&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;bridge&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;GolemServer&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;CounterAgent&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;configure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;    GolemServer&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Local&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;bridgetest&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;local&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c1&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;await&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c1&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;increment&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;await&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the configuration call we have to specify which Golem server to connect to - &lt;code&gt;Local&lt;/code&gt; is the default local &lt;code&gt;golem server run&lt;/code&gt; instance, but it can also connect to our hosted &lt;code&gt;Cloud&lt;/code&gt; or to any custom deployment. The second parameter is the application name (can be found in the &lt;code&gt;app:&lt;/code&gt; key of &lt;code&gt;golem.yaml&lt;/code&gt;), and the third is the environment name (there can be multiple environments on the same server for an application, for example staging and prod).&lt;/p&gt;
&lt;p&gt;Although not demonstrated by this simple example, these generated libraries are fully type-safe, defining all the custom data types used in the parameter list or return type of the agent methods.&lt;/p&gt;
&lt;h3 id=&quot;method-variants&quot;&gt;Method variants&lt;/h3&gt;
&lt;p&gt;Just like with agent-to-agent communication, each agent method has multiple &lt;em&gt;variants&lt;/em&gt; in the generated client. The default, matching the agent method&#39;s name, invokes the method and &lt;strong&gt;awaits&lt;/strong&gt; its result. There is also a way to just &lt;strong&gt;trigger&lt;/strong&gt; the invocation without awaiting it, or to &lt;strong&gt;schedule&lt;/strong&gt; it to be called at a given point in time.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.5 features - Part 10: WebSocket client</title>
        <published>2026-04-18T11:50:00+00:00</published>
        <updated>2026-04-18T11:50:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem15-part10-websocket/"/>
        <id>https://blog.vigoo.dev/posts/golem15-part10-websocket/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem15-part10-websocket/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I am writing a series of &lt;em&gt;short&lt;/em&gt; posts showcasing the new features of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, to be released at the end of April, 2026. The episodes of this series will be short and assume the reader knows what Golem is. Check my &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem/&quot;&gt;other Golem-related posts&lt;/a&gt; for more information!&lt;/p&gt;
&lt;p&gt;Parts released so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;Part 1: Code-first routes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part2-webhooks&quot;&gt;Part 2: Webhooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part3-mcp&quot;&gt;Part 3: MCP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part4-nodejs&quot;&gt;Part 4: Node.js compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part5-scala&quot;&gt;Part 5: Scala support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part6-user-defined-snapshotting&quot;&gt;Part 6: User-defined snapshotting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;Part 7: Configuration and Secrets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part8-template-simplifications&quot;&gt;Part 8: Template simplifications and automatic updates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;Part 9: Agent skills&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part10-websocket&quot;&gt;Part 10: WebSocket client&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;Part 11: Bridge libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;Part 12: REPL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part13-per-agent-config&quot;&gt;Part 13: Per-agent configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part14-otlp&quot;&gt;Part 14: OpenTelemetry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part15-moonbit&quot;&gt;Part 15: MoonBit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part16-quotas&quot;&gt;Part 16: Quotas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part17-semantic-retry-policies&quot;&gt;Part 17: Semantic retry policies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;websockets&quot;&gt;WebSockets&lt;/h2&gt;
&lt;p&gt;Golem applications are WebAssembly components and the only way they can make external requests is through the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/WebAssembly/WASI/tree/main/proposals/http/&quot;&gt;WASI HTTP interface&lt;/a&gt;. This is not really visible for Golem developers - in TypeScript and Scala the standard &lt;code&gt;fetch&lt;/code&gt; or &lt;code&gt;node:http&lt;/code&gt; interfaces are hiding this fact, just like in Rust where higher level HTTP libraries like &lt;code&gt;wstd::http&lt;/code&gt; can be used.&lt;/p&gt;
&lt;p&gt;This HTTP interface has its limitations; one such limitation is that it does not support upgrading to a WebSocket connection.&lt;/p&gt;
&lt;h3 id=&quot;websocket-client-api&quot;&gt;WebSocket client API&lt;/h3&gt;
&lt;p&gt;In &lt;strong&gt;Golem 1.5&lt;/strong&gt; we introduce a new WebSocket client API that complements the WASI HTTP one for connecting to 3rd party WebSocket servers.&lt;/p&gt;
&lt;p&gt;Under the hood this API is described by the following WebAssembly interface:&lt;/p&gt;
&lt;details&gt;
&lt;summary&gt;WIT definition of &lt;code&gt;golem:websocket@1.5.0&lt;/code&gt;&lt;/summary&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;wit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; golem&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;websocket&lt;/span&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1.5.0;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; client&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; wasi&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;io&lt;/span&gt;&lt;span&gt;/&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;poll&lt;/span&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0.2.3&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;pollable&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  variant&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; error&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    connection-failure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    send-failure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    receive-failure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    protocol-error&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    closed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;close-info&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    other&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  record&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; close-info&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    code&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u16&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    reason&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  ///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; A WebSocket message — text or binary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  variant&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; message&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    text&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    binary&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;u8&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  ///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; A WebSocket connection resource&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  resource&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; websocket-connection&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Connect to a WebSocket server at the given URL (ws:// or wss://)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Optional headers for auth, subprotocols, etc.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    connect&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; static &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      url&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      headers&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;tuple&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;websocket-connection&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Send a message (text or binary)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    send&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;message&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; message&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;_&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Receive the next message (blocks until available)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    receive&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;message&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Receive the next message with a timeout in milliseconds.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Returns none if the timeout expires before a message arrives.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    receive&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-with&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;timeout&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;timeout-ms&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u64&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;message&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Send a close frame with optional code and reason&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    close&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;code&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;u16&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; reason&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;_&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Returns a pollable that resolves when a message is available to read&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    subscribe&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; pollable&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/details&gt;
&lt;p&gt;This is just an implementation detail which can be mostly hidden by higher level APIs provided by our language-specific SDKs, just like the WASI HTTP interfaces are.&lt;/p&gt;
&lt;h3 id=&quot;higher-level-websocket-apis&quot;&gt;Higher level WebSocket APIs&lt;/h3&gt;
&lt;p&gt;In &lt;strong&gt;TypeScript&lt;/strong&gt; the WebSocket support is implemented through the standard browser &lt;a rel=&quot;external&quot; href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/WebSocket&quot;&gt;&lt;code&gt;WebSocket&lt;/code&gt;&lt;/a&gt; and &lt;a rel=&quot;external&quot; href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/WebSocketStream&quot;&gt;&lt;code&gt;WebSocketStream&lt;/code&gt;&lt;/a&gt; APIs.&lt;/p&gt;
&lt;p&gt;In &lt;strong&gt;Rust&lt;/strong&gt; we could not override the behavior of an existing WebSocket library so the Golem Rust SDK provides its own, inspired by the popular &lt;a rel=&quot;external&quot; href=&quot;https://github.com/snapview/tungstenite-rs&quot;&gt;tungstenite&lt;/a&gt; library.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Scala&lt;/strong&gt; compiles to JS on Golem, so the most straightforward approach is to use the browser &lt;code&gt;WebSocket&lt;/code&gt;/&lt;code&gt;WebSocketStream&lt;/code&gt; APIs. At the time of writing &lt;a rel=&quot;external&quot; href=&quot;https://ziohttp.com&quot;&gt;&lt;code&gt;zio-http&lt;/code&gt;&lt;/a&gt; does not support WebSockets on Scala.js, but this is something we could possibly fix!&lt;/p&gt;
&lt;p&gt;In &lt;strong&gt;MoonBit&lt;/strong&gt; we can directly use the low level WIT interface through the generated bindings.&lt;/p&gt;
&lt;h4 id=&quot;example&quot;&gt;Example&lt;/h4&gt;
&lt;p&gt;The following example shows how an agent method can initiate and run a WebSocket connection:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;void&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;resolve&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; reject&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; ws&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; WebSocket&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;wss://example.com/chat&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;      ws&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;onopen&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;        console&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Connected&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;        ws&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;send&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Hello, server!&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;      ws&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;onmessage&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MessageEvent&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-7&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; ===&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;          console&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Text:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;data&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;          console&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Binary:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Uint8Array&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;data&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;      ws&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;onerror&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; reject&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Error&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;WebSocket error&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;      ws&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;onclose&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CloseEvent&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;        console&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Closed [&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;z-l-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;] &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;z-l-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;reason&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        resolve&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_implementation&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; ExampleAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; ExampleAgentImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; WebSocketError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ws&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; WebsocketConnection&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;connect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;wss://example.com/chat&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        println!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Connected&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        ws&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;send&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;WebSocketMessage&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Text&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Hello, server!&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        loop&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            match&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ws&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;receive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;await&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;                Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;WebSocketMessage&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Text&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;text&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; println!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Text: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;                Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;WebSocketMessage&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Binary&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;data&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; println!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Binary: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;data:?&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;                Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;WebSocketError&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Closed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;info&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                    if&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; let&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;info&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; info&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;                        println!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Closed [&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;] &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; info&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;code&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; info&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;reason&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                    break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;                Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;err&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; return&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;err&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;        Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleAgentImpl&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; done&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ws&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; WebSocket&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;wss://example.com/chat&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ws.onopen &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;_&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      println&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Connected&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      ws.send&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Hello, server!&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ws.onmessage &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;event&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MessageEvent&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      event.data &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;match&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; text&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; println&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; $&lt;/span&gt;&lt;span&gt;text&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span&gt; other        &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; println&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; $&lt;/span&gt;&lt;span&gt;other&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ws.onerror &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;_&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      done.tryFailure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Exception&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;WebSocket error&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ws.onclose &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;event&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CloseEvent&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      println&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;${&lt;/span&gt;&lt;span&gt;event.code&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; \&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;${&lt;/span&gt;&lt;span&gt;event.reason&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      done.trySuccess&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    done.future&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleAgent&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; raise&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @common&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AgentError&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; conn&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; match&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @websocket_client&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WebsocketConnection&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;connect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;wss://example.com/chat&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;c&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;e&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; raise&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @common&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AgentError&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;InvalidInput&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Connect failed: &lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  println&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Connected&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  match&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; conn&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;send&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Text&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Hello, server!&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;_&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;e&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; raise&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @common&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AgentError&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;InvalidInput&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Send failed: &lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  while&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    match&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; conn&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;receive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Text&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;msg&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; println&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Text: &lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Binary&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;data&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; println&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Binary: &lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;data&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;length&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; bytes&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Closed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;info&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        println&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Closed [&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;info&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;] &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;info&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;reason&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        break&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Closed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;None&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; break&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;e&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; raise&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @common&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AgentError&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;InvalidInput&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Receive failed: &lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  conn&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;drop&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;durability&quot;&gt;Durability&lt;/h3&gt;
&lt;p&gt;Golem agents are durable, surviving failures and restarts. But what about these WebSocket connections? In &lt;strong&gt;Golem 1.5&lt;/strong&gt; we have limited support for recovering WebSocket connections in case of a restart.&lt;/p&gt;
&lt;p&gt;If the connection happened in the past, and was already closed, it works as expected - the whole communication is stored in the agent&#39;s &lt;strong&gt;oplog&lt;/strong&gt; and there isn&#39;t any problem recovering the agent&#39;s state.&lt;/p&gt;
&lt;p&gt;If the connection is still open, we have a problem because WebSocket connections are quite low level - there is no standard way to resume a connection. What Golem does is it assumes that the server supports transparent reconnections, and just reopens the connection and continues sending/receiving on it. For the Golem application&#39;s developer this is completely transparent, but it&#39;s the server&#39;s responsibility to support this kind of resumption.&lt;/p&gt;
&lt;p&gt;If the server does not support this kind of reconnection, then agents using these connections are no longer able to transparently survive failure scenarios. This is something we can further improve in upcoming Golem releases.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.5 features - Part 9: Agent skills</title>
        <published>2026-04-18T11:50:00+00:00</published>
        <updated>2026-04-18T11:50:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem15-part9-skills/"/>
        <id>https://blog.vigoo.dev/posts/golem15-part9-skills/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem15-part9-skills/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I am writing a series of &lt;em&gt;short&lt;/em&gt; posts showcasing the new features of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, to be released at the end of April, 2026. The episodes of this series will be short and assume the reader knows what Golem is. Check my &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem/&quot;&gt;other Golem-related posts&lt;/a&gt; for more information!&lt;/p&gt;
&lt;p&gt;Parts released so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;Part 1: Code-first routes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part2-webhooks&quot;&gt;Part 2: Webhooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part3-mcp&quot;&gt;Part 3: MCP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part4-nodejs&quot;&gt;Part 4: Node.js compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part5-scala&quot;&gt;Part 5: Scala support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part6-user-defined-snapshotting&quot;&gt;Part 6: User-defined snapshotting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;Part 7: Configuration and Secrets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part8-template-simplifications&quot;&gt;Part 8: Template simplifications and automatic updates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;Part 9: Agent skills&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;Part 11: Bridge libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;Part 12: REPL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part13-per-agent-config&quot;&gt;Part 13: Per-agent configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part14-otlp&quot;&gt;Part 14: OpenTelemetry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part15-moonbit&quot;&gt;Part 15: MoonBit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part16-quotas&quot;&gt;Part 16: Quotas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part17-semantic-retry-policies&quot;&gt;Part 17: Semantic retry policies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;skills&quot;&gt;Skills&lt;/h2&gt;
&lt;p&gt;Using coding agents has become a standard way of writing code. With &lt;strong&gt;Golem 1.5&lt;/strong&gt;, our application templates include an &lt;code&gt;AGENTS.md&lt;/code&gt; and a huge catalog of &lt;strong&gt;agent skills&lt;/strong&gt; describing every detail of creating, modifying and testing agents on the Golem platform.&lt;/p&gt;
&lt;p&gt;A bootstrap skill explaining the &lt;code&gt;golem new&lt;/code&gt; command is going to be available through &lt;a rel=&quot;external&quot; href=&quot;https://skills.sh&quot;&gt;skills.sh&lt;/a&gt;; then depending on the chosen programming language, a large set of Golem specific skills get unpacked to the application&#39;s directory.&lt;/p&gt;
&lt;h3 id=&quot;common-and-per-language-skills&quot;&gt;Common and per-language skills&lt;/h3&gt;
&lt;p&gt;The skill catalog is still being finalized for the release, but we will have 10-15 language-independent skills, and about 25-30 additional skills &lt;strong&gt;for each language&lt;/strong&gt; (TypeScript, Rust, Scala and MoonBit).&lt;/p&gt;
&lt;p&gt;We are running a benchmark with popular coding agents to see how well they can use these skills and this benchmark will run daily on CI from now on, making sure we can keep up with the rapidly changing ecosystem of coding agents.&lt;/p&gt;
&lt;h3 id=&quot;areas-covered&quot;&gt;Areas covered&lt;/h3&gt;
&lt;p&gt;The skills cover every aspect of writing and running applications on the Golem platform:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Creating new projects or extending existing ones&lt;/li&gt;
&lt;li&gt;Adding dependencies&lt;/li&gt;
&lt;li&gt;Configuring applications&lt;/li&gt;
&lt;li&gt;Writing new agents&lt;/li&gt;
&lt;li&gt;Exposing agents through HTTP and MCP&lt;/li&gt;
&lt;li&gt;Communication between multiple agents&lt;/li&gt;
&lt;li&gt;Working with databases&lt;/li&gt;
&lt;li&gt;Calling external services&lt;/li&gt;
&lt;li&gt;Integration with AI providers&lt;/li&gt;
&lt;li&gt;Creating webhooks&lt;/li&gt;
&lt;li&gt;Using advanced features like transactions, snapshotting, etc&lt;/li&gt;
&lt;li&gt;Troubleshooting&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Every new feature Golem gets starting from 1.5 will immediately have full support for agentic development.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.5 features - Part 8: Template simplifications and automatic updates</title>
        <published>2026-04-18T11:45:00+00:00</published>
        <updated>2026-04-18T11:45:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem15-part8-template-simplifications/"/>
        <id>https://blog.vigoo.dev/posts/golem15-part8-template-simplifications/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem15-part8-template-simplifications/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I am writing a series of &lt;em&gt;short&lt;/em&gt; posts showcasing the new features of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, to be released at the end of April, 2026. The episodes of this series will be short and assume the reader knows what Golem is. Check my &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem/&quot;&gt;other Golem-related posts&lt;/a&gt; for more information!&lt;/p&gt;
&lt;p&gt;Parts released so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;Part 1: Code-first routes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part2-webhooks&quot;&gt;Part 2: Webhooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part3-mcp&quot;&gt;Part 3: MCP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part4-nodejs&quot;&gt;Part 4: Node.js compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part5-scala&quot;&gt;Part 5: Scala support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part6-user-defined-snapshotting&quot;&gt;Part 6: User-defined snapshotting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;Part 7: Configuration and Secrets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part8-template-simplifications&quot;&gt;Part 8: Template simplifications and automatic updates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;Part 9: Agent skills&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;Part 11: Bridge libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;Part 12: REPL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part13-per-agent-config&quot;&gt;Part 13: Per-agent configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part14-otlp&quot;&gt;Part 14: OpenTelemetry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part15-moonbit&quot;&gt;Part 15: MoonBit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part16-quotas&quot;&gt;Part 16: Quotas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part17-semantic-retry-policies&quot;&gt;Part 17: Semantic retry policies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;golem-templates&quot;&gt;Golem templates&lt;/h2&gt;
&lt;p&gt;Golem comes with &lt;strong&gt;built-in templates&lt;/strong&gt; for the supported languages since 1.0. These templates not only contained simple examples of defining an agent running on Golem, but a large amount of build infrastructure as well, and a structure we created to make it easy for a Golem application to grow to be a big multi-component, multi-language project with additional user defined shared libraries and so on.&lt;/p&gt;
&lt;h3 id=&quot;previous-versions&quot;&gt;Previous versions&lt;/h3&gt;
&lt;p&gt;Although these old templates were quite powerful, they generated a lot of &quot;noise&quot; for reasons that many Golem applications never required. It was hard to understand what all these files are, where should we add our own code, what to touch and what to not touch.&lt;/p&gt;
&lt;p&gt;In addition to that, creating a project with &lt;code&gt;golem new&lt;/code&gt; applied that particular golem version&#39;s understanding of how a Golem application has to be built. If later the user updated golem itself, the process of updating the build definitions to match the new version was extremely hard.&lt;/p&gt;
&lt;h3 id=&quot;the-new-templates&quot;&gt;The new templates&lt;/h3&gt;
&lt;p&gt;We understood these problems and in &lt;strong&gt;Golem 1.5&lt;/strong&gt; we have a fully revised way of how these templates work!&lt;/p&gt;
&lt;p&gt;For example, creating a new &lt;strong&gt;TypeScript&lt;/strong&gt; project gives us the following:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-template&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ts/default&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ts-example&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ls&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ts-example&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;╭───┬──────────────────────────┬──────┬─────────┬─────────────────────╮&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;│&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; #&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; │           name           │ type │  size   │      modified       │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;├───┼──────────────────────────┼──────┼─────────┼─────────────────────┤&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;│&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ts-example/AGENTS.md&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;     │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; file&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 15.4&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; kB&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; 2026-04-18&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; 11:19:10&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;│&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ts-example/golem.yaml&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; file&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;   997&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; 2026-04-18&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; 11:19:10&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;│&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ts-example/package.json&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; file&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;   482&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; 2026-04-18&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; 11:19:10&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;│&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ts-example/src&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;           │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; dir&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  │&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;   128&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; 2026-04-18&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; 11:19:10&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;│&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ts-example/tsconfig.json&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; file&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;   578&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; 2026-04-18&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; 11:19:10&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;╰───┴──────────────────────────┴──────┴─────────┴─────────────────────╯&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ls&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ts-example/src&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;╭───┬─────────────────────────────────┬──────┬───────┬─────────────────────╮&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;│&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; #&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; │              name               │ type │ size  │      modified       │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;├───┼─────────────────────────────────┼──────┼───────┼─────────────────────┤&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;│&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ts-example/src/counter-agent.ts&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; file&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 593&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; 2026-04-18&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; 11:19:10&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;│&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ts-example/src/main.ts&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;          │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; file&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;  26&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; 2026-04-18&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; 11:19:10&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;╰───┴─────────────────────────────────┴──────┴───────┴─────────────────────╯&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The root of our application contains our application&#39;s &lt;code&gt;golem.yaml&lt;/code&gt; - this is where we can configure things like HTTP deployments, configuration, etc. Other than that we just see a simple &lt;code&gt;package.json&lt;/code&gt; and &lt;code&gt;tsconfig.json&lt;/code&gt; and the two TypeScript source files of the &lt;em&gt;default&lt;/em&gt; example.&lt;/p&gt;
&lt;h3 id=&quot;mixing-multiple-templates&quot;&gt;Mixing multiple templates&lt;/h3&gt;
&lt;p&gt;In the new template system we can also mix-in multiple templates into a single application, either during &lt;code&gt;golem new&lt;/code&gt; or later. These additional templates just bring in more example agents - for example we could have the default CounterAgent and next to it a &quot;human in the loop&quot; example. Our new templates are composable.&lt;/p&gt;
&lt;h3 id=&quot;automatic-updates&quot;&gt;Automatic updates&lt;/h3&gt;
&lt;p&gt;Another big problem with the old template system was that we could not easily update Golem applications to a new Golem version. New versions usually require updating the language-specific Golem SDKs, and sometimes also require changes to the build steps that &lt;code&gt;golem build&lt;/code&gt; performs under the hood. With the new templates, as seen above, these build steps are no longer dumped to static files generated by &lt;code&gt;golem new&lt;/code&gt;. They get dynamically extracted by the CLI tool to temporary directories, which means they can still be observed for troubleshooting purposes but they don&#39;t get checked in the application&#39;s repository, and a new golem version can just use an updated set of these build specific files if needed.&lt;/p&gt;
&lt;p&gt;Golem-specific dependencies in the project&#39;s &lt;code&gt;package.json&lt;/code&gt;/&lt;code&gt;Cargo.toml&lt;/code&gt;/etc also get verified by the &lt;code&gt;golem build&lt;/code&gt; command, and if anything needs to be updated, it is going to be reported with a helpful message.&lt;/p&gt;
&lt;p&gt;The automatic update also applies to &lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;our agent skills&lt;/a&gt;. Simply updating the CLI will guarantee the project always has the latest version of the golem-specific agent skills.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.5 features - Part 7: Configuration and Secrets</title>
        <published>2026-04-18T00:00:00+00:00</published>
        <updated>2026-04-18T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem15-part7-config-and-secrets/"/>
        <id>https://blog.vigoo.dev/posts/golem15-part7-config-and-secrets/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem15-part7-config-and-secrets/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I am writing a series of &lt;em&gt;short&lt;/em&gt; posts showcasing the new features of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, to be released at the end of April, 2026. The episodes of this series will be short and assume the reader knows what Golem is. Check my &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem/&quot;&gt;other Golem-related posts&lt;/a&gt; for more information!&lt;/p&gt;
&lt;p&gt;Parts released so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;Part 1: Code-first routes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part2-webhooks&quot;&gt;Part 2: Webhooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part3-mcp&quot;&gt;Part 3: MCP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part4-nodejs&quot;&gt;Part 4: Node.js compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part5-scala&quot;&gt;Part 5: Scala support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part6-user-defined-snapshotting&quot;&gt;Part 6: User-defined snapshotting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;Part 7: Configuration and Secrets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part8-template-simplifications&quot;&gt;Part 8: Template simplifications and automatic updates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;Part 9: Agent skills&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;Part 11: Bridge libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;Part 12: REPL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part13-per-agent-config&quot;&gt;Part 13: Per-agent configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part14-otlp&quot;&gt;Part 14: OpenTelemetry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part15-moonbit&quot;&gt;Part 15: MoonBit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part16-quotas&quot;&gt;Part 16: Quotas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part17-semantic-retry-policies&quot;&gt;Part 17: Semantic retry policies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;code-first-configuration&quot;&gt;Code-first Configuration&lt;/h2&gt;
&lt;p&gt;Before &lt;strong&gt;Golem 1.5&lt;/strong&gt;, the standard way to inject configuration values and secrets were through &lt;em&gt;environment variables&lt;/em&gt;. We can specify environment variables on a per-component, per agent type or per agent instance level, and the application can read them using the standard APIs such as &lt;code&gt;node:process&lt;/code&gt; or the Rust standard library. Environment variables also get inherited when agents are doing agent-to-agent RPC calls. This works, but has some drawbacks - they can only be strings, and most importantly they are not discoverable. An agent can read arbitrary environment variables and there is no way to tell in advance what they require.&lt;/p&gt;
&lt;p&gt;To solve these issues, the new Golem release introduces &lt;strong&gt;code-first configuration and secrets&lt;/strong&gt;. Similarly to &lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;code-first routes&lt;/a&gt;, agents code can define fully typed configuration types and these become part of an agent&#39;s &lt;strong&gt;type&lt;/strong&gt; - the deployment operation can verify that all of them are provided and have the proper type, and so on.&lt;/p&gt;
&lt;h3 id=&quot;configuration&quot;&gt;Configuration&lt;/h3&gt;
&lt;p&gt;Configuration types are record types which can be nested, and are injected specially to the agent&#39;s constructor:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DbConfig&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  host&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  port&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; number&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleConfig&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  debugLogs&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; boolean&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  alias&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-7&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  database&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DbConfig&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; nesting&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;    exampleParam&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    readonly&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; config&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Config&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ExampleConfig&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; injection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  useConfig&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; config&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;config&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;config&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;debugLogs&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;      console&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;debug&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Debug logs enabled&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;ConfigSchema&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DbConfig&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    host&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    port&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u16&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;ConfigSchema&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleConfig&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    debug_logs&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; bool&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    alias&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    database&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; DbConfig&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_definition&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; #&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;agent_config&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; config&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Config&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;ExampleConfig&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; use_config&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleAgentImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    config&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Config&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;ExampleConfig&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_implementation&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; ExampleAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; ExampleAgentImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;example_param&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; #&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;agent_config&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; config&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Config&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;ExampleConfig&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; config&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; use_config&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; config&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;config&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; config&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;debug_logs &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;            logging&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;logging&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Level&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Debug&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Debug logs enabled&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DbConfig&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  host&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  port&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DbConfig&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; schema&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DbConfig&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.derived&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleConfig&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  debugLogs&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Boolean&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  alias&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  database&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DbConfig&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleConfig&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; schema&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ExampleConfig&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.derived&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;agentDefinition&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AgentConfig&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ExampleConfig&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Id&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; exampleParam&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; useConfig&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;agentImplementation&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleAgentImpl&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;exampleParam&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;config&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Config&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ExampleConfig&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; useConfig&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; config&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; config.value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;config.debugLogs&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      js.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Dynamic&lt;/span&gt;&lt;span&gt;.global.console.debug&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Debug logs enabled&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.config&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;all&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DbConfig&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  host&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  port&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UInt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.config&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;all&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleConfig&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  debug_logs&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  alias&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  database&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DbConfig&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  // nesting&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.agent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;all&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  example_param&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  config&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @config&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Config&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ExampleConfig&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; // injection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleAgent&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  example_param&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  config&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @config&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Config&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ExampleConfig&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; example_param&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; config&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleAgent&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;use_config&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; config&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;config&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  if&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; config&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;debug_logs&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;    @log&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;debug&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Debug logs enabled&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Once we define these configuration &lt;strong&gt;requirements&lt;/strong&gt; in code, we can no longer deploy our agent without satisfying them first!&lt;/p&gt;
&lt;p&gt;We can assign values to each field of our structured configuration per agent in the application manifest:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;gents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  E&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;xampleAgent&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;onfig&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ebugLogs&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;lias&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;atabase&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        h&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ost&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;localhost&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ort&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 5432&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It is also possible to use the manifest&#39;s &lt;code&gt;preset&lt;/code&gt; feature to define reusable bits of configuration that can be easily applied to multiple agents, or to define config values that apply to &lt;em&gt;all&lt;/em&gt; agents within a component.&lt;/p&gt;
&lt;h3 id=&quot;secrets&quot;&gt;Secrets&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Secrets&lt;/strong&gt; are a special type of configuration - while regular configuration is tied to deployments, secrets can be updated dynamically for example when an API Token needs to be rotated. The difference between regular configuration and secrets is visible both in the code, and in the agent&#39;s metadata. The type difference encourages you to always &lt;code&gt;get&lt;/code&gt; the secret&#39;s current value to get the latest available value before each use.&lt;/p&gt;
&lt;p&gt;To define parts of the agent configuration as being secrets, wrap them in &lt;code&gt;Secret&lt;/code&gt;. The following example extends our previous &lt;code&gt;DbConfig&lt;/code&gt; type with a secret &lt;code&gt;password&lt;/code&gt; field:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DbConfig&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  host&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  port&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; number&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  password&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Secret&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;ConfigSchema&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DbConfig&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    host&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    port&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u16&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;config_schema&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;secret&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    password&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Secret&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DbConfig&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  host&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  port&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  password&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Secret&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.config&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;all&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DbConfig&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  host&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  port&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UInt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  password&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @config&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Secret&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Secret values are stored &lt;strong&gt;per environment&lt;/strong&gt; and not per agent deployment. If an environment does not have a secret yet, its initial value can be automatically set at deploy time by using the &lt;code&gt;secretDefaults&lt;/code&gt; section of the application manifest:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ecretDefaults&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ocal&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;b&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;assword&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      v&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;alue&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{{ DB_PASSWORD }}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;   #&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; env var substitution supported&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Just like in previous versions for environment variables, the &lt;code&gt;{{ X }}&lt;/code&gt; format can be used to set a secret value to an environment variable&#39;s value &lt;strong&gt;from the user&#39;s system&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Alternatively secrets can be created using CLI commands:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; agent-secret&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; create&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; db.password&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-secret-type&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-secret-value&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;pwd&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The secrets can be examined and updated any time using the CLI:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; agent-secret&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; list&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; agent-secret&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; update-value&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; db.password&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-secret-value&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;new-pwd&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; agent-secret&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; delete&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; db.password&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Deleting a secret can make running agents fail at runtime, if they use it.&lt;/p&gt;
&lt;p&gt;To access a secret&#39;s current value, use &lt;code&gt;get&lt;/code&gt; on the &lt;code&gt;Secret&lt;/code&gt; field — unlike regular config fields, this fetches the latest value each time:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; password&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; config&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;database&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;password&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; password&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; config&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;database&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;password&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; password&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; config.database.password.get&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; password&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; config&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;database&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;password&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;get&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This way our &lt;code&gt;password&lt;/code&gt; always gets the latest secret stored in the current environment.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.5 features - Part 6: User-defined snapshotting</title>
        <published>2026-04-15T00:00:00+00:00</published>
        <updated>2026-04-15T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem15-part6-user-defined-snapshotting/"/>
        <id>https://blog.vigoo.dev/posts/golem15-part6-user-defined-snapshotting/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem15-part6-user-defined-snapshotting/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I am writing a series of &lt;em&gt;short&lt;/em&gt; posts showcasing the new features of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, to be released at the end of April, 2026. The episodes of this series will be short and assume the reader knows what Golem is. Check my &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem/&quot;&gt;other Golem-related posts&lt;/a&gt; for more information!&lt;/p&gt;
&lt;p&gt;Parts released so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;Part 1: Code-first routes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part2-webhooks&quot;&gt;Part 2: Webhooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part3-mcp&quot;&gt;Part 3: MCP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part4-nodejs&quot;&gt;Part 4: Node.js compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part5-scala&quot;&gt;Part 5: Scala support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part6-user-defined-snapshotting&quot;&gt;Part 6: User-defined snapshotting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;Part 7: Configuration and Secrets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part8-template-simplifications&quot;&gt;Part 8: Template simplifications and automatic updates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;Part 9: Agent skills&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part10-websocket&quot;&gt;Part 10: WebSocket client&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;Part 11: Bridge libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;Part 12: REPL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part13-per-agent-config&quot;&gt;Part 13: Per-agent configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part14-otlp&quot;&gt;Part 14: OpenTelemetry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part15-moonbit&quot;&gt;Part 15: MoonBit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part16-quotas&quot;&gt;Part 16: Quotas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part17-semantic-retry-policies&quot;&gt;Part 17: Semantic retry policies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;snapshot-based-recovery&quot;&gt;Snapshot based recovery&lt;/h2&gt;
&lt;p&gt;One of the primary features of &lt;strong&gt;Golem&lt;/strong&gt; is that it can recover an agent&#39;s state transparently. Under the hood this is implemented by replaying an &lt;strong&gt;oplog&lt;/strong&gt; that records every side-effect&#39;s results among other things, so we can reconstruct the application&#39;s state on recovery. This works perfectly but can be slow if the agent does something CPU-heavy, or it simply have been running long enough to accumulate a long oplog.&lt;/p&gt;
&lt;p&gt;One way to fix this slowness is to do periodic &lt;strong&gt;snapshots&lt;/strong&gt; - a fully automated snapshotting mechanism would take the snapshot of the agent&#39;s memory, file system etc., and during recovery we would only need to replay the part of the oplog that happened &lt;em&gt;after&lt;/em&gt; the last snapshot. We did experiments with  automatic snapshotting like this, but it is not part of Golem at the time of writing yet.&lt;/p&gt;
&lt;p&gt;In &lt;strong&gt;Golem 1.5&lt;/strong&gt; we introduce a new feature that is in some ways more limited, but one can argue that for many use cases even more powerful than the fully automatic snapshotting.&lt;/p&gt;
&lt;h3 id=&quot;user-defined-snapshotting&quot;&gt;User-defined snapshotting&lt;/h3&gt;
&lt;p&gt;Instead of snapshotting automatically the whole memory and other state of an agent, an agent can &lt;strong&gt;opt-in&lt;/strong&gt; to snapshot support by implementing a pair of load/save functions. With this the agent only serializes the actual state that matters - but it is no longer transparent, it is something the developer must think through.&lt;/p&gt;
&lt;p&gt;Note that the load/save snapshot functions are not really new in Golem 1.5 - we had it since the first release, but previously it was only used as a way to migrate to new versions of an agent when the automatic update was not possible (code leading to diverging replays).&lt;/p&gt;
&lt;p&gt;The following example shows how the manually implemented save/load pair would look like for the default template&#39;s &lt;code&gt;CounterAgent&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; name&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; number&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; saveSnapshot&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Uint8Array&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; snapshot&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Uint8Array&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;4&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; view&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; DataView&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;snapshot&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;buffer&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;        view&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;setUint32&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; snapshot&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; loadSnapshot&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Uint8Array&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;void&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; view&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; DataView&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;bytes&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;buffer&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; view&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;getUint32&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_implementation&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    _name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    count&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u32&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_implementation&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CounterImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; load_snapshot&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; bytes&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;u8&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; arr&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;u8&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 4&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; bytes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;try_into&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map_err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Expected a 4-byte long snapshot&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;count &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u32&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;from_be_bytes&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;arr&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;        Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; save_snapshot&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;u8&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;        Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;count&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_be_bytes&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_vec&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;agentImplementation&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SnapshotCounterImpl&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;unused &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SnapshotCounter&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; saveSnapshot&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Array&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Future&lt;/span&gt;&lt;span&gt;.successful&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;encodeU32&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; loadSnapshot&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;bytes&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Array&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Future&lt;/span&gt;&lt;span&gt;.successful &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      value &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; decodeU32&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;bytes&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; encodeU32&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;i&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Array&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Array&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      (&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;i &lt;/span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 24&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0xff&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.toByte,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      (&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;i &lt;/span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 16&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0xff&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.toByte,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      (&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;i &lt;/span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 8&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0xff&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.toByte,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      (&lt;/span&gt;&lt;span&gt;i &lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0xff&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.toByte&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; decodeU32&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;bytes&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Array&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;bytes&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0xff&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 24&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      (&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;bytes&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0xff&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 16&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      (&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;bytes&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0xff&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 8&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      (&lt;/span&gt;&lt;span&gt;bytes&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0xff&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///|&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;/// Counter agent with snapshot persistence&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.agent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  name&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UInt64&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;// ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @agents&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Snapshottable&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; save_snapshot&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Bytes&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; snapshot&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Bytes&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;make&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;8&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  snapshot&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 56&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0xFF&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_byte&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  snapshot&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 48&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0xFF&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_byte&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  snapshot&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 40&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0xFF&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_byte&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  snapshot&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;3&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 32&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0xFF&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_byte&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  snapshot&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;4&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 24&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0xFF&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_byte&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  snapshot&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;5&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 16&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0xFF&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_byte&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  snapshot&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;6&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 8&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0xFF&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_byte&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  snapshot&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;7&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0xFF&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_byte&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  snapshot&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @agents&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Snapshottable&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; load_snapshot&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  bytes&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Bytes&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  if&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; bytes&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;length&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; !=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 8&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Invalid snapshot length: expected 8, got &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; bytes&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;length&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bytes&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_uint64&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 56&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bytes&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_uint64&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 48&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bytes&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_uint64&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 40&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bytes&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;3&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_uint64&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 32&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bytes&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;4&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_uint64&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 24&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bytes&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;5&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_uint64&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 16&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bytes&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;6&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_uint64&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 8&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    bytes&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;7&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_uint64&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;recovery-configuration&quot;&gt;Recovery configuration&lt;/h3&gt;
&lt;p&gt;Defining the pair of snapshotting functions is enough to use these for &lt;strong&gt;updating agents&lt;/strong&gt; but it does not enable &lt;strong&gt;snapshot-based recovery&lt;/strong&gt;. We can configure that through the agent annotation:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; snapshotting&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; periodic&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;5s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_definition&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;snapshotting &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;periodic(5s)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;agentDefinition&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;snapshotting &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;periodic(5 seconds)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;snapshotting&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;periodic(5)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The options can be &lt;code&gt;disabled&lt;/code&gt;, &lt;code&gt;enabled&lt;/code&gt; to use the server-side default (which is set to disabled by default), &lt;code&gt;every(N)&lt;/code&gt; meaning snapshot after every &lt;em&gt;Nth&lt;/em&gt; oplog entry, or &lt;code&gt;periodic(5s)&lt;/code&gt; to make a snapshot every 5 seconds.&lt;/p&gt;
&lt;h3 id=&quot;default-implementation&quot;&gt;Default implementation&lt;/h3&gt;
&lt;p&gt;Having snapshot-based recovery is really useful but we realized that writing these manual serialization functions may be too painful. In &lt;strong&gt;Golem 1.5&lt;/strong&gt; each supported language has a mechanism to opt-in for a &lt;strong&gt;default snapshotting implementation&lt;/strong&gt; while still allowing defining a fully custom pair of methods like we&#39;ve seen above.&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; For TypeScript, simply NOT defining loadSnapshot and saveSnapshot will &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; provide a default implementation that saves/loads the agent class itself &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; as JSON&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Serialize&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Deserialize&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterAgentImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    count&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u32&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;serde&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;skip&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    _id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_implementation&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CounterAgentImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Not overriding save_snapshot and load_snapshot will provide the &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; default implementation, if the agent type has serde Serialize &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; and Deserialize instances&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; For Scala we need to explicitly define the state to be serialized&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; and mix-in the `Snapshotted[T]` trait&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SnapshotCounterState&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SnapshotCounterState&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; schema&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;SnapshotCounterState&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.derived&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;agentImplementation&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterAgentImpl&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;unused &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Snapshotted&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;SnapshotCounterState&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; state&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SnapshotCounterState&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;                                 =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SnapshotCounterState&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; stateSchema&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;SnapshotCounterState&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SnapshotCounterState&lt;/span&gt;&lt;span&gt;.schema&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;snapshotting&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;every_n(1)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Counter&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  name&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UInt64&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; derive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ToJson&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @json&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;FromJson&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;// If an agent derives ToJson/FromJson and has no manual Snapshottable instance,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;// the SDK provides a default implementation&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;observability&quot;&gt;Observability&lt;/h3&gt;
&lt;p&gt;Using the default snapshotting implementation, or implementing one by hand that uses the &lt;code&gt;application/json&lt;/code&gt; content type has one more nice feature: when observing the &lt;strong&gt;oplog&lt;/strong&gt; of an agent for debugging purposes, we can see the snapshot entries with the serialized JSON state!&lt;/p&gt;
&lt;p&gt;Let&#39;s see an example:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#00021:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;INVOKE COMPLETED&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          at:                2026-04-15T13:19:04.618Z&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          consumed fuel:     200796&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          result:            AgentMethod(AgentInvocationOutputParameters { output: Tuple(ElementValues { elements: [] }) })&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#00022:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;SNAPSHOT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          at:                2026-04-15T13:19:04.619Z&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          data:              {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &amp;quot;principal&amp;quot;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;quot;tag&amp;quot;: &amp;quot;anonymous&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &amp;quot;state&amp;quot;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;quot;name&amp;quot;: &amp;quot;test1&amp;quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;quot;value&amp;quot;: 5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &amp;quot;version&amp;quot;: 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#00023:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ENQUEUED INVOCATION increment&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          at:                2026-04-15T13:19:05.355Z&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          idempotency key:   3da50be7-f426-427b-8f50-a05ced00d20a&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.5 features - Part 5: Scala support</title>
        <published>2026-04-14T20:45:00+00:00</published>
        <updated>2026-04-14T20:45:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem15-part5-scala/"/>
        <id>https://blog.vigoo.dev/posts/golem15-part5-scala/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem15-part5-scala/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I am writing a series of &lt;em&gt;short&lt;/em&gt; posts showcasing the new features of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, to be released at the end of April, 2026. The episodes of this series will be short and assume the reader knows what Golem is. Check my &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem/&quot;&gt;other Golem-related posts&lt;/a&gt; for more information!&lt;/p&gt;
&lt;p&gt;Parts released so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;Part 1: Code-first routes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part2-webhooks&quot;&gt;Part 2: Webhooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part3-mcp&quot;&gt;Part 3: MCP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part4-nodejs&quot;&gt;Part 4: Node.js compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part5-scala&quot;&gt;Part 5: Scala support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part6-user-defined-snapshotting&quot;&gt;Part 6: User-defined snapshotting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;Part 7: Configuration and Secrets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part8-template-simplifications&quot;&gt;Part 8: Template simplifications and automatic updates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;Part 9: Agent skills&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part10-websocket&quot;&gt;Part 10: WebSocket client&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;Part 11: Bridge libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;Part 12: REPL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part13-per-agent-config&quot;&gt;Part 13: Per-agent configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part14-otlp&quot;&gt;Part 14: OpenTelemetry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part15-moonbit&quot;&gt;Part 15: MoonBit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part16-quotas&quot;&gt;Part 16: Quotas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part17-semantic-retry-policies&quot;&gt;Part 17: Semantic retry policies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;scala-support&quot;&gt;Scala support&lt;/h2&gt;
&lt;p&gt;We love &lt;strong&gt;Scala&lt;/strong&gt; and always wanted to have it among the supported languages for &lt;strong&gt;Golem&lt;/strong&gt;. As Golem runs &lt;em&gt;WASM components&lt;/em&gt; this had some difficulties; there are ongoing projects trying to make Scala compiled to WASM, but none of them were production-ready yet a few months ago - and as far as I know, they still are not. So we took a different route - as we already put &lt;a href=&quot;/posts/golem15-part4-nodejs&quot;&gt;a lot of effort in our JS support&lt;/a&gt;, we decided to support Scala through &lt;strong&gt;Scala.js&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Scala gets compiled to JS using Scala.js, and executed in our QuickJS based runtime which is compiled to WASM. This sounds way too complicated, but it works; the important part is not this compilation chain, but rather the &lt;strong&gt;Golem Scala SDK&lt;/strong&gt; and its integration with Golem&#39;s CLI, which support everything that the other supported languages do.&lt;/p&gt;
&lt;h3 id=&quot;cli-integration&quot;&gt;CLI integration&lt;/h3&gt;
&lt;p&gt;Scala is a supported language in the Golem CLI - we can select it when creating a new application, and choose from existing &lt;em&gt;templates&lt;/em&gt; (although at the time of writing we only have one). When working with a Scala agent through the CLI, types and values (agent identifiers, method parameters, etc.) are parsed and printed using the Scala syntax. The CLI installs a huge catalog of Scala specific agentic skills helping your AI coding assistant write Scala code that runs on Golem.&lt;/p&gt;
&lt;p&gt;Part of the build chain is an &lt;code&gt;sbt&lt;/code&gt; plugin, but the whole application is built using &lt;code&gt;golem build&lt;/code&gt; just like with the other supported languages.&lt;/p&gt;
&lt;h4 id=&quot;what-s-not-supported-yet&quot;&gt;What&#39;s not supported yet?&lt;/h4&gt;
&lt;p&gt;Two features are not available yet for Scala:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;there is no Scala REPL yet, but you can use the TypeScript one to work with Scala (or any other) agents&lt;/li&gt;
&lt;li&gt;there is no Scala bridge library generator yet - this is a new feature to be explained in a future part of this series; it is a feature that makes it really easy to call agents from non-Golem applications in a typesafe way.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;code-first-features&quot;&gt;Code-first features&lt;/h3&gt;
&lt;p&gt;Just like with the other supported languages, we can define Golem &lt;strong&gt;agents&lt;/strong&gt; purely by writing Scala code and using some &lt;em&gt;annotations&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;agentDefinition&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;mount &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/counters/{name}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Id&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  @&lt;/span&gt;&lt;span&gt;prompt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Increase the count by one&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  @&lt;/span&gt;&lt;span&gt;description&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Increases the count by one and returns the new value&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  @&lt;/span&gt;&lt;span&gt;endpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;method &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;POST&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, path &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/increment&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; increment&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;agentImplementation&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterAgentImpl&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;unused &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; count&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; increment&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;.successful &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    count &lt;/span&gt;&lt;span&gt;+=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    count&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is the default template, a very simple stateful agent representing a counter. The example also demonstrates how &lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;code-first routes&lt;/a&gt; are also supported.&lt;/p&gt;
&lt;p&gt;Note that in every code example in &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem-1-5/&quot;&gt;this series&lt;/a&gt;, I&#39;m showing Scala examples as well.&lt;/p&gt;
&lt;h3 id=&quot;rpc&quot;&gt;RPC&lt;/h3&gt;
&lt;p&gt;One part of the custom build step implemented by the sbt plugin is generating &lt;strong&gt;client classes&lt;/strong&gt; for all the agents. This is very similar to how we do it in Rust - for a given agent called &lt;code&gt;Xyz&lt;/code&gt; we generate a new type called &lt;code&gt;XyzClient&lt;/code&gt; with &lt;strong&gt;client constructor methods&lt;/strong&gt; such as &lt;code&gt;get&lt;/code&gt; or &lt;code&gt;newPhantom&lt;/code&gt; to get an enriched interface of our agent&#39;s methods. This enriched interface not only has the original agent methods - calling them means &quot;invoke it on the remote agent, and await the result&quot; - but also has variants for &lt;em&gt;triggering&lt;/em&gt; a remote invocation without awaiting its results, and to &lt;em&gt;schedule it&lt;/em&gt; for a future point in time.&lt;/p&gt;
&lt;p&gt;Here is an example of how using these clients look like:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;agentDefinition&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;description&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Calls CounterAgent remotely and returns the result.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CallerAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Id&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  @&lt;/span&gt;&lt;span&gt;description&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Increments the given counter N times and returns the final value.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; incrementCounter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;counterId&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;times&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;agentImplementation&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CallerAgentImpl&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;unused &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CallerAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; incrementCounter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;counterId&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;times&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; counter&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterAgentClient&lt;/span&gt;&lt;span&gt;.get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;counterId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt; until times&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .foldLeft&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;counter.increment&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;prev, _&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        prev.flatMap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_ &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; counter.increment&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;http&quot;&gt;HTTP&lt;/h3&gt;
&lt;p&gt;As we compile to JS, we can (and have to) use &lt;code&gt;fetch&lt;/code&gt; for making HTTP requests. It can be called directly using Scala.js&#39;s interfaces, but it is also possible to use the &lt;a rel=&quot;external&quot; href=&quot;https://ziohttp.com&quot;&gt;zio-http&lt;/a&gt; library that has a fetch backend!&lt;/p&gt;
&lt;p&gt;Let&#39;s see an example agent using &lt;strong&gt;zio-http&lt;/strong&gt;!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;agentImplementation&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; FetchAgentImpl&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; FetchAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; fetchFromPort&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;port&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; effect&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        response &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;.serviceWithZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Client&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; client &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                      client.url&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.port&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;port&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.batched.get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/test&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        body &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; response.body.asString&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span&gt; body;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Unsafe&lt;/span&gt;&lt;span&gt;.unsafe &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; implicit&lt;/span&gt;&lt;span&gt; u &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      Runtime&lt;/span&gt;&lt;span&gt;.default.unsafe.runToFuture&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;effect.provide&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ZClient&lt;/span&gt;&lt;span&gt;.default&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;next-steps&quot;&gt;Next steps&lt;/h2&gt;
&lt;p&gt;This is just the beginning. The core Golem SDK is quite low level with no integration with ZIO or other libraries (although it uses the &lt;a rel=&quot;external&quot; href=&quot;https://www.youtube.com/watch?v=hWhxIYNl1T8&quot;&gt;new &lt;strong&gt;ZIO Schema&lt;/strong&gt;&lt;/a&gt; under the hood). With Scala support included in the &lt;strong&gt;Golem 1.5&lt;/strong&gt; release we can start building on top of this to have a first-class Scala experience for Golem!&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.5 features - Part 4: Node.js compatibility</title>
        <published>2026-04-14T00:00:00+00:00</published>
        <updated>2026-04-14T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem15-part4-nodejs/"/>
        <id>https://blog.vigoo.dev/posts/golem15-part4-nodejs/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem15-part4-nodejs/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I am writing a series of &lt;em&gt;short&lt;/em&gt; posts showcasing the new features of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, to be released at the end of April, 2026. The episodes of this series will be short and assume the reader knows what Golem is. Check my &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem/&quot;&gt;other Golem-related posts&lt;/a&gt; for more information!&lt;/p&gt;
&lt;p&gt;Parts released so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;Part 1: Code-first routes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part2-webhooks&quot;&gt;Part 2: Webhooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part3-mcp&quot;&gt;Part 3: MCP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part4-nodejs&quot;&gt;Part 4: Node.js compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part5-scala&quot;&gt;Part 5: Scala support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part6-user-defined-snapshotting&quot;&gt;Part 6: User-defined snapshotting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;Part 7: Configuration and Secrets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part8-template-simplifications&quot;&gt;Part 8: Template simplifications and automatic updates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;Part 9: Agent skills&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part10-websocket&quot;&gt;Part 10: WebSocket client&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;Part 11: Bridge libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;Part 12: REPL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part13-per-agent-config&quot;&gt;Part 13: Per-agent configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part14-otlp&quot;&gt;Part 14: OpenTelemetry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part15-moonbit&quot;&gt;Part 15: MoonBit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part16-quotas&quot;&gt;Part 16: Quotas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part17-semantic-retry-policies&quot;&gt;Part 17: Semantic retry policies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;js-ts-support&quot;&gt;JS/TS support&lt;/h2&gt;
&lt;p&gt;The previous release introduced our new QuickJS based &lt;strong&gt;JavaScript engine&lt;/strong&gt; and supported using &lt;strong&gt;TypeScript&lt;/strong&gt; for writing Golem applications. The runtime itself and the Golem SDK already worked well, however not many of the third party libraries of the JS/TS ecosystem were compatible with our runtime. We have put a lot of effort into increasing our runtime&#39;s compatibility with both browser APIs and Node.js modules.&lt;/p&gt;
&lt;h3 id=&quot;previously-supported&quot;&gt;Previously supported&lt;/h3&gt;
&lt;p&gt;The runtime shipped with the last Golem release supported the following APIs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Good support for: &lt;code&gt;Console&lt;/code&gt;, HTTP (&lt;code&gt;fetch&lt;/code&gt;), &lt;code&gt;URL&lt;/code&gt;, Streams, Timeout functions, Encoding, &lt;code&gt;Crypto&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Very limited support for parts of &lt;code&gt;node:util&lt;/code&gt;, &lt;code&gt;node:buffer&lt;/code&gt;, &lt;code&gt;node:fs&lt;/code&gt;, &lt;code&gt;node:path&lt;/code&gt;, &lt;code&gt;node:process&lt;/code&gt; and &lt;code&gt;node:stream&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;base64-js, ieee754&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;the-current-state&quot;&gt;The current state&lt;/h3&gt;
&lt;p&gt;During the development of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, we took Node.js&#39;s own test suite and tried to reach as high compatibility as possible with our runtime. Of course it cannot support it 100%, given the constraints of running on a different JS engine, in a single-threaded, sandboxed WASM environment. Still, we were able to implement a large part of Node.js&#39;s modules, tested some third party libraries extensively by hand and verified hundreds via automated coding agents.&lt;/p&gt;
&lt;p&gt;The summary of what we have now:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Web Platform APIs: &lt;code&gt;Console&lt;/code&gt;, HTTP (&lt;code&gt;fetch&lt;/code&gt;), &lt;code&gt;URL&lt;/code&gt;, Streams, Timers, Abort Controller, Encoding, Messaging, Events, &lt;code&gt;Intl&lt;/code&gt;, &lt;code&gt;Crypto&lt;/code&gt; (global), Structured Clone&lt;/li&gt;
&lt;li&gt;Node modules:
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;node:assert&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:async_hooks&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:buffer&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:constants&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:crypto&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:diagnostics_channel&lt;/code&gt; (implemented through Golem&#39;s invocation context APIs)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:dgram&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:dns&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:domain&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:events&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:fs&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:fs/promises&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:http&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:https&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:module&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:net&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:os&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:path&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:perf_hooks&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:process&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:punycode&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:querystring&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:readline&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:sqlite&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:stream&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:string_decoder&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:test&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:timers&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:trace_events&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:tty&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:url&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:util&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:vm&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:zlib&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Defined (to satisfy imports) but not implemented:
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;node:child_process&lt;/code&gt; (not possible in our current WASM runtime)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:cluster&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:inspector&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:http2&lt;/code&gt; (not possible in our current WASM runtime)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:repl&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:tls&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:v8&lt;/code&gt; (not running on V8)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;node:worker_threads&lt;/code&gt; (not possible in our current WASM runtime)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;more-information&quot;&gt;More information&lt;/h3&gt;
&lt;p&gt;For more information you can take a look at the vendored Node.js test &lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/wasm-rquickjs/blob/main/tests/node_compat/report.md&quot;&gt;compatibility report&lt;/a&gt; and the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/wasm-rquickjs/blob/main/tests/libraries/libraries.md&quot;&gt;automated library compatibility test report&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;After &lt;strong&gt;Golem 1.5&lt;/strong&gt; is released, we are going to further increase the compatibility of this runtime; please report any compatibility issues, real-world problems are going to be taken as highest priority.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.5 features - Part 3: MCP</title>
        <published>2026-04-11T00:00:00+00:00</published>
        <updated>2026-04-11T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem15-part3-mcp/"/>
        <id>https://blog.vigoo.dev/posts/golem15-part3-mcp/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem15-part3-mcp/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I am writing a series of &lt;em&gt;short&lt;/em&gt; posts showcasing the new features of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, to be released at the end of April, 2026. The episodes of this series will be short and assume the reader knows what Golem is. Check my &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem/&quot;&gt;other Golem-related posts&lt;/a&gt; for more information!&lt;/p&gt;
&lt;p&gt;Parts released so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;Part 1: Code-first routes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part2-webhooks&quot;&gt;Part 2: Webhooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part3-mcp&quot;&gt;Part 3: MCP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part4-nodejs&quot;&gt;Part 4: Node.js compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part5-scala&quot;&gt;Part 5: Scala support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part6-user-defined-snapshotting&quot;&gt;Part 6: User-defined snapshotting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;Part 7: Configuration and Secrets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part8-template-simplifications&quot;&gt;Part 8: Template simplifications and automatic updates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;Part 9: Agent skills&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part10-websocket&quot;&gt;Part 10: WebSocket client&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;Part 11: Bridge libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;Part 12: REPL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part13-per-agent-config&quot;&gt;Part 13: Per-agent configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part14-otlp&quot;&gt;Part 14: OpenTelemetry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part15-moonbit&quot;&gt;Part 15: MoonBit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part16-quotas&quot;&gt;Part 16: Quotas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part17-semantic-retry-policies&quot;&gt;Part 17: Semantic retry policies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;mcp&quot;&gt;MCP&lt;/h2&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://modelcontextprotocol.io/docs/getting-started/intro&quot;&gt;MCP (Model Context Protocol)&lt;/a&gt; became a standard way to connect AI applications. With the new Golem release any Golem application can be automatically &lt;strong&gt;exposed through MCP&lt;/strong&gt;. It does not require any code written, MCP is available for any agent automatically, but it needs to be enabled in the &lt;strong&gt;application manifest&lt;/strong&gt;:&lt;/p&gt;
&lt;h3 id=&quot;enabling&quot;&gt;Enabling&lt;/h3&gt;
&lt;p&gt;The same way how we can deploy HTTP APIs with the application manifest, we can add an &lt;code&gt;mcp&lt;/code&gt; section and choose which agents to deploy to which subdomains per environment:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;cp&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;eployments&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ocal&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omain&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;cp-demo.localhost:9007&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;gents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          C&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ounterAgent&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;security&quot;&gt;Security&lt;/h3&gt;
&lt;p&gt;The above manifest section already exposes the listed agent through MCP, but it is not protected by any form of authentication. Similar to how we can protect HTTP endpoints with OAuth, we can attach a &lt;strong&gt;security scheme&lt;/strong&gt; to our MCP deployment, let&#39;s call it &lt;code&gt;mcp-oauth&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;cp&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;eployments&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ocal&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omain&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;cp-demo.localhost:9007&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;gents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          C&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ounterAgent&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ecurityScheme&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;cp-oauth&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To set that up we need an OAuth provider and have to create a security scheme using the &lt;code&gt;golem&lt;/code&gt; CLI. The provider can be one of the common ones like Google, etc., or any custom one. In this post we are going to use the &lt;code&gt;mock-oauth2-server&lt;/code&gt; docker container:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;docker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; run&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;golem-mock-oauth2&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;9099:8080&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  ghcr.io/navikt/mock-oauth2-server:2.1.10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;CLIENT_ID&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;golem-mcp-client&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;CLIENT_SECRET&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;golem-mcp-secret&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;REDIRECT_URL&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;http://mcp-demo.localhost:9007/mcp/oauth/callback&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; api&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; security-scheme&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; create&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-provider-type&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; custom&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-custom-provider-name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;mock-oauth2&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-custom-issuer-url&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;http://localhost:9099/golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-client-id&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-5&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;CLIENT_ID&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-client-secret&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-5&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;CLIENT_SECRET&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-scope&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; openid&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-scope&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; email&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-scope&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; profile&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-redirect-url&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-5&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;REDIRECT_URL&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  mcp-oauth&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With that set, and running &lt;code&gt;golem deploy&lt;/code&gt;, our MCP server is ready to be used at &lt;code&gt;http://mcp-demo.localhost:9007/mcp&lt;/code&gt; using &lt;em&gt;streamable HTTP&lt;/em&gt; protocol, authenticated by the mock OAuth2 server.&lt;/p&gt;
&lt;h3 id=&quot;demo&quot;&gt;Demo&lt;/h3&gt;
&lt;p&gt;We can prove this by simply creating the default template (which simply implements a stateful counter), let&#39;s use the Rust one for this example and modify it slightly:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_definition&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;mount &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/counters/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; The agent constructor, its parameters identify the agent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;description&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Increment by a given number&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; increment_by&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; n&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u32&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u32&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    _name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    count&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u32&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_implementation&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CounterImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            _name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            count&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; increment_by&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; n&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u32&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u32&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;count &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; n&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;count&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We replaced the default &lt;code&gt;increment&lt;/code&gt; method with a parametrized &lt;code&gt;increment_by&lt;/code&gt;, which is going to be mapped into an &lt;strong&gt;MCP tool&lt;/strong&gt; in our MCP server automatically.&lt;/p&gt;
&lt;p&gt;If we start the &lt;a rel=&quot;external&quot; href=&quot;https://modelcontextprotocol.io/docs/tools/inspector&quot;&gt;MCP Inspector&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;npx&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; @modelcontextprotocol/inspector&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; node&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; build/index.js&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;img src=&quot;/images/golem15-mcp1.webp&quot; alt=&quot;MCP Inspector configured to connect to the Golem MCP server over Streamable HTTP&quot; width=&quot;377&quot; height=&quot;518&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;then click on the &lt;em&gt;quick auth flow&lt;/em&gt; and &lt;em&gt;connect&lt;/em&gt;, we can go to the &lt;em&gt;Tools&lt;/em&gt; page and see our counter incrementation tool:&lt;/p&gt;
&lt;img src=&quot;/images/golem15-mcp2.webp&quot; alt=&quot;MCP Inspector listing the CounterAgent increment_by tool after a successful run&quot; width=&quot;1498&quot; height=&quot;834&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;We can pass the &lt;code&gt;Counter&lt;/code&gt; agent&#39;s constructor parameter, &lt;code&gt;name&lt;/code&gt; and the &lt;code&gt;increment_by&lt;/code&gt; method&#39;s &lt;code&gt;n&lt;/code&gt; parameter, and invoke it through an MCP tool.&lt;/p&gt;
&lt;h3 id=&quot;mapping&quot;&gt;Mapping&lt;/h3&gt;
&lt;p&gt;As demonstrated above, &lt;code&gt;increment_by&lt;/code&gt; has been automatically exported as a &lt;strong&gt;tool&lt;/strong&gt;. But MCP not only defines tools, but also resources and resource templates. We support these with an automatic mapping in the following way:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Agent&lt;/th&gt;&lt;th&gt;Method&lt;/th&gt;&lt;th&gt;MCP entity&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Singleton&lt;/td&gt;&lt;td&gt;No parameters&lt;/td&gt;&lt;td&gt;Resource&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Non-singleton&lt;/td&gt;&lt;td&gt;No parameters&lt;/td&gt;&lt;td&gt;Resource template&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Any&lt;/td&gt;&lt;td&gt;Has parameters&lt;/td&gt;&lt;td&gt;Tool&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;metadata&quot;&gt;Metadata&lt;/h3&gt;
&lt;p&gt;For every agent and agent method, we can attach a &lt;strong&gt;description&lt;/strong&gt; and a &lt;strong&gt;prompt&lt;/strong&gt;:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;description&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Increments the counter by the number provided in the `n` parameter&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;prompt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Increment by a given number&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; increment_by&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;n&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;number&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;description&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Increments the counter by the number provided in the `n` parameter&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;prompt&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Increment by a given number&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; increment_by&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; n&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u32&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u32&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;description&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Increments the counter by the number provided in the `n` parameter&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;prompt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Increment by a given number&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; incrementBy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;n&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Increments the counter by the number provided in the `n` parameter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.prompt_hint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Increment by a given number&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Counter&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;increment&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; n&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UInt32&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UInt32&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Both are optional, and both are added to the MCP metadata.&lt;/p&gt;
&lt;h3 id=&quot;special-data-types&quot;&gt;Special data types&lt;/h3&gt;
&lt;p&gt;It&#39;s not strictly related to the MCP feature, and not even new in &lt;strong&gt;Golem 1.5&lt;/strong&gt;, but the three special data types supported by all the Golem SDKs are a good match for exposing some special tools and resources through the MCP protocol:&lt;/p&gt;
&lt;h4 id=&quot;unstructured-text&quot;&gt;Unstructured text&lt;/h4&gt;
&lt;p&gt;Any method parameter or return type can be defined as &lt;strong&gt;unstructured text&lt;/strong&gt;. Optionally a set of allowed &lt;strong&gt;language codes&lt;/strong&gt; can be attached to the type:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;myMethod&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  anyText&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;UnstructuredText&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  constrainedText&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;UnstructuredText&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;en&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;AllowedLanguages&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;enum&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MyLangs&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; En&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt; #&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; German&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; my_method&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    any_text&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; UnstructuredText&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    constrained_text&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; UnstructuredText&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;MyLangs&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; myMethod&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  anyText&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextSegment&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AllowedLanguages&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  constrainedText&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextSegment&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;MyLangs&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;sealed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MyLangs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MyLangs&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; En&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MyLangs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  @&lt;/span&gt;&lt;span&gt;golem.runtime.annotations.languageCode&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; German&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MyLangs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; allowed&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AllowedLanguages&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;MyLangs&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    golem.runtime.macros.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AllowedLanguagesDerivation&lt;/span&gt;&lt;span&gt;.derived  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.text_languages&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;constrained_text&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;en&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MyAgent&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;my_method&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  any_text&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UnstructuredText&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  constrained_text&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UnstructuredText&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4 id=&quot;unstructured-binary&quot;&gt;Unstructured binary&lt;/h4&gt;
&lt;p&gt;Similarly to &lt;strong&gt;unstructured text&lt;/strong&gt;, we can also use &lt;strong&gt;unstructured binary&lt;/strong&gt; parameters and return types, and optionally define the allowed &lt;em&gt;MIME types&lt;/em&gt; for them:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;myMethod&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  anyBinary&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;UnstructuredBinary&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  image&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;UnstructuredBinary&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;image/png&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;image/jpeg&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AllowedMimeTypes&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;enum&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Image&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;mime_type&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;image/png&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    Png&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;mime_type&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;image/jpeg&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    Jpeg&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; my_method&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    any_binary&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; UnstructuredBinary&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    image&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; UnstructuredBinary&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Image&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; myMethod&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  anyBinary&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinarySegment&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AllowedMimeTypes&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  image&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinarySegment&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Image&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;sealed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Image&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Image&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  @&lt;/span&gt;&lt;span&gt;golem.runtime.annotations.mimeType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;image/png&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Png&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Image&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  @&lt;/span&gt;&lt;span&gt;golem.runtime.annotations.mimeType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;image/jpeg&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Jpeg&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Image&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; allowed&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AllowedMimeTypes&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Image&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    golem.runtime.macros.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AllowedMimeTypesDerivation&lt;/span&gt;&lt;span&gt;.derived&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.mime_types&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;image/png&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;image/jpeg&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MyAgent&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;my_method&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  any_binary&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UnstructuredBinary&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  image&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UnstructuredBinary&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4 id=&quot;multimodal&quot;&gt;Multimodal&lt;/h4&gt;
&lt;p&gt;Finally there is a special parameter type called &lt;strong&gt;multimodal&lt;/strong&gt;, which is a special way to define methods (tools) that can work on multiple types of input. The default multimodal type just allows pasting either text or binary, but it is fully customizable with the above defined language and MIME type constraints, and can also include structured data.
By using multimodal types, and not just modelling the same input using custom data types, Golem can map these definitions better to MCP concepts.&lt;/p&gt;
&lt;p&gt;The simplest version just accepts either an arbitrary text, or an arbitrary binary:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;textOrBinary&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;input&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;Multimodal&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt; ...&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; text_or_binary&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Multimodal&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Multimodal&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; textOrBinary&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MultimodalItems&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Basic&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;MultimodalItems&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Basic&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MyAgent&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;text_or_binary&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  input&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @types&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Multimodal&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TextOrBinary&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; ...&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can add a third option in the form of a structured data type to this:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MyStructuredType&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt; ...&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;textOrBinaryOrStructured&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MultimodalCustom&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;MyStructuredType&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt; ...&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MyStructuredType&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; /*&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ... &lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;*/&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; text_or_binary_or_structured&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; MultimodalCustom&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;MyStructuredType&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; MultimodalCustom&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;MyStructuredType&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MyStructuredType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;/*&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ... &lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;*/&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MyStructuredType&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; schema&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;MyStructuredType&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.derived &lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; textOrBinaryOrStructured&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MultimodalItems&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WithCustom&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;MyStructuredType&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;MultimodalItems&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WithCustom&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;MyStructuredType&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.golem_schema&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;all&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MyStructuredType&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;*&lt;/span&gt;&lt;span&gt; ...&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;/&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MyAgent&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;text_or_binary_or_structured&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  input&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @types&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Multimodal&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;CustomModality&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;MyStructuredType&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; ...&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Or we can fully customize the multimodal behavior by defining our own variant type it maps to:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextOrImage&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2&quot;&gt;  |&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; tag&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UnstructuredText&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;en&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2&quot;&gt;  |&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; tag&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UnstructuredBinary&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;image/jpeg&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;image/png&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;fullyCustom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MultimodalAdvanced&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TextOrImage&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt; ...&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;AllowedLanguages&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;enum&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextLang&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; En&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt; #&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; German&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;AllowedMimeTypes&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;enum&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ImageType&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;mime_type&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;image/jpeg&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Jpeg&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;mime_type&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;image/png&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Png&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; MultimodalSchema&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;enum&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextOrImage&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    Text&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;UnstructuredText&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;TextLang&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    Image&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;UnstructuredBinary&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;ImageType&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; fully_custom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; MultimodalAdvanced&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;TextOrImage&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; MultimodalAdvanced&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;TextOrImage&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;sealed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextLang&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextLang&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  @&lt;/span&gt;&lt;span&gt;golem.runtime.annotations.languageCode&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;en&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; En&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextLang&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  @&lt;/span&gt;&lt;span&gt;golem.runtime.annotations.languageCode&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; De&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextLang&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; allowed&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AllowedLanguages&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TextLang&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    golem.runtime.macros.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AllowedLanguagesDerivation&lt;/span&gt;&lt;span&gt;.derived&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;sealed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ImageType&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ImageType&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  @&lt;/span&gt;&lt;span&gt;golem.runtime.annotations.mimeType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;image/jpeg&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Jpeg&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ImageType&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  @&lt;/span&gt;&lt;span&gt;golem.runtime.annotations.mimeType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;image/png&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Png&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ImageType&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; allowed&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AllowedMimeTypes&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ImageType&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    golem.runtime.macros.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AllowedMimeTypesDerivation&lt;/span&gt;&lt;span&gt;.derived&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextOrImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  text&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextSegment&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TextLang&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  image&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinarySegment&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ImageType&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextOrImage&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; schema&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; GolemSchema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TextOrImage&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; /*&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; derived &lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;*/&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; fullyCustom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Multimodal&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TextOrImage&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Multimodal&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TextOrImage&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.multimodal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;all&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; enum&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextOrImage&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Text&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;UnstructuredText&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Image&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;UnstructuredBinary&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.text_languages&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;input.Text&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;en&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.mime_types&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;input.Image&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;image/jpeg&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;image/png&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MyAgent&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;fully_custom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  input&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @types&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Multimodal&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TextOrImage&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; ...&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4 id=&quot;remarks&quot;&gt;Remarks&lt;/h4&gt;
&lt;p&gt;None of these special data types are MCP specific - using them in our agent code is not constraining them to be only called through MCP, they can still be invoked through agent-to-agent communication, mapped to HTTP APIs and so on.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.5 features - Part 2: Webhooks</title>
        <published>2026-04-10T00:00:00+00:00</published>
        <updated>2026-04-10T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem15-part2-webhooks/"/>
        <id>https://blog.vigoo.dev/posts/golem15-part2-webhooks/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem15-part2-webhooks/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I am writing a series of &lt;em&gt;short&lt;/em&gt; posts showcasing the new features of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, to be released at the end of April, 2026. The episodes of this series will be short and assume the reader knows what Golem is. Check my &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem/&quot;&gt;other Golem-related posts&lt;/a&gt; for more information!&lt;/p&gt;
&lt;p&gt;Parts released so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;Part 1: Code-first routes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part2-webhooks&quot;&gt;Part 2: Webhooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part3-mcp&quot;&gt;Part 3: MCP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part4-nodejs&quot;&gt;Part 4: Node.js compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part5-scala&quot;&gt;Part 5: Scala support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part6-user-defined-snapshotting&quot;&gt;Part 6: User-defined snapshotting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;Part 7: Configuration and Secrets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part8-template-simplifications&quot;&gt;Part 8: Template simplifications and automatic updates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;Part 9: Agent skills&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part10-websocket&quot;&gt;Part 10: WebSocket client&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;Part 11: Bridge libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;Part 12: REPL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part13-per-agent-config&quot;&gt;Part 13: Per-agent configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part14-otlp&quot;&gt;Part 14: OpenTelemetry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part15-moonbit&quot;&gt;Part 15: MoonBit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part16-quotas&quot;&gt;Part 16: Quotas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part17-semantic-retry-policies&quot;&gt;Part 17: Semantic retry policies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;webhooks&quot;&gt;Webhooks&lt;/h2&gt;
&lt;p&gt;We have seen how we can map our agents to HTTP APIs in the &lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;previous post&lt;/a&gt;. Another new feature of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, closely related to that, is the ability to &lt;strong&gt;create webhooks&lt;/strong&gt; and await them.&lt;/p&gt;
&lt;p&gt;Let&#39;s see how it works!&lt;/p&gt;
&lt;h3 id=&quot;creating-a-webhook&quot;&gt;Creating a webhook&lt;/h3&gt;
&lt;p&gt;Webhooks are built on top of &lt;a rel=&quot;external&quot; href=&quot;https://learn.golem.cloud/develop/promises&quot;&gt;Golem Promises&lt;/a&gt;, which were available in previous Golem releases as well. These are entities you can create from code and then await - while the agent is waiting, it gets fully suspended, consuming no resources.&lt;/p&gt;
&lt;p&gt;What&#39;s new is that we can now export these promises as webhook URLs that make it very easy to complete them from a third party system.&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; webhook&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; createWebhook&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; url&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; webhook&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;getUrl&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; At this point we can somehow advertise this `url` - return as a result, post to a 3rd party API, etc&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; payload&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; webhook&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; block until someone calls the webhook with a payload&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; payload&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;json&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; webhook&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; create_webhook&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; url&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; webhook&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;url&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; At this point we can somehow advertise this `url` - return as a result, post to a 3rd party API, etc&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; request&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; webhook&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;await&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; data&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; request&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;json&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;unwrap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; webhook&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HostApi&lt;/span&gt;&lt;span&gt;.createWebhook&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; url&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; webhook.url&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; At this point we can somehow advertise this `url` - return as a result, post to a 3rd party API, etc&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;webhook.await&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;.map &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; payload &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; event&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; payload.json&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; webhook&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @webhook&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;create&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; url&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; webhook&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;url&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;// At this point we can somehow advertise this `url` - return as a result, post to a 3rd party API, etc&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; payload&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; webhook&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;wait&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; text&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; payload&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;text&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;calling-the-webhook&quot;&gt;Calling the webhook&lt;/h3&gt;
&lt;p&gt;The webhook URL simply awaits a POST request with an arbitrary body. This body is what the &lt;code&gt;payload&lt;/code&gt;&#39;s helper methods are returning as raw byte array, string or parsed JSON.&lt;/p&gt;
&lt;h3 id=&quot;customizing-the-webhook-url&quot;&gt;Customizing the webhook URL&lt;/h3&gt;
&lt;p&gt;There are two ways to customize the webhook URL, which is having the following form:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;https://&amp;lt;domain&amp;gt;/&amp;lt;prefix&amp;gt;/&amp;lt;suffix&amp;gt;/&amp;lt;id&amp;gt; &lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;&amp;lt;domain&amp;gt;&lt;/code&gt; part is the domain where our API is deployed to. The &lt;code&gt;&amp;lt;prefix&amp;gt;&lt;/code&gt; is &lt;code&gt;/webhooks&lt;/code&gt; by default, and it can be customized in the deployment section of the Golem application manifest:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ttpApi&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;eployments&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;efault&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omain&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;xample.com&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        w&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ebhookUrl&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/my-custom-webhooks/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;gents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;          #&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;&amp;lt;suffix&amp;gt;&lt;/code&gt; part is the agent&#39;s type name in &lt;code&gt;kebab-case&lt;/code&gt; by default, so for example &lt;code&gt;my-workflow&lt;/code&gt; if our agent type is &lt;code&gt;MyWorkflow&lt;/code&gt;. It can be customized by setting &lt;strong&gt;webhook suffix&lt;/strong&gt; on our mount point:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  mount&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/workflow/{id}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  webhookSuffix&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/workflow-hooks&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Workflow&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_definition&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;    mount &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/workflow/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;    webhook_suffix &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/workflow-hooks&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Workflow&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;agentDefinition&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  mount &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/workflow/{id}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  webhookSuffix &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/workflow-hooks&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Workflow&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.agent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.mount&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/workflow/{id}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.mount_webhook_suffix&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/workflow-hooks&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;all&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Workflow&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  // ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.5 features - Part 1: Code-first routes</title>
        <published>2026-04-08T00:00:00+00:00</published>
        <updated>2026-04-08T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem15-part1-code-first-routes/"/>
        <id>https://blog.vigoo.dev/posts/golem15-part1-code-first-routes/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem15-part1-code-first-routes/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I am writing a series of &lt;em&gt;short&lt;/em&gt; posts showcasing the new features of &lt;strong&gt;Golem 1.5&lt;/strong&gt;, to be released at the end of April, 2026. The episodes of this series will be short and assume the reader knows what Golem is. Check my &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/tags/golem/&quot;&gt;other Golem-related posts&lt;/a&gt; for more information!&lt;/p&gt;
&lt;p&gt;Parts released so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part1-code-first-routes&quot;&gt;Part 1: Code-first routes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part2-webhooks&quot;&gt;Part 2: Webhooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part3-mcp&quot;&gt;Part 3: MCP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part4-nodejs&quot;&gt;Part 4: Node.js compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part5-scala&quot;&gt;Part 5: Scala support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part6-user-defined-snapshotting&quot;&gt;Part 6: User-defined snapshotting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part7-config-and-secrets&quot;&gt;Part 7: Configuration and Secrets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part8-template-simplifications&quot;&gt;Part 8: Template simplifications and automatic updates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part9-skills&quot;&gt;Part 9: Agent skills&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part10-websocket&quot;&gt;Part 10: WebSocket client&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part11-bridges&quot;&gt;Part 11: Bridge libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part12-repl&quot;&gt;Part 12: REPL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part13-per-agent-config&quot;&gt;Part 13: Per-agent configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part14-otlp&quot;&gt;Part 14: OpenTelemetry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part15-moonbit&quot;&gt;Part 15: MoonBit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part16-quotas&quot;&gt;Part 16: Quotas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/posts/golem15-part17-semantic-retry-policies&quot;&gt;Part 17: Semantic retry policies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;code-first-routes&quot;&gt;Code-first routes&lt;/h2&gt;
&lt;p&gt;In the previous Golem release we introduced &lt;strong&gt;code-first agents&lt;/strong&gt; - we started defining everything in code, with the help of some TypeScript decorators and Rust annotations. With this we could define &lt;strong&gt;agents&lt;/strong&gt; that expose a typed interface, can call each other and so on - but to expose these interfaces via regular HTTP endpoints, we had to define these endpoints in a OpenAPI-like YAML section and use a custom scripting language called &lt;strong&gt;Rib&lt;/strong&gt; to map between the request/response and the underlying agent interface.&lt;/p&gt;
&lt;p&gt;In &lt;strong&gt;Golem 1.5&lt;/strong&gt; this is no longer the case - no custom scripting language, no YAML description of endpoints, everything is possible directly from our agent&#39;s code!&lt;/p&gt;
&lt;h3 id=&quot;mount-points&quot;&gt;Mount points&lt;/h3&gt;
&lt;p&gt;First we have to define a &lt;strong&gt;mount point&lt;/strong&gt; for our agent:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  mount&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/task-agents/{name}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Tasks&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_definition&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;mount &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/task-agents/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Tasks&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;agentDefinition&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;mount &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/task-agents/{name}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Tasks&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.agent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.mount&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/task-agents/{name}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;all&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Tasks&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  // ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the mount path we can use placeholders like &lt;code&gt;{name}&lt;/code&gt; that identifies our agent - it maps directly to our agent constructor&#39;s &lt;code&gt;name&lt;/code&gt; parameter. If there are multiple agent parameters, they all have to be mapped in the mount path.&lt;/p&gt;
&lt;h3 id=&quot;endpoints&quot;&gt;Endpoints&lt;/h3&gt;
&lt;p&gt;Once we have our mount we can export individual agent methods as various &lt;strong&gt;endpoints&lt;/strong&gt;:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;endpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; post&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/tasks&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; createTask&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;request&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;CreateTaskRequest&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; getTasks&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;Task&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;endpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; post&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/tasks/{id}/complete&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; completeTask&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;id&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;number&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;endpoint&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;post &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/tasks&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; create_task&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; request&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CreateTaskRequest&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Task&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;endpoint&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;get &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/tasks&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_tasks&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Task&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;endpoint&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;post &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/tasks/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/complete&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; complete_task&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; usize&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Task&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;endpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;method &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;POST&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, path &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/tasks&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; createTask&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;request&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CreateTaskRequest&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Task&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;endpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;method &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;GET&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, path &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/tasks&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; getTasks&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Array&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Task&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;endpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;method &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;POST&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, path &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/tasks/{id}/complete&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; completeTask&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Task&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.endpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/tasks&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Tasks&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;create_task&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; request&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CreateTaskRequest&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Task&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  // ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.endpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/tasks&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Tasks&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get_tasks&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Array&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Task&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  // ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.endpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/tasks/{id}/complete&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Tasks&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;complete_task&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UInt32&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Task&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  // ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Endpoint paths are relative to the mount point, and they can also use placeholders mapped to parameters. Unmapped parameters are set from the request body. Query parameters are also supported in the &lt;code&gt;path&lt;/code&gt; patterns.&lt;/p&gt;
&lt;h3 id=&quot;additional-features&quot;&gt;Additional features&lt;/h3&gt;
&lt;p&gt;Custom headers can also be mapped to function parameters:&lt;/p&gt;
&lt;div class=&quot;code-tabs&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;endpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    get&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/example&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    headers&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;X-Foo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;location&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;X-Bar&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; example&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;location&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; name&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;endpoint&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;get &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/example&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt; headers&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;X-Foo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;location&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;X-Bar&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; example&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; location&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;endpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;method &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;GET&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, path &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/example&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; example&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;header&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;X-Foo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; location&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;header&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;X-Bar&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.endpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/example&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.endpoint_header&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;X-Foo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;location&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;#derive.endpoint_header&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;X-Bar&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExampleAgent&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;example&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  location&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  // ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Additionally, endpoint decorators support CORS and authentication. For CORS, we can add something like &lt;code&gt;cors = [&quot;*&quot;]&lt;/code&gt; to the decorator (syntax slightly varies by language).
We can turn on &lt;strong&gt;authentication&lt;/strong&gt; on the mount level or per individual endpoints. When authentication is enabled, agent constructors and methods optionally can receive a &lt;code&gt;Principal&lt;/code&gt; parameter that contains information about the authenticated user.&lt;/p&gt;
&lt;p&gt;It&#39;s also possible to tell the HTTP layer to create a &lt;strong&gt;phantom agent&lt;/strong&gt; for each request - this is useful for ephemeral, stateless agents serving as a gateway to internal agents as it allows requests to be processed completely parallel. This is a single line change (&lt;code&gt;phantomAgent = true&lt;/code&gt;) in the code, just changes how Golem internally maps each individual request to agent instances. Details of this technique will be provided in the updated documentation site.&lt;/p&gt;
&lt;h3 id=&quot;deployments&quot;&gt;Deployments&lt;/h3&gt;
&lt;p&gt;There is still a small step necessary in the application manifest file to make these code-first routes deployed:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ttpApi&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;eployments&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ocal&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omain&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;pp-name.localhost:9006&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;gents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        T&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;asks&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can specify what agents to deploy to what (sub)domains, and specify this per &lt;strong&gt;environment&lt;/strong&gt; (such as local/staging/prod, for example).&lt;/p&gt;
&lt;h3 id=&quot;openapi&quot;&gt;OpenAPI&lt;/h3&gt;
&lt;p&gt;Defining endpoints in code does not mean we cannot have proper OpenAPI specifications for them. Golem automatically adds an &lt;code&gt;openapi.yaml&lt;/code&gt; endpoint to each deployment:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; curl&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; http://routes.localhost:9006/openapi.yaml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;components:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;info:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  title:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Managed&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; api&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; provided&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; by&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Golem&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  version:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1.0.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;openapi:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 3.0.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;paths:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  /openapi.yaml:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    get:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;      responses:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;          content:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;            application/yaml:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;              schema:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;                additionalProperties:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;                type&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; object&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;          description:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Response&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  /task-agents/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;/tasks:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    get:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;      parameters:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; description:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Path parameter: name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;          explode:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          in: path&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;          name:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;          required:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;          schema:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;            type&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;          style:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; simple&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;      responses:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem as a Coding Agent engine</title>
        <published>2026-03-29T00:00:00+00:00</published>
        <updated>2026-03-29T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem-coding-agent/"/>
        <id>https://blog.vigoo.dev/posts/golem-coding-agent/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem-coding-agent/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Coding agents&lt;/strong&gt; became the new way of writing software for many of us; even though I was skeptical about this for a long time, in the past few months all my code was written by agents, with productivity I had never experienced before. The whole ecosystem is changing extremely fast, and people are trying to &lt;a rel=&quot;external&quot; href=&quot;https://x.com/jdegoes/status/2036931874057314390?s=20&quot;&gt;figure out&lt;/a&gt; what the best set of tools and workflows are to work efficiently in this new era. I cannot answer what set of coding agents, terminals, IDEs or post-IDE user interfaces fit best to the agentic development era. What I want to do is explore how &lt;a rel=&quot;external&quot; href=&quot;https://golem.cloud&quot;&gt;Golem&lt;/a&gt;, the agent-native durable execution platform I&#39;m working on could be a useful component of implementing reliable coding agents.&lt;/p&gt;
&lt;h3 id=&quot;what-is-a-coding-agent&quot;&gt;What is a coding agent?&lt;/h3&gt;
&lt;p&gt;There are many existing articles about what exactly coding agents are, and how you can write your own, for example &lt;a rel=&quot;external&quot; href=&quot;https://ampcode.com/notes/how-to-build-an-agent&quot;&gt;one from Thorsten Ball from last year&lt;/a&gt;. Today we have even &lt;a rel=&quot;external&quot; href=&quot;https://pi.dev&quot;&gt;frameworks to build your own&lt;/a&gt;. But the basic idea is very simple: you use an LLM and provide a set of &lt;strong&gt;tools&lt;/strong&gt; to it to be able to work on a project. The basic tools it needs are discovering files of your project, reading and editing these files. The models seem to like to do all kind of queries with standard piped Unix commands and &lt;a rel=&quot;external&quot; href=&quot;https://ripgrep.dev&quot;&gt;ripgrep&lt;/a&gt;; they need a way to look up external information on the web, and most importantly they need to be able to actually try things out, to have a full feedback loop. This means they need to be able to trigger compilation, running the code, the test suites, and so on.&lt;/p&gt;
&lt;p&gt;There are other aspects of course - finding and loading skill files, spawning sub-agents, dealing with the context size limitations, managing multiple connected threads etc.&lt;/p&gt;
&lt;p&gt;All of this is not really super complicated, but it gets worse if you start to think about what can go wrong:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;LLM calls can fail. There can be temporary outages, timeouts, etc. You don&#39;t want your expensive, long running agent session to be lost in this case&lt;/li&gt;
&lt;li&gt;We said above we have to give the agent &lt;em&gt;write access&lt;/em&gt; and ability to execute arbitrary(?) commands on our computer. How do we prevent it from doing something malicious?&lt;/li&gt;
&lt;li&gt;What if my computer crashed, or just accidentally closed the terminal where the agent was operating - do I have to start from scratch?&lt;/li&gt;
&lt;li&gt;Can we guarantee that all the parallel running agents together are not exceeding some kind of AI API quota?&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;golem&quot;&gt;Golem&lt;/h3&gt;
&lt;p&gt;Golem is an &lt;strong&gt;agent-native&lt;/strong&gt; cloud platform that provides &lt;strong&gt;durable execution&lt;/strong&gt;, exactly-once remote calls and many more features. The core entity in Golem is an &lt;strong&gt;agent&lt;/strong&gt; - a stateful, potentially long running entity that can process external &lt;strong&gt;invocations&lt;/strong&gt;, can spawn and communicate with other Golem agents, access external APIs and databases, and most importantly, can never die.&lt;/p&gt;
&lt;p&gt;This what makes Golem (and other durable execution platforms) special - even if the server process crashes, or just need to restart for reasons like rebalancing, updates, etc, the running agents are guaranteed to survive this without any assistance from the programmer. You can store your state in memory, and stop worrying about losing it for any external reasons.&lt;/p&gt;
&lt;p&gt;Another important property of Golem agents is that they are &lt;strong&gt;completely sandboxed&lt;/strong&gt;. They are &lt;a rel=&quot;external&quot; href=&quot;https://component-model.bytecodealliance.org&quot;&gt;WASM components&lt;/a&gt;, each having their own linear memory and their own file system. There is no way an agent can interfere with any other agent, only through the trusted agent-to-agent communication channels. There is no way an agent can access any files that are not in its sandboxed file system.&lt;/p&gt;
&lt;h2 id=&quot;golem-as-a-coding-agent-platform&quot;&gt;Golem as a Coding Agent platform&lt;/h2&gt;
&lt;p&gt;Let&#39;s start thinking about how we could take advantage of Golem&#39;s durability and sandboxing properties to implement our own coding agent!&lt;/p&gt;
&lt;p&gt;This is going to be a little unusual way to use Golem. Golem is a cloud platform - we provide our hosted infrastructure you can use, or you can bring up your own infrastructure as it is fully open source. We provide a local, single-executable version of it, which is primarily intended to be used for local development and testing.&lt;/p&gt;
&lt;p&gt;In this experiment, we are going to fully build on this local Golem build. This will be an important detail when we figure out how to combine the fully sandboxed Golem environment with the need to build and run the developed applications.&lt;/p&gt;
&lt;h3 id=&quot;architecture&quot;&gt;Architecture&lt;/h3&gt;
&lt;p&gt;In this experimental Golem based coding agent, I&#39;m going to mix in a little the question of managing parallel workspaces as well. The idea we are going to explore is that there is exactly one &lt;strong&gt;read/write coding agent&lt;/strong&gt; for a specific repository&#39;s specific &lt;strong&gt;branch&lt;/strong&gt;. There can be an arbitrary number of &lt;strong&gt;read-only coding agents&lt;/strong&gt; on the same branches, with no rights to do any changes. These can be used for exploratory work.&lt;/p&gt;
&lt;p&gt;In Golem every &lt;em&gt;golem agent&lt;/em&gt; is identified by their constructor parameters. There can be only one instance for a concrete value of these parameters, and invocations and every other command work by using &lt;em&gt;upsert&lt;/em&gt; semantics - if the agent identified by your provided constructor parameters (that we call the &lt;strong&gt;agent identity&lt;/strong&gt;) does not exist yet, it&#39;s going to be transparently created for you. There is an extra feature that is going to be very useful for us: &lt;strong&gt;phantom agents&lt;/strong&gt;. Phantom agents are &quot;secondary&quot; instances of an agent, distinguished from the primary one with an extra random uuid. This maps perfectly to our plan of representing read/write and read-only coding agents:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;CodingAgent(repository, branch)&lt;/code&gt; is the ID of a read/write coding agent&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CodingAgent(repository, branch)[uuid]&lt;/code&gt; is the ID of a read-only coding agent&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These Golem agents are going to run the main loop of the coding agent - checkout their repository&#39;s branch into their own sandboxed file system, do the LLM calls, and implement tools that work on the files. All sandboxed and all durable automatically.&lt;/p&gt;
&lt;p&gt;We also need a user-facing interface for this, of course. This is going to be a text user interface communicating with these Golem agents using another Golem feature - the ability to generate &lt;strong&gt;bridge libraries&lt;/strong&gt; that can be used to invoke Golem agents from the outside in a fully type-safe way. It can currently generate Rust and TypeScript libraries.&lt;/p&gt;
&lt;p&gt;This all looks straightforward, but we have not talked about one important thing yet:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;How do we make our agent be able to compile and run code?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;We can&#39;t assume that the agent can set up a build tool-chain in its sandboxed WASM environment. We need to delegate that work to the host machine of the user. Conceptually, we need something like this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The agent reaches a state of its branch where it wants to, let&#39;s say, compile and run all tests&lt;/li&gt;
&lt;li&gt;We need to mirror the current state of the whole repository to the host machine&lt;/li&gt;
&lt;li&gt;There, we need to use the host machine&#39;s tool-chains to compile and run all the tests&lt;/li&gt;
&lt;li&gt;The agent needs to be able to observe the results (standard output, files)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We are going to tweak Golem a little and take advantage that we run it locally. By default each agent&#39;s sandboxed virtual filesystem is stored in a separate temp directory on the host filesystem. We are going to enable deterministic, agent-id base locations for these directories. Then we expose some feature through a HTTP server from our host application. Our coding agents will get the ability to call the host app and run some tools there. We are going to define a strict set of allowed commands to be executed on the host. (For example to build a Rust project using &lt;code&gt;cargo&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Of course once we allow the agent to run arbitrary compiled code on our host, we are not safe anymore - I&#39;m not going to try to solve this in this post. Having sandboxed and durable editing sessions and being able to strictly limit the host-execution to build tools and the actual generated artifacts is already interesting and different enough from current state of art.&lt;/p&gt;
&lt;p&gt;One last thing we haven&#39;t talked about yet: LLM sessions have a limited context window so we cannot keep appending forever to a single session within one coding agent instance. We need to be able to start new threads, potentially do compaction of the old one, etc. With the proposed architecture of having one agent instance per &quot;feature branch&quot;, we can do all this in the agent&#39;s memory. We can store multiple threads in memory, provide ways to query old ones, and provide tools for the LLM to get the compacted version of previous threads, or to read from them if needed.&lt;/p&gt;
&lt;h2 id=&quot;implementation&quot;&gt;Implementation&lt;/h2&gt;
&lt;h3 id=&quot;agent-language&quot;&gt;Agent language&lt;/h3&gt;
&lt;p&gt;With the initial design we are ready to start implementing this. Let&#39;s first decide what &lt;em&gt;language&lt;/em&gt; to use for the agent side. Golem currently fully supports Rust and TypeScript, with experimental support for Scala.js and MoonBit. For this experiment, I&#39;m going to choose between Rust and TypeScript. What we need is a &lt;strong&gt;git client&lt;/strong&gt; that works in Golem (will get back to that), a fast grep library and a way to emulate standard Unix commands on the agent&#39;s file system, and of course a way to call an LLM.&lt;/p&gt;
&lt;p&gt;Let&#39;s see a comparison:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Aspect&lt;/th&gt;&lt;th&gt;Rust&lt;/th&gt;&lt;th&gt;TypeScript&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Git client&lt;/td&gt;&lt;td&gt;https://github.com/GitoxideLabs/gitoxide&lt;/td&gt;&lt;td&gt;https://isomorphic-git.org/&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;AI client&lt;/td&gt;&lt;td&gt;https://github.com/golemcloud/golem-ai&lt;/td&gt;&lt;td&gt;Several options, such as &lt;a rel=&quot;external&quot; href=&quot;https://effect.website/blog/effect-ai/&quot;&gt;@effect/ai&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Unix tools&lt;/td&gt;&lt;td&gt;https://crates.io/crates/brush&lt;/td&gt;&lt;td&gt;https://github.com/shelljs/shelljs&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Golem provides an increasing level of Node.js compatibility so many existing libraries can be used as-is. Also, the browser &lt;code&gt;fetch&lt;/code&gt; API and &lt;code&gt;node:http&lt;/code&gt; are both working so any library built on top of these will be able to call external services from a Golem agent.&lt;/p&gt;
&lt;p&gt;Unfortunately the Rust ecosystem makes this much harder. Golem does not provide low-level socket capabilities at the moment, the only way to make HTTP requests is through the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/WebAssembly/WASI/tree/main/proposals/http/&quot;&gt;WASI HTTP interface&lt;/a&gt;. There are &lt;a rel=&quot;external&quot; href=&quot;https://crates.io/crates/wstd&quot;&gt;existing Rust crates&lt;/a&gt; wrapping this interface but most of the Rust ecosystem is built on other ones such as &lt;code&gt;reqwest&lt;/code&gt;, which are &lt;em&gt;not&lt;/em&gt; working in Golem out of the box. This is why we provide our own set of AI connector libraries (&lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/golem-ai&quot;&gt;golem-ai&lt;/a&gt;) for Rust projects. But we also need a git client. So, if we&#39;d choose Rust, we would need to either:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;be able to plug in our own http library (&lt;code&gt;wstd&lt;/code&gt;&#39;s client) into the git library&lt;/li&gt;
&lt;li&gt;or implement the git cloning on our host and proxy it through a custom http request to our agents&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In addition to the &lt;code&gt;wasi-http&lt;/code&gt; requirement, all Rust crates we choose to depend on must be compilable to &lt;code&gt;wasm32-wasip2&lt;/code&gt; target.&lt;/p&gt;
&lt;p&gt;Based on a short AI analysis of the &lt;code&gt;gitoxide&lt;/code&gt; project, it seems like we &lt;em&gt;could&lt;/em&gt; extend it with a new, wasi-http based transport layer and most likely it could be compiled to &lt;code&gt;wasip2&lt;/code&gt;, but it requires forking the library. Even though having &lt;code&gt;brush&lt;/code&gt; would have been a nice tool for our agent implementation, for this experiment we are going to choose &lt;strong&gt;TypeScript&lt;/strong&gt;.&lt;/p&gt;
&lt;h3 id=&quot;tui-language&quot;&gt;TUI language&lt;/h3&gt;
&lt;p&gt;Using TypeScript on the backend does not mean we are forced to use TypeScript for the text user interface. For that, I&#39;m going to use &lt;strong&gt;Rust&lt;/strong&gt; simply as a personal preference and the easiness of building distributable single executables.&lt;/p&gt;
&lt;p&gt;Golem supports generating cross-language bridge libraries. This means our TypeScript agent&#39;s interface will be exposed as a generated Rust crate that we can use to communicate with the agents.&lt;/p&gt;
&lt;h3 id=&quot;the-first-version-of-our-agent&quot;&gt;The first version of our agent&lt;/h3&gt;
&lt;p&gt;We can start building the agent and build the host environment around it later. To not block on figuring out a nice name for this, I&#39;ll just call the project &lt;code&gt;golem-coding-agent&lt;/code&gt;. We can create a new TypeScript Golem application with the CLI:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; gca-agents&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Applying&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; template&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;s&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  Creating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /home/vigoo/projects/golem-coding-agent/gca-agents/.gitignore&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  Creating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /home/vigoo/projects/golem-coding-agent/gca-agents/AGENTS.md&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  Creating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /home/vigoo/projects/golem-coding-agent/gca-agents/golem.yaml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  Creating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /home/vigoo/projects/golem-coding-agent/gca-agents/package.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  Creating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /home/vigoo/projects/golem-coding-agent/gca-agents/src/counter-agent.ts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  Creating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /home/vigoo/projects/golem-coding-agent/gca-agents/src/main.ts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  Creating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /home/vigoo/projects/golem-coding-agent/gca-agents/tsconfig.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Finished&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; applying&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; template&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;s&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; [OK&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We select TypeScript and the basic template, and we end up with an initial TypeScript project implementing a simple agent that implements a counter.&lt;/p&gt;
&lt;p&gt;We can validate that it works by doing &lt;code&gt;golem build&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; build&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Finished&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; building&lt;/span&gt;&lt;span&gt; [OK&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The build command compiles and bundles the TypeScript project, injects it into a WASM base image, performs some pre-initialization optimizations and then it is ready to be deployed to Golem.&lt;/p&gt;
&lt;p&gt;The first thing we should check if we can add all the npm packages we are planning to use (as said, Golem is more and more compatible with Node.js, but there are still gaps). Let&#39;s do it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; npm&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; install&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; effect&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; npm&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; install&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; @effect/ai&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; npm&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; install&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; @effect/ai-openai&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; npm&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; install&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; isomorphic-git&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; npm&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; install&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; shelljs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; build&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It still compiles. Now we can start a local, fresh golem server with &lt;code&gt;golem server run --clean&lt;/code&gt; and then deploy our code to it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; deploy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Deployed&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; all&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; changes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;╔═&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Created&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; deployment&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Application:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;         gca-agents&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Environment:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;         local&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Environment&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ID:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;      019d3982-c09d-7822-b35e-0692657a0503&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Deployment&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Revision:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Hash:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;                75c45be092d7b5478df95af253bff74fa66ceb8f208a93d8c313ac9cc7a74c93&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Deploy&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Revision:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;     0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;╚═&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Summary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  Finished&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; deploying&lt;/span&gt;&lt;span&gt; [OK&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then try it out with Golem&#39;s TypeScript REPL:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; repl&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem-ts-repl[gca-agents][local]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; Available agent client types:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;   CounterAgent.get(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;name:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;     increment:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; number&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; To see this message again, use the &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;.agent-type-info&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; command&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem-ts-repl[gca-agents][local]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; const&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; CounterAgent.get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem-ts-repl[gca-agents][local]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; c.increment&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; awaiting Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;number&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;~&lt;/span&gt;&lt;span&gt;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem-ts-repl[gca-agents][local]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Everything seems to work. From this point I&#39;ll just let my (non-golem) coding agent do the actual implementation. First we replace the counter agent with a new agent called &lt;code&gt;CodingAgent&lt;/code&gt; that already takes the repository and branch names, as we planned, and uses the git library to clone and check out that branch:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CodingAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; repository&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; branch&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; readOnly&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; boolean&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  repoInitialized&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; boolean&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; false&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;repository&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; branch&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10 z-d-i&quot;&gt;    super&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;    this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;repository&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; repository&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;    this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;branch&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; branch&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;    this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;readOnly&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;phantomId&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; !==&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; undefined&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;    console&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;info&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;      `&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;[CodingAgent] Constructed: repository=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;repository&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;, branch=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;branch&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;, readOnly=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;z-l-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;readOnly&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;, phantomId=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;z-l-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;phantomId&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; dump&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Effect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;runPromise&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;      Effect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;gen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; function&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; logInfo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;[CodingAgent.dump] Starting dump&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;initRepo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; tree&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;buildTree&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; logInfo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;          `&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;[CodingAgent.dump] Dump complete, tree length=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;z-l-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; tree&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;initRepo&lt;/code&gt; and &lt;code&gt;buildTree&lt;/code&gt; functions are not very exciting - just effect-ts functions wrapping the git and node:fs modules.&lt;/p&gt;
&lt;p&gt;We can try it out by calling &lt;code&gt;dump()&lt;/code&gt; on &lt;code&gt;CodingAgent(&quot;https://github.com/vigoo/test-r.git&quot;, &quot;test1&quot;)&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;log&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T12:42:33.985Z&lt;/span&gt;&lt;span&gt;] [INVOKE  ] STARTED  golem:agent/guest@&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;5&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;.{invoke} (&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;06e40a0f-ccfd-4571-8614-f86ef22787eb&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T12:42:33.985Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;INFO&lt;/span&gt;&lt;span&gt;    ] [&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;CodingAgent.dump&lt;/span&gt;&lt;span&gt;] Starting dump&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T12:42:33.986Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;INFO&lt;/span&gt;&lt;span&gt;    ] [initRepo] Cloning repository &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;https://github.com/vigoo/test-r.git&lt;/span&gt;&lt;span&gt; into /&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T12:43:01.700Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;INFO&lt;/span&gt;&lt;span&gt;    ] [initRepo] Clone completed successfully&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T12:43:01.700Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt;DEBUG&lt;/span&gt;&lt;span&gt;   ] [initRepo] Listing local branches&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T12:43:01.701Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt;DEBUG&lt;/span&gt;&lt;span&gt;   ] [initRepo] Local branches: [&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;master&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T12:43:01.701Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt;DEBUG&lt;/span&gt;&lt;span&gt;   ] [initRepo] Listing remote branches&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T12:43:01.969Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt;DEBUG&lt;/span&gt;&lt;span&gt;   ] [initRepo] Remote branches: [&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;HEAD&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;experiment&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;gh-pages&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;master&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-10-05T17-10-16Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-10-06T09-00-48Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-10-06T10-04-28Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-10-06T13-49-33Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-10-06T14-16-30Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-10-06T15-07-52Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-10-06T15-12-36Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-10-06T15-23-15Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-10-06T16-21-51Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-10-11T08-35-32Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-10-11T09-10-17Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-10-11T10-10-21Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-10-14T11-31-50Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-10-21T08-45-47Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-10-21T08-46-34Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-10-21T13-29-32Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-10-27T13-18-39Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-12-29T10-03-43Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-12-29T11-06-51Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-12-29T12-13-17Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2024-12-29T13-17-50Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2025-01-30T14-13-36Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2025-04-05T11-48-31Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2025-05-14T18-08-55Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2025-05-14T19-47-48Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2025-05-31T11-07-46Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2025-05-31T17-56-08Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2025-06-01T20-31-20Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2025-06-02T07-12-42Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2025-08-27T15-56-32Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2026-02-25T11-24-02Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2026-02-25T12-10-01Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2026-02-25T12-23-41Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2026-02-25T13-29-14Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2026-02-25T13-31-11Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2026-02-25T13-54-34Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2026-02-25T14-17-43Z&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;release-plz-2026-03-02T15-30-44Z&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T12:43:01.969Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;INFO&lt;/span&gt;&lt;span&gt;    ] [initRepo] Branch &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;test1&amp;#39;&lt;/span&gt;&lt;span&gt; does not exist, creating it&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T12:43:01.979Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;INFO&lt;/span&gt;&lt;span&gt;    ] [initRepo] Created branch &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;test1&amp;#39;&lt;/span&gt;&lt;span&gt;, checking out&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T12:43:04.395Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;INFO&lt;/span&gt;&lt;span&gt;    ] [initRepo] Checked out new branch &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;test1&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T12:43:04.395Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;INFO&lt;/span&gt;&lt;span&gt;    ] [initRepo] Repository initialization complete&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T12:43:04.395Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;INFO&lt;/span&gt;&lt;span&gt;    ] [buildTree] Building file tree&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T12:43:04.395Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt;DEBUG&lt;/span&gt;&lt;span&gt;   ] [listFilesRecursive] Reading directory: /&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T12:43:04.577Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt;DEBUG&lt;/span&gt;&lt;span&gt;   ] [listFilesRecursive] Found &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;19&lt;/span&gt;&lt;span&gt; entries in /&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T12:43:04.577Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt;DEBUG&lt;/span&gt;&lt;span&gt;   ] [listFilesRecursive] Skipping .git directory&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So cloning the repository works! The next thing we should add is an agent method that sends a &lt;strong&gt;user prompt&lt;/strong&gt; to the LLM. Here we will start using the &lt;code&gt;@effect/ai&lt;/code&gt; library to communicate with the AI. As I wrote earlier, we will store sessions simply in the agent&#39;s memory. Golem is durable so we don&#39;t have to worry about loosing anything.&lt;/p&gt;
&lt;p&gt;So let&#39;s add a &lt;code&gt;Thread&lt;/code&gt; class that is going to be responsible for a single AI thread and remembering its full context:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; SYSTEM_PROMPT&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;You are a helpful coding assistant.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Message&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; role&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;assistant&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; content&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Thread&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  messages&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Message&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  private&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; chat&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-10&quot;&gt; Chat&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Service&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; undefined&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;    this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; id&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  private&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; initSession&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Effect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;gen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; function&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;chat&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        return&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;      this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;chat&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Chat&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;fromPrompt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          role&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          content&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; SYSTEM_PROMPT&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      ]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  send&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;    prompt&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-10&quot;&gt; Effect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Effect&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; unknown&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-10&quot;&gt; LanguageModel&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LanguageModel&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Effect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;gen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; function&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;initSession&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;      this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;messages&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;push&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; role&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; content&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; prompt&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; response&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;chat&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;!&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;generateText&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; prompt&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; text&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; response&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;text&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;      this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;messages&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;push&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; role&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;assistant&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; content&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; text&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      return&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; text&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note: in &lt;code&gt;@effect/ai&lt;/code&gt; the &lt;code&gt;Chat&lt;/code&gt; object itself remembers the session so there is no need to store it outside for ourselves. We still store all messages  in the &lt;code&gt;messages&lt;/code&gt; field, but it&#39;s not fed back to the chat session. It&#39;s just our history that will be easy to tweak and made queryable for our text user interface.&lt;/p&gt;
&lt;p&gt;In our &lt;code&gt;CodingAgent&lt;/code&gt; class we add a map of threads, and expose a new agent method:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CodingAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  threads&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Map&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Thread&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  activeThreadId&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;repository&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; branch&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; config&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Config&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;CodingAgentConfig&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; threadId&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; crypto&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;randomUUID&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; thread&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Thread&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;threadId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;    this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;threads&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;set&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;threadId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; thread&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;    this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;activeThreadId&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; threadId&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice the new &lt;code&gt;config&lt;/code&gt; parameter to our agent! It is a special parameter with the &lt;code&gt;Config&amp;lt;&amp;gt;&lt;/code&gt; type, so it is NOT participating in our agent&#39;s identity. We can still refer to an agent by only the repository and branch names, but it uses Golem&#39;s underlying configuration engine to access type-safe configuration that we have to provide deploy-time.&lt;/p&gt;
&lt;p&gt;For now, we only require an OpenAI API key:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CodingAgentConfig&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  openaiApiKey&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Secret&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and use it in our &lt;code&gt;sendPrompt&lt;/code&gt; method:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sendPrompt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;prompt&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    return Effect.runPromise(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      Effect.gen(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;this&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; function* () &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; initRepo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; thread&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;threads&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;activeThreadId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;thread&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;          return&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; ThreadNotFound&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; threadId&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;activeThreadId&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; apiKey&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;config&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;value&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;openaiApiKey&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; response&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; thread&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;send&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;prompt&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;pipe&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;          Effect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;provide&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;makeModelLayer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;apiKey&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; response&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;makeModelLayer&lt;/code&gt; just constructs the necessary effect-ai layer for running our effect:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  private&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; makeModelLayer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;apiKey&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; OpenAiLanguageModel&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;model&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;gpt-5.4&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;pipe&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;      Layer&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;provide&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;        OpenAiClient&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;layer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          apiKey&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Redacted&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;make&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;apiKey&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      )&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;      Layer&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;provide&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;FetchHttpClient&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;layer&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that even though our &lt;em&gt;configuration&lt;/em&gt; is injected through the constructor, &lt;strong&gt;secrets&lt;/strong&gt; are dynamic - they can be updated while your agent is running, and the &lt;code&gt;openaiApiKey.get()&lt;/code&gt; call will always return the latest value.&lt;/p&gt;
&lt;p&gt;We can set default values for our secrets in the application&#39;s manifest file:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ecretDefaults&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ocal&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;openaiApiKey&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      v&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;alue&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{{ OPENAI_API_KEY }}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&#39;s try this out with our REPL:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; repl&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; Available agent client types:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;   CodingAgent.get(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;repository:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; branch:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;     dump:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;     sendPrompt: &lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;prompt:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; To see this message again, use the &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;.agent-type-info&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; command&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem-ts-repl[gca-agents][local]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; const&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; testr&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; CodingAgent.get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;https://github.com/vigoo/test-r.git&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;test1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem-ts-repl[gca-agents][local]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; testr.sendPrompt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Who are you?&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; awaiting Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;I’m a coding assistant here to help with your programming questions and provide guidance on technical topics. How can I assist you today?&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;our-first-tools&quot;&gt;Our first tools&lt;/h3&gt;
&lt;p&gt;Now that we have the source code and the AI connection set up, we have to add our first &lt;strong&gt;tools&lt;/strong&gt; for our LLM so it can reach out and read and manipulate the source files.&lt;/p&gt;
&lt;p&gt;We are going to add the following tools in this step:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;List files&lt;/strong&gt; with optional starting directory and simple filtering&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Read file&lt;/strong&gt; with optional start/end line numbers&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Write file&lt;/strong&gt; creating/replacing a file&#39;s contents completely&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Replace in file&lt;/strong&gt; to replace a source string with a destination string, optionally constrained by line numbers&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note that we don&#39;t have to worry about the write/replace tools to do anything dangerous - we are operating in our agent&#39;s sandbox.&lt;/p&gt;
&lt;p&gt;We use &lt;code&gt;@effect/ai&lt;/code&gt;&#39;s tool definition mechanism to define these. For example, let&#39;s see how the &lt;strong&gt;read file tool&lt;/strong&gt; looks like:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; ReadFile&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Tool&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;make&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ReadFile&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  description&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Read the contents of a file. Optionally specify 1-indexed start/end line numbers &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;to read a slice of the file.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  parameters&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    path&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    startLine&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;optional&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Number&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    endLine&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;optional&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Number&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  success&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  failure&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Struct&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; message&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  failureMode&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; as&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; const&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;annotate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Tool&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Readonly&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; CodingToolkit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Toolkit&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;make&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;ListFiles&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; ReadFile&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; WriteFile&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; ReplaceInFile&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; CodingToolkitLayer&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; CodingToolkit&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;toLayer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  ReadFile&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;params&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;    Effect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;gen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; logDebug&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;        `&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;[Tool:ReadFile] path=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;z-l-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;, startLine=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;z-l-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;startLine&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;, endLine=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;z-l-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;endLine&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; content&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Effect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;tryPromise&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        try&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; fsPromises&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;readFile&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;params&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;path&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;utf8&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        catch&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;e&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; message&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; String&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;e&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;        params&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;startLine&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; !==&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; ||&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; params&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;endLine&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; !==&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-7&quot;&gt;          ?&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;              const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; lines&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; content&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;split&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;              const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; start&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;params&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;startLine&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; ??&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;              const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; end&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; params&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;endLine&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; ??&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; lines&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;length&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;              return&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; lines&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;slice&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;start&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; end&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;join&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-7&quot;&gt;          :&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; content&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; logDebug&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;[Tool:ReadFile] returned &lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;z-l-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; chars&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      return&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; result&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We inject these tools for our LLM using the &lt;code&gt;toolkit&lt;/code&gt; parameter:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; response&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;chat&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;!&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;generateText&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;    prompt&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    toolkit&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; CodingToolkit&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But this is not enough. When the LLM returns with tool call requests, it is our responsibility to execute the tools and loop:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; MAX_STEPS&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 15&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; currentPrompt&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; readonly&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; any&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; prompt&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; step&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; step&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; MAX_STEPS&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; step&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;++&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; response&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;chat&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;!&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;generateText&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    prompt&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; currentPrompt&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    toolkit&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; CodingToolkit&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;response&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;text&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;    this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;messages&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;push&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; role&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;assistant&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; content&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; response&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;text&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; response&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;text&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;response&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;toolCalls&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; ===&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;    this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;messages&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;push&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; role&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;assistant&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; content&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; toolResults&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; response&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;content&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;filter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;p&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; p&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; ===&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;tool-result&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; logDebug&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    `&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;[Thread:&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;z-l-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;] Tool calls executed: &lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;z-l-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;toolCalls&lt;/span&gt;&lt;span class=&quot;z-l-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;tc&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; tc&lt;/span&gt;&lt;span class=&quot;z-l-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;join&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;; looping back to model&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; History is accumulated by Chat — send empty prompt to let the model&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; see the tool results and continue.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  currentPrompt&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&#39;s see if we can actually ask our agent to work on our branch now!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; repl&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem-ts-repl[gca-agents][local]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; const&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; testr&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; CodingAgent.get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;https://github.com/vigoo/test-r.git&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;test1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem-ts-repl[gca-agents][local]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; testr.sendPrompt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;First take a look at this repository and tell me what it is&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; awaiting Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;~&lt;/span&gt;&lt;span&gt;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;2026-03-29T15:14:07.985Z&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;INFO    &lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;initRepo&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; Cloning repository https://github.com/vigoo/test-r.git into /&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;2026-03-29T15:14:41.264Z&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;INFO    &lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;Thread:54d30d83-a43a-4a3b-80dd-382741b95189&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; Initializing AI session&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;2026-03-29T15:14:41.268Z&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;INFO    &lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;Thread:54d30d83-a43a-4a3b-80dd-382741b95189&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; AI session initialized&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;2026-03-29T15:14:41.268Z&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;INFO    &lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;Thread:54d30d83-a43a-4a3b-80dd-382741b95189&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; Sending prompt: First take a look at this repository and tell me what it is...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;2026-03-29T15:14:41.268Z&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;DEBUG   &lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;Thread:54d30d83-a43a-4a3b-80dd-382741b95189&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; generateText step 1/15&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;2026-03-29T15:14:44.119Z&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;DEBUG   &lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;Tool:ListFiles&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; directory&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; filter&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;2026-03-29T15:14:44.467Z&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;DEBUG   &lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;Tool:ListFiles&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; returned 104 entries&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;2026-03-29T15:14:44.840Z&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;DEBUG   &lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;Thread:54d30d83-a43a-4a3b-80dd-382741b95189&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; Tool calls executed: ListFiles&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; looping&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; back&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; model&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;2026-03-29T15:14:44.840Z&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;DEBUG   &lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;Thread:54d30d83-a43a-4a3b-80dd-382741b95189&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; generateText step 2/15&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;2026-03-29T15:14:46.127Z&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;DEBUG   &lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;Tool:ReadFile&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; path&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;E&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;D&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;E&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; startLine&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; endLine&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;2026-03-29T15:14:46.128Z&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;DEBUG   &lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;Tool:ReadFile&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; returned 109 chars&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;2026-03-29T15:14:46.352Z&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;DEBUG   &lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;Thread:54d30d83-a43a-4a3b-80dd-382741b95189&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; Tool calls executed: ReadFile&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; looping&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; back&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; model&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;2026-03-29T15:14:46.352Z&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;DEBUG   &lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;Thread:54d30d83-a43a-4a3b-80dd-382741b95189&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; generateText step 3/15&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;2026-03-29T15:14:48.094Z&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;INFO    &lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;Thread:54d30d83-a43a-4a3b-80dd-382741b95189&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; Got text response,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; length&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;8&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;2026-03-29T15:14:48.094Z&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;DEBUG   &lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;Thread:54d30d83-a43a-4a3b-80dd-382741b95189&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; Response preview: This repository is &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;test-r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, a test framework and runner for Rust. It provides testing capabilities for Rust projects. More details can be found in its &lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;documentation&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;https://test-r.vigoo.dev&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;2026-03-29T15:14:48.102Z&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;INFO    &lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;CodingAgent.sendPrompt&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; Got response,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; length&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;8&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;~&lt;/span&gt;&lt;span&gt;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;This repository is for &amp;quot;test-r&amp;quot;, a test framework and runner for Rust. It provides testing capabilities for Rust projects. More details can be found in its [documentation](https://test-r.vigoo.dev).&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can see that the agent used our &lt;strong&gt;ListFiles&lt;/strong&gt; and &lt;strong&gt;ReadFile&lt;/strong&gt; tools to take a look at the repository, and gave a correct answer!&lt;/p&gt;
&lt;p&gt;Let&#39;s try another one (not showing all the debug lines now):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem-ts-repl[gca-agents][local]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; testr.sendPrompt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;What dependencies are in the cargo file?&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Here are the dependencies for the main components of this project:\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;### `test-r-macro`\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- **test-r-core**: Local path dependency\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- **darling**: &amp;quot;0.21.3&amp;quot;\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- **humantime**: &amp;quot;2.3.0&amp;quot;\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- **proc-macro2**: &amp;quot;1&amp;quot;\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- **quote**: &amp;quot;1&amp;quot;\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- **rand**: &amp;quot;0.10&amp;quot;\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- **syn**: &amp;quot;2&amp;quot; (with &amp;quot;full&amp;quot; features)\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;These packages form the core of the test framework, providing various utilities and functionality for creating and running tests in Rust.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And let&#39;s try if it is willing to edit files too:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem-ts-repl[gca-agents][local]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; testr.sendPrompt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Update the darling dependency to 0.23.0&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;2026-03-29T15:18:12.114Z&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;DEBUG   &lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;Tool:ReplaceInFile&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; 1 replacements in /test-r-macro/Cargo.toml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;The `darling` dependency has been updated to version &amp;quot;0.23.0&amp;quot; in the `test-r-macro` package.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;search-tool&quot;&gt;Search tool&lt;/h3&gt;
&lt;p&gt;Let&#39;s add a few more tools before we switch to the host side. It&#39;s very useful for a coding agent to be able to search the web. We are going to add a tool that uses &lt;a rel=&quot;external&quot; href=&quot;https://exa.ai&quot;&gt;exa&lt;/a&gt; under the hood.&lt;/p&gt;
&lt;p&gt;After registering to &lt;code&gt;exa&lt;/code&gt; and getting an API key, and telling my coding agent to wire up exa as a web search tool, we get the following:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; WebSearch&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Tool&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;make&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;WebSearch&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  description&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Search the web using Exa AI. Returns relevant results with titles, URLs, and text content. &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Use this to find documentation, research, code examples, or any web-based information. &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Optionally restrict results to specific domains or a date range.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  parameters&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    query&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    numResults&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;NullOr&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Number&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    includeDomains&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;NullOr&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Array&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    excludeDomains&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;NullOr&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Array&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    startPublishedDate&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;NullOr&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    category&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;NullOr&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;      Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Literal&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;company&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;research paper&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;news&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;pdf&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;personal site&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      )&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  success&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  failure&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Struct&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; message&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  failureMode&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; as&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; const&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;annotate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Tool&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Readonly&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;WebSearch&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;params&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;    Effect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;gen&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;      //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; res&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Effect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;tryPromise&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        try&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;          fetch&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;https://api.exa.ai/search&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            method&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;POST&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            headers&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;              &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Content-Type&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;application/json&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;              &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;x-api-key&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; exaApiKey&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            body&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; JSON&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;stringify&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;body&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        catch&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;e&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; message&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; String&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;e&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;      //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; logDebug&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;        `&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;[Tool:WebSearch] returned &lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;z-l-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; chars&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      return&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; result&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note: the official &lt;code&gt;exa-js&lt;/code&gt; package had some trouble on Golem (to be investigated) but the actual API is just a single POST call so we can do it by hand.&lt;/p&gt;
&lt;p&gt;Let&#39;s see if the agent uses the new tool:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;log&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T15:53:26.861Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;INFO&lt;/span&gt;&lt;span&gt;    ] [] [Thread:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;f7550e6c-d8d0-46aa-ac1a-1b1771d814b3&lt;/span&gt;&lt;span&gt;] Sending prompt: Does any of the dependencies of this project (based on any &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;Cargo.toml&lt;/span&gt;&lt;span&gt; in the repo) have a newer vers...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T15:53:38.483Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt;DEBUG&lt;/span&gt;&lt;span&gt;   ] [] [Tool:WebSearch] query=darling latest version, numResults=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T15:53:39.794Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt;DEBUG&lt;/span&gt;&lt;span&gt;   ] [] [Tool:WebSearch] returned &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2865&lt;/span&gt;&lt;span&gt; chars&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T15:53:39.795Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt;DEBUG&lt;/span&gt;&lt;span&gt;   ] [] [Tool:WebSearch] query=humantime latest version, numResults=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T15:53:41.099Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt;DEBUG&lt;/span&gt;&lt;span&gt;   ] [] [Tool:WebSearch] returned &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2841&lt;/span&gt;&lt;span&gt; chars&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T15:53:41.101Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt;DEBUG&lt;/span&gt;&lt;span&gt;   ] [] [Tool:WebSearch] query=proc-macro2 latest version, numResults=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T15:53:41.990Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt;DEBUG&lt;/span&gt;&lt;span&gt;   ] [] [Tool:WebSearch] returned &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2397&lt;/span&gt;&lt;span&gt; chars&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T15:53:41.992Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt;DEBUG&lt;/span&gt;&lt;span&gt;   ] [] [Tool:WebSearch] query=quote latest version, numResults=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;unix-commands&quot;&gt;Unix commands&lt;/h3&gt;
&lt;p&gt;Agents can more efficiently work on the code base if they can execute standard Unix commands. As we are in our sandboxed WASM environment, we can&#39;t actually run these commands - but we can use the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/shelljs/shelljs&quot;&gt;shelljs library&lt;/a&gt; which emulates them in pure JS.&lt;/p&gt;
&lt;p&gt;This library implements many of the standard unix commands in pure JavaScript, and supports composing them on the code level. But we should not just expose them as separate tools to the coding agent - we want it to be able to compose them (pipe them together, etc), and to use standard shell syntax for that. We will use the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/shell-quote/shell-quote&quot;&gt;shell-quote&lt;/a&gt; package for parsing the shell scripts passed by the LLM, and translate them to shelljs calls.&lt;/p&gt;
&lt;p&gt;We advertise this new tool towards the AI with the following tool definition:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; RunPipeline&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Tool&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;make&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;RunPipeline&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  description&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Run a shell-style pipeline. &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Supported commands: cat, grep, sed, head, tail, sort, uniq, wc, find, ls, echo, &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;cp, mv, rm, mkdir, touch, chmod, ln, pwd, cd. &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Supports pipes `|`, output redirection `&amp;gt;` and `&amp;gt;&amp;gt;`, quotes, and globs. &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Does NOT support `;`, `&amp;amp;&amp;amp;`, `||`, subshells, or env vars. &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Examples: `grep -rn &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;TODO&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; src/`, `find src -name &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;*.ts&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; | wc -l`, &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`sed -i &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s/foo/bar/g&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; file.ts`, `cp src/a.ts src/b.ts`, &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`cat file.ts | grep import | sort &amp;gt; imports.txt`.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  parameters&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    command&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    cwd&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;NullOr&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  success&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Struct&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    stdout&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    stderr&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    exitCode&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Number&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    truncated&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Boolean&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  failure&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Struct&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; message&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;String&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  failureMode&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; as&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; const&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The implementation itself is not that interesting to show here in details, so let&#39;s just have a summary of what it does:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;First it parses the command using &lt;code&gt;shell-quote&lt;/code&gt;&#39;s &lt;code&gt;parse&lt;/code&gt; function&lt;/li&gt;
&lt;li&gt;Based on the parsed tokens, it defines &lt;em&gt;stages&lt;/em&gt; - each stage is a command with arguments, and is either piped to the next stage or redirected to a file&lt;/li&gt;
&lt;li&gt;When running a command, it has a built-in set of allowed &lt;code&gt;shelljs&lt;/code&gt; commands and maps them to those - it also allows adding custom commands, and implements &lt;code&gt;wc&lt;/code&gt;, &lt;code&gt;find&lt;/code&gt; and &lt;code&gt;sed&lt;/code&gt; like that, as shelljs either did not implement them or it was not matching our requirements&lt;/li&gt;
&lt;li&gt;Finally it executes the whole pipeline of stages, and constructs the tool result&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Trying this out, we can see that the agent is trying to use the new tool, but runs into lot of failures, for example:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;log&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T17:02:49.957Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt;DEBUG&lt;/span&gt;&lt;span&gt;   ] [] [Tool:RunPipeline] command=find / -type f | grep -v &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;^/proc&amp;#39;&lt;/span&gt;&lt;span&gt; | grep -v &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;^/sys&amp;#39;&lt;/span&gt;&lt;span&gt; | grep -v &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;^/dev&amp;#39;&lt;/span&gt;&lt;span&gt; | grep -v &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;^/node_modules&amp;#39;&lt;/span&gt;&lt;span&gt; | grep -v &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;^/target&amp;#39;&lt;/span&gt;&lt;span&gt; | grep -E &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;\.(rs|toml|md|lock)$&amp;#39;&lt;/span&gt;&lt;span&gt; | xargs grep -Rni &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;desert[-_]rust\|desert_rust\|desert rust&amp;quot;&lt;/span&gt;&lt;span&gt;, cwd=/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;2026-03-29&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;T17:02:49.958Z&lt;/span&gt;&lt;span&gt;] [&lt;/span&gt;&lt;span class=&quot;z-l-8&quot;&gt;DEBUG&lt;/span&gt;&lt;span&gt;   ] [] [Tool:RunPipeline] exitCode=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2&lt;/span&gt;&lt;span&gt;, stdout=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt; chars, truncated=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;false&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What I did next: asked our coding agent about what it thinks about our &lt;code&gt;RunPipeline&lt;/code&gt; tool:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem-ts-repl[gca-agents][local]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; testr.sendPrompt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Can you analyse all your RunPipeline tool calls so far, and tell me what did you expect and did not work, etc? I&amp;#39;m a developer of this tool and looking for feedback&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This returned a huge report that I&#39;ve just pasted to my primary coding agent working on this example, to come up with a set of action items:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Here&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; are&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; the&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; action&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; items&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; extracted&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; from&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; the&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; feedback,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; priority&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; order:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Must&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; fix&lt;/span&gt;&lt;span&gt; (caused&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; real&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; failures&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Support&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; grep&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;R&lt;/span&gt;&lt;span&gt; (recursive&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; grep&lt;/span&gt;&lt;span&gt;) — Most impactful missing feature. Shelljs grep already supports this flag, so it&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s likely our args passing that&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;s broken &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;for&lt;/span&gt;&lt;span&gt; combined flags like -Rni.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;2.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Support&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; grep&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;E&lt;/span&gt;&lt;span&gt; (extended&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; regex&lt;/span&gt;&lt;span&gt;) — Shelljs grep uses JS RegExp which already supports extended syntax, so -E just needs to be accepted (&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;and&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ignored,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; since&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; JS&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; regex&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; is&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; already&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;extended&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;).&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;3.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Add&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; xargs&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; command&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; —&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Natural&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; companion&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; find.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; At&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; minimum&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; support&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; find&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ...&lt;/span&gt;&lt;span&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; xargs&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; grep&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; pattern.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;4.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Fix&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; empty&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; grep&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; error&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; message&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; —&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; grep:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  with&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; no&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; diagnostic&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; when&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; pattern&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; like&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;desert_rust\|desert-rust&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; fails.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Need&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; catch&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; regex&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; parse&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; errors&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; and&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; report&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; them&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; clearly.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Should&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; fix&lt;/span&gt;&lt;span&gt; (UX/documentation&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;5.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Improve&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; tool&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; description&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; —&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Rewrite&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; explicitly&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; say&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; it&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s not a full shell. Mention: no loops, no ;/&amp;amp;&amp;amp;/||, no subshells, no env vars. Say &amp;quot;restricted pipeline executor&amp;quot; not &amp;quot;shell-style pipeline&amp;quot;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;6. Document supported flags per command in the description — Even a brief summary like grep: -n, -i, -v, -l, -r, -c, -E.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;7. Clarify control structures are unsupported — Explicitly state no while, for, if, etc.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Nice to have&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;8. Standardize error messages — Every failure should say: which command, why, which flag/pattern caused it. No blank error strings.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&#39;s leave the agents figuring this out and move to our next topic - we can see that our proof of concept for emulating shell commands is working.&lt;/p&gt;
&lt;h3 id=&quot;read-only-mode&quot;&gt;Read-only mode&lt;/h3&gt;
&lt;p&gt;In our design we said we want to be able to have multiple &lt;strong&gt;read-only&lt;/strong&gt; sessions of the same repository/branch combination. Why have multiple agents for this, instead of just having multiple &lt;code&gt;Thread&lt;/code&gt;s within the same agent? Because Golem agents are single-threaded and they do not allow overlapping async invocations. This means that we cannot run two parallel threads within the same agents.&lt;/p&gt;
&lt;p&gt;We already determined that we are going to use the phantom agent feature for this. It&#39;s actually not requiring any further work - but we should explicitly disallow making changes in the phantom agents, to make sure the LLMs will not get confused. This means reducing the available tools presented to the AI based on our agent&#39;s phantom ID.&lt;/p&gt;
&lt;p&gt;We define a limited toolkit for read-only agents:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; ReadOnlyCodingToolkit&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Toolkit&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;make&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  ListFiles&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  ReadFile&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  WebSearch&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  ReadOnlyRunPipeline&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and define an abstraction over &lt;code&gt;chat.generateText&lt;/code&gt; using Effect&#39;s &lt;code&gt;Context.Tag&lt;/code&gt; service pattern:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ThreadAi&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Context&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Tag&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ThreadAi&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  ThreadAi&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    readonly&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; generateStep&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      chat&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-10&quot;&gt; Chat&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Service&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      prompt&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-10&quot;&gt; Prompt&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RawInput&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-10&quot;&gt; Effect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Effect&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ThreadStep&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-10&quot;&gt; AiError&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AiError&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-10&quot;&gt; LanguageModel&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LanguageModel&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we can change our &lt;code&gt;Thread&lt;/code&gt; to use &lt;code&gt;ThreadAi&lt;/code&gt; to send the messages, and we can provide the proper &lt;code&gt;ThreadAi&lt;/code&gt; implementation based on the read-only flag:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; const&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; makeThreadAiLayer&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;exaApiKey&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; readOnly&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; boolean&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;  Layer&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;succeed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;ThreadAi&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    generateStep&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; readOnly&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-7&quot;&gt;      ?&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;chat&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; prompt&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;          chat&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;generateText&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; prompt&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; toolkit&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; ReadOnlyCodingToolkit&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;pipe&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;              Effect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;provide&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;makeReadOnlyCodingToolkitLayer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;exaApiKey&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;              Effect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;normalize&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-7&quot;&gt;      :&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;chat&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; prompt&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;          chat&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;generateText&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; prompt&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; toolkit&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; CodingToolkit&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;pipe&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;              Effect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;provide&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;makeCodingToolkitLayer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;exaApiKey&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;              Effect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;normalize&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            )&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can try this out in the Golem REPL:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; repl&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem-ts-repl[gca-agents][local]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; const&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; testr&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; CodingAgent.get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;https://github.com/vigoo/test-r.git&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;test1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem-ts-repl[gca-agents][local]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; testr.sendPrompt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Give me a list of all the tools you have&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;I have these tools available in this environment:\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- `functions.ListFiles`\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- `functions.ReadFile`\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- `functions.WriteFile`\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- `functions.ReplaceInFile`\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- `functions.WebSearch`\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- `functions.RunPipeline`\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- `multi_tool_use.parallel`\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;I can also respond directly in chat without using a tool.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem-ts-repl[gca-agents][local]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; const&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; testrRo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; CodingAgent.newPhantom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;https://github.com/vigoo/test-r.git&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;test1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem-ts-repl[gca-agents][local]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; testrRo.sendPrompt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Gie me a list of all the tools you have&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Here are the tools I have available in this chat:\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- `functions.ListFiles`\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  - List files and directories.\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  - Can filter by substring.\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- `functions.ReadFile`\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  - Read a file’s contents.\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  - Can read specific line ranges.\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- `functions.WebSearch`\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  - Search the web for documentation, articles, research, etc.\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  - Can restrict by domain, date, and category.\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- `functions.RunPipeline`\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  - Run a restricted read-only shell-style pipeline.\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  - Supports commands like `find`, `grep`, `cat`, `head`, `tail`, `ls`, `sort`, `uniq`, `wc`, `echo`, `xargs`, `pwd`.\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;- `multi_tool_use.parallel`\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  - Run multiple developer tools in parallel when appropriate.\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Also, per your environment, I’m in read-only mode on the checked-out repo at `/`, so I can inspect and analyze code but not modify it.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can also see that we have two separate agent instances now:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; agent&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; list&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+--------------------+-------------------------------+-----------+--------+-------------&lt;/span&gt;&lt;span&gt;|&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; name&lt;/span&gt;&lt;span&gt;     |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Agent&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; name&lt;/span&gt;&lt;span&gt;                    |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Component&lt;/span&gt;&lt;span&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Status&lt;/span&gt;&lt;span&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Pending&lt;/span&gt;&lt;span&gt;     |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|&lt;/span&gt;&lt;span&gt;                    |&lt;/span&gt;&lt;span&gt;                               |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; revision&lt;/span&gt;&lt;span&gt;  |&lt;/span&gt;&lt;span&gt;        |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; invocations&lt;/span&gt;&lt;span&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+--------------------+-------------------------------+-----------+--------+-------------&lt;/span&gt;&lt;span&gt;|&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; gca-agents:ts-main&lt;/span&gt;&lt;span&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; CodingAgent(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;https://         |         0 |   Idle |           0 |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;|                    | github.com/vigoo/test-r.git&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;,&lt;/span&gt;&lt;span&gt; |&lt;/span&gt;&lt;span&gt;           |&lt;/span&gt;&lt;span&gt;        |&lt;/span&gt;&lt;span&gt;             |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|&lt;/span&gt;&lt;span&gt;                    |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;test1&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;1e0d3e2b-b20c-4cdc-  &lt;/span&gt;&lt;span&gt;|&lt;/span&gt;&lt;span&gt;           |&lt;/span&gt;&lt;span&gt;        |&lt;/span&gt;&lt;span&gt;             |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|&lt;/span&gt;&lt;span&gt;                    |&lt;/span&gt;&lt;span&gt; b1be-4d8ca1d22a72&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;            |&lt;/span&gt;&lt;span&gt;           |&lt;/span&gt;&lt;span&gt;        |&lt;/span&gt;&lt;span&gt;             |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+--------------------+-------------------------------+-----------+--------+-------------+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; gca-agents:ts-main&lt;/span&gt;&lt;span&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; CodingAgent(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;https://         |         0 |   Idle |           0 |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;|                    | github.com/vigoo/test-r.git&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;,&lt;/span&gt;&lt;span&gt; |&lt;/span&gt;&lt;span&gt;           |&lt;/span&gt;&lt;span&gt;        |&lt;/span&gt;&lt;span&gt;             |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|&lt;/span&gt;&lt;span&gt;                    |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;test1&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)                      &lt;/span&gt;&lt;span&gt;|&lt;/span&gt;&lt;span&gt;           |&lt;/span&gt;&lt;span&gt;        |&lt;/span&gt;&lt;span&gt;             |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+--------------------+-------------------------------+-----------+--------+-------------+&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;threads&quot;&gt;Threads&lt;/h3&gt;
&lt;p&gt;The last thing we will do in our coding agent is to have a basic concept of threads. We already started this - we have an active thread ID, and a map of threads, but so far we&#39;ve only created one thread. We are going to introduce a few new &lt;strong&gt;agent methods&lt;/strong&gt; for controlling the our coding agent&#39;s threads:&lt;/p&gt;
&lt;h4 id=&quot;archiving-threads&quot;&gt;Archiving threads&lt;/h4&gt;
&lt;p&gt;We add an &lt;code&gt;archiveThread()&lt;/code&gt; method that archives the current thread - archived threads are going to be marked as such, and a summary is going to be stored for them, generated by a new LLM call.&lt;/p&gt;
&lt;h4 id=&quot;listing-threads&quot;&gt;Listing threads&lt;/h4&gt;
&lt;p&gt;We also expose a &lt;code&gt;listThread()&lt;/code&gt; method, that returns all the threads with their ID, archive state and summary.&lt;/p&gt;
&lt;h4 id=&quot;activating-a-thread&quot;&gt;Activating a thread&lt;/h4&gt;
&lt;p&gt;There can be multiple non-archived threads, and we can switch between them. &lt;code&gt;sendPrompt&lt;/code&gt; always works on the active one.&lt;/p&gt;
&lt;h4 id=&quot;handoff&quot;&gt;Handoff&lt;/h4&gt;
&lt;p&gt;We can &lt;code&gt;handoff&lt;/code&gt; with a goal to archive the current thread, generate a summary and start a new thread. In the new thread&#39;s system prompt we include the parent thread&#39;s ID and the provided goal.&lt;/p&gt;
&lt;p&gt;Adding these new methods is straightforward and does not require anything that we haven&#39;t seen before - just manipulating in-memory data, and call out to the LLM using &lt;code&gt;@effect/ai&lt;/code&gt;. What is interesting is that we will also provide some new &lt;strong&gt;tools&lt;/strong&gt; for the LLM to work on threads:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;GetThreadInfo&lt;/code&gt; tool to get a thread&#39;s message count and summary by id&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ReadThreadMessages&lt;/code&gt; to read chunks of the full conversation of the given thread&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This way the agent can continue work in a new thread based on the summary of the parent thread (which is not included in the system prompt, just loaded on-demand with the tool) and it can decide to read more details from the parent thread if needed.&lt;/p&gt;
&lt;p&gt;Let&#39;s leave this feature for now, and get back to testing it once we have our text user interface!&lt;/p&gt;
&lt;h3 id=&quot;text-user-interface&quot;&gt;Text user interface&lt;/h3&gt;
&lt;p&gt;Let&#39;s start implementing our user interface for our coding agent system! As mentioned in the introduction, we are going to use &lt;strong&gt;Rust&lt;/strong&gt; for this. The first thing we want is to tell Golem to generate a Rust client library for our coding agent:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ridge&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  r&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ust&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;gents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next run of &lt;code&gt;golem build&lt;/code&gt; will generate our client crate:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; build&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Generating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; bridge&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; SDKs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  Generating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Rust&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; bridge&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; SDK&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; CodingAgent&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem-temp/bridge-sdk/rust/coding-agent-client&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Finished&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; building&lt;/span&gt;&lt;span&gt; [OK&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can ask our (real) coding agent to generate a first version of a user interface using this bridge SDK and the &lt;code&gt;ratatui&lt;/code&gt; crate:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Accept a repo and branch parameter and a read-only flag&lt;/li&gt;
&lt;li&gt;If not provided, ask for it on startup&lt;/li&gt;
&lt;li&gt;Expose all agent methods as &lt;code&gt;/command&lt;/code&gt; commands&lt;/li&gt;
&lt;li&gt;Everything else goes to &lt;code&gt;sendPrompt&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Setting up the client connection is straightforward and follows the same API that agent-to-agent communication and our REPL is providing:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;coding_agent_client&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;configure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;    golem_client&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;bridge&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;GolemServer&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Local&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;gca-agents&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;local&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; client&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; if&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; cli&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;readonly &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    CodingAgent&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new_phantom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;repo&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; branch&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;await&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    CodingAgent&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;repo&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; branch&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;await&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After a few short iterations, we have something like this:&lt;/p&gt;
&lt;div id=&quot;tui-demo1&quot;&gt;&lt;/div&gt;
&lt;h3 id=&quot;log-streaming&quot;&gt;Log streaming&lt;/h3&gt;
&lt;p&gt;Note that our coding agent interface is &lt;strong&gt;non-streaming&lt;/strong&gt; so we cannot see the agent&#39;s progress while waiting for the final answer to our prompt. With the current version of Golem, we cannot really make the agent methods streaming, but we can do a few things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In this particular project we could send messages to a local http server started by the host application - we need this http server anyway for exposing the build/test tools&lt;/li&gt;
&lt;li&gt;But Golem itself has a &lt;strong&gt;log stream&lt;/strong&gt; web socket API we can use to get all the logs generated by our agent. Let&#39;s go with this choice for now.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We will connect to this web-socket stream for the whole lifetime of the coding-agent TUI, but only show the messages while waiting for an answer - and collapsing them once the answer arrived.&lt;/p&gt;
&lt;div id=&quot;tui-demo2&quot;&gt;&lt;/div&gt;
&lt;h3 id=&quot;message-history&quot;&gt;Message history&lt;/h3&gt;
&lt;p&gt;We missed some necessary APIs from our agent to properly implement switching between threads:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;we should be able to start new active threads without handoff (&lt;code&gt;newThread()&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;we should store something for active threads that helps the user identifying them - let&#39;s say the first prompt&#39;s first 30 characters&lt;/li&gt;
&lt;li&gt;we need to be able to get the full history when activating a thread (including on startup). this way when switching between threads, or restarting the TUI, we will always get the whole thread in our display&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Adding these new features gives us the following agent interface (I also introduced a &lt;code&gt;ThreadId&lt;/code&gt; type just to make it more type-safe):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Exports:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;   CodingAgent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;repository:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; branch:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; agent&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; constructor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;   CodingAgent.activateThread&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;threadId:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ThreadId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;   CodingAgent.archiveThread&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;   CodingAgent.dump&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;   CodingAgent.getActiveThreadId&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ThreadId&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;   CodingAgent.getHistory&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; HistoryEntry[]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;   CodingAgent.handoff&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;goal:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ThreadId&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;   CodingAgent.listThreads&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ThreadInfo[]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;   CodingAgent.newThread&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ThreadId&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;║&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;   CodingAgent.sendPrompt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;prompt:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With the ability to start new threads we have two conflicting features now:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;we can have read-only phantom agents that can act fully in parallel&lt;/li&gt;
&lt;li&gt;we can have multiple parallel threads in our primary agent (or any other) that can all make modifications on the shared code base, but they cannot run in parallel&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Is this too much, or a useful feature? I am not sure, it would require some real user testing to find what&#39;s working best.&lt;/p&gt;
&lt;h3 id=&quot;host-features-as-tools&quot;&gt;Host features as tools&lt;/h3&gt;
&lt;p&gt;Up until this point we did not take any advantage of our Golem server being a local one. Everything we&#39;ve seen can just as well run in a hosted Golem infrastructure.&lt;/p&gt;
&lt;p&gt;The last feature we are going to add is to allow our coding agents to run build and test operations &lt;strong&gt;on our machine&lt;/strong&gt;; this, of course, as mentioned in the introduction makes them break out of their sandbox, but that&#39;s something we accept for this experiment. I&#39;m going to write some ideas of what else could be done at the end of this post.&lt;/p&gt;
&lt;p&gt;To develop this feature we are going to take advantage of Golem being started as a local process in our machine in two ways:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We make the agent filesystem&#39;s temporary directories deterministic, so we don&#39;t have to get the whole repository through agent invocation, we can mirror it directly on the file system&lt;/li&gt;
&lt;li&gt;We are going to start a local http server in our TUI that the agents can connect to through &lt;code&gt;localhost&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;deterministic-agent-file-systems&quot;&gt;Deterministic agent file systems&lt;/h4&gt;
&lt;p&gt;With a small tweak to &lt;code&gt;golem&lt;/code&gt; itself we can make it accept a command-line option to switch from temporary directories as agent file systems to deterministic ones - a known root directory, and a subdirectory derived from the &lt;code&gt;AgentId&lt;/code&gt; for each agent. Note that we &lt;strong&gt;must not&lt;/strong&gt; ever touch these directories, otherwise we break Golem&#39;s deterministic execution guarantees. This is just a hack to speed up file synchronization locally - so we are always going to only &lt;strong&gt;read&lt;/strong&gt; from these directories. We could enforce this with file-system permissions and users, but I&#39;m not going to do it in this proof of concept implementation.&lt;/p&gt;
&lt;p&gt;To find an agent&#39;s directory in this new deterministic mode, our host application will need to be able to resolve &lt;code&gt;&amp;lt;root&amp;gt;/&amp;lt;environment_id&amp;gt;/&amp;lt;component_id&amp;gt;/&amp;lt;agent_name&amp;gt;/&lt;/code&gt;.&lt;/p&gt;
&lt;h4 id=&quot;calling-out-to-the-host&quot;&gt;Calling out to the host&lt;/h4&gt;
&lt;p&gt;We are going to keep this simple, and offer two endpoints from our host application, both exposed as &lt;strong&gt;tools&lt;/strong&gt; to the LLM.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;RunOnHost&lt;/code&gt; is going to take a command and arguments. It&#39;s going to use a white-list on the host (for this experiment, we are only going to allow &lt;code&gt;cargo&lt;/code&gt; and anything in the &lt;code&gt;target&lt;/code&gt; dir) and we are always going to save the process output to a file. I have seen too many times agents applying filters (&lt;code&gt;grep&lt;/code&gt;, &lt;code&gt;head&lt;/code&gt;, &lt;code&gt;tail&lt;/code&gt;, etc) to expensive test runs just to realize in the end they don&#39;t see everything they are interested in. We will return the name of this file, and we will also provide timeout capability.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ReadProcessOutput&lt;/code&gt; to read the executed host process outputs. It will be similar to &lt;code&gt;ReadFile&lt;/code&gt;, but only for files generated by &lt;code&gt;RunOnHost&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We can easily start a HTTP server in our Rust application providing endpoints for these, I&#39;m not going to get into the details of it. The only trick is that, as said earlier, we must not change the agent&#39;s file system from the outside. So before &lt;code&gt;RunOnHost&lt;/code&gt; we need to synchronize from the agent&#39;s file system directory to a mirrored one, where we are running the actual commands.&lt;/p&gt;
&lt;p&gt;Let&#39;s see what we need to do in our Golem agent to make use of it!&lt;/p&gt;
&lt;p&gt;We extend our agent configuration with a new field, which is &lt;strong&gt;not&lt;/strong&gt; a secret:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CodingAgentConfig&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  openaiApiKey&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Secret&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  exaApiKey&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Secret&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;  hostHttpPort&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; number&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By having a non-secret configuration, we get a new &lt;code&gt;_with_config&lt;/code&gt; variant of the client constructor in our generated bridge SDK, where we can specify values for the non-secret fields:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; client&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; if&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; cli&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;readonly &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    CodingAgent&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new_phantom_with_config&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;repo&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; branch&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; host_http_port&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;await&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    CodingAgent&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get_with_config&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;repo&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; branch&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; host_http_port&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;await&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With the port known in the agent, wiring the HTTP endpoints as tools is straightforward.&lt;/p&gt;
&lt;h4 id=&quot;final-demo&quot;&gt;Final demo&lt;/h4&gt;
&lt;p&gt;Let&#39;s see how our final version works. We are going to open a rust repository, update a dependency and verify it compiles in one thread, and ask some questions in another thread:&lt;/p&gt;
&lt;div id=&quot;tui-demo3&quot;&gt;&lt;/div&gt;
&lt;h2 id=&quot;final-words&quot;&gt;Final words&lt;/h2&gt;
&lt;h3 id=&quot;durability&quot;&gt;Durability&lt;/h3&gt;
&lt;p&gt;We talked about durability in the introduction and then it never came up again. I did this intentionally, to show that when using &lt;strong&gt;Golem&lt;/strong&gt; you do NOT have to worry about durable execution at all!&lt;/p&gt;
&lt;p&gt;We can kill any of the processes involved any time - the local &lt;code&gt;golem&lt;/code&gt; server, the TUI, the REPL, and so on. We just restart them and continue from where we were. Even if the server was killed in the middle of an LLM call, only that last request is going to be recreated, all the history to build up the context is recovered for free.&lt;/p&gt;
&lt;h3 id=&quot;what-else&quot;&gt;What else?&lt;/h3&gt;
&lt;p&gt;With this I consider this experiment done, but there could be a lot more features added to make this a real coding agent. Let&#39;s see some of them:&lt;/p&gt;
&lt;h4 id=&quot;git-commands-as-tools-for-the-agents&quot;&gt;Git commands as tools for the agents&lt;/h4&gt;
&lt;p&gt;It can be useful for the agent to observe git history, see its own diffs, etc. We can expose git commands to the LLM directly by defining more tools that are using the &lt;code&gt;isomorphic-git&lt;/code&gt; library under the hood.&lt;/p&gt;
&lt;h4 id=&quot;committing-a-branch&quot;&gt;Committing a branch&lt;/h4&gt;
&lt;p&gt;We have not added any support yet to actually finish a branch and commit and push the changes before the agent gets closed.&lt;/p&gt;
&lt;h4 id=&quot;automatic-tracking-of-context-size-and-managing-threads&quot;&gt;Automatic tracking of context size and managing threads&lt;/h4&gt;
&lt;p&gt;We could automatically track the context size in the agent for each thread, and do automatic handoff when needed. The threads could expose metrics like the number of tokens used, or the cost involved, and report it to the text user interface.&lt;/p&gt;
&lt;h4 id=&quot;agents-md-skill-support&quot;&gt;Agents.md / skill support&lt;/h4&gt;
&lt;p&gt;Supporting &lt;code&gt;AGENTS.md&lt;/code&gt; and skill files is also straightforward. We can include the contents of all discovered agent files in the system prompt, and we can add new &lt;strong&gt;tools&lt;/strong&gt; for skill discovery and loading.&lt;/p&gt;
&lt;h4 id=&quot;execution-rules-per-repository&quot;&gt;Execution rules per repository&lt;/h4&gt;
&lt;p&gt;We&#39;ve just hard-coded some simple rules for the host execution, but that should be evolved into having proper configurable per-repository rules.&lt;/p&gt;
&lt;h4 id=&quot;use-a-live-web-socket-connection-to-the-agents&quot;&gt;Use a live web-socket connection to the agents&lt;/h4&gt;
&lt;p&gt;This is something that is not supported by the current Golem release, but we plan to support it in the future. By having a bidirectional WebSocket connection to our users, we can do the host tool invocations through that, and we can also live-stream the events directly, not through the log event stream as today.&lt;/p&gt;
&lt;h4 id=&quot;virtual-machines&quot;&gt;Virtual machines&lt;/h4&gt;
&lt;p&gt;The host execution feature is convenient and maybe necessary for some projects, but it breaks the sandboxing and basically lets the coding agent to run arbitrary code on our machine. We could evolve this to use virtual machines instead - the concept is the same, we mirror the agent&#39;s file-system into the virtual machine, set up the toolchain there and then run the commands the agent is asking for through its tool call. The split would remain the same - all the editing and exploration is done locally in the agent, it only calls out to run something that cannot be done in its sandboxed WASM environment.&lt;/p&gt;
&lt;p&gt;With either virtual machines, or live web socket connections, or a combination of both, this can be turned into a real hosted service that no longer needs to run locally.&lt;/p&gt;
&lt;h4 id=&quot;seeing-the-agent-s-changes&quot;&gt;Seeing the agent&#39;s changes&lt;/h4&gt;
&lt;p&gt;One drawback of this experimental implementation is that we cannot really see easily what changes the agent made. When using a coding agent on a local checked out repository we can use IDEs or git UIs to examine the changes. We can do the same with the deterministic agent working directory trick, but doing so risks of making changes from the outside to the agent&#39;s file system that we definitely don&#39;t want to. It would also not possible to move to a hosted golem later. What we could do is improve the syncing that we already have for the host execution to keep a live mirror of the agent&#39;s file system locally. Then we can use existing tools to observe the changes as they happen.&lt;/p&gt;
&lt;p&gt;We could also evolve our user interface to a text (or graphical) post-IDE agentic coding interface that has built-in support for viewing the repository and the changes happening in them.&lt;/p&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;The most important take-away from this experiment is, in my opinion, that writing this whole initial working proof-of-concept implementation took less than a day, and that includes several Golem fixes I had to make (we are in the bug fixing / testing phase for our new release) and includes writing this post.&lt;/p&gt;
&lt;p&gt;With Golem providing the durable agent infrastructure, the generated client libraries, configuration system etc, we (and our coding agent writing the actual TypeScript and Rust code) could focus on the important parts of defining our interface, our tools and our user interface. The hard parts are just working out of the box. Our final Golem release will even make this faster, because we are going to include a &lt;strong&gt;skill catalog&lt;/strong&gt; for coding agents so they don&#39;t have to figure out how to use Golem on their own. Even without those, I haven had to write a single line of code during this experiment.&lt;/p&gt;
&lt;p&gt;I am publishing the code for this &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/gca&quot;&gt;on GitHub&lt;/a&gt; but today it&#39;s not that easy to try it out - as I said I had to make some Golem fixes to make everything work, and some of those fixes have not even merged to &lt;code&gt;main&lt;/code&gt; yet. But &lt;strong&gt;Golem 1.5&lt;/strong&gt; is going to be released soon, in April, 2026. Within a few weeks you can do the same by just downloading the official Golem binaries!&lt;/p&gt;
&lt;link rel=&quot;preconnect&quot; href=&quot;https://cdn.jsdelivr.net&quot; crossorigin&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://cdn.jsdelivr.net/npm/asciinema-player@3.9.0/dist/bundle/asciinema-player.css&quot; /&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/asciinema-player@3.9.0/dist/bundle/asciinema-player.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;AsciinemaPlayer.create(&#39;/images/tui1.cast&#39;, document.getElementById(&#39;tui-demo1&#39;), { theme: &#39;monokai&#39;, fit: &#39;width&#39; });&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/asciinema-player@3.9.0/dist/bundle/asciinema-player.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;AsciinemaPlayer.create(&#39;/images/tui2.cast&#39;, document.getElementById(&#39;tui-demo2&#39;), { theme: &#39;monokai&#39;, fit: &#39;width&#39; });&lt;/script&gt;
&lt;script&gt;AsciinemaPlayer.create(&#39;/images/tui3.cast&#39;, document.getElementById(&#39;tui-demo3&#39;), { theme: &#39;monokai&#39;, fit: &#39;width&#39; });&lt;/script&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Agent patterns in Golem</title>
        <published>2025-12-19T00:00:00+00:00</published>
        <updated>2025-12-19T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem-agent-patterns/"/>
        <id>https://blog.vigoo.dev/posts/golem-agent-patterns/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem-agent-patterns/">&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://golem.cloud&quot;&gt;Golem&lt;/a&gt; is an &lt;em&gt;agent-native&lt;/em&gt; platform that provides high level of fault-tolerance and exactly-once (or in some cases, at-least once) semantics automatically without requiring to write any code for persisting and recovering state. We wrote several blog posts, demos and live coding sessions showing how this looks like. Yesterday I&#39;ve &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/posts/rust-agents-golem14/&quot;&gt;posted about writing a Golem application in Rust&lt;/a&gt;, and a week before John de Goes had a really nice &lt;a rel=&quot;external&quot; href=&quot;https://www.youtube.com/live/ovVn_fNIyJU&quot;&gt;live coding session showing writing a NoSQL database using Golem in Type Script&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In all these examples we are defining one or more &lt;strong&gt;agents&lt;/strong&gt; that interact with each other - it&#39;s the fundamental building block of a Golem application. But what are these agents? How should we structure our application, what are some common patterns?&lt;/p&gt;
&lt;p&gt;I&#39;m trying to answer some of these questions in this article.&lt;/p&gt;
&lt;h2 id=&quot;agents-as-workflows&quot;&gt;Agents as workflows&lt;/h2&gt;
&lt;p&gt;When we first released Golem, we said you can run any application (with some restrictions, of course) on it without having to rewrite it to use anything Golem specific. With the new, agent centric approach this might seem to be no longer true - but to some extent it still is. The simplest way to map an application to agents is that every unique run of the program is an agent - we can create such a unique instance of our program, and run it - and it&#39;s going to do some &lt;em&gt;side-effects&lt;/em&gt; such as calling remote HTTP endpoints or databases.&lt;/p&gt;
&lt;p&gt;In this setup the agent&#39;s identity is a unique identifier - for example a UUID, and the agent exposes a single callable entry point, similar how a traditional program&#39;s exposes it&#39;s single entry point in the form of a &lt;code&gt;main&lt;/code&gt; function. Let&#39;s see how an agent like this looks like in Golem, using TypeScript:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; agent&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; from&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;@golemcloud/golem-ts-sdk&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; validate&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; as&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; uuidValidate&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; from&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;uuid&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BlackboxWorkflow&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10 z-d-i&quot;&gt;        super&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;uuidValidate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            throw&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Error&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Invalid id, must be a UUID: &lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; id&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;void&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Potentially long running workflow doing a series of steps&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;run&lt;/code&gt; function can do a a series of steps - call &lt;code&gt;fetch&lt;/code&gt; multiple times, sleep, use third party libraries to connect to external systems, and so on, without having any Golem specific detail in it. Even an agent like this is completely &lt;strong&gt;durable&lt;/strong&gt; in Golem. For example, if it&#39;s execution gets interrupted by a scaling event the running agent can be &quot;moved&quot; (interrupted in one node, and restored in another) to another node and it can continue executing without any noticable effect, except for some latency.&lt;/p&gt;
&lt;h3 id=&quot;observing-a-workflow-s-state&quot;&gt;Observing a workflow&#39;s state&lt;/h3&gt;
&lt;p&gt;The above example is a &lt;strong&gt;black box&lt;/strong&gt; - once you started a workflow like this, by creating the agent and invoking it&#39;s &lt;code&gt;run&lt;/code&gt; method, you cannot really observe the workflow&#39;s inner state. You can observe its side effects - it may call external systems, write logs, etc, but there is no real way to query or control what&#39;s happening until &lt;code&gt;run&lt;/code&gt; finishes.&lt;/p&gt;
&lt;p&gt;The primary reason for this is that Golem agents are &lt;strong&gt;single-threaded&lt;/strong&gt; and &lt;strong&gt;invocations cannot overlap&lt;/strong&gt;. Although &lt;code&gt;run&lt;/code&gt; can contain overlapping asynchronous network calls, for example, even if we would export more methods than just &lt;code&gt;run&lt;/code&gt; from our agent, we would not be able to call them &lt;em&gt;during&lt;/em&gt; &lt;code&gt;run&lt;/code&gt; is executing. The other calls would end up in a message queue, waiting to be processed one by one.&lt;/p&gt;
&lt;p&gt;One very important property of &lt;strong&gt;agents&lt;/strong&gt; is that an agent can &lt;strong&gt;call&lt;/strong&gt; another agent; this call can be synchronous (the caller awaits a response) or just a trigger (the caller puts an invocation request in the other agent&#39;s message queue). Both of these are persistent and by that, Golem can guarantee &lt;strong&gt;exactly-once semantics&lt;/strong&gt;. If the code to call an agent from another agent runs, you can be sure that is going to happen, and only once, no matter what happens to the execution environment.&lt;/p&gt;
&lt;p&gt;We can use this feature to introduce simple observability to a long-running workflow like the one we defined above by defining a second agent representing the running workflow&#39;s state:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;    tag&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;not-started&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;    id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt; |&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;    tag&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;in-progress&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;    id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;    currentStep&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;    startedAt&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt; |&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;    tag&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;completed&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;    id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;    startedAt&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-7&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;    finishedAt&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;    results&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; number&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; some domain-specific result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; WorkflowState&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; state&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10 z-d-i&quot;&gt;        super&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;not-started&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; id&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      return&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;state&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    update&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;currentStep&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; ===&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;not-started&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;            this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;              tag&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;in-progress&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;              id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;id&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; currentStep&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;              startedAt&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Date&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;toISOString&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; ===&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;in-progress&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;            this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;currentStep&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; currentStep&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;completed&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            throw&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Error&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Cannot update completed workflow state&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    finished&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; number&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;completed&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            throw&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Error&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Cannot finish completed workflow state&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;            this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                tag&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;completed&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;id&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                startedAt&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; ===&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;in-progress&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-7&quot;&gt;              		?&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;startedAt&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-7&quot;&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; undefined&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                finishedAt&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Date&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;toISOString&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;                results&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this, we can modify our black box agent&#39;s &lt;code&gt;run&lt;/code&gt; method to report progress and completion to this other agent:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-7&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    const &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; WorkflowState&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;   	//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    state.update.trigger(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;step 1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    state.update.trigger(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;step 2&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    state.finished.trigger([1, 2, 3]);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;trigger&lt;/code&gt; in the agent calls means we don&#39;t want to block on these calls - we just trigger the update on the other agent, so it&#39;s very fast.&lt;/p&gt;
&lt;p&gt;Our &lt;code&gt;WorkflowState&lt;/code&gt; agent is very different from our first one, as it is reactive. Its methods are not long running, they are just updating the agent&#39;s inner state. We can call &lt;code&gt;get&lt;/code&gt; any time on it while the workflow is running, these &lt;code&gt;get&lt;/code&gt; calls are going to be interleaved between the status update calls.&lt;/p&gt;
&lt;h3 id=&quot;multi-step-workflows&quot;&gt;Multi-step workflows&lt;/h3&gt;
&lt;p&gt;As we&#39;ve seen on &lt;code&gt;WorkflowState&lt;/code&gt; agents can export &lt;strong&gt;multiple methods&lt;/strong&gt;. This suggests that instead of writing a workflow like in the first example - a single method performing all the steps sequentially, we could also expose these steps as agent methods.&lt;/p&gt;
&lt;p&gt;This is not always what we want, and it has pros and cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We may already have our code structured like this - extracting sub-steps into functions. Exposing them as agent methods is just a matter of making them public methods on the agent class&lt;/li&gt;
&lt;li&gt;However once we do that, we no longer have a single entry point for our workflow. We need something that orchestrates the workflow execution! This can be both an advantage and a disadvantage:
&lt;ul&gt;
&lt;li&gt;The external orchestrator may customize the execution flow by deciding which steps to call, etc&lt;/li&gt;
&lt;li&gt;But we need to write this orchestrator (potentially an another agent) that complicates our architecture&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; InnerWorkflow&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; number&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10 z-d-i&quot;&gt;        super&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; id&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; step1&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;void&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ..&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; step2&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;void&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ..&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; step3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;void&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ..&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OuterWorkflow&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; number&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10 z-d-i&quot;&gt;        super&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; id&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;void&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; inner&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;  =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; InnerWorkflow&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        await&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; inner&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;step1&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        await&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; inner&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;step2&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        await&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; inner&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;step3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can introduce these hierarchies of agents in as many layers as we want, but the reason why would want to do so is controlling &lt;strong&gt;concurrency&lt;/strong&gt;.&lt;/p&gt;
&lt;h3 id=&quot;concurrency&quot;&gt;Concurrency&lt;/h3&gt;
&lt;p&gt;Even though agent methods can be &lt;code&gt;async&lt;/code&gt; and do some operations in parallel - HTTP requests, remote agent calls and so on - agents are single threaded and their exported methods cannot overlap.&lt;/p&gt;
&lt;p&gt;We can implement fully parallel execution with two techniques:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Spawning child agents&lt;/li&gt;
&lt;li&gt;Forking agents&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We have already seen a simple example of spawning child agents to achieve concurrency when we created a &lt;code&gt;WorkflowState&lt;/code&gt; agent as a child of our &lt;code&gt;Workflow&lt;/code&gt; agent.&lt;/p&gt;
&lt;p&gt;In general the pattern looks like the following:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ConcurrentAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10 z-d-i&quot;&gt;        super&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; id&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;void&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; inputs&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; example chunks we want to process in parallel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; stepPromises&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; inputs&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;input&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; idx&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;            Substep&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;id&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; idx&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;input&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; results&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;all&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;stepPromises&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Substep&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; substepId&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; number&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; substepId&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; number&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10 z-d-i&quot;&gt;        super&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; id&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;substepId&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; substepId&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Some operation on input producing an output&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This example takes every element of &lt;code&gt;inputs&lt;/code&gt; and spawns a separate &lt;strong&gt;child agent&lt;/strong&gt; to process that chunk. The &lt;code&gt;Substep&lt;/code&gt; agent is responsible for performing work on one chunk - its &lt;em&gt;identity&lt;/em&gt; is no longer just the main workflow&#39;s ID, but it also contains an index. This can be useful if this number (&lt;code&gt;substepId&lt;/code&gt; in the example) holds some important meaning in our problem domain (imagine it&#39;s not just an index, but a domain-specific identifier of the chunk of data it works on).&lt;/p&gt;
&lt;p&gt;In case the identity of the substeps does not matter, just that we get a separate instance for each substep that can run in parallel, we can use the &lt;strong&gt;phantom agent&lt;/strong&gt; feature of Golem to make it even simpler:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PhantomSubstep&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10 z-d-i&quot;&gt;        super&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;      //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; stepPromises&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; inputs&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; PhantomSubstep&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;newPhantom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;input&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;newPhantom&lt;/code&gt; always creates a new instance of the agent, even if there is no user-defined unique identity to distinguish them.&lt;/p&gt;
&lt;h4 id=&quot;results&quot;&gt;Results&lt;/h4&gt;
&lt;p&gt;The above example was &lt;strong&gt;awaiting all results&lt;/strong&gt; before moving forward. This is just one of the possibilities. When using &lt;code&gt;Promise.all&lt;/code&gt; to await all the agent invocations, we were using the divide and conquer strategy to delegate work to other agents running in parallel, to speed up execution. We need all the results to move forward to the next step (or completion) of our main agent.&lt;/p&gt;
&lt;p&gt;It&#39;s also possible do a &lt;strong&gt;race&lt;/strong&gt; instead - we wait for the first child agent to complete, and use its results, ignoring the others. This is problematic in Golem 1.4 though, because &lt;code&gt;Promise.race&lt;/code&gt; does not cancel the losing promises, and Golem&#39;s current JS runtime &lt;strong&gt;ensures that all promises complete&lt;/strong&gt; before an invocation stops. So even though we would get the winner promise as soon as possible, our method would only return when every other sub-agents finished as well. This is a temporary limitation and future Golem TypeScript SDK versions will provide a way to pass an &lt;a rel=&quot;external&quot; href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal&quot;&gt;&lt;code&gt;AbortSignal&lt;/code&gt;&lt;/a&gt; to the invocations.&lt;/p&gt;
&lt;p&gt;Until then, let&#39;s see how racing looks like in a Rust agent!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_definition&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustRaceSubAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; millis&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u64&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustRaceSubAgentImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_implementation&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; RustRaceSubAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; RustRaceSubAgentImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; millis&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u64&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;        std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;thread&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;sleep&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;time&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Duration&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;from_millis&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;millis&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        format!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;slept &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; millis&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; millis&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_definition&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustRaceAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustRaceAgentImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    _name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_implementation&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; RustRaceAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; RustRaceAgentImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            _name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; RustRaceSubAgentClient&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new_phantom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; RustRaceSubAgentClient&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new_phantom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; RustRaceSubAgentClient&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new_phantom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; f1&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; a&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1000&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; f2&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; b&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2000&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; f3&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;10000&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;f1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; f2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; f3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;race&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;await&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        println!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This works as expected - the main agent finishes in 1 second.&lt;/p&gt;
&lt;p&gt;What can we do in TypeScript until invocations became abortable? There is a workaround - we can use &lt;strong&gt;Golem Promises&lt;/strong&gt; to signal completion, instead of blocking asynchronous method invocations.&lt;/p&gt;
&lt;h4 id=&quot;golem-promises&quot;&gt;Golem Promises&lt;/h4&gt;
&lt;p&gt;A &lt;strong&gt;Golem Promise&lt;/strong&gt; is a cluster-level entity that can be &lt;strong&gt;completed&lt;/strong&gt; either from an agent, or even from the outside. Completing it from the outside is our basic building block for introducing &lt;strong&gt;human-in-the-loop&lt;/strong&gt; for agentic workflows.&lt;/p&gt;
&lt;p&gt;Let&#39;s see how we can use promises as a workaround for the race issue in TypeScript!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;awaitPromise&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; completePromise&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; createPromise&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; PromiseId&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; from&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;@golemcloud/golem-ts-sdk&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RaceAgentWithPromise&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10 z-d-i&quot;&gt;        super&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; id&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;void&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; promise&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; createPromise&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;        RaceSubstepWithPromise&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;newPhantom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;promise&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;run&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;trigger&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1000&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;        RaceSubstepWithPromise&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;newPhantom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;promise&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;run&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;trigger&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2000&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;        RaceSubstepWithPromise&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;newPhantom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;promise&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;run&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;trigger&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;10000&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; awaitPromise&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;promise&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;        console&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; TextDecoder&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;decode&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;result&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RaceSubstepWithPromise&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; promiseId&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PromiseId&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;promiseId&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PromiseId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10 z-d-i&quot;&gt;        super&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;promiseId&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; promiseId&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; number&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;void&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; sleep&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;            setTimeout&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; resolve&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Slept &lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; millis&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; sleep&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        completePromise&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;promiseId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; TextEncoder&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;encode&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;result&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Instead of returning the result as a return value from our method, we are completing a &lt;strong&gt;Golem promise&lt;/strong&gt; with it. This way we don&#39;t need await the invocation itself on the caller side, instead we await the promise - and the first sub-agent that completes it will unblock that await.&lt;/p&gt;
&lt;h4 id=&quot;forking&quot;&gt;Forking&lt;/h4&gt;
&lt;p&gt;Forking is another way to do work in parallel from Golem agents. It is a special way to spawn a new agent - instead of explicitly creating a new agent with an initial state, &lt;code&gt;fork()&lt;/code&gt; creates a &lt;strong&gt;copy&lt;/strong&gt; of the agent it is called in, and both agents continue running from the fork point. The new copy inherits the state of the original agent, including all the values of all the variables etc. The only distinction between the two copies is the &lt;strong&gt;return value&lt;/strong&gt; of &lt;code&gt;fork()&lt;/code&gt; - it can be used to decide what to do next.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Golem promises&lt;/strong&gt; are an important part of working with forking, as in this case there is no invoked method to return values from.&lt;/p&gt;
&lt;p&gt;Forking can be a convenient way to parallize some work in cases where the context necessary for the child agent is big and would be hard to pass down as parameters.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-7&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    const &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;inputs&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    const &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;promises&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; input&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; of&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; inputs&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; promise&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; createPromise&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;        promises&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;push&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;processInput&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;promise&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;forked&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;		if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;!this.forked&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; results&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;all&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;promises&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;      console&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;results&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; processInput&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;promise&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;PromiseId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    switch&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;fork&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.tag) &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;original&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;            //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; awaiting the promise in the original copy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            const &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; awaitPromise&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;promise&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            return new &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;TextDecoder&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;decode&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;bytes&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;forked&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;            //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; do the actual work in the forked copy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            const &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;processed&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; input&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;            completePromise&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;promise&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; TextEncoder&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;encode&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;processed&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	          this.&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;forked&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            return processed;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that we need to remember that we got into a forked copy to avoid forking again from the fork - as all copies are (almost) identical. This makes things easy as the forked copy has the same state as the original at the fork point, but also makes things hard by having to remember to not get back to the same code path in both instances.&lt;/p&gt;
&lt;h2 id=&quot;agents-as-domain-entities&quot;&gt;Agents as domain entities&lt;/h2&gt;
&lt;p&gt;So far we represented workflows and their subtasks as agents. We can also &lt;strong&gt;model our domain using agents&lt;/strong&gt;!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Domain-driven design&lt;/strong&gt; as an approach is with us since more than 20 years, and many books and articles discuss how to model our application based on the domains its applied to. The entities identified can be directly mapped to Golem agents and the interaction between these entities can be done with our &lt;strong&gt;agent-to-agent&lt;/strong&gt; communication.&lt;/p&gt;
&lt;p&gt;It is important that agents are &lt;strong&gt;durable&lt;/strong&gt; by default, which can significantly simplify the implementation of these entities, so we end up with something that closely maps to our domain model.&lt;/p&gt;
&lt;p&gt;To demonstrate this, consider a simple e-commerce example where we identify three entities: &lt;strong&gt;Customer&lt;/strong&gt;, &lt;strong&gt;Order&lt;/strong&gt; and &lt;strong&gt;OrderItem&lt;/strong&gt;. Customers and orders are top-level entities directly accessible by their unique identifier: the customer e-mail address and the order ID. Each order is associated with a customer, and can have one or more items.&lt;/p&gt;
&lt;p&gt;We said that both Customer and Order are top-level entities with unique identifiers. This maps directly to Golem agents:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OrderId&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Customer&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; email&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10 z-d-i&quot;&gt;        super&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; email&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Order&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; orderId&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OrderId&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;orderId&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OrderId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10 z-d-i&quot;&gt;        super&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;orderId&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; orderId&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can refer to a customer directly by its domain-specific unique identifier, for example in Golem REPL:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; let vigoo = customer(&amp;quot;vigoo@email.address&amp;quot;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The third entity, an item associated with an Order is important from a data modelling perspective but probably not something we would  map to an individual Golem agent. It is small enough that we can just model it as a record type and associate the list of items directly with our Order by just adding a new field to it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OrderItem&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;    productId&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;    quantity&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; number&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;    price&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; number&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Order&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; orderId&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OrderId&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; items&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OrderItem&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Operations on these entities are going to be &lt;strong&gt;agent methods&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;item&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;OrderItem&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;    this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;items&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;push&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;item&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;getItems&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;OrderItem&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;items&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The agents can simply call each other when needed using &lt;strong&gt;agent-to-agent calls&lt;/strong&gt;. For example, assuming we also added a way to attach the customer&#39;s email address to an order, we could have a method in &lt;code&gt;Customer&lt;/code&gt; like the following:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; newOrder&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;OrderId&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    const &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;newId&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; uuidv4&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    const &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Order&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;newId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    await order.setCustomer(this.email);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    return newId;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The facts that agents are &lt;strong&gt;durable&lt;/strong&gt; and that calls between agents are guaranteed to be &lt;strong&gt;exactly-once&lt;/strong&gt; make these trivial implementations very powerful.&lt;/p&gt;
&lt;h2 id=&quot;patterns&quot;&gt;Patterns&lt;/h2&gt;
&lt;p&gt;We can identify some useful patterns that can help a lot in developing agent based applications on Golem. As people will write more and more applications on this platform, we are going to identify more of these patterns.&lt;/p&gt;
&lt;h3 id=&quot;cluster-level-singletons&quot;&gt;Cluster level singletons&lt;/h3&gt;
&lt;p&gt;The first pattern we discuss is &lt;strong&gt;cluster level singletons&lt;/strong&gt;. A cluster level singleton agent has exactly one instance in the whole application. In Golem an agent&#39;s identity is its constructor parameters - so if our agent does not have any constructor parameter, it can only have a single instance, and any other agent or external call referring to it will refer to the same instance.&lt;/p&gt;
&lt;p&gt;As an example, let&#39;s assume that our &lt;code&gt;OrderId&lt;/code&gt; type from the previous section is not a UUID but need to be a unique, sequential number. It&#39;s quite easy to achieve this of course if we have some kind of database in our application. But in Golem we don&#39;t need a third party database for this - we can just create a singleton agent responsible for generating new unique order IDs:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OrderId&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; number&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OrderIds&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; next&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OrderId&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10 z-d-i&quot;&gt;        super&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    nextId&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OrderId&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;++&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Customer&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; newOrder&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OrderId&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; newId&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; OrderIds&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;nextId&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;shard-singletons&quot;&gt;Shard singletons&lt;/h3&gt;
&lt;p&gt;Having a single shared global state using &lt;strong&gt;cluster singletons&lt;/strong&gt; can be very useful, however it can very soon become a bottleneck. One possible way to reduce this bottleneck is to not only have a single instance in the whole cluster, but define somehow a set of distinct &lt;strong&gt;shards&lt;/strong&gt;, and have a single instance &lt;strong&gt;per shard&lt;/strong&gt;. It is very much depending on the actual domain whether this technique can be applied or not, but in general the idea is that if every agent can calculate the &lt;strong&gt;shard&lt;/strong&gt; it belongs to, and there is no need to share information among the shards, they can simply access the shard instance they need to.&lt;/p&gt;
&lt;p&gt;To demonstrate this, let&#39;s assume we want to maintain a &lt;strong&gt;list of all customers&lt;/strong&gt; with a count of how many orders they have. As this map will have to be updated every time an order is created, we don&#39;t want to maintain it in a cluster level singleton. Instead, we associate the customers into N (=100) shards, and have a separate &lt;code&gt;Customer-&amp;gt;OrderCount&lt;/code&gt; map in each shard:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Shard&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; number&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ShardedCustomers&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; shard&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Shard&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; customerWithOrderCount&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Map&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; number&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;shard&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Shard&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10 z-d-i&quot;&gt;        super&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;shard&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; shard&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    registerCustomer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;customer&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;customerWithOrderCount&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;set&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;customer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    registerOrder&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;customer&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; orderId&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OrderId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; count&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;customerWithOrderCount&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;customer&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; ??&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;customerWithOrderCount&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;set&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;customer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; count&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can then write a function that determines the &lt;code&gt;Shard&lt;/code&gt; from a customer&#39;s identifier (an email address string):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; md5&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; from&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;md5-ts&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; shardOfCustomer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Shard&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; SHARDS&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 100&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; hash&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; md5&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;email&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; hashPefixAsNumber&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; parseInt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;hash&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;substring&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 8&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 16&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; hashPefixAsNumber&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; SHARDS&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using this function we can call &lt;code&gt;registerCustomer&lt;/code&gt; and &lt;code&gt;registerOrder&lt;/code&gt; in an asynchronous way using &lt;code&gt;trigger&lt;/code&gt; on the remote agent interface:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; in class Order:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;setCustomer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;customer&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;    this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;customer&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; customer&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;    ShardedCustomers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;shardOfCustomer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;customer&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;registerOrder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;trigger&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;customer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;orderId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; in class Customer:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;email&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10 z-d-i&quot;&gt;    super&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;    this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; email&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9&quot;&gt;    ShardedCustomers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;shardOfCustomer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;email&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;registerCustomer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;trigger&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;email&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By using &lt;code&gt;trigger&lt;/code&gt; we guarantee that even if the shard-level singleton gets overloaded with registration messages, this does not block our Order or Customer agents.&lt;/p&gt;
&lt;h3 id=&quot;agents-as-message-queues&quot;&gt;Agents as message queues&lt;/h3&gt;
&lt;p&gt;It&#39;s not really a pattern but this last statement also tells an important feature of Golem - every agent &lt;strong&gt;has its own persistent message queue&lt;/strong&gt;. If every agent invocation is done using &lt;code&gt;trigger&lt;/code&gt; (or &lt;code&gt;schedule&lt;/code&gt; to trigger an invocation in a future point in time), an agent actually works as a persistent message queue from the senders point of view.&lt;/p&gt;
&lt;p&gt;The example above demonstrated this - &lt;code&gt;Order&lt;/code&gt; and &lt;code&gt;Customer&lt;/code&gt; were just sending &lt;em&gt;registration messages&lt;/em&gt; to a message queue, without blocking on anything. The consumer of the message queue was the &lt;code&gt;ShardedCustomers&lt;/code&gt; agent itself.&lt;/p&gt;
&lt;p&gt;These message queues are just as durable as everything else in Golem. Once the &lt;code&gt;trigger&lt;/code&gt; call returned we can be sure that the invocation is in the target agent&#39;s pending invocations queue, and even if the agent gets restarted, or relocated to another node, it will always have it in its invocation queue.&lt;/p&gt;
&lt;p&gt;A different example could be a cluster-level singleton for aggregating important domain-level events from various agents:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Event&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; EventLog&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10 z-d-i&quot;&gt;        super&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;        console&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;/ Log an event from anywhere&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;    EventLog&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;log&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;trigger&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our &lt;code&gt;log&lt;/code&gt; processor could do anything - just log the event in the singleton agent&#39;s log stream, or store it in memory, extract and aggregate some information, etc. Even just logging it can help with debugging distributed systems, as it is a developer-defined aggregated view of the logs important for the application&#39;s logic itself, unlike the lower level server logs of the Golem platform itself.&lt;/p&gt;
&lt;h3 id=&quot;ephemeral-agents&quot;&gt;Ephemeral agents&lt;/h3&gt;
&lt;p&gt;Finally let&#39;s talk about &lt;strong&gt;ephemeral agents&lt;/strong&gt; which are so important that Golem has built-in support for them and make the use of them very convenient. Ephemeral agents are agents - they can have constructor parameters and exported methods, but they are &lt;strong&gt;not durable&lt;/strong&gt;. For each invocation a fresh instance is created, and although Golem allows you to observe past ephemeral agent instances for debugging purposes, you cannot invoke them again.&lt;/p&gt;
&lt;p&gt;Calling an ephemeral agent&#39;s method is Golem&#39;s equivalent of &lt;strong&gt;serverless functions&lt;/strong&gt;. As each invocation gets a fresh instance, we don&#39;t have to worry about the message queueing property of agents - all the invoations are going to run in parallel. This is a very good fit for writing &lt;strong&gt;request handlers&lt;/strong&gt; at the edge of a Golem application.&lt;/p&gt;
&lt;p&gt;As an example we are going to write an ephemeral agent with a single function that enumerates &lt;strong&gt;all customers&lt;/strong&gt; in the system. In a real application where we have so many customers that we are managing the list of them in sharded singletons, we would not want a single function returning all of them at once, of course; but for simplicity, let&#39;s implement it anyway.&lt;/p&gt;
&lt;p&gt;First we add a new agent method to &lt;code&gt;ShardedCustomers&lt;/code&gt; to get the list of customers in that particular shard:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;getCustomers&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;string&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Array&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;from&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;customerWithOrderCount&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;keys&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then we create an &lt;strong&gt;ephemeral&lt;/strong&gt; &lt;code&gt;RequestHandler&lt;/code&gt; agent:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ephemeral&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RequestHandler&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10 z-d-i&quot;&gt;        super&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; getAllConsumers&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; SHARDS&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 100&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; allCustomers&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; shard&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; shard&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; SHARDS&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; shard&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;++&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; customers&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; ShardedCustomers&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;shard&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;getCustomers&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;            allCustomers&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;push&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;customers&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; allCustomers&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is a long running call that makes 100 remote agent calls in sequence. But it is done every time on a fresh ephemeral agent instance without affecting any potentially already running request handlers.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;As I demonstrated the basic building blocks of Golem - &lt;strong&gt;agents&lt;/strong&gt; - can be used in many different ways to write applications that can run in a safe way, surviving external failure conditions, automatically providing observability and many other features with almost zero boilerplate. Some of these benefits apply even if the application is not really designed with a distributed net of agents in mind, but to use the full potential of Golem we should think about how our application can be modelled like that.&lt;/p&gt;
&lt;p&gt;It is nothing fundamentally new - there are many publications discussing domain driven design, distributed actor systems, reactive messaging patterns and so on. These existing materials can be useful inspiration, but keep in mind that in Golem each agent is durable (persistent) by default - this provides a lot of guarantees out of the box, which would have been solved by explicit architecture in other systems.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Rust agents in Golem 1.4</title>
        <published>2025-12-18T00:00:00+00:00</published>
        <updated>2025-12-18T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/rust-agents-golem14/"/>
        <id>https://blog.vigoo.dev/posts/rust-agents-golem14/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/rust-agents-golem14/">&lt;p&gt;The &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/posts/golem-code-first-agents/&quot;&gt;previous version of Golem, 1.3&lt;/a&gt; made a big leap from earlier versions by introducing &lt;strong&gt;code-first agents&lt;/strong&gt;. We could now write stateful, persistent entities called agents by simply defining TypeScript classes with some annotations, while previously it required learning the WebAssembly interface definition language and using that to first define our application&#39;s public interface, then figure out how to implement that.&lt;/p&gt;
&lt;p&gt;But in Golem 1.3 we only supported this to TypeScript - we dropped support for all languages, as we wanted to only support this new experience; with Golem 1.4, &lt;a rel=&quot;external&quot; href=&quot;https://x.com/GolemCloud/status/2000696015142228039&quot;&gt;launched on 22nd of December&lt;/a&gt; we have a similar new code-first developer experience for &lt;strong&gt;Rust&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;In this post I&#39;m showing how to write a small Golem application with the new Rust SDK.&lt;/p&gt;
&lt;p&gt;Note that even though this post is about Rust agents, everything we are going to see is possible with TypeScript agents as well, in a very similar way (with slightly even less boilerplate).&lt;/p&gt;
&lt;h2 id=&quot;the-example&quot;&gt;The example&lt;/h2&gt;
&lt;p&gt;The application we are going to develop, although not being extremely useful by itself, is small enough to fit to this post, but still contains many interesting details of how writing Rust agents in Golem 1.4 feels.&lt;/p&gt;
&lt;p&gt;Our application is going to be a graph database of &lt;em&gt;libraries&lt;/em&gt; for various &lt;em&gt;programming languages&lt;/em&gt;, organized into &lt;em&gt;topics&lt;/em&gt;. By selecting a topic (let&#39;s say &lt;code&gt;json&lt;/code&gt;) we will be able to discover libraries (Rust and JavaScript libraries) that associated with that topic - then all hits are going to be analyzed and stored. Each analyzed library is going to also add new topics, and these topics can be used to do more search for libraries, and so on.&lt;/p&gt;
&lt;p&gt;For searching libraries for a given topic, we are going to use the Google programmable search API to look for repositories on GitHub. To analyze the hits, we are going to ask OpenAI to give a short description and set of topics based on the project&#39;s README.&lt;/p&gt;
&lt;p&gt;The number of libraries and topics will be arbitrarily scalable, as well as the number of parallel topic discoveries (but limited by the 3rd party provider&#39;s limitations, of course).&lt;/p&gt;
&lt;h2 id=&quot;implementation&quot;&gt;Implementation&lt;/h2&gt;
&lt;p&gt;In general it is a good idea to design the whole architecture of an application like this in advance, and start working on it with a good understanding of what to do. I&#39;m not going to show the overall design at this point, because introducing the parts one by one  will make it easier to explain the concepts and decisions.&lt;/p&gt;
&lt;h3 id=&quot;starting-the-project&quot;&gt;Starting the project&lt;/h3&gt;
&lt;p&gt;The only prerequisites to implement this example are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Golem 1.4 (the &lt;code&gt;golem&lt;/code&gt; CLI application)&lt;/li&gt;
&lt;li&gt;Rust toolchain with the &lt;code&gt;wasm32-wasip1&lt;/code&gt; target installed&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cargo-component 0.21.1&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;See the Rust tab on &lt;a rel=&quot;external&quot; href=&quot;https://learn.golem.cloud/develop/setup&quot;&gt;the official setup page&lt;/a&gt; to learn how exactly set these up.&lt;/p&gt;
&lt;p&gt;Once we have them, we can create a new application using &lt;code&gt;golem&lt;/code&gt; :&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ golem new &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt; Application name: lib-db&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt; Select language for the new component Rust&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt; Select a template for the new component: default: A simple agent implementing a counter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt; Component Package Name: libdb:backend&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt; Add another component or create application? Create application&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Created application directory: lib-db&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Adding component libdb:backend&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Added new app component libdb:backend&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Created application lib-db&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The application is called &lt;code&gt;lib-db&lt;/code&gt;, and it consists of a single component, &lt;code&gt;libdb:backend&lt;/code&gt;. Components are an organizational unit in Golem - an application can have multiple components, and each component can any number of &lt;strong&gt;agents&lt;/strong&gt;. In this example we don&#39;t need to use multiple components. A possible reason could be to have different update/deploy strategies for different subsets of our agents.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;default&lt;/code&gt; template we&#39;ve chosen consists of a simple agent called &lt;code&gt;CounterAgent&lt;/code&gt;, implementing a stateful counter identified by a name, with a single &lt;code&gt;increment&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;We can delete that, and start implementing our own agents. All agents are going to be defined in &lt;code&gt;components-rust/libdb-backend/src&lt;/code&gt; - the module structure can be anything, I prefer putting each agent in its own submodule, and the shared data types (if not many) in the root module.&lt;/p&gt;
&lt;h3 id=&quot;the-library-agent&quot;&gt;The library agent&lt;/h3&gt;
&lt;p&gt;What is an agent in Golem? A stateful, durable entity, identified by its constructor parameters, exposing methods. Agents can run in parallel, but each agent itself executes their invoked methods sequentially. They also scale horizontally as each agent is put on one executor of the whole Golem cluster, based on some internal sharing logic.&lt;/p&gt;
&lt;p&gt;One good candidate for an agent in our example application is a &lt;strong&gt;library&lt;/strong&gt;. A library is an entity identified by its name and programming language, and it holds state - was it already analyzed? If it was, it has some data - description, set of topics.&lt;/p&gt;
&lt;p&gt;As the agent state is not publicly visible, we also need to expose a method to query it.&lt;/p&gt;
&lt;p&gt;Let&#39;s see how this looks like in Rust!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; golem_rust&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt;agent_definition&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; agent_implementation&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; http&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Uri&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;collections&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;HashSet&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_definition&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Library&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;reference&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; LibraryReference&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_details&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;LibraryDetails&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LibraryImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    reference&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; LibraryReference&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    state&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; LibraryState&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;enum&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LibraryState&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    Unknown&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    Analysed&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        repository&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Uri&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        description&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        topics&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; HashSet&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    Failed&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        message&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_implementation&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Library&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; LibraryImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;reference&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; LibraryReference&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            reference&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            state&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; LibraryState&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Unknown&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_details&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;LibraryDetails&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        match&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;state &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;            LibraryState&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Failed&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; message&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;message&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;            LibraryState&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Analysed&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                description&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                topics&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                repository&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;LibraryDetails&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                description&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; description&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;reference&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                language&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;reference&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;language&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                repository&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; repository&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                topics&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; topics&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;iter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;cloned&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;collect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;            LibraryState&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Unknown&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Library not yet analyzed&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And some common data types used in the above snippet:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; golem_rust&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Hash&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; PartialEq&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Eq&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; enum&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Language&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    Rust&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    JavaScript&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Hash&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; PartialEq&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Eq&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LibraryReference&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    language&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Language&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Display&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; LibraryReference&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; fmt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Formatter&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;_&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;fmt&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Result&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        write!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;f&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:?&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;language&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LibraryDetails&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    language&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Language&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    repository&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Uri&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    description&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    topics&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; HashSet&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In these snippets we only have three Golem-specific details:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Data types used anywhere in the agent&#39;s interface must derive &lt;code&gt;Schema&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The agent must be implemented as a pair of a &lt;code&gt;trait&lt;/code&gt; and an implementation, with two macros: &lt;code&gt;agent_definition&lt;/code&gt; and &lt;code&gt;agent_implementation&lt;/code&gt; applied&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That&#39;s all that is required - our application can be built with &lt;code&gt;golem build&lt;/code&gt; and deployed with &lt;code&gt;golem deploy&lt;/code&gt;, assuming we have a locally started Golem server (&lt;code&gt;golem server run&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Let&#39;s do that, and try it out with &lt;code&gt;golem repl&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; let testr = library({ name: &amp;quot;test-r&amp;quot;, language: rust})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; testr.get-details()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;err(&amp;quot;Library not yet analysed&amp;quot;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; let desertrs = library({ name: &amp;quot;desert-rust&amp;quot;, language: rust})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; desertrs.get-details()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;err(&amp;quot;Library not yet analysed&amp;quot;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; let golem-ts-sdk = library({ name: &amp;quot;golem-ts-sdk&amp;quot;, language: java-script})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; golem-ts-sdk.get-details()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;err(&amp;quot;Library not yet analysed&amp;quot;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Of course every agent is initialized with &lt;code&gt;LibraryState::Unknown&lt;/code&gt; so we can&#39;t see anything interesting yet. If we get out of the REPL, and check &lt;code&gt;golem agent list&lt;/code&gt;, we can see that it indeed created three different agents in our server:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Selected app: lib-db, env: local, server: local - builtin (http://localhost:9881)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;+----------------+-----------------------------+-----------+--------+-------------+--------------------------+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| Component name | Agent name                  | Component | Status | Pending     | Created at               |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|                |                             | revision  |        | invocations |                          |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;+----------------+-----------------------------+-----------+--------+-------------+--------------------------+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| libdb:backend  | library({name:&amp;quot;desert-      |         0 |   Idle |           0 | 2025-12-18T14:53:14.531Z |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|                | rust&amp;quot;,language:rust})       |           |        |             |                          |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;+----------------+-----------------------------+-----------+--------+-------------+--------------------------+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| libdb:backend  | log()                       |         0 |   Idle |           0 | 2025-12-18T14:52:50.858Z |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;+----------------+-----------------------------+-----------+--------+-------------+--------------------------+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| libdb:backend  | library({name:&amp;quot;test-        |         0 |   Idle |           0 | 2025-12-18T14:52:50.731Z |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|                | r&amp;quot;,language:rust})          |           |        |             |                          |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;+----------------+-----------------------------+-----------+--------+-------------+--------------------------+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| libdb:backend  | library({name:&amp;quot;golem-ts-    |         0 |   Idle |           0 | 2025-12-18T14:53:38.352Z |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|                | sdk&amp;quot;,language:java-script}) |           |        |             |                          |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;+----------------+-----------------------------+-----------+--------+-------------+--------------------------+&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;One thing to notice - although we defined our data types and method names in the normal convention of Rust - pascal case for the type names, snake case for the method names and fields - when using the REPL and other Golem CLI commands, we have to use a &lt;code&gt;kebab-cased&lt;/code&gt; version of everything. This is a limitation of Golem 1.4 that&#39;s going to be removed in the next version. For now, because of how it builds on WebAssembly components under the hood, we need to accept this, but at least the REPL provides auto-completion to make it easier to discover these transformed names.&lt;/p&gt;
&lt;h3 id=&quot;the-topic-agent&quot;&gt;The topic agent&lt;/h3&gt;
&lt;p&gt;The second entity in our system is going to be a &lt;strong&gt;topic&lt;/strong&gt;. Let&#39;s just define a topic as something identified by a (lowercase) string, and has two methods: one to get the known list of libraries implementing this topic, and another to start &lt;strong&gt;discovering&lt;/strong&gt; more libraries for this topic.&lt;/p&gt;
&lt;p&gt;We want to keep this discovery process on-demand, otherwise we would create an exponentially growing system trying to explore the whole GitHub.&lt;/p&gt;
&lt;p&gt;We can create another submodule and just sketch an initial version of this agent:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_definition&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Topic&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; discover_libraries&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_libraries&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;HashSet&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;LibraryReference&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TopicImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    libraries&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; HashSet&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;LibraryReference&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    failures&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_implementation&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Topic&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TopicImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_lowercase&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; !=&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;            panic!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Topic names must be lowercase&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            name&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            libraries&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; HashSet&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            failures&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; discover_libraries&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        todo!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_libraries&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;HashSet&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;LibraryReference&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;failures&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;is_empty&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;            Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;libraries&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;            Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;failures&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With out two main entities defined, we can finally switch to implement the topic discovery and library analysis!&lt;/p&gt;
&lt;h3 id=&quot;topic-discovery&quot;&gt;Topic discovery&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;discover_libraries&lt;/code&gt; method that we left unimplemented so far need to do some Google search calls to find links to libraries on GitHub, and then for each hit, spawn a &lt;strong&gt;Library agent&lt;/strong&gt; that is going to analyze that library and in case the analysis was successful, register it to belong to our &lt;strong&gt;topic&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;At this point we could write some code in &lt;code&gt;discover_libraries&lt;/code&gt; implementing this - do requests to Google, process the response, loop on paginated results, etc. But this is a slow process, and as I mentioned earlier, &lt;strong&gt;agents are single-threaded and their invocations are sequential&lt;/strong&gt;. If we would do it this way, the topic agent would be unresponsive during the discovery process, we could not even query it&#39;s status (for example invoking  &lt;code&gt;get_libraries&lt;/code&gt; on it would just be enqueued to be executed &lt;em&gt;after&lt;/em&gt; the discovery method returned).&lt;/p&gt;
&lt;p&gt;There are two ways to solve this in Golem - forking and spawning child agents. In this example we are going to define a new agent, &lt;strong&gt;TopicDiscovery&lt;/strong&gt;, which is going to be responsible for the long-running process of searching for libraries, while the Topic agents remain responsible.&lt;/p&gt;
&lt;p&gt;When defining a new agent, we need to think about two things: its identity (constructor parameters) and its methods. In this case we have only one really good choice for the agent identity - we could say that each &lt;strong&gt;topic&lt;/strong&gt; can have maximum 1 &lt;strong&gt;topic discovery&lt;/strong&gt;. To achieve this, we can make the topic discovery agent also be identified by &lt;strong&gt;the topic name&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;If we would choose something with smaller cardinality, for example we would make the &lt;strong&gt;TopicDiscovery&lt;/strong&gt; a singleton with no constructor parameters, then we could not run searches for multiple topics in parallel. If we would just assign a random ID (Golem has a built-in feature for that called &lt;strong&gt;phantom agents&lt;/strong&gt;) then we would be able to run multiple searches for the &lt;em&gt;same topic&lt;/em&gt; in parallel, which also would not make much sense.&lt;/p&gt;
&lt;p&gt;So we are going to have a 1-1 mapping, and use the topic name as our discovery agent&#39;s identity, and we are going to add a &lt;strong&gt;single run method&lt;/strong&gt; to it. This is going to be long-running, but it does not matter because there isn&#39;t anything else to be called on this agent.&lt;/p&gt;
&lt;p&gt;Let&#39;s first define the &quot;skeleton&quot; of this agent without an actual implementation:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_definition&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TopicDiscovery&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TopicDiscoveryImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TopicDiscoveryImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; try_run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; anyhow&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Language&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; SearchResult&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        todo!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; A&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_implementation&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TopicDiscovery&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TopicDiscoveryImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        match&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;try_run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;            Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;results&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;                todo!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;            Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;err&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;                todo!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; C&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are two new things in this snippet we haven&#39;t seen so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;agent methods (and also the constructor) can be &lt;code&gt;async&lt;/code&gt;. Even being single-threaded, and invocations not being able to overlap, within an invocation there are async operations that can overlap, such as RPC calls, HTTP requests, and waiting for external events (using Golem promises).&lt;/li&gt;
&lt;li&gt;we can easily add helper methods to our implementations - everything outside of the &lt;code&gt;agent_implementation&lt;/code&gt; is an implementation detail of that agent&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are three &lt;code&gt;todo!&lt;/code&gt;s in the above implementation, let&#39;s discuss them one by one.&lt;/p&gt;
&lt;h4 id=&quot;searching-the-web-a&quot;&gt;Searching the web (A)&lt;/h4&gt;
&lt;p&gt;We could manually do HTTP requests to use Google&#39;s search APIs (the recommended way is using the &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/wstd/latest/wstd/&quot;&gt;wstd crate&#39;s HTTP client&lt;/a&gt;) but we have a better option. Golem comes with a large number of &lt;strong&gt;connectors&lt;/strong&gt; for 3rd party providers: LLMs, embeddings, text-to-speech, speech-to-text, video generation, code snippet execution, vector databases, searching, etc. Each of these categories define a unified API for working with various third-party providers in that category.&lt;/p&gt;
&lt;p&gt;For implementing &lt;code&gt;try_run&lt;/code&gt;, we are going to use the &lt;code&gt;golem_rust::golem_ai::golem::web_search&lt;/code&gt; module and as a separate step, we are going to choose Google as the selected implementation for it.&lt;/p&gt;
&lt;p&gt;The first step is to enable these connectors in the &lt;code&gt;Cargo.toml&lt;/code&gt; file, as they are disabled in the default template:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;toml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;golem-rust&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; version&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1.10.3&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; features&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;export_golem_agentic&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;golem_ai&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Adding the &lt;code&gt;golem_ai&lt;/code&gt; feature enables access to all the connectors defined in the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/golem-ai&quot;&gt;golem-ai repo&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Then we can use these bindings in our method implementation:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; golem_rust&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;golem_ai&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;web_search&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;types&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;SearchParams&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; SearchResult&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; golem_rust&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;golem_ai&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;web_search&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;web_search&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;start_search&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; LANGUAGES&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Language&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Language&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Rust&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Language&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;JavaScript&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; vec!&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; language&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; LANGUAGES&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; search&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; start_search&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;SearchParams&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        query&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; format!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; library for &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:?&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; language&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        include_domains&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;vec!&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;github.com&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        include_images&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;false&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; everything else is undefined below&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        safe_search&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; language&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        region&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; max_results&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; time_range&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        exclude_domains&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; include_html&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; advanced_answer&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    loop&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; page&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; search&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;next_page&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; page&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;is_empty&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        result&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;extend&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;page&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;into_iter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;language&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; r&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;result&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We perform a separate search query for each language we are interested in, and go through all pages of the results for each.&lt;/p&gt;
&lt;p&gt;Note that the search API (and all the others in &lt;code&gt;golem-ai&lt;/code&gt;) is NOT async. This is a limitation coming from being built on the current version of the WASM component model, and it is going to be lifted in the next Golem release.&lt;/p&gt;
&lt;p&gt;This search interface is not limited to Google search - we have implementations for Brave Search, Google Custom Search, Serper.dev and Tavily AI at the moment. Before deploying our application we need to choose which provider to use, by editing the &lt;code&gt;golem.yaml&lt;/code&gt; file of our component. It comes by default with commented-out sections for all these connectors. To enable Google, we need to add a &lt;code&gt;dependency&lt;/code&gt; and two entries to the &lt;code&gt;env&lt;/code&gt; section:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omponents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ibdb:backend&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;emplates&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ust&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    e&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nv&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      G&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;OOGLE_API_KEY&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{{ GOOGLE_API_KEY }}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      G&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;OOGLE_SEARCH_ENGINE_ID&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{{ GOOGLE_SEARCH_ENGINE_ID }}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ependencies&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ype&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;asm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        u&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;rl&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ttps://github.com/golemcloud/golem-ai/releases/download/v0.4.0-dev.1/golem_web_search_google-dev.wasm&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using the &lt;code&gt;{{ X }}&lt;/code&gt; syntax for the environment variables allow the &lt;code&gt;golem&lt;/code&gt; CLI tool to read them from the environment during deployment, so we don&#39;t accidentally commit our keys in our repo. See the &lt;a rel=&quot;external&quot; href=&quot;https://developers.google.com/custom-search/v1/introduction&quot;&gt;official Google page&lt;/a&gt; to learn how to define an API key and a search engine ID.&lt;/p&gt;
&lt;h4 id=&quot;processing-results-b&quot;&gt;Processing results (B)&lt;/h4&gt;
&lt;p&gt;If the search was successful, we end up having a list of &lt;code&gt;SearchResult&lt;/code&gt; values - these are records defined in the web-search API. In this example we are only going to use the &lt;code&gt;url&lt;/code&gt; field of it, which is the search result URL.&lt;/p&gt;
&lt;p&gt;For each result we are going to spawn a &lt;strong&gt;LibraryAnalysis&lt;/strong&gt; agent. The idea is the same as with &lt;strong&gt;Topic&lt;/strong&gt; vs &lt;strong&gt;TopicDiscovery&lt;/strong&gt; - we want something that runs in the background, not affecting the actual Library, so it can be accessed freely while the analysis runs. Let&#39;s assume we identify a library analysis by the library reference (1-1 mapping between a library and its analysis agent), and we pass additional information, such as the GitHub repository our search revealed, to its &lt;strong&gt;run method&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;language&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; results&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; library_analysis&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; LibraryAnalysisClient&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;LibraryReference&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; extract_github_repo_name&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;result&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;url&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    language&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  library_analysis&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; extract_github_repo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;result&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;url&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;await&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Before talking about the more interesting parts of this snippet, let&#39;s just quickly define what &lt;code&gt;extract_github_repo_name&lt;/code&gt; and &lt;code&gt;extract_github_repo&lt;/code&gt; are. Our search is constrained to only give hits within https://github.com and repository links are having the format &lt;code&gt;https://github.com/&amp;lt;org&amp;gt;/&amp;lt;name&amp;gt;&lt;/code&gt;. These helper functions are just extracting &lt;code&gt;name&lt;/code&gt; and the root repository URL from an arbitrary search result URL (that can point to anywhere within a repo).&lt;/p&gt;
&lt;p&gt;But more importantly, what we see here is &lt;strong&gt;agent-to-agent communication&lt;/strong&gt;!&lt;/p&gt;
&lt;p&gt;Every agent we define with the &lt;code&gt;#[agent_definition]&lt;/code&gt; macro automatically creates a &lt;strong&gt;client&lt;/strong&gt; type - if the agent name is &lt;code&gt;LibraryAnalysis&lt;/code&gt;, the client is a type called &lt;code&gt;LibraryAnalyisClient&lt;/code&gt;. Each such agent client has a &lt;code&gt;get&lt;/code&gt; method, with exactly the same parameters as the agent&#39;s constructor. The semantics of this get method is &quot;upsert&quot; - as the constructor parameters are the identity of an agent, calling this method either returns a reference to an existing agent with the given identity, or to a new one if it had not existed before. This explains why is it called &lt;code&gt;get&lt;/code&gt; and not &lt;code&gt;new&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;There are two more client constructor methods in each client (&lt;code&gt;new_phantom&lt;/code&gt; and &lt;code&gt;get_phantom&lt;/code&gt;) but we don&#39;t need them for this example.&lt;/p&gt;
&lt;p&gt;The clients returned by &lt;code&gt;get&lt;/code&gt; have an &lt;strong&gt;async method&lt;/strong&gt; for each &lt;strong&gt;agent method&lt;/strong&gt; the agent exports, with the same parameters as in the original definition. No matter if the agent method was async or not, the method on the client is always &lt;code&gt;async&lt;/code&gt; - as it represents an async remote call awaiting the method&#39;s result.&lt;/p&gt;
&lt;p&gt;So, &lt;code&gt;.await&lt;/code&gt;-ing &lt;code&gt;run&lt;/code&gt; in the loop means we do the analysis one by one. I did that to reduce the load on my OpenAI account which the analysis is using. We could also trigger an analysis for all libraries together, or in batches - these are standard Rust futures, so we could use crates like &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/futures-concurrency/latest/futures_concurrency/index.html&quot;&gt;futures_concurrency&lt;/a&gt; to manage them.&lt;/p&gt;
&lt;h4 id=&quot;search-failures-c&quot;&gt;Search failures (C)&lt;/h4&gt;
&lt;p&gt;If the search failed, we want to report this back to the &lt;strong&gt;Topic&lt;/strong&gt; because that&#39;s the &quot;user-facing&quot; representation of our topic. So let&#39;s add a new agent method to the topic agent (both its trait and its impl):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; record_failure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; failure&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;    self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;failures&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;push&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;failure&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and then use &lt;strong&gt;agent-to-agent communication&lt;/strong&gt; again to call this from our &lt;code&gt;Err&lt;/code&gt; branch in the topic discovery implementation:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; topic&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TopicClient&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;topic&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;record_failure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;err&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;await&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;logging&quot;&gt;Logging&lt;/h3&gt;
&lt;p&gt;Before implementing &lt;strong&gt;LibraryAnalysis&lt;/strong&gt;, let&#39;s take a look at logging. Now that we can spawn multiple parallel web searches running in background agents, if we would start playing with out application (which we can&#39;t at the moment, without defining the library analysis agent first), it would be very hard to observe what is happening on each agent.&lt;/p&gt;
&lt;p&gt;In Golem each agent can emit log events - writing to the standard output is a log event, but in Rust we can also use the &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/log/0.4.29/log/&quot;&gt;log crate&lt;/a&gt; to emit log events in different log levels. This log stream is &lt;strong&gt;per agent&lt;/strong&gt;. We can observe it by using for example the &lt;code&gt;golem agent stream&lt;/code&gt; command:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;golem agent stream &amp;#39;library({ name: &amp;quot;test-r&amp;quot;, language: rust})&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Invocations from the Golem REPL are also automatically streaming the log events back to the REPL. We haven&#39;t seen that before in this example because we did not log anything.&lt;/p&gt;
&lt;p&gt;Golem does not have any built-in support for observing logs from a tree of agents currently. So if we want to see - after asking our application to discover a topic - logs from our topic discovery agents, and then from each library analysis agent that we spawned, we are in trouble.&lt;/p&gt;
&lt;p&gt;But we can simply solve this by building our &lt;strong&gt;own log aggregator&lt;/strong&gt; in Golem itself! As we&#39;ve seen, it&#39;s very easy to call an agent from another agent. We can define a &lt;strong&gt;log agent&lt;/strong&gt; that receives messages, and then emits them as its own log events that we can stream with Golem&#39;s CLI.&lt;/p&gt;
&lt;p&gt;But if we would only have what we have seen so far, this would have a terrible effect on our application. The &lt;strong&gt;log agent&lt;/strong&gt; would be a single instance processing log messages one by one, and every remote log method call would block until it processed the message.&lt;/p&gt;
&lt;p&gt;Fortunately the &lt;strong&gt;agent clients&lt;/strong&gt; have another variant of each agent method on their interface - they can &lt;strong&gt;trigger&lt;/strong&gt; an agent method invocation in a non-blocking way. This is very fast and returns immediately (as soon as the invocation is enqueued in the remote agent). It is also very safe - no message is going to be lost. Golem guarantees exactly-once calling semantics between agents, and the log agent itself is also automatically durable.&lt;/p&gt;
&lt;p&gt;(With a very large number of agents, or large log entries of course having a single log agent can be a bottleneck - it may not be able to process the messages fast enough; we are not going to solve this problem in this post)&lt;/p&gt;
&lt;p&gt;Let&#39;s define our log agent!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; golem_rust&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;bindings&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;wasi&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;logging&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;logging&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt;log&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Level&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_definition&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Log&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; level&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Level&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; sender&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; message&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; LogImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_implementation&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Log&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; LogImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; level&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Level&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; sender&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; message&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;level&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;sender&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;message&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is a very simple agent. It has &lt;strong&gt;no constructor parameters&lt;/strong&gt;, which means it is a &lt;strong&gt;cluster-level singleton&lt;/strong&gt;. It has a single method, that just delegates the call to the low-level &lt;code&gt;log&lt;/code&gt; function defined in the Golem Rust SDK.&lt;/p&gt;
&lt;p&gt;To make this agent nice to use, we define a helper struct called &lt;code&gt;Logger&lt;/code&gt;, which we can use in our other agents to conveniently log messages.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Logger&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    client&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; LogClient&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    sender&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Logger&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;sender&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            client&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; LogClient&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            sender&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; sender&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; info&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; message&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AsRef&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;      log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Level&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Info&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;sender&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; message&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;as_ref&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;      self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;trigger_log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;        Level&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Info&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;sender&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        message&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;as_ref&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We create the remote client in the constructor, and then expose methods for each log level. In these methods we first emit the log message in our &quot;own&quot; agent&#39;s log stream, and then also enqueue the log message in the singleton log agent&#39;s message queue.&lt;/p&gt;
&lt;p&gt;We do this by calling &lt;strong&gt;trigger_log&lt;/strong&gt; on the client, instead of &lt;strong&gt;log&lt;/strong&gt; - this is the non-blocking method to trigger an invocation without awaiting its execution.&lt;/p&gt;
&lt;p&gt;With this set up, we can add a &lt;code&gt;Logger&lt;/code&gt; to our other agents, for example:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TopicDiscoveryImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    logger&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Logger&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and then use it to log messages:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; language&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; LANGUAGES&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;logger&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;debug&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;format!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Searching for libraries in &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;language:?&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When running our application we can observe all logs by running&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;golem agent stream &amp;#39;log()&amp;#39; --logs-only&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;library-analysis&quot;&gt;Library analysis&lt;/h3&gt;
&lt;p&gt;We already seen how our &lt;strong&gt;library analysis agent&lt;/strong&gt; will look like:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_definition&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LibraryAnalysis&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;reference&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; LibraryReference&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; parent_topic&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; repo_uri&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Uri&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The agent&#39;s identity is the same as the library agent&#39;s - there is a 1-1 mapping between them. The only agent method is the long-running &lt;code&gt;run&lt;/code&gt; method, that gets some details (which topic initiated the analysis, and what is the GitHub repo URL).&lt;/p&gt;
&lt;p&gt;As mentioned earlier, we also have an LLM library with implementations for various providers: Anthropic, OpenAI, OpenRouter, Amazon Bedrock, Grok and Ollama. It works the same way as I explained with the web search - we use the library through a module of &lt;code&gt;golem_rust&lt;/code&gt;, and then configure the provider and its API keys in &lt;code&gt;golem.yaml&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The library analysis itself won&#39;t be very sophisticated - just serving example purposes. We are going to ask an LLM to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Read the repository&#39;s front page&lt;/li&gt;
&lt;li&gt;Check if it&#39;s a library of the programming language we believe it is&lt;/li&gt;
&lt;li&gt;If yes, write a short summary of what the library does&lt;/li&gt;
&lt;li&gt;Also collect a set of tags (or topics) representing what the library implements&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We ask it to return this in a structured (JSON) format. If it does not, or anything else fails, we mark the library analysis as failed.&lt;/p&gt;
&lt;p&gt;In either way, at the end we will call something in the corresponding &lt;strong&gt;LibraryAgent&lt;/strong&gt; to store the analysis results.&lt;/p&gt;
&lt;p&gt;So first let&#39;s extend &lt;strong&gt;LibraryAgent&lt;/strong&gt; with two new methods to store the results:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_definition&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Library&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; analysis_failed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; message&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; analysis_succeeded&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      repository&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Uri&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      description&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      topics&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;analysis_failed&lt;/code&gt; implementation just changes the state and logs a message:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; analysis_failed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; message&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;    self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;logger&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;error&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;format!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Library analysis failed: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;    self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;state &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; LibraryState&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Failed&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; message&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;analysis_succeeded&lt;/code&gt; also registers the library into the &lt;strong&gt;topics&lt;/strong&gt; the LLM identified it belongs to! This way we continuously build our information graph. To register a library to a topic, we can add a simple &lt;code&gt;add&lt;/code&gt; method to the &lt;strong&gt;TopicAgent&lt;/strong&gt; and then trigger the invocation (to not introduce any slowdowns here):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; analysis_succeeded&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    repository&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Uri&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    description&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    topics&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;    self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;logger&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;info&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;format!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Library analysis based on &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;repository&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; succeeded with description: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;description&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; and topics: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;topics:?&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    for&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; topic&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; in&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;topics&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; topic&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TopicClient&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;topic&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        topic&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;trigger_add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;reference&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;    self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;state &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; LibraryState&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Analysed&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        repository&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        description&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        topics&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; topics&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;into_iter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;collect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this being ready, let&#39;s go back to our &lt;strong&gt;LibraryAnalysis&lt;/strong&gt; agent&#39;s &lt;code&gt;run&lt;/code&gt; method!&lt;/p&gt;
&lt;p&gt;We start by using Golem&#39;s LLM connector to ask a question:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; response&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; send&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Event&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Message&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    Message&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        role&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Role&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;User&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        content&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; vec!&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;            ContentPart&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Text&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;format!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Let&amp;#39;s analyse the GitHub repository at &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;. First check if this is a library for &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:?&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;. If it is, then come up with a list of tags describing what this library is for, and return it as a JSON array of strings. If it is not for the given language, return an empty tag array.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; repo_uri&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;reference&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;language&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;            ContentPart&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Text&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;In addition to the array of tags, also return a short description of the library in a separate field of the result JSON object.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;            ContentPart&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Text&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Always response with a JSON object with the following structure: { &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;description&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;short description of the library&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;tags&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;: [&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;tag1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;tag2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;, ...] }&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Config&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        model&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;gpt-3.5-turbo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        temperature&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        max_tokens&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        stop_sequences&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        tools&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        tool_choice&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        provider_options&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; library&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; LibraryClient&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;reference&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;match&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; response&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The response is either &lt;code&gt;Ok&lt;/code&gt; or &lt;code&gt;Err&lt;/code&gt;. If it was successful, it just contains a list of &lt;code&gt;ContentPart&lt;/code&gt;s. We just naively try to concatenate those and decode as our expected JSON:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt; serde&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Deserialize&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExpectedLlmResponse&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    description&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    tags&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; raw_string_content&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; response&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span&gt;content&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;iter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; match&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;        ContentPart&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Text&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;s&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; s&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        _&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;collect&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;join&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;logger&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;debug&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;format!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;LLM response: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;raw_string_content&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;match&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; serde_json&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;from_str&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;ExpectedLlmResponse&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;raw_string_content&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If the &lt;code&gt;tags&lt;/code&gt; in the response is empty, or anything else fails, we call &lt;code&gt;analysis_failed&lt;/code&gt; through &lt;code&gt;library&lt;/code&gt;, otherwise we call &lt;code&gt;analysis_succeeded&lt;/code&gt; with the information gathered from the LLM&#39;s response.&lt;/p&gt;
&lt;p&gt;At this point we can build and deploy our application, and start playing with it:&lt;/p&gt;
&lt;img src=&quot;/images/libdb-golem-rust-1.webp&quot; alt=&quot;REPL session discovering Rust libraries in the mp3 topic and querying the puremp3 decoder details&quot; width=&quot;709&quot; height=&quot;726&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;and the log stream:&lt;/p&gt;
&lt;img src=&quot;/images/libdb-golem-rust-2.webp&quot; alt=&quot;Agent logs registering discovered Rust libraries and topics in the catalog&quot; width=&quot;955&quot; height=&quot;553&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;h3 id=&quot;catalog-agent&quot;&gt;Catalog agent&lt;/h3&gt;
&lt;p&gt;The application we created so far spawns many top-level agents automatically - discovering one topic can create a lot of new topic agents, all ready to further investigate by calling &lt;code&gt;discover-libraries&lt;/code&gt; on them.&lt;/p&gt;
&lt;p&gt;To see what topics and libraries we&#39;ve discovered so far, we can use the &lt;code&gt;golem agent list&lt;/code&gt; command - but that&#39;s just a debug tool, not suitable for using as part of our application&#39;s API. If we want to for example build a UI on top of this app, we would need a way to enumerate all the topics we currently know about.&lt;/p&gt;
&lt;p&gt;This can be very easily done by introducing a new &lt;strong&gt;singleton agent&lt;/strong&gt; to just keep a catalog of all the topics and libraries in its memory. This, however, will become a bottleneck if we want to scale this application significantly. There are solutions to that, for example we could define a sharded multi-agent catalog. In this post, however, we are going to do the simple version and just define it as a singleton agent with two lists:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_definition&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Catalog&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_libraries&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;LibraryReference&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_topics&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; register_library&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; library&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; LibraryReference&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; register_topic&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; topic&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The implementation is straightforward - just store the libraries and topics in two &lt;code&gt;Vec&lt;/code&gt;s in the agent&#39;s internal state. We can call &lt;code&gt;register_library&lt;/code&gt; from the &lt;code&gt;analysis_succeeded&lt;/code&gt; method of &lt;code&gt;Library&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; catalog&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CatalogClient&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;catalog&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;trigger_register_library&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;reference&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And similarly, &lt;code&gt;register_topic&lt;/code&gt; from the &lt;strong&gt;constructor&lt;/strong&gt; of &lt;code&gt;Topic&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; catalog&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CatalogClient&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;catalog&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;trigger_register_topic&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;public-api&quot;&gt;Public API&lt;/h2&gt;
&lt;p&gt;At this point we are mostly done with our application&#39;s implementation, but we can only interact with it through debug tools like the Golem REPL. We could also use Golem&#39;s &lt;a rel=&quot;external&quot; href=&quot;https://learn.golem.cloud/rest-api/worker&quot;&gt;REST API to invoke agents&lt;/a&gt; but that&#39;s not a very nice way for integration our application to for example a user interface.&lt;/p&gt;
&lt;p&gt;Fortunately Golem supports &lt;strong&gt;defining custom APIs&lt;/strong&gt; for applications. In Golem 1.4, this has to be done in the application manifest - defining routes in the &lt;code&gt;golem.yaml&lt;/code&gt;, and mapping logic in a custom scripting language called &lt;a rel=&quot;external&quot; href=&quot;https://learn.golem.cloud/rib&quot;&gt;Rib&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This is something that is going to be changing in the next release (a few months from now), and we are going to be able to define these APIs fully from code, in our chosen programming language. Until then, let&#39;s see how the current method looks like!&lt;/p&gt;
&lt;p&gt;In our component&#39;s &lt;code&gt;golem.yaml&lt;/code&gt; file, there is a &lt;code&gt;httpApi&lt;/code&gt; section:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ttpApi&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;efinitions&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ibdb-backend-api&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      v&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ersion&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;0.0.1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      r&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;outes&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        #&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we can list endpoints, and for each endpoint include a script that can access information from the request, &lt;strong&gt;call an agent&lt;/strong&gt; and use the agent&#39;s results to construct HTTP response.&lt;/p&gt;
&lt;p&gt;A simple one can be an endpoint that lists &lt;em&gt;all the libraries&lt;/em&gt; by invoking the &lt;strong&gt;Catalog&lt;/strong&gt; agent:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ethod&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; G&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ET&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;libdb-backend-api/libs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          b&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;inding&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ype&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;efault&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omponentName&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ibdb:backend&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            r&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;esponse&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;              let agent = catalog();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;              let libs = agent.get-libraries();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;              { status: 200, body: { libraries: libs } }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The language used in these scripts is the same that we were using in the Golem REPL.&lt;/p&gt;
&lt;p&gt;For more advanced cases, we may need to use pattern matching in the scripts. For example to get &lt;em&gt;the libraries belonging to a topic&lt;/em&gt;, our agent method returns a Rust &lt;code&gt;Result&lt;/code&gt; which we have to process in the script:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		-&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ethod&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; G&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ET&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;libdb-backend-api/topics/{name}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          b&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;inding&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ype&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;efault&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omponentName&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ibdb:backend&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            r&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;esponse&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;              let name: string = request.path.name;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;              let agent = topic(name);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;              let res = agent.get-libraries();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;              match res {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;                ok(libraries) =&amp;gt; { status: 200, body: { libraries: some(libraries), failures: none } },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;                err(failures) =&amp;gt; { status: 500, body: { libraries: none, failures: some(failures) } }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;              }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;One important trick here is that the branches of the &lt;code&gt;match&lt;/code&gt; must evaluate to the same type. So we can&#39;t just use &lt;code&gt;body : { libraries: libraries }&lt;/code&gt; in one branch and &lt;code&gt;body: { failures: failures }&lt;/code&gt; in the other, as Rib cannot unify those two body types.&lt;/p&gt;
&lt;p&gt;We can add more endpoints to get details of a library or trigger discovery of a topic, etc. Once we&#39;ve done with all that, simply running &lt;code&gt;golem deploy&lt;/code&gt; again will make these endpoints available on the chosen deployment. For locally running Golem server, that&#39;s by default is &lt;code&gt;http://lib-db.localhost:9006&lt;/code&gt; for this example.&lt;/p&gt;
&lt;p&gt;Deployments are also configurable in the application manifest, and there can be different environments such as local, prod, etc with different properties.&lt;/p&gt;
&lt;p&gt;Once the API is deployed we can try it out with &lt;code&gt;curl&lt;/code&gt; for example:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ curl -X GET http://lib-db.localhost:9006/libdb-backend-api/topics&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&amp;quot;status&amp;quot;:200,&amp;quot;topics&amp;quot;:[&amp;quot;mp3&amp;quot;]}%&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;frontend&quot;&gt;Frontend&lt;/h2&gt;
&lt;h3 id=&quot;writing-the-frontend&quot;&gt;&quot;Writing&quot; the frontend&lt;/h3&gt;
&lt;p&gt;Now that we have a public REST API for our application, we can build a simple web application on top of it, and host it from our Golem application itself. As the frontend itself is not the focus of this post, we are going to generate it with AI and just see how we can integrate it within our application.&lt;/p&gt;
&lt;p&gt;The first step we can do is to export an &lt;strong&gt;OpenAPI definition&lt;/strong&gt; for our API, hoping that our AI tools will understand it better than Golem&#39;s own API definition language. Running the following command:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ golem api definition open-api libdb-backend-api&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Selected app: lib-db, env: local, server: local - builtin (http://localhost:9881)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Exported OpenAPI spec for libdb-backend-api to libdb-backend-api.yaml&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we can ask our favorite coding agent to use this to build a frontend for us. I asked for a single HTML page with embedded scripts, with no dependencies or build steps necessary, for simplicity: &lt;a rel=&quot;external&quot; href=&quot;https://ampcode.com/threads/T-019b313f-191c-7444-80ca-d8f46ba7fdee&quot;&gt;see the amp thread&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;With this we have an &lt;code&gt;index.html&lt;/code&gt;, and we want to host that as part of our application.&lt;/p&gt;
&lt;h3 id=&quot;hosting-the-frontend&quot;&gt;Hosting the frontend&lt;/h3&gt;
&lt;p&gt;One thing we can do is to modify the &lt;code&gt;golem.yaml&lt;/code&gt; again, and list files to be added to our &lt;strong&gt;agent&#39;s file system&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omponents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ibdb:backend&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    f&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;iles&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ourcePath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ndex.html&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;argetPath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;index.html&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ermissions&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ead-only&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With Rust, however, it is much easier to include a single HTML file by using &lt;code&gt;include_bytes!&lt;/code&gt; macro. This is compile time, so we don&#39;t need to add any files to our agent&#39;s run-time file system.&lt;/p&gt;
&lt;p&gt;We can define a new agent with the only purpose to be able to return this file:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_definition&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;ephemeral&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Frontend&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; index&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;u8&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; FrontendImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;agent_implementation&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Frontend&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; FrontendImpl&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; index&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;u8&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; bytes&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; include_bytes!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;../index.html&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        bytes&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_vec&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This agent is &lt;strong&gt;singleton&lt;/strong&gt; - there is only one way to return this &lt;code&gt;index.html&lt;/code&gt;, we don&#39;t need multiple agents with different parameters to do so. On the other hand we already learned that agents are executing a single request at the same time, so if we would serve our &lt;code&gt;index.html&lt;/code&gt; through a single agent instance, that would be a significant performance problem.&lt;/p&gt;
&lt;p&gt;The solution for these cases in Golem is to mark the agent as &lt;strong&gt;ephemeral&lt;/strong&gt;. In Rust we can do it in the parameter of the &lt;code&gt;agent_definition&lt;/code&gt; macro, as shown above. Ephemeral agents are different from the default, &lt;strong&gt;durable agents&lt;/strong&gt; in the following ways:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;They are faster and cheaper - their state is not persisted (but to some extent their history is still preserved in long-term storage)&lt;/li&gt;
&lt;li&gt;They are not durable - every invocation is starting from a fresh state, and cannot survive failures&lt;/li&gt;
&lt;li&gt;Invoking an ephemeral agent from an API definition (or the REPL, etc) &lt;strong&gt;creates a separate agent every time&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This last feature allows us to serve an arbitrary number of &lt;code&gt;index.html&lt;/code&gt; requests simultaneously, even though our agent looks like a singleton as there is no constructor parameter to distinguish these parallel instances. Golem has a built-in feature called &lt;strong&gt;phantom-id&lt;/strong&gt; that is appended to the identity of these agents in this case.&lt;/p&gt;
&lt;h3 id=&quot;endpoint-for-index-html&quot;&gt;Endpoint for index.html&lt;/h3&gt;
&lt;p&gt;With this new &lt;strong&gt;Frontend&lt;/strong&gt; agent we can add a new endpoint to our routes:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ethod&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; G&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ET&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;libdb-backend-api&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          b&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;inding&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ype&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;efault&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omponentName&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;libdb:backend&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            r&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;esponse&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;              let agent = frontend();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;              let file = agent.index();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;              {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;                headers: { &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;                  Content-Type: &amp;quot;text/html; charset=utf-8&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;                },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;                body: file&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;              }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&#39;s deploy this and try out in the browser! The page gets downloaded, but it does not work - failing with CORS errors.&lt;/p&gt;
&lt;h3 id=&quot;cors&quot;&gt;CORS&lt;/h3&gt;
&lt;p&gt;We need to add CORS Preflight endpoints to our route to make the scripts work. In the current version of Golem this is a bit inconvenient, as we need to add them one by one for each endpoint we defined, for example:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ethod&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; O&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;PTIONS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;libdb-backend-api/topics/{name}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          b&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;inding&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ype&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ors-preflight&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once we added all of them and redeployed, our frontend works as expected!&lt;/p&gt;
&lt;img src=&quot;/images/libdb-golem-rust-3.webp&quot; alt=&quot;Library Discovery web UI listing the discovered JavaScript libraries under the javascript topic&quot; width=&quot;1020&quot; height=&quot;736&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I hope this post shows how much more fun it is to write applications for Golem in this new release. The important thing is to think about the problem to be solved as a set of durable agents communicating with each other. You can think of a Golem application as a distributed, persistent actor system, if you are familiar with those concepts. Once the architecture is done, it&#39;s mostly just writing the application logic, without dealing with code generators, new languages (except Rib, for now), or boilerplate to set the network up. Everything is automatically persisted, the agents remain available forever, and by scaling the Golem Cluster your application scales horizontally as well.&lt;/p&gt;
&lt;p&gt;The example is available on &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/golem-example-lib-db&quot;&gt;GitHub&lt;/a&gt;.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.3&#39;s code-first TypeScript agents</title>
        <published>2025-10-04T00:00:00+00:00</published>
        <updated>2025-10-04T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem-code-first-agents/"/>
        <id>https://blog.vigoo.dev/posts/golem-code-first-agents/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem-code-first-agents/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;In the &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/posts/golem-new-js-engine/&quot;&gt;previous post about Golem 1.3&lt;/a&gt; I explained that the new release comes with a new JavaScript engine. This engine fixes some important bugs and allows us to move faster and provide a better JS/TS experience - but it comes with a price. One of the tradeoffs was that building an actual WebAssembly component containing this engine requires the Rust toolchain - we are generating a Rust component that implements the component&#39;s (WIT) interface by delegating the calls to &lt;code&gt;rquickjs&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;But we don&#39;t want TypeScript developers to compile Rust components. Even more importantly, we don&#39;t want them to have to learn about the WebAssembly Component Model&#39;s own interface definition language, WIT, or any other WASM specific tooling; they should just write TypeScript code and Golem should deal with all the underlying complexity.&lt;/p&gt;
&lt;h2 id=&quot;history&quot;&gt;History&lt;/h2&gt;
&lt;p&gt;Before showing how we solved this issue, let&#39;s talk about our previous approach. Up until today Golem embraced the component model - we&#39;ve built it on it from the beginning, and I talked multiple times (on &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/posts/golem-and-the-wasm-component-model/&quot;&gt;LambdaConf&lt;/a&gt; and on &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/posts/golem-powered-by-wasm/&quot;&gt;Wasm I/O&lt;/a&gt;) about how we take advantage of various properties of it. Golem components were just arbitrary WebAssembly components, and Golem directly exposed the component&#39;s exported interface through its invocation and remote procedure call APIs. Although we provided more and more help in &lt;em&gt;building&lt;/em&gt; these components by making our CLI tool aware of WIT interfaces, relationship between components and so on, the idea still was that Golem should just work with any component built by any WebAssembly tooling.&lt;/p&gt;
&lt;p&gt;We take a leap from this with Golem 1.3 and while the underlying technology is still the same, we decided to hide its complexity more from the users, and focus on one (and a few more later) supported language and do it in a way that our users are not exposed to the ever changing and evolving complexity of WebAssembly tooling. The first such supported language is &lt;strong&gt;TypeScript&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-new-way&quot;&gt;The new way&lt;/h2&gt;
&lt;p&gt;Let&#39;s go through the technical details of how we are doing it!&lt;/p&gt;
&lt;p&gt;We can avoid the need to generate and build Rust crates (when using TS) and avoid having to learn about WIT with a simple shift in Golem&#39;s approach to user defined components: we no longer support components with an arbitrary, user-defined WIT interface. There is one specific &lt;code&gt;WIT world&lt;/code&gt; (a set of imports and exports) applied to every Golem component. This world &lt;em&gt;imports&lt;/em&gt; all the supported host APIs of Golem - it&#39;s durability controls, forking, ability to update and query information about agents, etc. It also imports all the supported AI libraries &lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/golem-ai&quot;&gt;of the golem-ai project&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;With this predefined set of imports, we can generate the Rust crate with &lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/wasm-rquickjs/&quot;&gt;wasm-rquickjs&lt;/a&gt; once, build time, and the resulting WASM will contain a QuickJs engine with all the bindings set up to work with Golem. This WASM is then packaged in our TypeScript SDK and published &lt;a rel=&quot;external&quot; href=&quot;https://www.npmjs.com/package/@golemcloud/golem-ts-sdk&quot;&gt;on npmjs.com&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;It&#39;s clear that this way we can provide support for the fixed set of features Golem provides. But we still want our users to be able to define their own interfaces that can be invoked through &lt;a rel=&quot;external&quot; href=&quot;https://learn.golem.cloud/invoke&quot;&gt;Golem&#39;s invocation API&lt;/a&gt;, bound to HTTP routes, or called through RPC from one agent to another. How can we do this while having a static WIT interface which is even hidden from our users?&lt;/p&gt;
&lt;p&gt;The answer is again a tradeoff - we give up some performance and composability coming from the component model to have something much more flexible and extensible.&lt;/p&gt;
&lt;p&gt;The idea is that every Golem component implements the following interface:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;package golem:agent;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;interface guest {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  use common.{agent-error, agent-type, data-value};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  /// Initializes the agent of a given type with the given constructor parameters.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  /// If called a second time, it fails.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  initialize: func(agent-type: string, input: data-value) -&amp;gt; result&amp;lt;_, agent-error&amp;gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  /// Invokes an agent. If create was not called before, it fails&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  invoke: func(method-name: string, input: data-value) -&amp;gt; result&amp;lt;data-value, agent-error&amp;gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  /// Gets the agent type. If create was not called before, it fails&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  get-definition: func() -&amp;gt; agent-type;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  /// Gets the agent types defined by this component&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  discover-agent-types: func() -&amp;gt; result&amp;lt;list&amp;lt;agent-type&amp;gt;, agent-error&amp;gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is quite low level and dynamic, so let&#39;s see what this means:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Every Golem &lt;strong&gt;component&lt;/strong&gt; can implement one or more &lt;strong&gt;agent types&lt;/strong&gt;. The agent types are defined by the &lt;code&gt;agent-type&lt;/code&gt; data type and the component can self-describe the set of agent types it implements, using the &lt;code&gt;discover-agent-types&lt;/code&gt; exported function.&lt;/li&gt;
&lt;li&gt;Every &lt;strong&gt;instance&lt;/strong&gt; of a Golem component (called worker in previous Golem versions) is a single instance of one of the &lt;strong&gt;agent types&lt;/strong&gt; implemented by the component.&lt;/li&gt;
&lt;li&gt;The instance is initialized by the &lt;code&gt;initialize&lt;/code&gt; exported function - this selects the agent type the instance belongs to, and passes &lt;strong&gt;constructor parameters&lt;/strong&gt; (in form of a dynamic value of the &lt;code&gt;data-value&lt;/code&gt; type). The initialize call is always the first call to an agent and it is automatically called by Golem itself.&lt;/li&gt;
&lt;li&gt;Once an agent is initialized, it can tell its own agent-type (with &lt;code&gt;get-definition&lt;/code&gt;), and more importantly it can be &lt;strong&gt;invoked&lt;/strong&gt; dynamically using the exported &lt;code&gt;invoke&lt;/code&gt; function. This is dynamic in a sense that it is a single exported WIT function that takes the invoked &lt;strong&gt;agent method&lt;/strong&gt;&#39;s name as a string, and the parameters as an arbitrary &lt;code&gt;data-value&lt;/code&gt; (just like with the constructor parameters). This is not type safe on the component level - but type safety is guaranteed by Golem on both the invocation side and the SDK side.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To have a better sense of what this interface is capable of, let&#39;s take a look at some parts of the &lt;code&gt;agent-type&lt;/code&gt; and &lt;code&gt;data-value&lt;/code&gt; types.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;agent-type&lt;/code&gt; is all the metadata available about an agent type, including it&#39;s constructor and methods, with full type information. In addition to that it can contain additional metadata to help integration with AI systems for example.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;record agent-type {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  type-name:    string,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  description:  string,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  %constructor: agent-constructor,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  methods:      list&amp;lt;agent-method&amp;gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  dependencies: list&amp;lt;agent-dependency&amp;gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Dependencies are not used at the moment, but it is going to allow us to statically know the dependency graph of agents. Both the constructor and the agent&#39;s methods are using the &lt;code&gt;data-schema&lt;/code&gt; type to describe their input (parameters) and output (return type). The earlier mentioned &lt;code&gt;data-value&lt;/code&gt; type is an instance of a type defined by &lt;code&gt;data-schema&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In Golem 1.3, &lt;code&gt;data-schema&lt;/code&gt; is still tightly coupled with the component model - it supports all the data types supported by the component model, but extends them with some concepts that are more agent specific. It&#39;s defined in the following way:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;variant data-schema {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  /// List of named elements&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  %tuple(list&amp;lt;tuple&amp;lt;string, element-schema&amp;gt;&amp;gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  /// List of named variants that can be used 0 or more times in a multimodal `data-value`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  multimodal(list&amp;lt;tuple&amp;lt;string, element-schema&amp;gt;&amp;gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;variant element-schema {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  component-model(wit-type),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  unstructured-text(text-descriptor),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  unstructured-binary(binary-descriptor),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Without going into much detail, an input or output of an agent method can be either a tuple of elements, or multimodal. The tuple case is the traditional case - for example a method with three parameters would have a schema describing a 3-tuple. Multimodal, on the other hand, can be thought of as a list of variant values, where each element of the multimodal schema can appear any number of times. An actual example for such an interface can be an chat agent that accepts (and/or returns) content in multiple media formats, such as text, audio, or image.&lt;/p&gt;
&lt;p&gt;An element of these tuple or multimodal schemas can be one of the WebAssembly component model types (&lt;code&gt;wit-type&lt;/code&gt;), an unstructured text (possibly annotated with a language code) or unstructured binary (annotated with a MIME type) data.&lt;/p&gt;
&lt;h2 id=&quot;code-first-sdk&quot;&gt;Code-first SDK&lt;/h2&gt;
&lt;p&gt;The above defined interface explains how we can define and implement multiple agent types without writing any WIT definitions, but using it directly would be very inconvenient.&lt;/p&gt;
&lt;p&gt;In TypeScript, when using &lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/wasm-rquickjs/&quot;&gt;wasm-rquickjs&lt;/a&gt;, the implementation would require writing the following functions:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; namespace&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-10&quot;&gt; guest&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    /**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;     * Initializes the agent of a given type with the given constructor parameters.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;     * If called a second time, it fails.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;     * &lt;/span&gt;&lt;span class=&quot; z-l-i z-d-7 z-d-i&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-7 z-d-i&quot;&gt;throws&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; AgentError&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;     */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; function&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; initialize&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;agentType&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; input&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DataValue&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;void&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    /**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;     * Invokes an agent. If create was not called before, it fails&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;     * &lt;/span&gt;&lt;span class=&quot; z-l-i z-d-7 z-d-i&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-7 z-d-i&quot;&gt;throws&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; AgentError&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;     */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; function&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; invoke&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;methodName&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; input&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DataValue&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DataValue&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    /**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;     * Gets the agent type. If create was not called before, it fails&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;     */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; function&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; getDefinition&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AgentType&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    /**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;     * Gets the agent types defined by this component&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;     * &lt;/span&gt;&lt;span class=&quot; z-l-i z-d-7 z-d-i&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-7 z-d-i&quot;&gt;throws&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; AgentError&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;     */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; function&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; discoverAgentTypes&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AgentType&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It&#39;s inconvenient to write these by hand, but it&#39;s just TypeScript code - we can just write a library on top of it that makes it more user friendly!&lt;/p&gt;
&lt;h3 id=&quot;a-pure-typescript-approach&quot;&gt;A pure TypeScript approach&lt;/h3&gt;
&lt;p&gt;One possibility would be to write a TypeScript library that exports functions to define the data schemas and agent type metadata, then connect an implementation to each. This can be made very type safe using advanced type level techniques - for example defining the schema would not only assemble a &lt;code&gt;DataSchema&lt;/code&gt; value, but would also track the value type (such as a tuple of the agent method parameters) on the type system level. Then when attaching an actual implementation to the defined method, the compiler can infer that the parameters are having these types.&lt;/p&gt;
&lt;p&gt;The SDK would expose some kind of global registry to define these well typed agents in, possibly using a builder-like fluent API. In the end it just implements the above WIT exports itself using the registered agent definitions.&lt;/p&gt;
&lt;p&gt;Parts of this would be very similar to how some TypeScript libraries define schemas for validation. It is important to mention though that with Golem validating the input is not necessary - the runtime guarantees that the agent constructor and agent methods are only called values matching the types from the agent type metadata.&lt;/p&gt;
&lt;p&gt;With a library like this, you could define the simplest possible stateful Golem agent, a counter, in a way like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;defineAgentType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  typeName&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  description&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;An example Golem agent implementing a counter&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  id&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; type_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  state&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;_id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  methods&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    agentMethod&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; type_u32&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;implement&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  initialize&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;    console&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Counter &lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; created&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  increment&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;state&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;    state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; +=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here the agent&#39;s identity (constructor parameters), state and agent methods would be defined in terms of typed versions of &lt;code&gt;DataSchema&lt;/code&gt;, with schema constructors such as &lt;code&gt;type_string()&lt;/code&gt;. Then in the &lt;code&gt;implement&lt;/code&gt; call the object passed would require implementations for the constructor and the methods using the data schema to infer their parameter and return types.&lt;/p&gt;
&lt;p&gt;Note that this is just a sketch - we decided to &lt;em&gt;not&lt;/em&gt; implement a library like this.&lt;/p&gt;
&lt;h3 id=&quot;golem-s-typescript-sdk&quot;&gt;Golem&#39;s TypeScript SDK&lt;/h3&gt;
&lt;p&gt;The approach we chose is to take advantage of &lt;strong&gt;decorators&lt;/strong&gt; and the &lt;a rel=&quot;external&quot; href=&quot;https://ts-morph.com&quot;&gt;ts-morph library&lt;/a&gt; to make writing the agents even more convenient. The primary advantage is &lt;em&gt;not&lt;/em&gt; having to specify data schemas at all. When compiling the TypeScript code the types are extracted and made available for the SDK in runtime - it can transform the TypeScript AST to the matching &lt;code&gt;DataSchema&lt;/code&gt; values and &lt;code&gt;AgentType&lt;/code&gt; definitions, or fail in a user friendly way if something in the user&#39;s code is not supported.&lt;/p&gt;
&lt;p&gt;Before looking into the details, see how the same &lt;em&gt;counter&lt;/em&gt; example looks like with the actual Golem TS SDK!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    BaseAgent&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    agent&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    description&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; from&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;@golemcloud/golem-ts-sdk&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;agent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BaseAgent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; readonly&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; name&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; number&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    constructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10 z-d-i&quot;&gt;        super&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; name&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    @&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;description&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Increases the count by one and returns the new value&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; increment&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;number&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; +=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Every class annotated with &lt;code&gt;@agent&lt;/code&gt; becomes an &lt;strong&gt;exported agent type&lt;/strong&gt;. There are not many restrictions - they have to extend &lt;code&gt;BaseAgent&lt;/code&gt;, and every type used in the constructor and the methods of this class must be something that the SDK can express with a &lt;code&gt;DataSchema&lt;/code&gt;. But it is fully automatic - we can define and use complex custom data types in the agent&#39;s interface without having to manually write any schema for them.&lt;/p&gt;
&lt;p&gt;A component can define as many agent types (classes decorated as &lt;code&gt;@agent()&lt;/code&gt; as necessary). The only reason to have multiple &lt;em&gt;components&lt;/em&gt; in an application is to have different update policies or other configuration for them.&lt;/p&gt;
&lt;p&gt;In addition to automatically converting these annotated classes into agent type definitions and their implementations, the SDK also provides support for &lt;strong&gt;remote agent calls&lt;/strong&gt;. Every agent class gets a static method on it (put there by the decorator) called &lt;code&gt;get&lt;/code&gt;. The get method has &quot;get-or-create&quot; semantics. Every agent is identified by their constructor parameters. There can be only one instance with a specific constructor parameter value. The get-or-create semantics of the &lt;code&gt;get&lt;/code&gt; method guarantees that this is true (it creates a new agent if it did not exist yet, otherwise returns a reference to the existing one).&lt;/p&gt;
&lt;p&gt;In the above example our counter has a string identifier called &lt;code&gt;name&lt;/code&gt;. As I wrote earlier, in Golem every component instance corresponds to a single agent. This means that referring to and calling any other agent (either the same type, or another type) ends up being an &quot;agent-to-agent&quot; remote procedure call under the hood.&lt;/p&gt;
&lt;p&gt;With the SDK this is very convenient - we can use the &lt;code&gt;get&lt;/code&gt; method to get a remote agent reference by just passing the constructor parameters to it, then call any of the agent&#39;s methods directory on the agent reference:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;typescript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; anotherCounter&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; CounterAgent&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;not-my-name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; newValue&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; anotherCounter&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;increment&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What happens under the hood is that when compiling the TypeScript agent (using &lt;code&gt;golem-cli app build&lt;/code&gt;), first a pre-compilation step called &lt;code&gt;golem-typegen&lt;/code&gt; analyses the source code and emits a JSON describing the agent classes and their method parameter and return types. This step uses &lt;a rel=&quot;external&quot; href=&quot;https://ts-morph.com&quot;&gt;ts-morph&lt;/a&gt;, which is a wrapper of the TypeScript compiler API, to get the AST of the user code and extract the necessary information by traversing that.&lt;/p&gt;
&lt;p&gt;This generated JSON gets bundled into the final JS code and it&#39;s used by the decorator logic to implement the &lt;code&gt;discoverAgentTypes&lt;/code&gt;, &lt;code&gt;initialize&lt;/code&gt; and &lt;code&gt;invoke&lt;/code&gt; methods.&lt;/p&gt;
&lt;h3 id=&quot;future&quot;&gt;Future&lt;/h3&gt;
&lt;p&gt;Note that this approach of having the low-level agent interface, and building SDKs on top means that even though we chose a specific approach we support as the official way of writing TypeScript agents for Golem, it is easy to experiment with alternative techniques and publish alternative SDKs. Also the official can be extended with different styles of agent definitions, if we decide to do so.&lt;/p&gt;
&lt;p&gt;It is also possible to experiment with supporting other languages. The next Golem release after 1.3 will bring back support for using Rust. With Rust we are getting the same code-first agent SDK as with TypeScript, only instead of ts-morph generated ASTs and decorators it is going to be built on proc macros and type classes.&lt;/p&gt;
&lt;h2 id=&quot;composition&quot;&gt;Composition&lt;/h2&gt;
&lt;p&gt;There are two additional interesting build steps hidden behind the scenes that we haven&#39;t talked about yet. Both are implemented using WebAssembly &lt;strong&gt;component composition&lt;/strong&gt; - something that we no longer expose to our users (to avoid having to fall back to WASM tooling and hand-written WIT specs) but we still use it under the hood.&lt;/p&gt;
&lt;h3 id=&quot;base-wasm-and-user-js&quot;&gt;Base WASM and user JS&lt;/h3&gt;
&lt;p&gt;The result of the compilation steps described above - &lt;code&gt;golem-typegen&lt;/code&gt; and then compiling the TypeScript code itself - results in a single JS file. On the other hand we want to have a Golem component - which is a WASM component. I explained above that by restricting a Golem component to always implement a specific world, we can precompile the JavaScript engine with all the import and export bindings. This precompiled WASM is part of the &lt;code&gt;golem-ts-sdk&lt;/code&gt; NPM package. We still have to somehow inject the compiled JavaScript file into this component!&lt;/p&gt;
&lt;p&gt;What we do is the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The precompiled WASM &lt;strong&gt;imports&lt;/strong&gt; a specific WIT interface with a single method that returns a JS string&lt;/li&gt;
&lt;li&gt;We generate &lt;em&gt;another WASM component&lt;/em&gt; bundling the compiled JS and exposing a single &lt;strong&gt;export&lt;/strong&gt; that returns this string.&lt;/li&gt;
&lt;li&gt;The import matches the export so we can &lt;strong&gt;compose&lt;/strong&gt; the two WASM components into one.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Generating raw WASM component bytecode might not be very difficult for this particular use case, but we wanted something more scalable. As you will see in the next section, this is not the only place where we needed to generate WASM on the fly.&lt;/p&gt;
&lt;p&gt;Instead we are using &lt;a rel=&quot;external&quot; href=&quot;https://www.moonbitlang.com&quot;&gt;MoonBit&lt;/a&gt; to compile high level MoonBit source code directly into WASM. We can even embed the MoonBit compiler in Golem&#39;s CLI so there are no external dependencies for our users, thanks to that the &lt;a rel=&quot;external&quot; href=&quot;https://www.moonbitlang.com/blog/moonbit-wasm-compiler&quot;&gt;compiler itself is running on WASM&lt;/a&gt;. MoonBit is a high level and very exciting new programming language, and what makes it a perfect choice for this job is that it generates really concise WASM bytecode.&lt;/p&gt;
&lt;p&gt;I have written a small helper crate for Rust, &lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/moonbit-component-generator&quot;&gt;moonbit-component-generator&lt;/a&gt; that embeds the compiler and helps with generating these small wrapper components.&lt;/p&gt;
&lt;p&gt;For injecting the scripts, we basically give the WIT of the component we want to generate:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; MoonBitComponent&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;empty_from_wit&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    package golem:script-source;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    world script-source {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;      export get-script: func() -&amp;gt; string;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;#&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;script-source&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we define the MoonBit bindings based on this WIT, and add our implementation as a MoonBit source string:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;component&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;define_bindgen_packages&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;context&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Defining bindgen packages&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; stub_mbt&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;uwriteln!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;stub_mbt&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;// Generated by `moonbit-component-generator`&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;uwriteln!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;stub_mbt&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;uwriteln!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;stub_mbt&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;pub fn get_script() -&amp;gt; String {{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; line&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; script&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;lines&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  uwriteln!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;stub_mbt&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    #|&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;uwriteln!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;stub_mbt&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;component&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;write_world_stub&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;stub_mbt&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;context&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Writing world stub&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And finally build the WASM component:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;component&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;build&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; target&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;context&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Building component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The resulting WASM is ready to be composed with the prebuilt JavaScript engine. For this we can use the &lt;a rel=&quot;external&quot; href=&quot;https://crates.io/crates/wac-graph&quot;&gt;wac-graph&lt;/a&gt; Rust crate (or the &lt;code&gt;wac&lt;/code&gt; command line tool when doing it by hand).&lt;/p&gt;
&lt;h3 id=&quot;wit-wrapper-for-agents&quot;&gt;WIT wrapper for Agents&lt;/h3&gt;
&lt;p&gt;As I wrote in the &lt;em&gt;History&lt;/em&gt; section of this post, Golem built directly on the WASM component model. It&#39;s component exports and invocation mechanism directly depends on analyzing the component&#39;s WIT exports and providing ways to invoke them remotely.&lt;/p&gt;
&lt;p&gt;We&#39;ve also created a &lt;a rel=&quot;external&quot; href=&quot;https://learn.golem.cloud/rib&quot;&gt;scripting language called &lt;strong&gt;Rib&lt;/strong&gt;&lt;/a&gt; that is used for defining HTTP APIs on top of Golem components as well as for playing with them through a REPL; this scripting language uses WASM-specific syntax and naming conventions allow users to call component model interfaces through Golem. Rib here is just used as an example of something still depending on component exports. For more information check the linked official documentation, or &lt;a rel=&quot;external&quot; href=&quot;https://www.youtube.com/watch?v=vgrZxN0t-N0&quot;&gt;Afsal Thaj&#39;s presentation from Wasm I/O 2025&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Although we decided to move our user experience to the higher level agent interface described in this post, many parts of Golem still depends on components exporting their interfaces on the component model level. Most of these can be evolved in future versions to directly know about the agent type metadata, etc., but this is going to be a migration process through multiple Golem releases.&lt;/p&gt;
&lt;p&gt;Until then, without some additional trick, we would be in a very bad situation when for example using Rib to define HTTP API mappings or just manually trying to invoke an agent method. The only relevant exported WIT interface is the low-level dynamic one - each invocation would need to be put together by passing agent type name and agent method name strings, and assembling parameter values by converting them to the &lt;code&gt;data-value&lt;/code&gt; component model value representation. This would be completely unusable in practice.&lt;/p&gt;
&lt;p&gt;What we did for this release to avoid rewriting a large part of the system is that as part of the build process we generate &lt;strong&gt;a static wrapper&lt;/strong&gt; that exports a WIT interface that represents the user-defined agent types coming from code.&lt;/p&gt;
&lt;p&gt;The steps to do this are the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;First the user&#39;s code is compiled to JS and composed with the base WASM, as explained before&lt;/li&gt;
&lt;li&gt;Then we instantiate this WASM and call the &lt;code&gt;discoverAgentTypes&lt;/code&gt; export - we get back the agent type metadata&lt;/li&gt;
&lt;li&gt;Using this we generate a &lt;strong&gt;WIT interface per agent type&lt;/strong&gt;, with an &lt;code&gt;initialize&lt;/code&gt; function representing the constructor and one exported function for each agent method&lt;/li&gt;
&lt;li&gt;We generate a &lt;strong&gt;MoonBit&lt;/strong&gt; implementation of these interfaces. These implementations encode/decode the values into &lt;code&gt;data-value&lt;/code&gt; and calls the underlying component&#39;s dynamic &lt;code&gt;initialize&lt;/code&gt; and &lt;code&gt;invoke&lt;/code&gt; exports.&lt;/li&gt;
&lt;li&gt;Finally we compose this wrapper with the original component too. The resulting WASM will have all the exports and imports as the original one, but in addition to that will also export static, well typed interfaces for each agent type defined in the TypeScript code.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Even though this static wrapper will most likely not be needed in the next major Golem release, the technique may remain used if we want to use a Golem component in context of another WASM environment or tool.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem 1.3&#39;s new JavaScript engine</title>
        <published>2025-09-19T00:00:00+00:00</published>
        <updated>2025-09-19T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem-new-js-engine/"/>
        <id>https://blog.vigoo.dev/posts/golem-new-js-engine/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem-new-js-engine/">&lt;p&gt;As we are rapidly approaching the release data for Golem 1.3, a major update, I&#39;m going to publish a series of small posts talking about some of the technical details of this new release. In this first one, let&#39;s talk about the &lt;em&gt;new JavaScript engine&lt;/em&gt;.&lt;/p&gt;
&lt;h2 id=&quot;javascript-support-in-previous-versions&quot;&gt;JavaScript support in previous versions&lt;/h2&gt;
&lt;p&gt;In previous Golem versions we tried to support JavaScript (and TypeScript) using the &quot;official&quot; way of using these languages in the WASM Component Model: using the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/bytecodealliance/ComponentizeJS&quot;&gt;ComponentizeJs project&lt;/a&gt;. This embeds a special version of the &lt;a rel=&quot;external&quot; href=&quot;https://spidermonkey.dev&quot;&gt;SpiderMonkey JS engine&lt;/a&gt;, called StarlingMonkey in a WASM component together with the user&#39;s JS code, and generates import and export bindings based on the component model interface definition (WIT). In addition to this, ComponentizeJs also does a &lt;em&gt;preinitialization step&lt;/em&gt; - basically pre-running and snapshotting parts of the resulting component compile time to make the component&#39;s initialization time quicker.&lt;/p&gt;
&lt;p&gt;Although this all sounds very good, this project is still considered &lt;em&gt;experimental&lt;/em&gt; and we ran into serious issues with it, especially around it&#39;s implementation of &lt;code&gt;fetch&lt;/code&gt; and async boundaries. We reported these issues, and also tried to fix some of them ourselves, but working on this project is extremely difficult and we did not reach a point where our users would be guaranteed to be able to build on top of these core JS APIs.&lt;/p&gt;
&lt;h2 id=&quot;the-new-engine&quot;&gt;The new engine&lt;/h2&gt;
&lt;p&gt;Instead trying to fix ComponentizeJs or waiting for others to do so, we decided to try to &lt;em&gt;replace it&lt;/em&gt; for the next Golem release. This worked out so well that we were able to refocus our language support to be primarily TypeScript for the next release.&lt;/p&gt;
&lt;p&gt;So what did I do?&lt;/p&gt;
&lt;p&gt;The goal was to have a similar solution - take the user&#39;s JS and an interface definition (&lt;a rel=&quot;external&quot; href=&quot;https://component-model.bytecodealliance.org/design/wit.html&quot;&gt;WIT&lt;/a&gt;) and get a WebAssembly component implementing this interface by running the user&#39;s JavaScript code. But we wanted something that is significantly easier to work with, and easier to extend with more and more &quot;build-in&quot; JS APIs. This is important for us as we want people to be able to use as many existing libraries in their Golem applications as possible. There must be a trade-off somewhere, of course - and there are two that I&#39;m going to talk about in details. First, our new engine supposed to have worse performance than ComponentizeJs, although it has not been benchmarked yet; and the second one is the need of a Rust compiler toolchain to convert the JavaScript code to WASM. This, however, is not affecting Golem users due to some other changes we introduced; more about it later.&lt;/p&gt;
&lt;p&gt;So with all these constraints, I ended up creating &lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/wasm-rquickjs&quot;&gt;wasm-rquickjs&lt;/a&gt;, with the following properties:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It&#39;s built on the &lt;a rel=&quot;external&quot; href=&quot;https://quickjs-ng.github.io/quickjs/&quot;&gt;QuickJS-NG engine&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;But, to make it much easier to maintain and extend, it is using this engine through Rust, using the excellent &lt;a rel=&quot;external&quot; href=&quot;https://github.com/DelSkayn/rquickjs&quot;&gt;rquickjs crate&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;It generates glue code to bridge the JS world with the Rust bindings generated by &lt;code&gt;wit-bindgen-rust&lt;/code&gt; for the component model exports and imports&lt;/li&gt;
&lt;li&gt;And also defines a growing set of built-in JS APIs, some implemented from scratch, others by taking various open-source polyfill libraries&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The result is a CLI tool (&lt;code&gt;wasm-rquickjs-cli&lt;/code&gt;) and embeddable Rust library that can take a WIT world, a JS file, and ends up generating a standalone Rust crate that, when compiled using &lt;a rel=&quot;external&quot; href=&quot;https://github.com/bytecodealliance/cargo-component&quot;&gt;cargo-component&lt;/a&gt;, emits the WASM that we need.&lt;/p&gt;
&lt;p&gt;It also support emitting TypeScript module definitions for all the imports and exports of the component.&lt;/p&gt;
&lt;h2 id=&quot;details&quot;&gt;Details&lt;/h2&gt;
&lt;p&gt;To understand why I chose to go with generating Rust crates and using the above mentioned &lt;code&gt;rquickjs&lt;/code&gt; library, let&#39;s take a closer look at how things are done within &lt;code&gt;wasm-rquickjs&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;defining-built-in-apis&quot;&gt;Defining built-in APIs&lt;/h3&gt;
&lt;p&gt;We wanted to be able to easily increase the set of supported built-in APIs to have increased compatibility with the existing JS ecosystem. Some of these APIs can be introduced with pure JS polyfill libraries, but many of them requires to be somehow implemented on top of imported WebAssemby system interfaces (WASI). A good example can be implementing (a subset of) the &lt;code&gt;node:fs&lt;/code&gt; API to work with files and filesystems.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;rquickjs&lt;/code&gt; crate really makes this very easy to do - it has a convenient way to bind native Rust functions into the JavaScript context, and it also solves the difficult problem of bridging the world of JS promises with &lt;em&gt;async Rust&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;This means we can write Rust functions in which we can use the Rust standard library or any other imported WIT bindings and then call these functions from JS. For example we can define a &lt;code&gt;read_file&lt;/code&gt; function that exposes &lt;code&gt;std::fs::read&lt;/code&gt; for JavaScript:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;rquickjs&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; read_file&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;path&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ctx&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Ctx&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;_&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;   List&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;TypedArray&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;_&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u8&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; path&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Path&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;path&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  match&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;fs&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;read&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;path&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bytes&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; typed_array&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;        TypedArray&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new_copy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ctx&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bytes&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;expect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Failed to create TypedArray&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;      List&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;typed_array&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;err&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; error_message&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; format!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Failed to read file &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;path:?&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;      List&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;error_message&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then the actual JavaScript API can be implemented in JS itself, using these native functions:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; function&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; readFile&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;path&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; optionsOrCallback&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; callback&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;contents&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; error&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; read_file&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;path&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; ===&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; undefined&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; buffer&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Buffer&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;from&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;contents&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;            callback&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;buffer&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;            callback&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;undefined&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; error&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This makes it really convenient to add support for more and more APIs, and as mentioned earlier, these native functions can be &lt;code&gt;async&lt;/code&gt; Rust functions too, which simply translates to async JS functions.&lt;/p&gt;
&lt;p&gt;For example, part of the &lt;code&gt;fetch&lt;/code&gt; implementation is sending the request body asynchronously:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; function&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sendBody&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;bodyWriter&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; body&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; reader&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; body&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;getReader&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;true&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        const&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;done&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; value&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; reader&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;read&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;done&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        await&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; bodyWriter&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;writeRequestBodyChunk&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;    bodyWriter&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;finishBody&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;writeRequestBodyChunk&lt;/code&gt; method is a native Rust method defined like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;rquickjs&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;methods&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;rename_all &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;camelCase&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; WrappedRequestBodyWriter&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;qjs&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;        WrappedRequestBodyWriter&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; writer&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; write_request_body_chunk&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; chunk&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TypedArray&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;_&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u8&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;implementing-imports&quot;&gt;Implementing imports&lt;/h3&gt;
&lt;p&gt;With the above technique, we could have a precompiled WASM JS engine that is capable of running user code while providing them a fix set of supported APIs. This is what a similar project, &lt;a rel=&quot;external&quot; href=&quot;https://github.com/second-state/wasmedge-quickjs&quot;&gt;wasmedge-quickjs&lt;/a&gt; does.&lt;/p&gt;
&lt;p&gt;But &lt;code&gt;wasm-rquickjs&lt;/code&gt; does not stop here - it uses the same method of defining JS modules with native Rust bindings to define a JS module for &lt;em&gt;each imported WIT interface&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;So a code generator takes the WIT imports, and emits Rust code in the style of the above examples that exposes these WIT imports to JavaScript by calling the Rust WIT bindings, generated by &lt;code&gt;wit-bindgen-rust&lt;/code&gt; (this happens automatically under the hood when using the already mentioned &lt;code&gt;cargo-component&lt;/code&gt; build tool).&lt;/p&gt;
&lt;p&gt;Every data type WIT supports is mapped to a specific JS construct, and &lt;em&gt;resources&lt;/em&gt; are mapped to JS classes. The following example shows the generated function for one of the exported functions of &lt;code&gt;golem:llm&lt;/code&gt; &lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/golem-ai&quot;&gt;from the Golem AI libraries&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;rquickjs&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; send&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    messages&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bindings&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;golem&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;llm&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;llm&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Message&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    config&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;bindings&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;llm&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;llm&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Config&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;bindings&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;llm&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;llm&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;ChatEvent&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;bindings&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;llm&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;llm&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;ChatEvent&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;bindings&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;llm&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;llm&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;send&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        messages&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;into_iter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; v&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;collect&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;_&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;as_slice&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;config&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This simply uses &lt;code&gt;rquickjs&lt;/code&gt;&#39;s native binding macro to do the hard work, and calls the generated Rust bindings under the hood.&lt;/p&gt;
&lt;p&gt;Of course to make this work, &lt;code&gt;rquickjs&lt;/code&gt; also needs to know how to encode these data types, such as the LLM &lt;code&gt;Message&lt;/code&gt;, as JS. So the code generator also emits instances of the &lt;code&gt;ToJs&lt;/code&gt; and &lt;code&gt;FromJs&lt;/code&gt; type classes, such as:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;js&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; rquickjs&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;IntoJs&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;js&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;bindings&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;llm&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;llm&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Message&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; into_js&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;    self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    ctx&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;rquickjs&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Ctx&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;js&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; rquickjs&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;rquickjs&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Value&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;js&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; obj&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; rquickjs&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Object&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ctx&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; role&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;bindings&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;llm&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;llm&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Role&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;role&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    obj&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;set&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;role&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; role&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;obj&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;into_value&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;js&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; rquickjs&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;FromJs&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;js&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;bindings&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;llm&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;llm&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Message&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; from_js&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    _ctx&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;rquickjs&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Ctx&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;js&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; rquickjs&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Value&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;js&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; rquickjs&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;Self&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; obj&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; rquickjs&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Object&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;from_value&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; role&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;bindings&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;llm&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;llm&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Role&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; obj&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;role&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The main difficulty was not generating these JS mappings - it was matching the expected signatures of &lt;code&gt;wit-bindgen-rust&lt;/code&gt;, as it has some complex rules for deciding when to pass things by value or by reference.&lt;/p&gt;
&lt;h3 id=&quot;implementing-exports&quot;&gt;Implementing exports&lt;/h3&gt;
&lt;p&gt;For all the exported interfaces in a component&#39;s WIT definition, &lt;code&gt;wit-bindgen-rust&lt;/code&gt; generates a &lt;em&gt;trait&lt;/em&gt; to be implemented. We expect the JS developers to implement all these imports with some well defined rules (interfaces becoming exported objects, kebab-case names becoming camel cased, etc.). With the assumption that the user&#39;s JS code implements all the exports, &lt;code&gt;wasm-rquickjs&lt;/code&gt; can generate implementations for these rust traits that are calling into the QuickJS engine, running these functions.&lt;/p&gt;
&lt;p&gt;Part of the problem is very similar to what we have with imports - converting from the Rust types (coming from the WIT types) to JS types. This is done using the same conversion type classes we already talked about.&lt;/p&gt;
&lt;p&gt;When setting up the JS context, we always store a reference to the user&#39;s module in a global variable, so the generated export code can easily access it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; module&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Object&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ctx&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;globals&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;userModule&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;expect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Failed to get userModule&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are similar global helper tables for tracking the class instances for WIT resource instances.&lt;/p&gt;
&lt;p&gt;Once we have the module object, we can apply the naming rules and find the function value and call it with &lt;code&gt;rquickjs&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; call_with_this&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;js&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; A&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; R&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    ctx&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Ctx&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;js&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    function&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Function&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;js&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    this&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Object&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;js&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    args&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; A&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; rquickjs&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    A&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; IntoArgs&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;js&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    R&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; FromJs&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;js&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; num&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; args&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;num_args&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; accum_args&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Args&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ctx&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; num&lt;/span&gt;&lt;span&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    accum_args&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;this&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;this&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    args&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;into_args&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; accum_args&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    function&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;call_arg&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;accum_args&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A nice property we can offer is that we don&#39;t have to constrain the user to always implement the exported functions as async JavaScript functions. We can simply check the return value before trying to convert it to the Rust equivalent whether it is a &lt;code&gt;Promise&lt;/code&gt; or not. And if it is, we can just &lt;code&gt;await&lt;/code&gt; it in the Rust code!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;is_promise&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; promise&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;into_promise&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;unwrap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; promise_future&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; promise&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;into_future&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  match&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; promise_future&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;await&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;async-all-the-way-down&quot;&gt;Async all the way down&lt;/h3&gt;
&lt;p&gt;This seamless integration of the JS and Rust async world is a key component in making &lt;code&gt;wasm-rquickjs&lt;/code&gt; easy to work with. But it&#39;s not enough that &lt;code&gt;rquickjs&lt;/code&gt; implements the boundary between JS and Rust. The end result is a WASM component, which is single threaded and only provides a very specific set of system APIs to build on; we cannot just use Tokio for example as our Rust runtime (at the time of writing). At the bottom of all the Rust and JS async stacks, there is single small WASI API supporting all this: &lt;code&gt;wasi:io/poll&lt;/code&gt;. &lt;a rel=&quot;external&quot; href=&quot;https://blog.yoshuawuyts.com/building-an-async-runtime-for-wasi/&quot;&gt;Yoshua Wuyts has an excellent blog post&lt;/a&gt; about the topic. &lt;code&gt;wasm-rquickjs&lt;/code&gt; builds on his &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/wasi-async-runtime/latest/wasi_async_runtime/&quot;&gt;&lt;code&gt;wasi_async_runtime&lt;/code&gt;&lt;/a&gt; crate (and soon will be migrated to the newer &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/wstd/latest/wstd/&quot;&gt;&lt;code&gt;wstd&lt;/code&gt; crate&lt;/a&gt;).&lt;/p&gt;
&lt;h2 id=&quot;trade-offs&quot;&gt;Trade-offs&lt;/h2&gt;
&lt;p&gt;As I mentioned in the introduction, this approach naturally comes with some trade-offs when comparing to ComponentizeJs.&lt;/p&gt;
&lt;h3 id=&quot;performance&quot;&gt;Performance&lt;/h3&gt;
&lt;p&gt;We are not doing any precompilation at the moment, so component initialization time for bigger projects is definitely supposed to be slower. On the other hand the engine itself is much smaller than the modified SpiderMonkey in ComponentizeJs, so this may balance out the difference in some cases. I also expect SpiderMonkey to be faster in general than QuickJS, although this is not as clear &lt;a rel=&quot;external&quot; href=&quot;https://cfallin.org/blog/2023/10/11/spidermonkey-pbl/&quot;&gt;because SpiderMonkey also has to run in interpreter mode&lt;/a&gt; on WASM.&lt;/p&gt;
&lt;h3 id=&quot;rust-compilation&quot;&gt;Rust compilation&lt;/h3&gt;
&lt;p&gt;A more serious trade-off is that by generating a Rust crate, we force the JS/TS users to have a Rust tool-chain available and compile these generated crates to WASM.&lt;/p&gt;
&lt;p&gt;We&#39;ve spent a lot of effort in the past year hiding the complexity of having these build tools, and especially having the &lt;em&gt;correct version&lt;/em&gt; of WASM / component model related tools automatically set up and invoked by hiding the component creating process in Golem&#39;s own CLI interface.&lt;/p&gt;
&lt;p&gt;Still, having to set up Rust to just run a simple JavaScript snippet on Golem is too much to ask. We worked around this issue by not allowing users to work directly on the component model level anymore - no WIT, no composition for them. This way we can embed a precompiled WASM binary in our tooling that can be combined with the user&#39;s JavaScript code to form a final WASM component. I am going to write a separate post about this decision and its technical details.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/wasm-rquickjs/&quot;&gt;wasm-rquickjs&lt;/a&gt; turned out to be a very capable alternative for ComponentizeJs, that is much easier to iterate on. It is a standalone project, completely usable outside of Golem; if the above two trade-offs are acceptable, it provides a nice experience of writing JavaScript or TypeScript code for the WASM Component Model.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>[Video] Missing Testing Features in Rust @ LambdaConf 2025</title>
        <published>2025-06-23T00:00:00+00:00</published>
        <updated>2025-06-23T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/missing-testing-features-in-rust/"/>
        <id>https://blog.vigoo.dev/posts/missing-testing-features-in-rust/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/missing-testing-features-in-rust/">&lt;p&gt;My talk at &lt;a rel=&quot;external&quot; href=&quot;https://www.lambdaconf.us&quot;&gt;LambdaConf 2025&lt;/a&gt; about my Rust test framework &lt;a rel=&quot;external&quot; href=&quot;https://test-r.vigoo.dev&quot;&gt;test-r&lt;/a&gt;.&lt;/p&gt;
&lt;iframe width=&quot;800&quot; height=&quot;450&quot; src=&quot;https://www.youtube.com/embed/Yf5oIj816mw?list=PL7DZ7q3nEWhwo2OmeaMzNggy7sof9qg5p&quot; title=&quot;Daniel Vigovszky - Missing Testing Features in Rust&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share&quot; referrerpolicy=&quot;strict-origin-when-cross-origin&quot; allowfullscreen&gt;&lt;/iframe&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>LambdaConf 2024-2025 - one year of Golem</title>
        <published>2025-05-13T00:00:00+00:00</published>
        <updated>2025-05-13T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/one-year-of-golem/"/>
        <id>https://blog.vigoo.dev/posts/one-year-of-golem/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/one-year-of-golem/">&lt;p&gt;I&#39;m on the last LambdaConf at the moment, and exactly a year ago I gave a &lt;a href=&quot;/posts/golem-and-the-wasm-component-model&quot;&gt;talk about Golem on LambdaConf 2024&lt;/a&gt;. Someone asked me what happened with Golem since then? So many things I could not properly answer. So here is a short summary of one year amount of Golem development, from LambdaConf 2024 to LambdaConf 2025.&lt;/p&gt;
&lt;p&gt;We had a Golem Hackathon on last LambdaConf, and had a fresh release of Golem for it which we called &lt;strong&gt;Golem 0.0.100&lt;/strong&gt;. We&#39;ve spent the summer after that to prepare the first production ready release of Golem OSS, &lt;strong&gt;Golem 1.0&lt;/strong&gt;, which we released 23th of August, 2024. In the few months between the hackathon and Golem 1.0, we made Golem&#39;s oplog store more scalable, introduced &lt;em&gt;environment inheritance&lt;/em&gt; between workers when they are spawned through RPC, added a worker scheduler that tracks worker memory usage and suspends workers when necessary to keep the system responsible. The stability of the executor itself has been improved significantly. We tried to make the CLI for 1.0 more user friendly, created precompiled binaries of it so users don&#39;t have to compile it themselves, and created the first usable version of &lt;strong&gt;Rib&lt;/strong&gt;, our scripting language used in Golem&#39;s API gateway.&lt;/p&gt;
&lt;p&gt;Rib itself did not stop there - we continued working on it in the rest of the year and it got much better type inference, error messages, new language features such as first-class worker support, list comprehensions and aggregations and so on.&lt;/p&gt;
&lt;p&gt;Our next milestone was &lt;strong&gt;Golem 1.1&lt;/strong&gt;, which has been released on the 9th of December, 2024. With this release we were no longer just targeting durable execution but realized that most applications also need components that are ephemeral - so we added support for &lt;strong&gt;ephemeral components&lt;/strong&gt;, which are stateless programs getting a fresh instance for each incoming call. We added the concept of &lt;strong&gt;plugins&lt;/strong&gt; in this release, although not fully complete yet, but with a vision of a future plugin ecosystem where these plugins can transform user&#39;s components and observe the living workers realtime. The API gateway got support for things like &lt;strong&gt;authentication&lt;/strong&gt; and &lt;strong&gt;CORS&lt;/strong&gt;, and we created tools to better observe the Golem worker&#39;s history by &lt;strong&gt;querying their oplog&lt;/strong&gt; itself. This was the first release with the ability to add an &lt;strong&gt;initial file system&lt;/strong&gt; for components.&lt;/p&gt;
&lt;p&gt;We were trying to make it easier for users, especially if they are not Rust developers, to use Golem. So we created precompiled, downloadable &lt;strong&gt;single executable Golem versions&lt;/strong&gt; for local development.&lt;/p&gt;
&lt;p&gt;Golem 1.1 was also the first version introducing the &lt;strong&gt;Golem application manifest&lt;/strong&gt;. This brings the concept of an &lt;em&gt;application&lt;/em&gt;, consist of one or more components, with the ability to describe (RPC) &lt;strong&gt;dependencies&lt;/strong&gt; between these components in a declarative way. This significantly simplified the way how these multi-component Golem applications are built, especially the iterative development process.&lt;/p&gt;
&lt;p&gt;For &lt;strong&gt;Golem 1.2&lt;/strong&gt; we decided to make this application manifest feature a core element of Golem development. We have redesigned the CLI to be based on the application concept, with single commands to build and deploy whole, multi-component Golem applications with support for dependencies between these components and allowing them to be written in different programming languages, even within a single application.&lt;/p&gt;
&lt;p&gt;We also improved our RPC solution so it no longer requires a working Rust toolchain. Instead we are &lt;strong&gt;linking&lt;/strong&gt; the RPC clients dynamically in the executor.&lt;/p&gt;
&lt;p&gt;Other improvements added in the 3-4 months of development of Golem 1.2 consisted of the first version of Golem&#39;s &lt;strong&gt;debugging&lt;/strong&gt; service, which will allow interactive debug and observation of running workers once it is done. Other features helping with the debugging of Golem code are the support for &lt;strong&gt;reverting&lt;/strong&gt; workers and cancel pending invocations. We have added support for special kind of workers implementing a HTTP &lt;strong&gt;request handler&lt;/strong&gt; (using the wasi-http interface) that can be directly mapped in the API gateway to various endpoints, and get the whole incoming HTTP request to be processed in the worker itself. We also support now &lt;strong&gt;scheduling&lt;/strong&gt; invocations (even through RPC) to be done at an arbitrary point in time instead of being executed immediately.&lt;/p&gt;
&lt;p&gt;Golem 1.2 has been released on 27th of March, 2025.&lt;/p&gt;
&lt;p&gt;In the roughly 1.5 months since then we were focusing on further improving the developer experience by updating our &lt;strong&gt;language support&lt;/strong&gt; to the latest version of everything (especially the JavaScript and TypeScript, Python and Go tooling). We can now define &lt;em&gt;plugin installations&lt;/em&gt; and &lt;em&gt;APIs&lt;/em&gt; in the &lt;strong&gt;application manifest&lt;/strong&gt; itself. We continued making the application manifest the primary way to work with Golem by further simplifying the CLI interface and enforcing that every Golem component is named the same as the WIT package it defines. We&#39;ve introduced a &lt;strong&gt;new dependency type&lt;/strong&gt; in the application manifest for directly depending on another WASM component, composing them build-time. This dependency type even supports downloading these WASM components from remote URLs, which is an nice way to use WASM components as language-independent libraries. The first such library we provide is &lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/golem-llm&quot;&gt;&lt;strong&gt;golem-llm&lt;/strong&gt;&lt;/a&gt;. The dependencies can now also added using a CLI command for those who prefer this method. In addition to that, we improved the CLI&#39;s &lt;strong&gt;error messages&lt;/strong&gt; significantly. Another small detail is that in Golem Cloud accounts can be now referenced by their &lt;strong&gt;e-mail address&lt;/strong&gt;, which allows us to for example share a project with other accounts by using their e-mail. Another small improvement is the ability to define &lt;strong&gt;environment variables&lt;/strong&gt; on a per-component level now (not just per worker). A nice new feature available for Golem programs is the ability to &lt;strong&gt;fork&lt;/strong&gt; workers.&lt;/p&gt;
&lt;p&gt;Rib continued to evolve, being more and more stable and having better error reporting. It is no longer just a scripting language to be used as glue code in the API gateway, but it is also integrated into the CLI as a &lt;strong&gt;REPL&lt;/strong&gt;, a convenient way to interact with Golem workers.&lt;/p&gt;
&lt;p&gt;All these DX improvements are going to be released today as &lt;strong&gt;Golem 1.2.2&lt;/strong&gt;, the version to be used on the LambdaConf 2025 hackathon.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>[Video] Golem powered by WebAssembly @ Wasm I/O 2025</title>
        <published>2025-04-04T00:00:00+00:00</published>
        <updated>2025-04-04T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem-powered-by-wasm/"/>
        <id>https://blog.vigoo.dev/posts/golem-powered-by-wasm/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem-powered-by-wasm/">&lt;p&gt;My talk at &lt;a rel=&quot;external&quot; href=&quot;https://www.wasm.io&quot;&gt;Wasm I/O 2025&lt;/a&gt; explaining how &lt;a rel=&quot;external&quot; href=&quot;https://golem.cloud&quot;&gt;Golem&lt;/a&gt; is built on WebAssembly and the Component Model.&lt;/p&gt;
&lt;iframe width=&quot;800&quot; height=&quot;450&quot; src=&quot;https://www.youtube.com/embed/_oEhuFjTyeQ?si=zYPcpJLasEBV-vGE&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share&quot; referrerpolicy=&quot;strict-origin-when-cross-origin&quot; allowfullscreen&gt;&lt;/iframe&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Durable Execution is not just for failures</title>
        <published>2025-03-28T00:00:00+00:00</published>
        <updated>2025-03-28T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/durable-execution-is-not-just-for-failures/"/>
        <id>https://blog.vigoo.dev/posts/durable-execution-is-not-just-for-failures/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/durable-execution-is-not-just-for-failures/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;When talking about &lt;a rel=&quot;external&quot; href=&quot;https://golem.cloud&quot;&gt;Golem&lt;/a&gt; or other &lt;strong&gt;durable execution engines&lt;/strong&gt; the most important property we are always pointing out is that by making the application &lt;em&gt;durable&lt;/em&gt;, it can automatically survive various failure scenarios. In case of a transient error, or some other external event such as updating or restarting the underlying servers durable programs can survive by seamlessly continuing their execution from the point where they were interrupted, without any visible (except for some latency, of course) effect for the application&#39;s users.&lt;/p&gt;
&lt;p&gt;But having this core capability has many other interesting consequences.&lt;/p&gt;
&lt;p&gt;A durable program can be dropped out of memory any time without having to explicitly save its state or shut it down in any way - and whenever it is needed it can be automatically recovered and it continues from where it left. The application developers can rely on very simple code storing everything in memory - as it is guaranteed that the in-memory state never gets lost.&lt;/p&gt;
&lt;p&gt;If a &lt;strong&gt;Golem worker&lt;/strong&gt; (a running durable program) is not performing any active job at the moment - for example it is waiting to be invoked, or waiting for some scheduled event - they automatically get dropped out of the executor&#39;s memory to make space for other workers. This means we can have an (almost arbitrary) large number of &quot;running&quot; workers, if they are not performing CPU intensive tasks. Sure, having to continuously recover dropped out workers is affecting latency, but still, it means we can run these large number of simultaneous, stateful programs even on a locally started Golem on a developer machine.&lt;/p&gt;
&lt;h2 id=&quot;demo&quot;&gt;Demo&lt;/h2&gt;
&lt;h3 id=&quot;setting-it-up&quot;&gt;Setting it up&lt;/h3&gt;
&lt;p&gt;In this short blog post we are going to demonstrate this. We are going to start the latest version of Golem (1.2) locally, then use the CLI (and some &lt;a rel=&quot;external&quot; href=&quot;https://www.nushell.sh&quot;&gt;Nushell&lt;/a&gt; snippets) to build, deploy and run a large number of workers.&lt;/p&gt;
&lt;p&gt;First we download the latest &lt;code&gt;golem&lt;/code&gt; command line application &lt;a rel=&quot;external&quot; href=&quot;https://learn.golem.cloud/quickstart&quot;&gt;according to Golem&#39;s Quick Start pages&lt;/a&gt;. With that we can start our local Golem cluster - all the core Golem services are integrated in this single &lt;code&gt;golem&lt;/code&gt; binary:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;nushell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; server&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; run&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We are going to use the same &lt;code&gt;golem&lt;/code&gt; CLI application to create, deploy and invoke Golem components.&lt;/p&gt;
&lt;p&gt;Next we create a new &lt;em&gt;golem application&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;nushell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; app&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; manyworkers&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; rust&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;img src=&quot;/images/2025-03-28/1.webp&quot; alt=&quot;Golem CLI creating the manyworkers Rust application&quot; width=&quot;1100&quot; height=&quot;143&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;Golem comes with a set of &lt;strong&gt;components templates&lt;/strong&gt; for all supported languages. One of these templates is a simple &lt;em&gt;shopping cart&lt;/em&gt; implementation in Rust, where each Golem worker (running instance of this component) represents a single shopping cart, keeping its contents in memory.&lt;/p&gt;
&lt;p&gt;We are going to create &lt;strong&gt;10&lt;/strong&gt; (identical) versions of this template, simulating that we have more than one applications running in a cluster. Even though they are going to be exactly the same to keep the post simple, from Golem&#39;s point of view it is going to be 10 different applications, compiled and deployed separately.&lt;/p&gt;
&lt;p&gt;Let&#39;s call the &lt;code&gt;golem component new&lt;/code&gt; command 10 times in the newly generated application to set this up!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;nushell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; each&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; |&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;x&lt;/span&gt;&lt;span&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; rust/example-shopping-cart&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; $&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo:cart&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This command created 10 components in our application, with names &lt;code&gt;demo:cart0&lt;/code&gt; to &lt;code&gt;demo:cart9&lt;/code&gt;. First let&#39;s build and deploy these components:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;nushell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; app&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; build&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; app&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; deploy&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;img src=&quot;/images/2025-03-28/2.webp&quot; alt=&quot;Golem CLI creating the demo:cart9 durable component and listing its exported shopping cart API&quot; width=&quot;1100&quot; height=&quot;318&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;To see the interface of this example, let&#39;s query one using &lt;code&gt;component get&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;nushell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; get&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; demo:cart0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;img src=&quot;/images/2025-03-28/3.webp&quot; alt=&quot;Component metadata of demo:cart0 with its WASM exports&quot; width=&quot;1100&quot; height=&quot;289&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;Before spawning our thousands of workers, we try out this exported interface by creating a single worker of &lt;code&gt;demo:cart0&lt;/code&gt; called &lt;code&gt;test&lt;/code&gt; and calling a few methods in it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;nushell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; invoke&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; demo:cart0/test&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; initialize-cart&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;user1&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;img src=&quot;/images/2025-03-28/4.webp&quot; alt=&quot;Invoking initialize-cart on the demo:cart0 worker through the Golem CLI&quot; width=&quot;1100&quot; height=&quot;137&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;nushell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; invoke&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; demo:cart0/test&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; add-item&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{ product-id: &amp;quot;p1&amp;quot;, name: &amp;quot;Example product&amp;quot;, price: 1000.0, quantity: 2 }&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;img src=&quot;/images/2025-03-28/5.webp&quot; alt=&quot;Invoking add-item on the demo:cart0 worker through the Golem CLI&quot; width=&quot;1100&quot; height=&quot;99&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;nushell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; invoke&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; demo:cart0/test&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; get-cart-contents&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;img src=&quot;/images/2025-03-28/6.webp&quot; alt=&quot;Invoking get-cart-contents on the demo:cart0 worker, returning the stored cart&quot; width=&quot;1100&quot; height=&quot;105&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;For some more context, we can also check the size of the compiled WASM files (we were doing a debug build so they are relatively large) for these components:&lt;/p&gt;
&lt;img src=&quot;/images/2025-03-28/7.webp&quot; alt=&quot;Worker metadata of the demo:cart0 test worker showing status and memory usage&quot; width=&quot;1100&quot; height=&quot;418&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;We can also query metadata of the created worker to get the same size information, and it also going to tell us the amount of &lt;strong&gt;memory&lt;/strong&gt; the instance allocates on startup:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;nushell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; get&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; demo:cart0/test&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;img src=&quot;/images/2025-03-28/9.webp&quot; alt=&quot;File listing of the twelve demo cart debug WASM components, each 3.1 megabytes&quot; width=&quot;1100&quot; height=&quot;510&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;And we can query the test worker&#39;s &lt;em&gt;oplog&lt;/em&gt; to get an idea of how much additional memory it allocated dynamically runtime:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;nushell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; oplog&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; demo:cart0/test&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7 z-d-i&quot;&gt; --&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; memory&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;img src=&quot;/images/2025-03-28/8.webp&quot; alt=&quot;Invocation log of the demo:cart0 worker showing repeated durable grow memory operations&quot; width=&quot;1100&quot; height=&quot;329&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;h3 id=&quot;spawning-many-workers&quot;&gt;Spawning many workers&lt;/h3&gt;
&lt;p&gt;Now that we have seen how a single worker looks like, let&#39;s spawn 1000 workers of each test component. This is going to take some time as it actually &lt;strong&gt;instantiates&lt;/strong&gt; the WASM program for each to make the initial two invocations.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;nushell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; j&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;loop&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    loop&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;           golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; $&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo:cart&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;$j&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;           golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; invoke&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; $&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo:cart&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;$j&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; initialize-cart&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;user1&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;           golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; invoke&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; $&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo:cart&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;$j&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; add-item&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; $&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{ product-id: &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;, name: &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Example product &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;$j&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;, price: 1000.0, quantity: 2 }&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;           if&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; $i&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; &amp;gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 9&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; break&lt;/span&gt;&lt;span&gt;; &lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;           $i&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; $i&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    if&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; $j&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; &amp;gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 999&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; break&lt;/span&gt;&lt;span&gt;; &lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    $j&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; $j&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After that, we have 10000 &quot;running&quot; workers (all idle, waiting for a next invocation). We can check by listing for example one of the component&#39;s workers:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;nushell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; list&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; demo:cart5&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;img src=&quot;/images/2025-03-28/10.webp&quot; alt=&quot;Cart contents of a thousand demo cart workers listed by the Golem CLI&quot; width=&quot;1100&quot; height=&quot;732&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;Of course only some of these workers (the last accessed ones) are really in the locally running executor&#39;s memory. Whenever a worker that&#39;s not in memory is going to be accessed, it is loaded and its state is transparently restored before it gets the request. Golem is tracking the resource usage of its running components and if there is not enough memory to load the new component, an old one is going to be dropped out.&lt;/p&gt;
&lt;h3 id=&quot;trying-it-out&quot;&gt;Trying it out&lt;/h3&gt;
&lt;p&gt;To demonstrate this, we can just invoke workers randomly from the 10000 we&#39;ve created:&lt;/p&gt;
&lt;img src=&quot;/images/2025-03-28/11.webp&quot; alt=&quot;Invoking get-cart-contents on several durable shopping cart workers in sequence&quot; width=&quot;1100&quot; height=&quot;383&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;Thanks to the durable execution model, every one of the 10000 workers react just as if it was running.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Using MoonBit with Golem Cloud</title>
        <published>2025-01-03T00:00:00+00:00</published>
        <updated>2025-01-03T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/moonbit-with-golem/"/>
        <id>https://blog.vigoo.dev/posts/moonbit-with-golem/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/moonbit-with-golem/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://www.moonbitlang.com&quot;&gt;MoonBit&lt;/a&gt;, a new programming language has been open sourced a few weeks ago - see &lt;a rel=&quot;external&quot; href=&quot;https://www.moonbitlang.com/blog/compiler-opensource&quot;&gt;this blog post&lt;/a&gt;. MoonBit is an exciting modern programming language that natively supports WebAssembly, including the component model - this makes it a perfect fit for writing applications for &lt;a rel=&quot;external&quot; href=&quot;https://golem.cloud&quot;&gt;Golem Cloud&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In this post I&#39;m exploring the current state of MoonBit and whether it is ready for writing Golem components, by implementing an example application more complex than a simple &quot;hello world&quot; example.&lt;/p&gt;
&lt;p&gt;The application to be implemented is a simple collaborative list editor - on the &lt;a rel=&quot;external&quot; href=&quot;https://youtu.be/11Cig1iH6S0&quot;&gt;launch event of Golem 1.0&lt;/a&gt; I have live-coded the same example using three different programming languages (TypeScript, Rust and Go) for the three main modules it requires. In this post I am implementing all three using &lt;strong&gt;MoonBit&lt;/strong&gt;, including the e-mail sending feature that was omitted from the live demo due to time constraints.&lt;/p&gt;
&lt;p&gt;The application can handle an arbitrary number of simultaneously open &lt;strong&gt;lists&lt;/strong&gt;. Each list consists of a list of string items. These items can be appended, inserted and deleted simultaneously by multiple users; the current list state can be queried any time, as well as the active connections (users who can perform editing operations on the list). Modification is only allowed for connected editors, and there is a &lt;code&gt;poll&lt;/code&gt; function exposed for them which returns the new changes since the last poll call. Lists can be archived, in which case they are no longer editable and their contents are saved in a separate &lt;strong&gt;list archive&lt;/strong&gt;. Then the list itself can be deleted, its last state remains stored forever in the archive. An additional feature is that if a list is &lt;em&gt;not archived&lt;/em&gt; and there were no changes for a certain period of time, all the connected editors are notified by sending an &lt;strong&gt;email&lt;/strong&gt; to them.&lt;/p&gt;
&lt;h2 id=&quot;golem-architecture&quot;&gt;Golem Architecture&lt;/h2&gt;
&lt;p&gt;In Golem a good architecture to run this is to have three different &lt;strong&gt;golem components&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the list&lt;/li&gt;
&lt;li&gt;the archive&lt;/li&gt;
&lt;li&gt;the email notifier&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These are compiled WebAssembly components, each exporting a distinct set of functions. Golem provides APIs to invoke these functions from the external world (for example mapping them to a HTTP API) and also allows &lt;strong&gt;workers&lt;/strong&gt; (instances of these components) to invoke each other. A component can have an arbitrary number of instances, each such worker being identified by a unique name.&lt;/p&gt;
&lt;p&gt;We can use this feature to have a very simple and straightforward implementation of the list editor - each document (editable list) will be mapped to its own worker, identified by the list&#39;s identifier. This way our list component only has to deal with a single list; scaling it up to handle multiple (possibly even millions) of lists is done automatically by Golem.&lt;/p&gt;
&lt;p&gt;For archiving lists, we want to store each archived list in a single place - so we are going to have only a single instance of our archive component, where each archived list information is sent to. This singleton worker can store the archived lists in some database if needed - but because Golem&#39;s durable execution guarantees, it is enough to just store them in memory (one important exception is if we want to store a really large amount of archived lists not fitting in a single worker&#39;s memory). Golem guarantees that the worker&#39;s state is restored in any case of failure or rescaling event so the archive component can really remain very simple.&lt;/p&gt;
&lt;p&gt;Finally, because Golem workers are single threaded and does not support async calls overlapping with its invocations at the moment, we need a third component to implement the delayed email sending functionality. There will be an &lt;strong&gt;email sending worker&lt;/strong&gt; corresponding to each &lt;strong&gt;list worker&lt;/strong&gt; and this worker will be suspended for an extended period of time (the amount we want to wait before sending out the email). Again, because of Golem&#39;s durable execution feature we can just &quot;sleep&quot; for an arbitrary long time in this component and we don&#39;t need to care about what can happen to our execution environment during that long period.&lt;/p&gt;
&lt;h2 id=&quot;initial-moonbit-implementation&quot;&gt;Initial MoonBit implementation&lt;/h2&gt;
&lt;p&gt;Before going into details of how to develop Golem components with MoonBit, let&#39;s try to implement the above described components in this new language, without any Golem or WebAssembly specifics.&lt;/p&gt;
&lt;p&gt;First we create a new &lt;code&gt;lib&lt;/code&gt; project using &lt;code&gt;moon new&lt;/code&gt;. This creates a new &lt;strong&gt;project&lt;/strong&gt; with a single &lt;strong&gt;package&lt;/strong&gt;. To match our architecture let&#39;s start by creating multiple packages, one for each component to develop (&lt;code&gt;list&lt;/code&gt;, &lt;code&gt;archive&lt;/code&gt;, &lt;code&gt;email&lt;/code&gt;)&lt;/p&gt;
&lt;p&gt;We create a folder for each package, with a &lt;code&gt;moon.pkg.json&lt;/code&gt; in each:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;json&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;list-model&quot;&gt;List model&lt;/h3&gt;
&lt;p&gt;Let&#39;s start by modelling our &lt;strong&gt;list&lt;/strong&gt;. The edited &quot;document&quot; itself is just an array of strings:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Document&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; items&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Array&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can implement &lt;strong&gt;methods&lt;/strong&gt; on &lt;code&gt;Document&lt;/code&gt; corresponding to the document editing operations we want to support. On this level we don&#39;t care about collaborative editing or connected users, just model our document as a pure data structure:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Creates an empty document&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Document&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Document&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; items&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Adds a new item to the document&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Document&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; item&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  if&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;items&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;search&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;item&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;is_empty&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;items&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;push&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;item&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Deletes an item from the document&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; delete&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Document&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; item&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;items&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;filter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;i&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; item&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; !=&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; i&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Inserts an item to the document after an existing item. If `after` is not in the document, the new item is inserted at the end.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; insert&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Document&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; after&lt;/span&gt;&lt;span&gt;~&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;~&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; index&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;items&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;search&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;after&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  match&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; index&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;index&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;items&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;insert&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    None&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Gets a view of the document&amp;#39;s items&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Document&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ArrayView&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;items&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Iterates the items in the document&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; iter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Document&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Iter&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;items&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;iter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can also use MoonBit&#39;s built-in test feature to write unit tests for this. The following test contains an assertion that the initial document is empty:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;new document is empty&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; empty&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Document&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  assert_eq&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;empty&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;items&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With the &lt;code&gt;inspect&lt;/code&gt; function tests can use &lt;strong&gt;snapshot values&lt;/strong&gt; to compare values with. The &lt;code&gt;moon&lt;/code&gt; CLI tool and the IDE integration provides a way to automatically update the snapshot values (the &lt;code&gt;content=&lt;/code&gt; part) in these test functions when needed:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;basic document operations&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; doc&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Document&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ..&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ..&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ..&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ..&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;insert&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;after&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ..&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;insert&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;after&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ..&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;delete&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ..&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;delete&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  inspect&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    doc&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    content&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;      #|[&amp;quot;x&amp;quot;, &amp;quot;y&amp;quot;, &amp;quot;w&amp;quot;, &amp;quot;b&amp;quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;list-editor-state&quot;&gt;List editor state&lt;/h3&gt;
&lt;p&gt;The next step is to implement the editor state management on top of this &lt;code&gt;Document&lt;/code&gt; type. As a reminder, we decided that every instance (Golem worker) of the list component will be only responsible for editing a single list. So we don&#39;t need to care about storing and indexing the lists, or routing connections to the corresponding node where the list state is - this is all going to be managed by Golem.&lt;/p&gt;
&lt;p&gt;What we need to do, however, is write stateful code to handle connecting and disconnecting users (&quot;editors&quot;), adding some validation on top of the document editing API so only connected editors can make changes, and collect change events for the polling API.&lt;/p&gt;
&lt;p&gt;We can start by defining a new datatype holding our document editing state:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Document state&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  document&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Document&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  connected&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Map&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ConnectionId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; EditorState&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; last_connection_id&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ConnectionId&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; archived&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email_deadline&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @datetime&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DateTime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email_recipients&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Array&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;EmailAddress&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Beside the actual document we are going to store:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A map of connected editors, with some per-editor state associated with them&lt;/li&gt;
&lt;li&gt;The last used connection ID so we can always generate a new unique one&lt;/li&gt;
&lt;li&gt;Whether the document has been archived or not&lt;/li&gt;
&lt;li&gt;When should we send out the email notification, and to what recipients&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So far we have only defined the &lt;code&gt;Document&lt;/code&gt; type so let&#39;s continue by specifying all these other types used in &lt;code&gt;State&lt;/code&gt;s fields.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ConnectionId&lt;/code&gt; is going to be a &lt;strong&gt;newtype&lt;/strong&gt; wrapping an integer:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Identifier of a connected editor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ConnectionId&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; derive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Eq&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Hash&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Generates a next unique connection ID&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; next&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ConnectionId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ConnectionId&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  ConnectionId&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We want to use this type as a &lt;strong&gt;key&lt;/strong&gt; of a &lt;code&gt;Map&lt;/code&gt; so we need instances of the &lt;code&gt;Eq&lt;/code&gt; and &lt;code&gt;Hash&lt;/code&gt; type classes. MoonBit can derive it for us automatically for newtypes. In addition to that, we also define a method called &lt;code&gt;next&lt;/code&gt; that generates a new connection ID with an incremented value.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;EditorState&lt;/code&gt; structure holds information for each connected editor. To keep things simple, we only store the editor&#39;s &lt;strong&gt;email address&lt;/strong&gt; and a buffer of change events since the last call to &lt;code&gt;poll&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;An email address is a newtype of a &lt;code&gt;String&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Email address of a connected editor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; EmailAddress&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;Change&lt;/code&gt; enum describes the possible changes made to the document:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| An observable change of the edited document&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;enum&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Change&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Added&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Deleted&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Inserted&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;after&lt;/span&gt;&lt;span&gt;~&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;~&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; derive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Show&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Deriving &lt;code&gt;Show&lt;/code&gt; (or implementing it by hand) makes it possible to use the &lt;code&gt;inspect&lt;/code&gt; test function to compare string snapshots of array of changes with the results of our &lt;code&gt;poll&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;Finally, let&#39;s define &lt;code&gt;EditorState&lt;/code&gt; using these two new types:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| State per connected editor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; EditorState&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  email&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; EmailAddress&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; events&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Array&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Change&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;email&lt;/code&gt; field never changes of a connected editor - but the &lt;code&gt;events&lt;/code&gt; array is, as every call to &lt;code&gt;poll&lt;/code&gt; will reset this so the next poll returns only the new changes. To be able to do this, we have to mark it as &lt;code&gt;mut&lt;/code&gt;-able.&lt;/p&gt;
&lt;p&gt;The last new type we need to introduce for &lt;code&gt;State&lt;/code&gt; is something representing a point in time. MoonBit&#39;s &lt;code&gt;core&lt;/code&gt; standard library does not have currently anything for this, but there is already a package database, &lt;a rel=&quot;external&quot; href=&quot;https://mooncakes.io&quot;&gt;mooncakes&lt;/a&gt;, with published MoonBit packages. Here we can find a &lt;a rel=&quot;external&quot; href=&quot;https://mooncakes.io/docs/#/suiyunonghen/datetime/&quot;&gt;package called &lt;code&gt;datetime&lt;/code&gt;&lt;/a&gt;. Adding it to our project can be done with the &lt;code&gt;moon&lt;/code&gt; CLI:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; moon&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; add&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; suiyunonghen/datetime&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and then importing it into the &lt;code&gt;list&lt;/code&gt; package by modifying its &lt;code&gt;moon.pkg.json&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;json&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;suiyunonghen/datetime&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this we can refer to the &lt;code&gt;DateTime&lt;/code&gt; type in this package using &lt;code&gt;@datetime.DateTime&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Before starting to implement methods for &lt;code&gt;State&lt;/code&gt;, we have to think about error handling too - some of the operations on &lt;code&gt;State&lt;/code&gt; may fail, for example if a wrong connection ID is used, or a document editing operation comes in for an already archived list. MoonBit has built-in support for error handling, and it starts by defining our own error type in the following way:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Error type for editor state operations&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; EditorError&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  ///| Error returned when an invalid connection ID is used&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  InvalidConnection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ConnectionId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  ///| Error when trying to modify an already archived document&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  AlreadyArchived&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this we are ready to implement the collaborative list editor! I&#39;m not going to list &lt;em&gt;all&lt;/em&gt; the methods of &lt;code&gt;State&lt;/code&gt; in this post, but the full source code is available &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/golem-moonbit-example&quot;&gt;on GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;connect&lt;/code&gt; method associates a new connection ID with the connected user, and also returns the current document state. This is important to be able to use the results of &lt;code&gt;poll&lt;/code&gt; - the returned list of changes have to be applied to exactly this document state on the client side.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Connects a new editor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; connect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  email&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; EmailAddress&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ConnectionId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ArrayView&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; connection_id&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;last_connection_id&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;next&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;last_connection_id&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; connection_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;connected&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;set&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;connection_id&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; EditorState&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;email&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;connection_id&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;document&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;em&gt;editing operations&lt;/em&gt; are more interesting. They build on top of the editing operations we already defined for &lt;code&gt;Document&lt;/code&gt;, but in addition to that, they all perform the following tasks:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Validating the connection ID&lt;/li&gt;
&lt;li&gt;Validating that the document is not archived yet&lt;/li&gt;
&lt;li&gt;Adding a &lt;code&gt;Change&lt;/code&gt; event to each connected editor&#39;s state&lt;/li&gt;
&lt;li&gt;Updating the &lt;code&gt;email_deadline&lt;/code&gt; and &lt;code&gt;email_recipients&lt;/code&gt; fields, as each editing operation &lt;em&gt;resets&lt;/em&gt; the timeout for sending out the emails&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&#39;s go through these steps one by one. For validations, we define two helper methods as we want to reuse them in all editing methods:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Fails if the document is archived&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; ensure_not_archived&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;EditorError&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  guard&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; not&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;archived&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; raise&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AlreadyArchived&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Fails if the given `connection_id` is not in the connection map&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; ensure_is_connected&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  connection_id&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ConnectionId&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;EditorError&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  guard&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;connected&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;contains&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;connection_id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    raise&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; InvalidConnection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;connection_id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;Unit!EditorError&lt;/code&gt; result type indicates that these methods can fail with &lt;code&gt;EditorError&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We can also define a helper method for adding a change event to each connected editor&#39;s state:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Adds a change event to each connected editor&amp;#39;s state&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; add_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; change&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Change&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  for&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; editor_state&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;connected&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;values&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    editor_state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;events&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;push&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;change&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And finally one for resetting the email-sending deadline and list of recipients:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Updates the `email_deadline` and `email_recipients` fields after an update.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; update_email_properties&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; now&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @datetime&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DateTime&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;from_unix_mseconds&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; // TODO&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; send_at&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; now&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;inc_hour&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;12&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email_list&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;connected_editors&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;email_deadline&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; send_at&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;email_recipients&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email_list&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that the &lt;code&gt;datetime&lt;/code&gt; library we imported has no concept of getting the &lt;em&gt;current&lt;/em&gt; date and time which we need for this function to work properly. We are going to address this problem once we start targeting WebAssembly (and Golem) as getting the current system time is something depending on the target platform.&lt;/p&gt;
&lt;p&gt;With these helper functions, implementing the editor functions, for example &lt;code&gt;add&lt;/code&gt;, is straightforward:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Adds a new element to the document as a connected editor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  connection_id&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ConnectionId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  value&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;EditorError&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ensure_not_archived&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ensure_is_connected&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;connection_id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;document&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;add_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Change&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Added&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;update_email_properties&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Implementing &lt;code&gt;poll&lt;/code&gt; is also easy, as we already maintain the list of changes per connection, we just need to reset it after each call:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Returns the list of changes occurred since the last call to poll&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; poll&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  connection_id&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ConnectionId&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Array&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Change&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;EditorError&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  match&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;connected&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;connection_id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;editor_state&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; events&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; editor_state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;events&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      editor_state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;events&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      events&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    None&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; raise&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; InvalidConnection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;connection_id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;list-archiving&quot;&gt;List archiving&lt;/h3&gt;
&lt;p&gt;As mentioned in the introduction, we are going to have a singleton Golem worker to store &lt;strong&gt;archived lists&lt;/strong&gt;. At this point we are still not having anything Golem or WebAssembly specific, like RPC calls, so let&#39;s just implement the list archive store in the simplest possible way. As I wrote earlier, we can simply store the archived lists in memory, and Golem will take care of persisting it.&lt;/p&gt;
&lt;p&gt;We don&#39;t want to reuse the same &lt;code&gt;Document&lt;/code&gt; type as it represents a live, editable document. Instead we define a few new types in the &lt;code&gt;archive&lt;/code&gt; package:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Unique name of a document&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DocumentName&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; derive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Eq&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Hash&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Show instance for DocumentName&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Show&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DocumentName&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; output&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; logger&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;_&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;output&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;logger&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| A single archived immutable document, encapsulating the document&amp;#39;s name and its items&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ArchivedDocument&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  name&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DocumentName&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  items&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Array&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; derive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Show&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Archive is a list of archived documents&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Archive&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  documents&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Map&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DocumentName&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ArchivedDocument&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All we need is an &lt;code&gt;insert&lt;/code&gt; method and a way to iterate all the archived documents:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Archives a named document&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; insert&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Archive&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  name&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DocumentName&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  items&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Array&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;documents&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;set&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; items&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Iterates all the archived documents&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; iter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Archive&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Iter&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ArchivedDocument&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;documents&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;values&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this done, we first implement the list archiving in the &lt;code&gt;list&lt;/code&gt; package using simple method calls. Later we are going to replace it with Golem&#39;s own &lt;em&gt;Worker to Worker communication&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;As there will be a singleton archive worker, we can simulate this for now by having a top-level &lt;code&gt;Archive&lt;/code&gt; instance in the &lt;code&gt;archive&lt;/code&gt; package:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;pub let archive: Archive = Archive::new()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And calling this in our &lt;code&gt;State::archive&lt;/code&gt; method:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; archive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;archived&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @archive&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DocumentName&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;TODO&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;  @archive&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;archive&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;insert&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;document&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;iter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_array&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that so far we have no way to know the document&#39;s name in &lt;code&gt;State&lt;/code&gt; - we did not store it anywhere. This is intentional, as we discussed earlier the &lt;strong&gt;worker name&lt;/strong&gt; will be used as the document&#39;s unique identifier. Getting the worker&#39;s name will be done in a Golem specific way once we get there.&lt;/p&gt;
&lt;h3 id=&quot;sending-an-email&quot;&gt;Sending an email&lt;/h3&gt;
&lt;p&gt;We already prepared some part of the email sending logic in the &lt;code&gt;State&lt;/code&gt; type: it has a &lt;em&gt;deadline&lt;/em&gt; and a list of &lt;em&gt;recipients&lt;/em&gt;. The idea is that we start an &lt;strong&gt;email sending worker&lt;/strong&gt; when a new list is created, and this runs in parallel to our editing session, in a loop. In this loop it first queries the deadline and list of recipients from our list editing state, and then just sleeps until that given deadline. When it wakes up (after 12 hours), it queries the list again, and if it is &lt;em&gt;past&lt;/em&gt; the deadline, it means there were no further editing operations in the meantime. Then it sends the notification emails to the list of recipients.&lt;/p&gt;
&lt;p&gt;There is no library on &lt;a rel=&quot;external&quot; href=&quot;https://mooncakes.io&quot;&gt;mooncakes&lt;/a&gt; yet for sending emails or even for making HTTP requests, so this is something we will have to do ourselves. Also, spawning the worker to run it in parallel is something Golem specific, so at this point we are not going to implement anything for the &lt;code&gt;email&lt;/code&gt; package. We will get back to it once the rest of the application is already compiled as Golem components.&lt;/p&gt;
&lt;h2 id=&quot;compiling-as-golem-components&quot;&gt;Compiling as Golem Components&lt;/h2&gt;
&lt;p&gt;It is time to try to compile our code as &lt;strong&gt;Golem components&lt;/strong&gt; - these are WebAssembly components (using the &lt;a rel=&quot;external&quot; href=&quot;https://component-model.bytecodealliance.org&quot;&gt;component model&lt;/a&gt;) exporting an API described with the Wasm Interface Type (WIT) language.&lt;/p&gt;
&lt;h3 id=&quot;bindings&quot;&gt;Bindings&lt;/h3&gt;
&lt;p&gt;In the current world of the WASM component model, components are defined in a spec-first way - first we write the WIT files describing types and exported interfaces, and then use a &lt;em&gt;binding generator&lt;/em&gt; to generate language-specific glue code from them. Fortunately the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/bytecodealliance/wit-bindgen&quot;&gt; &lt;code&gt;wit-bindgen&lt;/code&gt; tool&lt;/a&gt; already has MoonBit support, so we can start by installing the latest version:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cargo install wit-bindgen-cli&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that Golem&#39;s documentation recommends an older, specific version of &lt;code&gt;wit-bindgen&lt;/code&gt; - but that version did not support MoonBit yet. The new version should work well but the example codes for Golem were not tested with it.&lt;/p&gt;
&lt;p&gt;We will reuse the WIT definitions that were created for the Golem 1.0 launch demo.&lt;/p&gt;
&lt;p&gt;For the &lt;code&gt;list&lt;/code&gt; component, it is the following:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;wit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; demo&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;lst&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; api&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  record&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; connection&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u64&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  record&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; insert-params&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    after&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  variant&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; change&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    added&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    deleted&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    inserted&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;insert-params&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  add&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;c&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; connection&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;_&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  delete&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;c&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; connection&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;_&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  insert&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;c&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; connection&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; after&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;_&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  get&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  poll&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;c&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; connection&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;change&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  connect&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;email&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; tuple&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;connection&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  disconnect&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;c&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; connection&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;_&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  connected&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;editors&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  archive&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  is&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;archived&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; bool&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; email-query&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  deadline&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;u64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  recipients&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;world&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; lst&lt;/span&gt;&lt;span&gt;  {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  // .. imports to be explained later ..&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  export&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; api&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  export&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; email-query&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This interface definition exports two APIs - one is the public API of our list editors, very similar to the methods we already implemented for the &lt;code&gt;State&lt;/code&gt; type. The other is an internal API for the &lt;code&gt;email&lt;/code&gt; component to query the deadline and recipients as it was explained earlier.&lt;/p&gt;
&lt;p&gt;For simplicity, we are using &lt;code&gt;string&lt;/code&gt; as an error type on the public API.&lt;/p&gt;
&lt;p&gt;For the &lt;code&gt;archive&lt;/code&gt; component, we define a much simpler interface:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;wit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; demo&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;archive&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; api&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  record&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; archived-list&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    items&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  store&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; items&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  get&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;all&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;archived-list&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;world&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; archive&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  // .. imports to be explained later ..&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  export&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; api&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And finally, for the &lt;code&gt;email&lt;/code&gt; component:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;wit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; demo&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; api&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; golem&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;rpc&lt;/span&gt;&lt;span&gt;/&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;types&lt;/span&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0.1.0&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;uri&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  send&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;email&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;list-uri&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; uri&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;world&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; email&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  // .. imports to be explained later ..&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  export&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; api&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we are using a Golem specific type: &lt;code&gt;uri&lt;/code&gt;. This is needed because the &lt;code&gt;email&lt;/code&gt; workers need to call the specific &lt;code&gt;list&lt;/code&gt; worker it was spawned from. The details of this will be explained later.&lt;/p&gt;
&lt;p&gt;These WIT definitions need to be put in &lt;code&gt;wit&lt;/code&gt; directories of each package, and dependencies in subdirectories of &lt;code&gt;wit/deps&lt;/code&gt;. Check the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/golem-moonbit-example&quot;&gt;repository&lt;/a&gt; for reference.&lt;/p&gt;
&lt;p&gt;We started with defining a single MoonBit &lt;strong&gt;module&lt;/strong&gt; (identified by &lt;code&gt;moon.mod.json&lt;/code&gt; in the root) and just created &lt;code&gt;list&lt;/code&gt;, &lt;code&gt;email&lt;/code&gt; and &lt;code&gt;archive&lt;/code&gt; as internal packages. At this point we have to change this because we need to have a separate module for each chunk of code we want to compile to a separate Golem component. By running &lt;code&gt;wit-bindgen&lt;/code&gt; in each of the three subdirectories (shown below), it actually generates module definitions for us.&lt;/p&gt;
&lt;p&gt;We reorganize the directory structure a bit, moving &lt;code&gt;src/archive&lt;/code&gt; to &lt;code&gt;archive&lt;/code&gt; etc, and moving the previously written source code to &lt;code&gt;archive/src&lt;/code&gt;. This way the generated bindings and our hand-written implementation will be put next to each other. We can also delete the top-level module definition JSON.&lt;/p&gt;
&lt;p&gt;Now in all the three directories we can generate the bindings:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;wit-bindgen&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; moonbit&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; wit&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that once we start modifying the generated &lt;code&gt;stub.wit&lt;/code&gt; files, running this command again will overwrite our changes. To avoid that, it can be run in the following way:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;wit-bindgen&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; moonbit&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; wit&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-ignore-stub&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this done,&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;moon&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; build&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-target&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; wasm&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;will compile a WASM module for us in &lt;code&gt;./target/wasm/release/build/gen/gen.wasm&lt;/code&gt;. This is not yet a WASM &lt;strong&gt;component&lt;/strong&gt; - so it&#39;s not ready to be used directly in Golem. To do so, we will have to use another command line tool, &lt;a rel=&quot;external&quot; href=&quot;https://github.com/bytecodealliance/wasm-tools&quot;&gt;&lt;code&gt;wasm-tools&lt;/code&gt;&lt;/a&gt; to convert this module into a component that self-describes its higher level exported interface.&lt;/p&gt;
&lt;h3 id=&quot;wit-dependencies&quot;&gt;WIT dependencies&lt;/h3&gt;
&lt;p&gt;We are going to need to depend on some WIT packages, some from WASI (WebAssembly System Interface) to access things like environment variables and the current date/time, and some Golem specific ones to implement worker-to-worker communication.&lt;/p&gt;
&lt;p&gt;The simplest way to get the appropriate version of all the dependencies Golem provides is to use Golem&#39;s &quot;all&quot; packaged interfaces with the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/bytecodealliance/wit-deps&quot;&gt;&lt;code&gt;wit-deps&lt;/code&gt;&lt;/a&gt; tool.&lt;/p&gt;
&lt;p&gt;So first we install &lt;code&gt;wit-deps&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;cargo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; install&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; wit-deps-cli&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And create a &lt;code&gt;deps.toml&lt;/code&gt; file in each &lt;code&gt;wit&lt;/code&gt; directory we have created with the following contents:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;toml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;all&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;https://github.com/golemcloud/golem-wit/archive/main.tar.gz&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And finally we run the following command to fill the &lt;code&gt;wit/deps&lt;/code&gt; directory:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;wit-deps&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; update&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;implementing-the-exports&quot;&gt;Implementing the exports&lt;/h3&gt;
&lt;p&gt;Before setting up this compilation chain let&#39;s see how we can connect the generated bindings with our existing code. Let&#39;s start with the &lt;code&gt;archive&lt;/code&gt; component, as it is the simplest one.&lt;/p&gt;
&lt;p&gt;The binding generator creates a &lt;code&gt;stub.mbt&lt;/code&gt; file at &lt;code&gt;archive/gen/interface/demo/archive/api/stub.mbt&lt;/code&gt; with the two exported functions to be implemented. Here we face the usual question when working with code generators: we have a definition of &lt;code&gt;archived-list&lt;/code&gt; in WIT and the binding generator generated the following MoonBit definition from it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;// Generated by `wit-bindgen` 0.36.0. DO NOT EDIT!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ArchivedList&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      name&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; items&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Array&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; derive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But we already defined a very similar structure called &lt;code&gt;ArchivedDocument&lt;/code&gt;! The only differences are the use of the &lt;code&gt;DocumentName&lt;/code&gt; newtype and that our version was deriving a &lt;code&gt;Show&lt;/code&gt; instance. We could decide to give up using the newtype, and use the generated type in our business logic, or we could keep the generated types separated from our actual code. (This is not really specific to MoonBit or the WASM tooling, we face the same issue with any code generator based approach).&lt;/p&gt;
&lt;p&gt;In this post I will keep the generated code separate from our already written business logic, and just show how to implement the necessary conversions to implement the &lt;code&gt;stub.mbt&lt;/code&gt; file(s).&lt;/p&gt;
&lt;p&gt;The first exported function to implement is called &lt;code&gt;store&lt;/code&gt;. We can implement it by just calling &lt;code&gt;insert&lt;/code&gt; on our singleton top level &lt;code&gt;Archive&lt;/code&gt; as we did before when we directly wired the &lt;code&gt;archive&lt;/code&gt; package to the &lt;code&gt;list&lt;/code&gt; package:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; store&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; items&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Array&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;      @src&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;archive&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;insert&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;@src&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DocumentName&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; items&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that we need to import our main &lt;code&gt;archive&lt;/code&gt; source in the stub&#39;s package JSON:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;json&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo/archive/ffi&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ffi&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo/archive/src&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The second function to be implemented needs to convert between the two representations of an archived document:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_all&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Array&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ArchivedList&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;  @src&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;archive&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;iter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;archived&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; archived&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;_&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; items&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; archived&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;items&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_array&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that for this to work, we also have to make the previously defined &lt;code&gt;struct ArchivedDocument&lt;/code&gt; a &lt;code&gt;pub struct&lt;/code&gt; otherwise we cannot access it&#39;s &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;items&lt;/code&gt; fields from the stub package.&lt;/p&gt;
&lt;p&gt;(Note: at the time of writing https://github.com/bytecodealliance/wit-bindgen/pull/1100 was not merged yet, and it is needed for the binding generator to produce working code with Golem wasm-rpc; Until it is merged, it is possible to compile the fork and use it directly)&lt;/p&gt;
&lt;p&gt;The same way we can implement the two generated stubs in the &lt;code&gt;list&lt;/code&gt; module (in &lt;code&gt;list/gen/interface/demo/lst/api/stub.mbt&lt;/code&gt; and &lt;code&gt;list/gen/interface/demo/lst/emailQuery/stub.mbt&lt;/code&gt;) using our existing implementation of &lt;code&gt;State&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;One interesting details is how we can map the &lt;code&gt;EditorError&lt;/code&gt; failures into the string errors used in the WIT definition. First we define a &lt;code&gt;to_string&lt;/code&gt; method for &lt;code&gt;EditorError&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; EditorError&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  match&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    InvalidConnection&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Invalid connection ID: &lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;id&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    AlreadyArchived&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Document is already archived&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then use &lt;code&gt;?&lt;/code&gt; and &lt;code&gt;map_err&lt;/code&gt; in the stubs:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;c&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Connection&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;  @src&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;state&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;?&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_connection_id&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;c&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map_err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;err&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; err&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;using-host-functions&quot;&gt;Using host functions&lt;/h3&gt;
&lt;p&gt;When we implemented the &lt;code&gt;update_email_properties&lt;/code&gt; function earlier, we could not properly query the current time to calculate the proper deadline. Now that we are targeting Golem, we can use the WebAssembly system interface (WASI) to access things like the system time. One way would be to use the published &lt;a rel=&quot;external&quot; href=&quot;https://mooncakes.io/docs/#/yamajik/wasi-bindings/&quot;&gt;&lt;code&gt;wasi-bindings&lt;/code&gt; package&lt;/a&gt; but as we are already generating bindings from WIT anyway, we can just use our own generated bindings to imported host functions.&lt;/p&gt;
&lt;p&gt;First, we need to import the WASI wall-clock interface into our WIT world:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;wit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;world&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; lst&lt;/span&gt;&lt;span&gt;  {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  export&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; api&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  export&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; email-query&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  import&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; wasi&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;clocks&lt;/span&gt;&lt;span&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;wall-clock&lt;/span&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0.2.0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we regenerate the bindings (make sure to use &lt;code&gt;--ignore-stub&lt;/code&gt; to avoid rewriting our stub implementation!) and import it into our main (&lt;code&gt;src&lt;/code&gt;) package:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;json&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;suiyunonghen/datetime&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo/lst/interface/wasi/clocks/wallClock&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;wallClock&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With that we can call the WASI &lt;code&gt;now&lt;/code&gt; function to query the current system time, and convert it to the &lt;code&gt;datetime&lt;/code&gt; module&#39;s &lt;code&gt;DateTime&lt;/code&gt; type which we were using before:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Queries the WASI wall clock and returns it as a @datetime.DateTime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;/// Note that DateTime has only millisecond precision&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; now&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @datetime&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DateTime&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; wasi_now&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @wallClock&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;now&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; base_ms&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  wasi_now&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;seconds&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;reinterpret_as_int64&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1000&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; nano_ms&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;wasi_now&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nanoseconds&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;reinterpret_as_int&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1000000&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_int64&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;  @datetime&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DateTime&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;from_unix_mseconds&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;base_ms&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; nano_ms&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;golem-app-manifest&quot;&gt;Golem app manifest&lt;/h2&gt;
&lt;p&gt;In the next step of our implementation we will have to connect our two existing components: &lt;code&gt;list&lt;/code&gt; and &lt;code&gt;archive&lt;/code&gt; in a way that &lt;code&gt;list&lt;/code&gt; can do remote procedure calls to &lt;code&gt;archive&lt;/code&gt;. With the same technique we will be able to implement the third component, &lt;code&gt;email&lt;/code&gt; which needs to be both called &lt;em&gt;from&lt;/em&gt;&lt;code&gt; list&lt;/code&gt; (when started) and called back (when getting the deadline and recipients).&lt;/p&gt;
&lt;p&gt;Golem has tooling supporting this - but before trying to use it, let&#39;s convert our project into a &lt;strong&gt;golem application&lt;/strong&gt; described by &lt;strong&gt;app manifests&lt;/strong&gt;. This will enable us to use &lt;code&gt;golem-cli&lt;/code&gt; to generate the necessary files for worker-to-worker communication, and will also make it easier to deploy the compiled components into Golem.&lt;/p&gt;
&lt;h3 id=&quot;the-build-steps&quot;&gt;The build steps&lt;/h3&gt;
&lt;p&gt;To build a single MoonBit module into a Golem component, without any worker-to-worker communication involved, we have to perform the following steps:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;(Optionally) regenerate the WIT bindings with &lt;code&gt;wit-bindgen ... --ignore-stub&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Compile the MoonBit source code into a WASM module with &lt;code&gt;moon build --target wasm&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Embed the WIT specification into a custom WASM section using &lt;code&gt;wasm-tools component embed&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Convert the WASM module into a WASM &lt;em&gt;component&lt;/em&gt; using &lt;code&gt;wasm-tools component new&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When we will start to use worker-to-worker communication it will require even more steps, as we are going to generate stub WIT interfaces, and compile and link multiple WASM components. An earlier version of this was &lt;a rel=&quot;external&quot; href=&quot;https://blog.vigoo.dev/posts/w2w-communication-golem/&quot;&gt;described in the Worker to Worker communication in Golem&lt;/a&gt; blog post last year.&lt;/p&gt;
&lt;p&gt;The Golem app manifest and the corresponding CLI tool, introduced with &lt;strong&gt;Golem 1.1&lt;/strong&gt;, automates all these steps for us.&lt;/p&gt;
&lt;h3 id=&quot;manifest-template&quot;&gt;Manifest template&lt;/h3&gt;
&lt;p&gt;We start by creating a root app manifest, &lt;code&gt;golem.yaml&lt;/code&gt;, in the root of our project. We start by setting up a temporary directory and a shared directory for the WIT dependencies we previously fetched with &lt;code&gt;wit-deps&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Schema for IDEA:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; $schema: https://schema.golem.cloud/app/golem/1.1.0/golem.schema.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Schema for vscode-yaml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; yaml-language-server: $schema=https://schema.golem.cloud/app/golem/1.1.0/golem.schema.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;empDir&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;arget/golem-temp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;itDeps&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ommon-wit/deps&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By moving our previous &lt;code&gt;deps.toml&lt;/code&gt; into &lt;code&gt;common-wit&lt;/code&gt; and doing a &lt;code&gt;wit-deps update&lt;/code&gt; in the root, we can fill up this &lt;code&gt;deps&lt;/code&gt; directory with all the WASI and Golem APIs we need.&lt;/p&gt;
&lt;p&gt;Then we define a &lt;strong&gt;template&lt;/strong&gt; for building MoonBit components with Golem CLI. In the template, we are going to define two &lt;strong&gt;profiles&lt;/strong&gt; - one for doing a &lt;strong&gt;release&lt;/strong&gt; build and one for &lt;strong&gt;debug&lt;/strong&gt;. In the post I&#39;m only going to show the release build.&lt;/p&gt;
&lt;p&gt;It starts by specifying some directory names and where the final WASM files will be placed:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;emplates&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;oonbit&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    p&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;rofiles&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      r&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;elease&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ourceWit&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;it&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        g&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;eneratedWit&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;it-generated&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omponentWasm&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; .&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;./target/release/{{ componentName }}.wasm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;inkedWasm&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; .&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;./target/release/{{ componentName }}-linked.wasm&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These directories are relative to the components subdirectories (for example &lt;code&gt;archive&lt;/code&gt;) so what we say here is that once all the components are built, they al will be put in the root &lt;code&gt;target/release&lt;/code&gt; directory.&lt;/p&gt;
&lt;p&gt;Then we specify the &lt;strong&gt;build steps&lt;/strong&gt;, described in the previous section:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        b&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;uild&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ommand&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;it-bindgen moonbit wit-generated --ignore-stub --derive-error --derive-show&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ources&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;it-generated&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;argets&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; f&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;fi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;nterface&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;orld&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ommand&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;oon build --target wasm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ommand&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;asm-tools component embed wit-generated target/wasm/release/build/gen/gen.wasm -o ../target/release/{{ componentName }}.module.wasm --encoding utf16&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          m&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;kdirs&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; .&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;./target/release&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ommand&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;asm-tools component new ../target/release/{{ componentName }}.module.wasm -o ../target/release/{{ componentName }}.wasm&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, we can define additional directories to be cleaned by the &lt;code&gt;golem app clean&lt;/code&gt; command, and we can even define custom commands to be executed with &lt;code&gt;golem app xxx&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;lean&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;arget&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;it-generated&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ustomCommands&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          u&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;pdate-deps&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ommand&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;it-deps update&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ir&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; ..&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          r&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;egenerate-stubs&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ommand&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;it-bindgen moonbit wit-generated&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this set, we can add a new &lt;em&gt;MoonBit module&lt;/em&gt;* to this &lt;strong&gt;Golem project&lt;/strong&gt; by creating a &lt;code&gt;golem.yaml&lt;/code&gt; in its directory - so &lt;code&gt;archive/golem.yaml&lt;/code&gt; and &lt;code&gt;list/golem.yaml&lt;/code&gt; for now.&lt;/p&gt;
&lt;p&gt;In these sub-manifests we can use the above defined template to tell Golem that this is a MoonBit module. It is possible to mix Golem components written in different languages in a single application.&lt;/p&gt;
&lt;p&gt;For example the &lt;code&gt;archive&lt;/code&gt; component&#39;s manifest will look like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Schema for IDEA:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; $schema: https://schema.golem.cloud/app/golem/1.1.0/golem.schema.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Schema for vscode-yaml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; yaml-language-server: $schema=https://schema.golem.cloud/app/golem/1.1.0/golem.schema.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omponents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  a&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;rchive&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;emplate&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;oonbit&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;building-the-components&quot;&gt;Building the components&lt;/h3&gt;
&lt;p&gt;With this set, the whole application (with its two already written components) can be compiled by simply saying&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;golem app build&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are a few organizational things to do first, as &lt;code&gt;golem app build&lt;/code&gt; does some transformations on the WIT definitions. This means that our previously written &lt;strong&gt;stubs&lt;/strong&gt; are a wrong place. The easiest way to fix this is to delete all the wit-bindgen generated directories (but first backup the hand-written stubs!) and then copy back the stubs into the new directories created. We are not going to discuss this in more details here. The blog post incrementally discovers how to build Golem applications with MoonBit and introduces the app manifest in a late stage, but the recommended way is to start immediately with an app manifest and then there is no need to do these fixes.&lt;/p&gt;
&lt;h3 id=&quot;first-try&quot;&gt;First try&lt;/h3&gt;
&lt;p&gt;Running the build command results in two WASM files that are ready to be used with Golem! Although they are not able to communicate with each other yet (so the archiving functionality does not work), it is already possible to try them out with Golem.&lt;/p&gt;
&lt;p&gt;To do so, we can start Golem locally by downloading the latest release of &lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/golem/releases/tag/v1.1.0&quot;&gt;single-executable Golem&lt;/a&gt; or using our hosted Golem Cloud. With the &lt;code&gt;golem&lt;/code&gt; binary, we just use the following command to start up the services locally:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; start&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;vv&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then, from the root of our project, we can upload the two compiled components using the same command:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; add&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-component-name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; archive&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Added&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; archive&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; URN:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;     urn:component:bde2da89-75a8-4adf-953f-33b360c978d0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; name:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    archive&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; version:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; size:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;    9.35&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; KiB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Created&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; at:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;        2025-01-03&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; 15:09:05.166785&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; UTC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Exports:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  demo:archive-interface/api.{get-all}&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;record &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; name:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; items:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;strin&lt;/span&gt;&lt;span&gt;g&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  demo:archive-interface/api.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;(name:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; items:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;strin&lt;/span&gt;&lt;span&gt;g&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; add&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-component-name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; list&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Added&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; list&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; URN:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;     urn:component:b6420554-62b5-4902-8994-89c692a937f7&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; name:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    list&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; version:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; size:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;    28.46&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; KiB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Created&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; at:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;        2025-01-03&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; 15:09:09.743733&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; UTC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Exports:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  demo:lst-interface/api.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;(c:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; record&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; id:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; u64&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; },&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; value:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string&lt;/span&gt;&lt;span&gt;) -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;_, string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  demo:lst-interface/api.{archive}&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  demo:lst-interface/api.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;(email:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; tuple&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;record &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; id:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; u64&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; },&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;strin&lt;/span&gt;&lt;span&gt;g&lt;/span&gt;&lt;span&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  demo:lst-interface/api.{connected-editors}&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  demo:lst-interface/api.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;(c:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; record&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; id:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; u64&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; },&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; value:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;_, string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  demo:lst-interface/api.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;(c:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; record&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; id:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; u64&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; }&lt;/span&gt;&lt;span&gt;) -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;_, string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  demo:lst-interface/api.{get}&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  demo:lst-interface/api.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;(c:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; record&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; id:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; u64&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; },&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; after:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; value:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;_, string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  demo:lst-interface/api.{is-archived}&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  demo:lst-interface/api.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;(c:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; record&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; id:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; u64&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; }&lt;/span&gt;&lt;span&gt;) -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;variant &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; added(string&lt;/span&gt;&lt;span&gt;), deleted(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;string&lt;/span&gt;&lt;span&gt;), inserted(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;record&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; after:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; value:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; string&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; }&lt;/span&gt;&lt;span&gt;) &lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;, string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  demo:lst-interface/email-query.{deadline}&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;u&lt;/span&gt;&lt;span&gt;64&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  demo:lst-interface/email-query.{recipients}&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can try out the &lt;code&gt;archive&lt;/code&gt; component by first invoking the &lt;code&gt;store&lt;/code&gt; function, and then the &lt;code&gt;get-all&lt;/code&gt; function, using the CLI&#39;s &lt;code&gt;worker invoke-and-await&lt;/code&gt; command:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; invoke-and-await&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; urn:worker:bde2da89-75a8-4adf-953f-33b360c978d0/archive&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-function&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo:archive-interface/api.{store}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-arg&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;list1&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-arg&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;[&amp;quot;x&amp;quot;, &amp;quot;y&amp;quot;, &amp;quot;z&amp;quot;]&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Empty&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; result.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; invoke-and-await&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; urn:worker:bde2da89-75a8-4adf-953f-33b360c978d0/archive&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-function&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo:archive-interface/api.{get-all}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Invocation&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; results&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; WAVE&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; format:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;[{name: &amp;quot;list1&amp;quot;, items: [&amp;quot;x&amp;quot;, &amp;quot;y&amp;quot;, &amp;quot;z&amp;quot;]}]&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Similarly we can try out the &lt;code&gt;list&lt;/code&gt; component, keeping in mind that the &lt;strong&gt;worker name&lt;/strong&gt; is the list name:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When we try out list, we get an error (and if we used the &lt;code&gt;debug&lt;/code&gt; profile - using &lt;code&gt;--build-profile debug&lt;/code&gt; then we also get a nice call stack):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Failed to create worker b6420554-62b5-4902-8994-89c692a937f7/list6: Failed to instantiate worker -1/b6420554-62b5-4902-8994-89c692a937f7/list6: error while executing at wasm backtrace:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    0: 0x19526 - wit-component:shim!indirect-wasi:clocks/wall-clock@0.2.0-now&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    1: 0x414b - &amp;lt;unknown&amp;gt;!demo/lst/interface/wasi/clocks/wallClock.wasmImportNow&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    2: 0x4165 - &amp;lt;unknown&amp;gt;!demo/lst/interface/wasi/clocks/wallClock.now&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    3: 0x42c1 - &amp;lt;unknown&amp;gt;!demo/lst/src.now&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    4: 0x433d - &amp;lt;unknown&amp;gt;!@demo/lst/src.State::update_email_properties&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    5: 0x440e - &amp;lt;unknown&amp;gt;!@demo/lst/src.State::new&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    6: 0x5d81 - &amp;lt;unknown&amp;gt;!*init*/38&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The reason is we are creating a global variable of &lt;code&gt;State&lt;/code&gt; and in its constructor we are tryting to call a WASI function (to get the current date-time). This is too early for that; so let&#39;s modify the &lt;code&gt;State::new&lt;/code&gt; method to not call any host functions:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Creates a new empty document editing state&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; state&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    document&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Document&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    connected&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Map&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    last_connection_id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ConnectionId&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    archived&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; false&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    email_deadline&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @datetime&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DateTime&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;from_unix_mseconds&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; // Note: can&amp;#39;t use now() here because it will run in initialization-time (due to the global `state` variable)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    email_recipients&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  state&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This fixes the issue! Now we can create and play with our collaboratively editable lists:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; start&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; urn:component:b6420554-62b5-4902-8994-89c692a937f7&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-worker-name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; list7&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Added&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; list7&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; URN:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    urn:worker:b6420554-62b5-4902-8994-89c692a937f7/list7&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; URN:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; urn:component:b6420554-62b5-4902-8994-89c692a937f7&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; name:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;   list7&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; invoke-and-await&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; urn:component:b6420554-62b5-4902-8994-89c692a937f7&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-worker-name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; list7&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-function&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo:lst-interface/api.{connect}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-arg&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;demo@vigoo.dev&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Invocation&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; results&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; WAVE&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; format:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;({id: 1}, [])&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; invoke-and-await&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; urn:component:b6420554-62b5-4902-8994-89c692a937f7&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-worker-name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; list7&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-function&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo:lst-interface/api.{add}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-arg&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{ id: 1}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-arg&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;a&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Invocation&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; results&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; WAVE&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; format:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ok&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; invoke-and-await&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; urn:component:b6420554-62b5-4902-8994-89c692a937f7&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-worker-name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; list7&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-function&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo:lst-interface/api.{add}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-arg&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{ id: 1}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-arg&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;b&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Invocation&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; results&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; WAVE&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; format:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ok&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; invoke-and-await&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; urn:component:b6420554-62b5-4902-8994-89c692a937f7&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-worker-name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; list7&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-function&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo:lst-interface/api.{connect}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-arg&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;demo2@vigoo.dev&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Invocation&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; results&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; WAVE&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; format:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;({id: 2}, [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;])&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;worker-to-worker-communication&quot;&gt;Worker to Worker communication&lt;/h2&gt;
&lt;h3 id=&quot;list-calling-archive&quot;&gt;List calling archive&lt;/h3&gt;
&lt;p&gt;The first worker-to-worker communication we want to set up is the &lt;code&gt;list&lt;/code&gt; component calling the &lt;code&gt;archive&lt;/code&gt; component - basically, when we call &lt;code&gt;archive()&lt;/code&gt; on the list, it needs to call &lt;code&gt;store&lt;/code&gt; in a singleton archive worker, sending its data to it.&lt;/p&gt;
&lt;p&gt;The first step is to simply state this dependency in the app manifest of &lt;code&gt;list&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;omponents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ist&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;emplate&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;oonbit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ependencies&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ist&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ype&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;asm-rpc&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;arget&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;rchive&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Running &lt;code&gt;golem app build&lt;/code&gt; after this will run a lot of new build steps - including generating and compiling some Rust source code, which is something that will no longer be needed in the next release of Golem.&lt;/p&gt;
&lt;p&gt;We are not going into details of what is generated for worker to worker communication in this post - what is important is that after this change, and running build once, we can &lt;strong&gt;import&lt;/strong&gt; a generated &lt;strong&gt;stub&lt;/strong&gt; of our &lt;code&gt;archive&lt;/code&gt; component in our &lt;code&gt;list&lt;/code&gt; component&#39;s moonbit package:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;json&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;suiyunonghen/datetime&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo/lst/interface/wasi/clocks/wallClock&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;wallClock&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo/lst/interface/demo/archive_stub/stubArchive&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;stubArchive&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo/lst/interface/golem/rpc/types&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;rpcTypes&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we can add the following code into our &lt;code&gt;archive&lt;/code&gt; function to call the remote worker:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; archive_component_id&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;bde2da89-75a8-4adf-953f-33b360c978d0&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; // TODO&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; archive&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @stubArchive&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Api&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;api&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;urn:worker:&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;archive_component_id&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/archive&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;TODO&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; // TODO&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  archive&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;blocking_store&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;document&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;iter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_array&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In line 2 we construct the remote interface by pointing to a specific &lt;strong&gt;worker&lt;/strong&gt;, by using the component ID and the worker&#39;s name. (In the next Golem release this is going to be simplified by being able to use the component&#39;s name instead). In line 5 we call the remote &lt;code&gt;store&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;What is missing are two things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We should not hard-code the archive component&#39;s ID as it is automatically generated when the component is first uploaded to Golem&lt;/li&gt;
&lt;li&gt;We need to know our own &lt;strong&gt;worker name&lt;/strong&gt; to be used as the list&#39;s name&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The solution to both is to use &lt;strong&gt;environment variables&lt;/strong&gt; - Golem automatically sets the &lt;code&gt;GOLEM_WORKER_NAME&lt;/code&gt; environment variable to the worker&#39;s name, and we can manually provide values to workers through custom environment variables. This allows us to inject the component ID from the outside (until a more sophisticated configuration feature is added in Golem 1.2).&lt;/p&gt;
&lt;p&gt;We have already seen how we can use WASI to query the current date/time; we can use another WASI interface to get environment variables. So once again, we add an import to our WIT file:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;wit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  import&lt;/span&gt;&lt;span&gt; wasi:cli/environment@0.2.0;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then run &lt;code&gt;golem app build&lt;/code&gt; to regenerate the bindings, and import it in the &lt;code&gt;list/src&lt;/code&gt; MoonBit package:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;json&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo/lst/interface/wasi/cli/environment&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;environment&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and implement a helper function to get a specific key from the environment variables:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Gets an environment variable using WASI&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_env&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;key&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String?&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;  @environment&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get_environment&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;iter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;find_first&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;pair&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    pair&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;.0&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; key&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;pair&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    pair&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;.1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can use this to get the worker&#39;s name and the archive component ID:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; archive_component_id&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_env&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ARCHIVE_COMPONENT_ID&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;or&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;unknown&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;// ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_env&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;GOLEM_WORKER_NAME&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;or&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;unknown&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When starting the list workers, we have to explicitly specify &lt;code&gt;ARCHIVE_COMPONENT_ID&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; start&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; urn:component:b6420554-62b5-4902-8994-89c692a937f7&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-worker-name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; list10&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-env&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ARCHIVE_COMPONENT_ID=bde2da89-75a8-4adf-953f-33b360c978d0&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With that we can try connecting to the list, adding some items and then calling &lt;code&gt;archive&lt;/code&gt; on it, and finally calling &lt;code&gt;get-all&lt;/code&gt; on the archive worker - we can see that the remote procedure call works!&lt;/p&gt;
&lt;h3 id=&quot;list-and-email&quot;&gt;List and email&lt;/h3&gt;
&lt;p&gt;We haven&#39;t implemented the third component of the application yet - the one responsible for sending an email after some deadline. Setting up the component and the worker-to-worker communication works exactly the same as it was demonstrated above. The app manifest supports circular dependencies, so we can add say that &lt;code&gt;list&lt;/code&gt; depends on &lt;code&gt;email&lt;/code&gt; via &lt;code&gt;wasm-rpc&lt;/code&gt;, and also &lt;code&gt;email&lt;/code&gt; depends on &lt;code&gt;list&lt;/code&gt; via &lt;code&gt;wasm-rpc&lt;/code&gt;. We need to communicate in both directions.&lt;/p&gt;
&lt;p&gt;We will have to use the WASI &lt;code&gt;monotonic-clock&lt;/code&gt; interface&#39;s &lt;code&gt;subscribe-instant&lt;/code&gt; function to &lt;strong&gt;sleep&lt;/strong&gt; until the given deadline.&lt;/p&gt;
&lt;p&gt;Without showing all the details, here is the MoonBit code implementing the single &lt;code&gt;send-email&lt;/code&gt; function we defined in the &lt;code&gt;email.wit&lt;/code&gt; file:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Structure holding an email sender&amp;#39;s configuration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;all&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Email&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  list_worker_urn&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Run the email sending loop&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Email&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  while&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    match&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get_deadline&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;epoch_ms&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; now&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @wallClock&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;now&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; now_ms&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; now&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;seconds&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1000&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;now&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nanoseconds&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;reinterpret_as_int&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1000000&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_uint64&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; duration_ms&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; epoch_ms&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;reinterpret_as_int64&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; -&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;          now_ms&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;reinterpret_as_int64&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; duration_ms&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;          sleep&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;duration_ms&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;reinterpret_as_uint64&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;          send_emails&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get_recipients&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        continue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      None&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; break&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We use the &lt;code&gt;wall-clock&lt;/code&gt; interface again to query the current time and calculate the duration to sleep for based on the deadline got from the corresponding list worker. The &lt;code&gt;get_deadline&lt;/code&gt; and &lt;code&gt;get_recipients&lt;/code&gt; methods are just using Golem&#39;s Worker to Worker communication as shown before:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Get the current deadline from the associated list worker&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_deadline&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Email&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UInt64?&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; api&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @stubLst&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;EmailQuery&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;email_query&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;list_worker_urn&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  api&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;blocking_deadline&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Get the current list of recipients from the associated list worker&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_recipients&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Email&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Array&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; api&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @stubLst&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;EmailQuery&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;email_query&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;list_worker_urn&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  api&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;blocking_recipients&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The two remaining interesting part are sleeping and sending emails.&lt;/p&gt;
&lt;p&gt;We can &lt;strong&gt;sleep&lt;/strong&gt; by calling the &lt;code&gt;subscribe-duration&lt;/code&gt; function in the WASI &lt;code&gt;monotonic-clock&lt;/code&gt; package to get a pollable, and then poll for it. As we only pass a single pollable to the list, it won&#39;t return until the deadline we want to wait for expires:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Sleep for the given amount of milliseconds&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sleep&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ms&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UInt64&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ns&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ms&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1000000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; pollable&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @monotonicClock&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;subscribe_duration&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ns&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; _&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @poll&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;poll&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;pollable&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On the &lt;code&gt;list&lt;/code&gt; side, we don&#39;t want to block until this email sending loop runs - as it would block our list from receiving new requests. The generated RPC stubs support this, we simply use the non-blocking version on the generated &lt;code&gt;Api&lt;/code&gt; type:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  if&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; not&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;email_worker_started&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email_component_id&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_env&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;EMAIL_COMPONENT_ID&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;or&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;unknown&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_env&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;GOLEM_WORKER_NAME&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;or&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;unknown&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self_component_id&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_env&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;GOLEM_COMPONENT_ID&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;or&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;unknown&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; api&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @stubEmail&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Api&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;api&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;urn:worker:&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;email_component_id&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    api&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;send_email&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;urn:worker:&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;self_component_id&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;email_worker_started&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;  =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;sending-emails&quot;&gt;Sending emails&lt;/h2&gt;
&lt;p&gt;Sending actual emails is a bit more difficult, as there are no HTTP client libraries in the MoonBit ecosystem at the moment. But Golem implements the WASI HTTP interface, so we can use the already demonstrated techniques to import WASI HTTP through WIT, generate bindings for it, and then use it from MoonBit code to send emails through a third party provider.&lt;/p&gt;
&lt;p&gt;In the example we are going to use &lt;a rel=&quot;external&quot; href=&quot;https://sendgrid.com/en-us&quot;&gt;Sendgrid&lt;/a&gt; as a provider. This means we have to send a HTTP &lt;strong&gt;POST&lt;/strong&gt; request to &lt;code&gt;https://api.sendgrid.com/v3/mail/send&lt;/code&gt; with a pre-configured authorization header, and a JSON body describing our email sending request.&lt;/p&gt;
&lt;p&gt;First we are going to define a few helper constants and functions to assemble the parts of the requests:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AUTHORITY&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;api.sendgrid.com&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PATH&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/v3/mail/send&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HttpClientError&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The payload is a JSON, which can be constructed using MoonBit&#39;s built-in JSON literal feature. However in the WASI HTTP interface we have to write it out as a byte array. MoonBit strings are UTF-16 but SendGrid requires the payload to be in UTF-8. Unfortunately there isn&#39;t any string encoding library available for MoonBit yet, so we write a simple function that fails if any of the characters is not ASCII:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Converts a string to ASCII byte array if all characters are ASCII characters, otherwise fails&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; string_to_ascii&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  what&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  value&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; FixedArray&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;HttpClientError&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; FixedArray&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;makei&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;length&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;_&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  for&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; i&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ch&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    if&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ch&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_int&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 256&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      result&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;i&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ch&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_int&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_byte&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      raise&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HttpClientError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;The &lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;what&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; contains non-ASCII characters&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this we can construct the payload and we can also read the sendgrid API key from an environment variable:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Constructs a SendGrid send message payload as an ASCII byte array&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; payload&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;recipients&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Array&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; FixedArray&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;HttpClientError&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email_addresses&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; recipients&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;iter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;email&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_array&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_json&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; from&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Json&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo@vigoo.dev&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Daniel Vigovszky&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; json&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Json&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;personalizations&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email_addresses&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;cc&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;bcc&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; from&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;subject&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Collaborative list editor warning&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;text/html&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;lt;p&amp;gt;The list opened for editing has not been changed in the last 12 hours&amp;lt;/p&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; json_str&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; json&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  string_to_ascii&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;constructed JSON body&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; json_str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///| Gets the SENDGRID_API_KEY environment variable as an  ASCII byte array&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; authorization_header&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; FixedArray&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;HttpClientError&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; key_str&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @environment&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get_environment&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;iter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;find_first&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;pair&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; pair&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;.0&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;SENDGRID_API_KEY&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;pair&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; pair&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;.1&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;unwrap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  string_to_ascii&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;provided authorization header via SENDGRID_API_KEY&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; key_str&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The next step is to create the data structures for sending out the HTTP request. In WASI HTTP, outgoing requests are modeled as WIT &lt;strong&gt;resources&lt;/strong&gt;, which means we have to construct them with a constructor and call various methods to set properties of the request. All these methods have a &lt;code&gt;Result&lt;/code&gt; result type so our code is going to be quite verbose:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; headers&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @httpTypes&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Fields&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;fields&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  headers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;append&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Authorization&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; authorization_header&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map_err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;error&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    HttpClientError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Failed to set Authorization header: &lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;unwrap_or_error&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; request&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @httpTypes&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OutgoingRequest&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;outgoing_request&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;headers&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  request&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;set_authority&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AUTHORITY&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map_err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;_&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HttpClientError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Failed to set request authority&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;unwrap_or_error&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  request&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;set_method&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;@httpTypes&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Method&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Post&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map_err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;_&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HttpClientError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Failed to set request method&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;unwrap_or_error&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  request&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;set_path_with_query&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PATH&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map_err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;_&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HttpClientError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Failed to set request path&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;unwrap_or_error&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  request&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;set_scheme&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;@httpTypes&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Scheme&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Https&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map_err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;_&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HttpClientError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Failed to set request scheme&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;unwrap_or_error&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; outgoing_body&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; request&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;body&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map_err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;_&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HttpClientError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Failed to get the outgoing body&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;unwrap_or_error&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; stream&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; outgoing_body&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;write&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map_err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;_&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      HttpClientError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Failed to open the outgoing body stream&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;unwrap_or_error&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; _&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; stream&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;blocking_write_and_flush&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;payload&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;recipients&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map_err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;error&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      HttpClientError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Failed to write request body: &lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;unwrap_or_error&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; _&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; outgoing_body&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;finish&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;None&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map_err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;_&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HttpClientError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Failed to close the outgoing body&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;unwrap_or_error&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At this point we have our &lt;code&gt;request&lt;/code&gt; variable initialized with everything we need, so we can call the &lt;code&gt;handle&lt;/code&gt; function to initiate the HTTP request:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; future_incoming_response&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @outgoingHandler&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;handle&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;request&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map_err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;error&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HttpClientError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Failed to send request: &lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;unwrap_or_error&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Sending a request is an async operation and what we have a result here is just a handle for a future value we have to await somehow. As we don&#39;t want to do anything else in parallel in this example, we just write a loop that awaits for the result and checks for errors:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  while&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    match&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; future_incoming_response&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;response&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; status&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; response&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;status&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; status&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 200&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; status&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 300&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;          break&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;          raise&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HttpClientError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Http request returned with status &lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;code&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        raise&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HttpClientError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Http request failed with &lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;_&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; raise&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HttpClientError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Http request failed&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      None&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; pollable&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; future_incoming_response&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;subscribe&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; _&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @poll&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;poll&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;pollable&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We are ignoring the response body in this example - but in other applications, &lt;code&gt;response&lt;/code&gt; could be used to open an incoming body stream and read chunks from it.&lt;/p&gt;
&lt;p&gt;With this we implemented the simplest possible way to call the SendGrid API for sending an e-mail using WASI HTTP provided by Golem.&lt;/p&gt;
&lt;h2 id=&quot;debugging&quot;&gt;Debugging&lt;/h2&gt;
&lt;p&gt;When compiled to debug (using &lt;code&gt;golem app build --build-profile debug&lt;/code&gt;), Golem shows a nice stack trace when something goes wrong in a MoonBit component. Another useful way to observe a worker is to write a &lt;strong&gt;log&lt;/strong&gt; in it, which can be realtime watched (or queried later) using tools like &lt;code&gt;golem worker connect&lt;/code&gt; or the Golem Console.&lt;/p&gt;
&lt;p&gt;The best way to write logs from MoonBit is to use the WASI Logging interface. We can import it as usual in our WITs:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;wit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;import&lt;/span&gt;&lt;span&gt; wasi:logging/logging;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and then to our MoonBit packages:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;json&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;demo/archive/interface/wasi/logging/logging&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and then write out log messages of various levels from our application logic:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;moonbit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; recipients&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get_recipients&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;@logging&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;@logging&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Level&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;INFO&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Sending emails to recipients: &lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;recipients&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;match&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; send_emails&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;?&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;recipients&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;_&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @logging&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;@logging&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Level&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;INFO&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Sending emails succeeded&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;error&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; @logging&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;log&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;@logging&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Level&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERROR&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Failed to send emails: &lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;MoonBit is a nice new language that is quite powerful and expressive, and seems to be a very good fit for developing applications for Golem. The resulting WASM binaries are very small - a few tens of kilobytes for this application (only increased by the generated Rust stubs - but those are going away soon). A few things in the language felt a little bit inconvenient - but maybe it is just a matter of personal taste - mostly the JSON files describing MoonBit packages, the anonymous function syntax and the way the built-in formatter organizes things. I&#39;m sure some of these, especially the tooling, will greatly improve in the future.&lt;/p&gt;
&lt;p&gt;The support for WASM and the Component Model are still in an early stage - but working. It requires many manual steps, but fortunately Golem&#39;s app manifest feature can automate most of this for us. Still the generated directory structure of &lt;code&gt;wit-bindgen moonbit&lt;/code&gt; felt a little overwhelming first.&lt;/p&gt;
&lt;p&gt;I hope the MoonBit ecosystem will get some useful libraries in the near future, convenient wappers for WASI and WASI HTTP, (and Golem specific ones!), string encoding utilities, etc. As there are not many libraries yet, it is very easy to find something useful to work on.&lt;/p&gt;
&lt;p&gt;I&#39;m looking forward to have official support for MoonBit in Golem, such as templates for the &lt;code&gt;golem new ...&lt;/code&gt; command and extensive documentation on our website.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>[Video] Golem and the WASM Component Model @ LambdaConf 2024</title>
        <published>2024-06-16T00:00:00+00:00</published>
        <updated>2024-06-16T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem-and-the-wasm-component-model/"/>
        <id>https://blog.vigoo.dev/posts/golem-and-the-wasm-component-model/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem-and-the-wasm-component-model/">&lt;p&gt;My talk at &lt;a rel=&quot;external&quot; href=&quot;https://www.lambdaconf.us&quot;&gt;LambdaConf 2024&lt;/a&gt; explaining how &lt;a rel=&quot;external&quot; href=&quot;https://golem.cloud&quot;&gt;Golem&lt;/a&gt; takes advantage of the WebAssembly Component Model.&lt;/p&gt;
&lt;iframe width=&quot;800&quot; height=&quot;450&quot; src=&quot;https://www.youtube.com/embed/g5uUQSByvI4?si=lxlQgFztHp94WjrU&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share&quot; referrerpolicy=&quot;strict-origin-when-cross-origin&quot; allowfullscreen&gt;&lt;/iframe&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Zig and the WASM Component Model</title>
        <published>2024-05-09T00:00:00+00:00</published>
        <updated>2024-05-09T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/zig-wasm-component-model/"/>
        <id>https://blog.vigoo.dev/posts/zig-wasm-component-model/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/zig-wasm-component-model/">&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://golem.cloud&quot;&gt;Golem&lt;/a&gt; always considered &lt;a rel=&quot;external&quot; href=&quot;https://ziglang.org&quot;&gt;Zig&lt;/a&gt; a supported language, but until now the only documented way to use it was to compile a program with a single &lt;code&gt;main&lt;/code&gt; function into a &lt;em&gt;core WebAssembly module&lt;/em&gt; and then wrap that as a component that can be uploaded to Golem for execution. This is very limiting, as in order to take full advantage of Golem (and any other part of the evolving &lt;em&gt;WASM Component Model ecosystem&lt;/em&gt;) a Zig program must have definitions for both &lt;em&gt;importing&lt;/em&gt; and &lt;em&gt;exporting&lt;/em&gt; functions and data types in order to be a usable component.&lt;/p&gt;
&lt;h2 id=&quot;binding-generators&quot;&gt;Binding generators&lt;/h2&gt;
&lt;p&gt;For many supported languages the workflow is to write a &lt;strong&gt;WIT&lt;/strong&gt; file, which is the Component Model&#39;s &lt;a rel=&quot;external&quot; href=&quot;https://component-model.bytecodealliance.org/design/wit.html&quot;&gt;interface definition language&lt;/a&gt; and then use a &lt;em&gt;binding generator&lt;/em&gt;, such as &lt;a rel=&quot;external&quot; href=&quot;https://github.com/bytecodealliance/wit-bindgen/&quot;&gt;wit-bindgen&lt;/a&gt; to create statically typed representation of the component&#39;s imports and exports in the targeted language.&lt;/p&gt;
&lt;p&gt;The binding generator does not support Zig, but it does support C. So the best we can do with existing tooling is to use the C binding generator and Zig&#39;s excellent C interoperability together to be able to create WASM components with Zig.&lt;/p&gt;
&lt;h2 id=&quot;the-steps&quot;&gt;The steps&lt;/h2&gt;
&lt;p&gt;The primary steps are the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Define&lt;/strong&gt; the component&#39;s interface using WIT&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Generate&lt;/strong&gt; C bindings from this definition&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Implement&lt;/strong&gt; the exported functions in Zig, potentially using other imported interfaces and data types available through the generated binding&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compile&lt;/strong&gt; the whole project into WASM&lt;/li&gt;
&lt;li&gt;As Zig&#39;s standard library still uses &lt;em&gt;WASI Preview 1&lt;/em&gt;, and outputs a single WASM module, we also have to &lt;strong&gt;compose&lt;/strong&gt; our resulting module with an &lt;em&gt;adapter component&lt;/em&gt; in order to get a WASM component depending on &lt;em&gt;WASI Preview 2&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The first step is manual work - although we may eventually get code-first approaches in some languages where the WIT interface is generated as part as the build flow, it is not the case for Zig at the moment.&lt;/p&gt;
&lt;p&gt;For generating the bindings we use &lt;code&gt;wit-bindgen&lt;/code&gt;, and once the implementation is done we compile the Zig source code, together with the generated C bindings into a WASM module using zig&#39;s build system (&lt;code&gt;zig build&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Finally we can use &lt;code&gt;wasm-tools compose&lt;/code&gt; to take this WASM module and an appropriate version of a Preview1 adapter such as &lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/golem-wit/blob/main/adapters/tier1/wasi_snapshot_preview1.wasm&quot;&gt;the one we provide for Golem&lt;/a&gt; to get the final component that&#39;s ready to be used with Golem.&lt;/p&gt;
&lt;h2 id=&quot;zig-s-build-system&quot;&gt;Zig&#39;s build system&lt;/h2&gt;
&lt;p&gt;Executing all these steps manually is not convenient but fortunately we can integrate all the steps within Zig&#39;s &lt;em&gt;build system&lt;/em&gt;. Let&#39;s see how!&lt;/p&gt;
&lt;p&gt;We need to write a custom &lt;code&gt;build.zig&lt;/code&gt; in the following way. First, let&#39;s do some imports and start defining our build flow:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; @import&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; Builder&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; std&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;build&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Builder&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; CrossTarget&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; std&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;zig&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;CrossTarget&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; build&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;b&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Builder&lt;/span&gt;&lt;span&gt;) &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;void&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first non-manual thing on our list of steps is &lt;strong&gt;generating&lt;/strong&gt; the C bindings. Let&#39;s define a build step that just runs &lt;code&gt;wit-bindgen&lt;/code&gt; for us:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; bindgen&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; b&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;addSystemCommand&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;{ &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;wit-bindgen&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;--autodrop-borrows&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;yes&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    	&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;./wit&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;--out-dir&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;src/bindings&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; })&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is just a description of running the binding generator, not integrated within the build flow yet. The next step is &lt;strong&gt;compiling&lt;/strong&gt; our Zig and C files into WASM.&lt;/p&gt;
&lt;p&gt;First we define it as an &lt;em&gt;executable target&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; optimize&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; b&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;standardOptimizeOption&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;preferred_optimize_mode&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ReleaseSmall&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    })&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; wasm&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; b&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;addExecutable&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;{ &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    	.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;root_source_file&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; .&lt;/span&gt;&lt;span&gt;{ &lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;src/main.zig&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; .&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;cpu_arch&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;wasm32&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;os_tag&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;wasi&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    	}&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    	.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;optimize&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; optimize&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    })&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This already defines we want to use WASM and target WASI and points to our root source file. We are not done yet though, as if we run the binding generator step defined above, we will end up having a couple of files generated in our &lt;code&gt;src/bindings&lt;/code&gt; directory:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;λ l src/bindings&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;.rw-r--r-- 909 vigoo  9 May 09:34 zig3.c&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;.rw-r--r-- 371 vigoo  9 May 09:34 zig3.h&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;.rw-r--r-- 299 vigoo  9 May 09:34 zig3_component_type.o&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;.c&lt;/code&gt;/&lt;code&gt;.h&lt;/code&gt; pair contains the generated binding, while the object file holds the binary representation of the WIT interface it was generated from.&lt;/p&gt;
&lt;p&gt;We need to add the C source and the object file into our build, and the header file to the include file paths. As the name of the generated files depend on the WIT file&#39;s contents, we need to list all files in this &lt;code&gt;bindings&lt;/code&gt; directory and mutate our &lt;code&gt;wasm&lt;/code&gt; build target according to what we find:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; binding_root&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; b&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;pathFromRoot&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;src/bindings&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; binding_root_dir&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; try&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; std&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;fs&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;cwd&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;openIterableDir&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;binding_root&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; .&lt;/span&gt;&lt;span&gt;{})&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    defer&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; binding_root_dir&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;close&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; it&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; try&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; binding_root_dir&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;walk&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;b&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;allocator&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; it&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;next&lt;/span&gt;&lt;span&gt;()) &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;entry&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        switch&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;entry&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;kind&lt;/span&gt;&lt;span&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                const&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; path&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; b&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;pathJoin&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;{ &lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;binding_root&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; entry&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;path&lt;/span&gt;&lt;span&gt; })&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;std&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;mem&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;endsWith&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;u8&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; entry&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;basename&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                    wasm&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;addCSourceFile&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;{ &lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; .&lt;/span&gt;&lt;span&gt;{ &lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; path&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; .&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;{} })&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                } &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;std&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;mem&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;endsWith&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;u8&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; entry&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;basename&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                    wasm&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;addObjectFile&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;{ &lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; path&lt;/span&gt;&lt;span&gt; })&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            else&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; continue&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This registers all the &lt;code&gt;.c&lt;/code&gt; and &lt;code&gt;.o&lt;/code&gt; files from the generated bindings, but we still need to add the whole directory as an include path:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    wasm&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;addIncludePath&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;{ &lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; binding_root&lt;/span&gt;&lt;span&gt; })&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and enable linking with &lt;code&gt;libc&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    wasm&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;linkLibC&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now that we defined two build steps - the generating the bindings and compiling to a WASM module - we define the third step which is &lt;strong&gt;composing&lt;/strong&gt; the generated module and the preview1 adapter into a WASM component:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; adapter&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; b&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;option&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    	[]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; u8&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    	&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;adapter&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    	&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Path to the Golem Tier1 WASI adapter&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;) &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;orelse&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;adapters/tier1/wasi_snapshot_preview1.wasm&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; out&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; try&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; std&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;fmt&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;allocPrint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;b&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;allocator&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;zig-out/bin/{s}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; .&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;wasm&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;out_filename&lt;/span&gt;&lt;span&gt;})&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; b&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;addSystemCommand&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;{ &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;wasm-tools&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    	&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;zig-out/bin/component.wasm&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;--adapt&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; adapter&lt;/span&gt;&lt;span&gt; })&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we provide a way to override the path to the adapter WASM using &lt;code&gt;zig build -Dadapter=xxx&lt;/code&gt; but default to &lt;code&gt;adapters/tier1/wasi_snapshot_preview1.wasm&lt;/code&gt; in case it is not specified.&lt;/p&gt;
&lt;p&gt;The final step is to set up dependencies between these build steps and wire them to the main build flow:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    wasm&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;step&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;dependOn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bindgen&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;step&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    component&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;step&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;dependOn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;wasm&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;step&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    b&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;installArtifact&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;wasm&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    b&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;getInstallStep&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;dependOn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;component&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;step&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;trying-it-out&quot;&gt;Trying it out&lt;/h2&gt;
&lt;p&gt;Let&#39;s try this out by implementing a simple counter component. We start with the first step - defining our WIT file, putting it into &lt;code&gt;wit/counter.wit&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;wit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; golem&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; api&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  add&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u64&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  get&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u64&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;world&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; counter&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  export&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; api&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We also save the above defined build script as &lt;code&gt;build.zig&lt;/code&gt; (full version &lt;a rel=&quot;external&quot; href=&quot;https://gist.github.com/vigoo/19ed4b5d3e47ca2f5f1258d1ae8b28a4&quot;&gt;available here&lt;/a&gt;) and then write an initial  &lt;code&gt;src/main.zig&lt;/code&gt; file:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; @import&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; main&lt;/span&gt;&lt;span&gt;() &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;anyerror&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;void&lt;/span&gt;&lt;span&gt; {}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&#39;s place the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/golem-wit/raw/main/adapters/tier1/wasi_snapshot_preview1.wasm&quot;&gt;adapter WASM&lt;/a&gt; as well in the &lt;code&gt;adapters/tier1&lt;/code&gt; directory, and then try to compile this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;λ zig build --summary all                                                                                 ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;zig build-exe main Debug wasm32-wasi: error: the following command failed with 2 compilation errors:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;error: wasm-ld: /Users/vigoo/projects/demo/counter/zig-cache/o/a212123ad3dcf4839747c2bd77f7ef4e/counter.o:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;undefined symbol: exports_golem_example_api_add&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;error: wasm-ld: /Users/vigoo/projects/demo/counter/zig-cache/o/a212123ad3dcf4839747c2bd77f7ef4e/counter.o:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;undefined symbol: exports_golem_example_api_get&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It fails because we defined two exported functions: &lt;code&gt;api/add&lt;/code&gt; and &lt;code&gt;api/get&lt;/code&gt; in our WIT file but haven&#39;t implemented them yet. Let&#39;s do that:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; state&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; u64&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; exports_golem_example_api_add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; u64&lt;/span&gt;&lt;span&gt;) &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;void&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; stdout&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; std&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;io&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;getStdOut&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;writer&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    stdout&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;print&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Adding {} to state&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; .&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span&gt;}) &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;catch&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; unreachable&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    state&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; +=&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; exports_golem_example_api_get&lt;/span&gt;&lt;span&gt;() &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;u64&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; state&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then compile it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;λ zig build --summary all&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Generating &amp;quot;src/bindings/counter.c&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Generating &amp;quot;src/bindings/counter.h&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Generating &amp;quot;src/bindings/counter_component_type.o&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Build Summary: 5/5 steps succeeded&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;install success&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;├─ install main cached&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;│  └─ zig build-exe main Debug wasm32-wasi cached 9ms MaxRSS:29M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;│     └─ run wit-bindgen success 3ms MaxRSS:3M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;└─ run wasm-tools success 11ms MaxRSS:8M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   └─ zig build-exe main Debug wasm32-wasi (+1 more reused dependencies)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and we can verify our resulting &lt;code&gt;zig-out/component.wasm&lt;/code&gt; using &lt;code&gt;wasm-tools&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;λ wasm-tools print --skeleton zig-out/bin/component.wasm &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;(component&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (instance (;11;) (instantiate 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      (with &amp;quot;import-func-add&amp;quot; (func 16))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      (with &amp;quot;import-func-get&amp;quot; (func 17))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (export (;12;) &amp;quot;golem:example/api&amp;quot; (instance 11))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (@producers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (processed-by &amp;quot;wit-component&amp;quot; &amp;quot;0.20.1&amp;quot;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;using-imports&quot;&gt;Using imports&lt;/h2&gt;
&lt;p&gt;After this simple example let&#39;s try &lt;em&gt;importing&lt;/em&gt; some interface and using that from our Zig code. What we are going to do is every time our counter changes, we are going to also save that value to an external key-value store. This is usually not something you need to do when writing a Golem application, because your program will be durable anyway - you can just keep the counter in memory. But it is a simple enough example to demonstrate how to use imported interfaces from Zig.&lt;/p&gt;
&lt;p&gt;First let&#39;s add some additional WIT files into &lt;code&gt;wit/deps&lt;/code&gt; from the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/golem-wit&quot;&gt;golem-wit repository&lt;/a&gt; (Note that the WASI Key-Value interface is defined &lt;a rel=&quot;external&quot; href=&quot;https://github.com/WebAssembly/wasi-keyvalue&quot;&gt;here&lt;/a&gt;, the &lt;code&gt;golem-wit&lt;/code&gt; repo just stores the exact version of its definitions which is currently implemented by Golem ).&lt;/p&gt;
&lt;p&gt;We need the following directory tree:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;λ tree wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;├── counter.wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;└── deps&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ├── io&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    │   ├── error.wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    │   ├── poll.wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    │   ├── streams.wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    │   └── world.wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    └── keyvalue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ├── atomic.wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ├── caching.wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ├── error.wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ├── eventual-batch.wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ├── eventual.wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ├── handle-watch.wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ├── types.wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        └── world.wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;4 directories, 13 files&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we can import the key-value interface to &lt;code&gt;counter.wit&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;wit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; golem&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; api&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  add&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u64&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  get&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u64&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;world&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; counter&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  import&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; wasi&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;keyvalue&lt;/span&gt;&lt;span&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;eventual&lt;/span&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0.1.0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  export&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; api&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By recompiling the project we can verify everything still works, and we will also get our new bindings generated in the C source.&lt;/p&gt;
&lt;p&gt;Before implementing writing to the key-value store in Zig, let&#39;s just take a look at the WIT interface of &lt;code&gt;wasi:keyvalue/eventual@0.1.0&lt;/code&gt; to understand what we will have to do:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;wit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; eventual&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  // ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  set&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;    bucket&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; borrow&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;bucket&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;    key&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; key&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;    outgoing-value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; borrow&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;outgoing-value&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;_&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We will need to pass a &lt;code&gt;bucket&lt;/code&gt; and an &lt;code&gt;outgoing-value&lt;/code&gt;, both being &lt;em&gt;WIT resources&lt;/em&gt; so we first need to create them, then borrow references of them for the &lt;code&gt;set&lt;/code&gt; call, and finally drop them.&lt;/p&gt;
&lt;p&gt;The bucket resource can be constructed with a static function called &lt;code&gt;open-bucket&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;wit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;resource&lt;/span&gt;&lt;span&gt; bucket&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  open-bucket:&lt;/span&gt;&lt;span&gt; static&lt;/span&gt;&lt;span&gt; func(name:&lt;/span&gt;&lt;span&gt; string)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; result&amp;lt;bucket,&lt;/span&gt;&lt;span&gt; error&amp;gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Searching for this in the generated C bindings reveals the following:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;extern&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; bool&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; wasi_keyvalue_types_static_bucket_open_bucket&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;  counter_string_t&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;  wasi_keyvalue_types_own_bucket_t&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;ret&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;  wasi_keyvalue_types_own_error_t&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;err&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We will have to drop the created bucket with&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;extern&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; void&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; wasi_keyvalue_types_bucket_drop_own&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;  wasi_keyvalue_types_own_bucket_t&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; handle&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With all this information let&#39;s try to open a bucket in Zig by directly using the generated C bindings. First we need to import the C headers:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; @cImport&lt;/span&gt;&lt;span&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;    @cDefine&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;_NO_CRT_STDIO_INLINE&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;    @cInclude&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;counter.h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;})&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We also define an initial error type for our function for using later:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; KVError&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; error&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    FailedToOpenBucket&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then start implementing the store function by first storing the bucket&#39;s name in &lt;code&gt;counter_string_t&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; record_state&lt;/span&gt;&lt;span&gt;() &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;anyerror&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;void&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; stdout&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; std&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;io&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;getStdOut&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;writer&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; bucket_name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;counter_string_t&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; undefined&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;counter_string_dup&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bucket_name&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    defer&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;counter_string_free&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bucket_name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and then invoking the &lt;code&gt;wasi_keyvalue_types_static_bucket_open_bucket&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; bucket&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;wasi_keyvalue_types_own_bucket_t&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; undefined&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; bucket_err&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;wasi_keyvalue_wasi_keyvalue_error_own_error_t&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; undefined&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;wasi_keyvalue_types_static_bucket_open_bucket&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bucket_name&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bucket&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bucket_err&lt;/span&gt;&lt;span&gt;)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        defer&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;wasi_keyvalue_types_bucket_drop_own&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bucket&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-7 z-d-i&quot;&gt; TODO&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    } &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        defer&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;wasi_keyvalue_wasi_keyvalue_error_error_drop_own&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bucket_err&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        try&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; stdout&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;print&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Failed to open bucket&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; .&lt;/span&gt;&lt;span&gt;{})&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; KVError&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;FailedToOpenBucket&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now that we have an open bucket we want to call the &lt;code&gt;set&lt;/code&gt; function to update a key&#39;s value:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;extern&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; bool&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; wasi_keyvalue_eventual_set&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;  wasi_keyvalue_eventual_borrow_bucket_t&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; bucket&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;  wasi_keyvalue_eventual_key_t&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;key&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;  wasi_keyvalue_eventual_borrow_outgoing_value_t&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; outgoing_value&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;  wasi_keyvalue_eventual_own_error_t&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;err&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We already have our bucket, but we &lt;em&gt;own&lt;/em&gt; it and we need to pass a &lt;em&gt;borrowed&lt;/em&gt; bucket to this function. What&#39;s the difference? There is no difference in the actual value - both just store a &lt;em&gt;handle&lt;/em&gt; to a resource that exists in the runtime engine, but we still have to borrow the owned value using the &lt;code&gt;wasi_keyvalue_types_borrow_bucket&lt;/code&gt; function. The &lt;code&gt;wasi_keyvalue_eventual_key_t&lt;/code&gt; type is just an alias for &lt;code&gt;counter_string_t&lt;/code&gt; and &lt;code&gt;wasi_keyvalue_eventual_borrow_outgoing_value_t&lt;/code&gt; is another resource we need to construct first. Let&#39;s put this together!&lt;/p&gt;
&lt;p&gt;First we borrow the owned bucket:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; borrowed_bucket&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;wasi_keyvalue_types_borrow_bucket&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bucket&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;defer&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;wasi_keyvalue_types_bucket_drop_borrow&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;borrowed_bucket&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we create an &lt;em&gt;outgoing value&lt;/em&gt; that&#39;s going to be stored in the key-value store:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; outgoing_value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;wasi_keyvalue_types_static_outgoing_value_new_outgoing_value&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;defer&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;wasi_keyvalue_types_outgoing_value_drop_own&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;outgoing_value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; borrowed_outgoing_value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;wasi_keyvalue_types_borrow_outgoing_value&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;outgoing_value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;defer&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;wasi_keyvalue_types_outgoing_value_drop_borrow&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;borrowed_outgoing_value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; body&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;counter_string_t&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; undefined&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; try&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; std&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;fmt&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;allocPrint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;gpa&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;allocator&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{d}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; .&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;state&lt;/span&gt;&lt;span&gt;})&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;counter_string_set&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;body&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; @ptrCast&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span&gt;))&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;defer&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;counter_string_free&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;body&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; write_err&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;wasi_keyvalue_types_own_error_t&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; undefined&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;wasi_keyvalue_types_method_outgoing_value_outgoing_value_write_body_sync&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    borrowed_outgoing_value&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;    @ptrCast&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;body&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-1&quot;&gt;    &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;bucket_err&lt;/span&gt;&lt;span&gt;)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    defer&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;wasi_keyvalue_wasi_keyvalue_error_error_drop_own&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;write_err&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    try&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; stdout&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;print&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Failed to set outgoing value&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; .&lt;/span&gt;&lt;span&gt;{})&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; KVError&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;FailedToSetKey&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Also we need to create a string for holding the &lt;em&gt;key&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; key&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;counter_string_t&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; undefined&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;counter_string_dup&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;key&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;latest&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;defer&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;counter_string_free&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;key&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And finally call the &lt;code&gt;set&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; set_err&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;wasi_keyvalue_eventual_own_error_t&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; undefined&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;wasi_keyvalue_eventual_set&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;borrowed_bucket&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;key&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; borrowed_outgoing_value&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;set_err&lt;/span&gt;&lt;span&gt;)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    try&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; stdout&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;print&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Failed to set key&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; .&lt;/span&gt;&lt;span&gt;{})&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; KVError&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;FailedToSetKey&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this implementation we can compile our new version of our WASM component which now also depends on &lt;code&gt;wasi:keyvalue&lt;/code&gt; and stores the latest value in a remote storage every time it gets updated.&lt;/p&gt;
&lt;h2 id=&quot;what-s-next&quot;&gt;What&#39;s next?&lt;/h2&gt;
&lt;p&gt;With the above technique we have a way to impelment WASM components in Zig, but working with the generated C bindings is a bit inconvenient. It would be nice to have a more idiomatic Zig interface to the component model, and maybe it can be achieved just by using Zig&#39;s metaprogramming features without having to create a Zig specific binding generator in addition to the existing ones.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Golem&#39;s Rust transaction API</title>
        <published>2024-04-13T00:00:00+00:00</published>
        <updated>2024-04-13T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/golem-rust-transaction-api/"/>
        <id>https://blog.vigoo.dev/posts/golem-rust-transaction-api/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/golem-rust-transaction-api/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;A few weeks ago we have added a new set of &lt;em&gt;host functions&lt;/em&gt; to &lt;a rel=&quot;external&quot; href=&quot;https://golem.cloud&quot;&gt;Golem&lt;/a&gt;, which allow programs running on this platform to control some of the persistency and transactional behavior of the executor. You can learn about these low-level functions on &lt;a rel=&quot;external&quot; href=&quot;https://learn.golem.cloud/docs/transaction-api&quot;&gt;the corresponding learn page&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;These exported functions allow a lot of control but they are very low level, and definitely not pleasant to use directly. To make them nicer we can write language-specific wrapper libraries on top of them, providing a first class experience for the supported programming languages.&lt;/p&gt;
&lt;p&gt;The first such wrapper library is &lt;a rel=&quot;external&quot; href=&quot;http://github.com/golemcloud/golem-rust&quot;&gt;golem-rust&lt;/a&gt;, and this post explains some of the Rust specific technical details of how this library works.&lt;/p&gt;
&lt;h2 id=&quot;regional-changes&quot;&gt;Regional changes&lt;/h2&gt;
&lt;p&gt;The easy part is providing higher level support for temporarily changing the executor&#39;s behavior. The common property of these host functions is that they come in pairs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;mark-begin-operation&lt;/code&gt;/&lt;code&gt;mark-end-operation&lt;/code&gt; pair defines a region that is treated as an atomic operation&lt;/li&gt;
&lt;li&gt;We can get the current retry policy and change it to something else with the &lt;code&gt;get-retry-policy&lt;/code&gt; and &lt;code&gt;set-retry-policy&lt;/code&gt; functions&lt;/li&gt;
&lt;li&gt;We can control persistency with &lt;code&gt;get-oplog-persistence-level&lt;/code&gt; and &lt;code&gt;set-oplog-persistence-level&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;And we can change whether the executor assumes that external calls are idempotent using the &lt;code&gt;get-idempotence-mode&lt;/code&gt; and &lt;code&gt;set-idempotence-mode&lt;/code&gt; pair.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For all these, a simple way to make them more safe and more idiomatic is to connect the lifetime of the temporarily changed behavior to the lifetime of a rust variable. For example in the following snippet, the whole function will be treated as an atomic region, but as soon the function returns, the region ends:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; some_atomic_operation&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; _atomic&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; golem_rust&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;mark_atomic_operation&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Implement these wrappers is quite simple. First we need to define &lt;em&gt;data type&lt;/em&gt; which the wrapper will return. Let&#39;s call it &lt;code&gt;AtomicOperationGuard&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AtomicOperationGuard&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    begin&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; OplogIndex&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We store the return value of Golem&#39;s &lt;code&gt;mark-begin-operation&lt;/code&gt; in it, as we have to pass this value to the &lt;code&gt;mark-end-operation&lt;/code&gt; when we want to close the atomic region.&lt;/p&gt;
&lt;p&gt;We want to close the atomic region when this value is dropped - so we can call Golem&#39;s &lt;code&gt;mark-end-operation&lt;/code&gt; in an explicitly implemented &lt;code&gt;drop&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Drop&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AtomicOperationGuard&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; drop&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        mark_end_operation&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;begin&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally we define the wrapper function which returns this guard value:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;must_use&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; mark_atomic_operation&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AtomicOperationGuard&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; begin&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; mark_begin_operation&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    AtomicOperationGuard&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; begin&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By using the &lt;code&gt;#[must_use]&lt;/code&gt; attribute we can make the compiler give a warning if the result value is not used - this is important, because that would mean that the atomic region gets closed as soon as it has been opened.&lt;/p&gt;
&lt;p&gt;With this basic building block we can also support an alternative style where we pass a function to be executed with the temporary change in Golem&#39;s behavior. These are higher order functions, taking a function as a parameter, and just using the already defined wrapper to apply the change:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; atomically&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; FnOnce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; _guard&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; mark_atomic_operation&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    f&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The same pattern can be used for all the mentioned host function pairs to get a pair of wrappers (one returning a guard, the other taking a function as a parameter):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;use_retry_policy&lt;/code&gt; and &lt;code&gt;with_retry_policy&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;use_idempotence_mode&lt;/code&gt; and &lt;code&gt;with_idempotence_mode&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;use_persistence_level&lt;/code&gt; and &lt;code&gt;with_persistence_level&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;transactions&quot;&gt;Transactions&lt;/h2&gt;
&lt;p&gt;Golem provides &lt;strong&gt;durable execution&lt;/strong&gt; and that comes with guarantees that your program will always run until it terminates, and (by default) all external operations are performed &lt;em&gt;at least once&lt;/em&gt;. (Here &lt;em&gt;at least once&lt;/em&gt; is the guarantee we can provide - naturally it does not mean that we just rerun all operations in case of a failure event. Golem tries to perform every operation exactly once but this cannot be guaranteed without special collaboration with the remote host. This behavior can be switched to &lt;em&gt;at most once&lt;/em&gt; by changing the &lt;strong&gt;idempotence mode&lt;/strong&gt; with the helper functions we defined above.)&lt;/p&gt;
&lt;p&gt;Many times external operations (such as HTTP calls to remote hosts) need to be executed &lt;em&gt;transactionally&lt;/em&gt;. If some of the operations failed the transaction need to be rolled back - &lt;strong&gt;compensation actions&lt;/strong&gt; need to undo whatever the already successfully performed operations did.&lt;/p&gt;
&lt;p&gt;We identified and implemented two different transaction types - both provide different guarantees and both can be useful.&lt;/p&gt;
&lt;p&gt;A &lt;strong&gt;fallible transaction&lt;/strong&gt; only deals with domain errors. Within the transaction every &lt;strong&gt;operation&lt;/strong&gt; that succeeds gets recorded. If an operation fails, all the recorded operations get &lt;em&gt;compensated&lt;/em&gt; in reverse order before the transaction block returns with a failure.&lt;/p&gt;
&lt;p&gt;What if anything non-domain specific failure happens to the worker? It can be an unexpected fatal error, hardware failure, an executor restarted because of a deployment, etc. A fallible transaction is completely implemented as regular user code, so Golem&#39;s durable execution guarantees apply to it. If for example the executor dies while 3 operation were completed out of the 5 in the transaction, the execution will continue from where it was - continuing with the 4th operation. If the 4th operation fails with a domain error, and the &lt;code&gt;golem-rust&lt;/code&gt; library starts executing the compensation actions, and then a random failure causes a panic in the middle of this, the execution will continue from the middle of the compensation actions making sure that all the operations are properly rolled back.&lt;/p&gt;
&lt;p&gt;Another possibility is what we call &lt;strong&gt;infallible transaction&lt;/strong&gt;s. Here we say that the transaction must not fail - but still if a step fails in it, we want to run compensation actions before we retry.&lt;/p&gt;
&lt;p&gt;To implement this we need some of the low-level transaction controls Golem provides. First of all, we need to mark the whole transaction as an &lt;em&gt;atomic region&lt;/em&gt;. This way if a (non domain level) failure happens during the transaction, the previously performed external operations will be automatically retried as the atomic region was never committed.&lt;/p&gt;
&lt;p&gt;We can capture the domain errors in user code and perform the compensation actions just like in the &lt;em&gt;fallible transaction&lt;/em&gt; case. But what should we do when all operations have been rolled back? We can use the &lt;code&gt;set-oplog-index&lt;/code&gt; host function to tell Golem to &quot;go back in time&quot; to the beginning of the transaction, forget everything that was performed after it, and start executing the transaction again.&lt;/p&gt;
&lt;p&gt;There is a third, more complete version of &lt;strong&gt;infallible transactions&lt;/strong&gt; which is not implemented yet - in this version we can guarantee that the compensation actions are performed even in case of a non-domain failure event. This can be implemented with the existing features of Golem but it is out of the scope of this post.&lt;/p&gt;
&lt;h3 id=&quot;operation-and-transaction&quot;&gt;Operation and Transaction&lt;/h3&gt;
&lt;p&gt;Let&#39;s see how we can implement this transaction feature.&lt;/p&gt;
&lt;p&gt;The first thing we need to define is an &lt;em&gt;operation&lt;/em&gt; - something that pairs an arbitrary action with a compensation action that undoes it. We can define it as a trait with two methods:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Operation&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; In&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Executes the operation which may fail with a domain error&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; execute&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;In&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;Self&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Executes a compensation action for the operation.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; compensate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;In&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If the operation succeeds, its result of type &lt;code&gt;Out&lt;/code&gt; will be stored - if it fails, &lt;code&gt;compensate&lt;/code&gt; will be called for all the previous operations with these stored output values.&lt;/p&gt;
&lt;p&gt;We also need something that defines the boundaries of a transaction, and allows executing these operations. Here we can create two slightly different interfaces for fallible and infallible transactions - to make it more user friendly.&lt;/p&gt;
&lt;p&gt;For fallible transactions we can define a higher order function where the user&#39;s logic itself can fail, and in the end we get back a transaction result:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; fallible_transaction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span&gt; +&lt;/span&gt;&lt;span&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;static&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; FnOnce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; FallibleTransaction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TransactionResult&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The result type here is just an alias to the standard Rust &lt;code&gt;Result&lt;/code&gt; type, in which the error type will be &lt;code&gt;TransactionFailure&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TransactionResult&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TransactionFailure&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; enum&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TransactionFailure&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; One of the operations failed with an error, and the transaction was fully rolled back.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    FailedAndRolledBackCompletely&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; One of the operations failed with an error, and the transaction was partially rolled back&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; because the compensation action of one of the operations also failed.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    FailedAndRolledBackPartially&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        failure&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        compensation_failure&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The function we pass to &lt;code&gt;fallible_transaction&lt;/code&gt; gets a mutable reference to a transaction object - this is what we can use to execute operations:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; FallibleTransaction&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span&gt; +&lt;/span&gt;&lt;span&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;static&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; FallibleTransaction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; execute&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;OpIn&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span&gt; +&lt;/span&gt;&lt;span&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;static&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; OpOut&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span&gt; +&lt;/span&gt;&lt;span&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;static&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        operation&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Operation&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;In&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; OpIn&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; OpOut&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; +&lt;/span&gt;&lt;span&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;static&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; OpIn&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;OpOut&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This looks a bit verbose but all it says is you can pass an arbitrary &lt;code&gt;Operation&lt;/code&gt; to this function, but all of them needs to have the same failure type, and you provide an _input_value for your operation. This separation of operation and input makes it possible to define reusable operations by implementing the &lt;code&gt;Operation&lt;/code&gt; trait manually - we will see more ways to define operations later.&lt;/p&gt;
&lt;p&gt;We also define a similar function and corresponding data type for &lt;em&gt;infallible transactions&lt;/em&gt;. There are two main differences:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;infallible_transaction&lt;/code&gt; function&#39;s result type is simply &lt;code&gt;Out&lt;/code&gt; - it can never fail&lt;/li&gt;
&lt;li&gt;Similarly, &lt;code&gt;execute&lt;/code&gt; it self cannot fail and this means that the transactional function itself cannot fail - and no need to use &lt;code&gt;?&lt;/code&gt; or other ways to deal with result types.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Storing the compensation actions in these structs is easy - we can just create closures capturing the input and output values and calling the trait&#39;s &lt;code&gt;compensate&lt;/code&gt; function, and store these closures in a vec:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CompensationAction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    action&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Box&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;dyn&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CompensationAction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; execute&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;action&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; FallibleTransaction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    compensations&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;CompensationAction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A last thing we can do in this level of the API is to think about cases where one would write generic code that works both with fallible and infallible transactions. Using a unified interface would not be as nice as using the dedicated one - as it deal with error types even if the transaction can never fail - but may provide better code reusability. We can hide the difference by defining a trait:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Transaction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; execute&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;OpIn&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span&gt; +&lt;/span&gt;&lt;span&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;static&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; OpOut&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span&gt; +&lt;/span&gt;&lt;span&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;static&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        operation&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Operation&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;In&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; OpIn&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; OpOut&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; +&lt;/span&gt;&lt;span&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;static&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; OpIn&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;OpOut&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; fail&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; error&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; FnOnce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TransactionResult&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The trait provides a way to execute operations and explicitly fail the transaction, and it also generalizes the &lt;code&gt;fallible_transaction&lt;/code&gt; and &lt;code&gt;infallible_transaction&lt;/code&gt; function with a static function called &lt;code&gt;run&lt;/code&gt;. Implementing this interface for our two transaction types is straightforward.&lt;/p&gt;
&lt;h3 id=&quot;defining-operations&quot;&gt;Defining operations&lt;/h3&gt;
&lt;p&gt;We defined an &lt;code&gt;Operation&lt;/code&gt; trait but haven&#39;t talked yet about how we will declare new operations. One obvious way is to define a type and implement the trait for it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CreateAccount&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; configuration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Operation&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CreateAccount&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; In&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;  =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AccountDetails&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AccountId&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; DomainError&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; execute&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AccountDetails&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;AccountId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; DomainError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;      todo!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Create the account&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; compensate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AccountDetails&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AccountId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; Self&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;      todo!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Delete the account&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The library provides a more concise way to define ad-hoc operations by just passing two functions:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; operation&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;In&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    execute_fn&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;In&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; +&lt;/span&gt;&lt;span&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;static&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    compensate_fn&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;In&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; +&lt;/span&gt;&lt;span&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;static&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Operation&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;In&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; In&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; ...&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; op&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; operation&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  move&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;account_details&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AccountDetails&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    todo!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Create the account&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	}&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  move&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;account_details&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AccountDetails&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; account_id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AccountId&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    todo!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Delete the account&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Under the hood this creates a struct called &lt;code&gt;FnOperation&lt;/code&gt; storing these two closures in it.&lt;/p&gt;
&lt;p&gt;There is a third way though. Let&#39;s see how it looks like, and then explore how it can be implemented with &lt;em&gt;Rust macros&lt;/em&gt;!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;golem_operation&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;compensation&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;delete_account&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; create_account&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;username&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;AccountId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; DomainError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  todo!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Create the account&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; delete_account&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;account_id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AccountId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; DomainError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  todo!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Delete the account&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;infallible_transaction&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; account_id&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; tx&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;create_account&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;vigoo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;x@y&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;operation-macro&quot;&gt;Operation macro&lt;/h3&gt;
&lt;p&gt;In the above example &lt;code&gt;golem_operation&lt;/code&gt; is a macro. It is a function executed compile time that takes the annotated item - in this case the &lt;code&gt;create_account&lt;/code&gt; function and &lt;strong&gt;transforms&lt;/strong&gt; it to something else.&lt;/p&gt;
&lt;p&gt;The first thing to figure out when writing a macro like that is what exactly we want to transform the function into. Let&#39;s see what this macro generates, and then I explain how to get there.&lt;/p&gt;
&lt;p&gt;If we expand the macro for the above example we get the following:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; create_account&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;username&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;AccountId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; DomainError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    todo!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Create the account&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CreateAccount&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; create_account&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; username&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;AccountId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; DomainError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Transaction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;DomainError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CreateAccount&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; create_account&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; username&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;AccountId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; DomainError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;execute&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;          operation&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-1&quot;&gt;            |&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;username&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;              create_account&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;username&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-1&quot;&gt;            |&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;username&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; op_result&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AccountId&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    	        call_compensation_function&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                delete_account&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                op_result&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;username&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;              )&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map_err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; err&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;username&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So seems like the macro leaves the function in its original form, but generates some additional items: a &lt;em&gt;trait&lt;/em&gt; which contains the same function signature as the annotated one, and then an &lt;em&gt;implementation&lt;/em&gt; for this trait for any &lt;code&gt;&amp;amp;mut T&lt;/code&gt; where &lt;code&gt;T&lt;/code&gt; is a &lt;code&gt;Transaction&amp;lt;DomainError&amp;gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;As I explained above, &lt;code&gt;Transaction&lt;/code&gt; is a trait that provides a unified interface for both the fallible and infallible transactions. With this instance we define an &lt;strong&gt;extension method&lt;/strong&gt; for the &lt;code&gt;tx&lt;/code&gt; value we get in our transaction functions - this is what allows us to write &lt;code&gt;tx.create_account&lt;/code&gt; in the above example.&lt;/p&gt;
&lt;p&gt;Two more details to notice:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Our &lt;code&gt;Operation&lt;/code&gt; type deals with a single input value but our annotated function can have arbitrary number of parameters. We can solve this by defining the operation&#39;s input as a &lt;strong&gt;tuple&lt;/strong&gt; containing all the function parameters.&lt;/li&gt;
&lt;li&gt;The compensation function (&lt;code&gt;delete_action&lt;/code&gt;) is not called directly, but through a helper called &lt;code&gt;call_compensation_function&lt;/code&gt;. This allows us to support compensation functions of different shapes, and I will explain how it works in details.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;defining-the-macro-and-parsing-the-function&quot;&gt;Defining the macro and parsing the function&lt;/h4&gt;
&lt;p&gt;This type of Rust macro which is invoked by annotating items in the code is called a &lt;a rel=&quot;external&quot; href=&quot;https://doc.rust-lang.org/reference/procedural-macros.html&quot;&gt;proc-macro&lt;/a&gt;. We need to create a separate Rust &lt;em&gt;crate&lt;/em&gt; for defining the macro, and set &lt;code&gt;proc-macro = true&lt;/code&gt; in its &lt;code&gt;Cargo.toml&lt;/code&gt; file and then create a top-level function annotated with &lt;code&gt;#[proc_macro_attribute]&lt;/code&gt; to define our macro:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;proc_macro_attribute&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; golem_operation&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;attr&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TokenStream&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; item&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TokenStream&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TokenStream&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Rust macros are transformations on &lt;strong&gt;token streams&lt;/strong&gt;. The first parameter of our macro gets the &lt;em&gt;parameters&lt;/em&gt; passed to the macro - so in our example it will contain a stream of tokens representing &lt;code&gt;compensation=delete_account&lt;/code&gt;. The second parameter is the annotated item itself - in our case it&#39;s a stream of tokens of the whole function definition including its body.&lt;/p&gt;
&lt;p&gt;The result of the function is also a token stream and the easiest thing we can do is to just return &lt;code&gt;item&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;proc_macro_attribute&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; golem_operation&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;attr&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TokenStream&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; item&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TokenStream&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TokenStream&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  item&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is a valid macro that does not do anything.&lt;/p&gt;
&lt;p&gt;We somehow have to generate a trait and a trait implementation with only having these two token streams. Before we can generate anything we need to understand the annotated function - we need its name, its parameters, its result type etc.&lt;/p&gt;
&lt;p&gt;We can use the &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/syn/latest/syn/&quot;&gt;syn&lt;/a&gt; create for this to parse the stream of tokens into a Rust AST.&lt;/p&gt;
&lt;p&gt;To parse &lt;code&gt;item&lt;/code&gt; as a function, we can write:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ast&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; ItemFn&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; syn&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;parse&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;item&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;expect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Expected a function&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is something we can extract information from, for example &lt;code&gt;ItemFn&lt;/code&gt; has the following contents:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ItemFn&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  pub&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; attrs&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Attribute&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  pub&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; vis&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Visibility&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  pub&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; sig&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Signature&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  pub&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; block&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Box&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Block&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And &lt;code&gt;sig&lt;/code&gt; contains things like the function&#39;s name, parameters and return type. It is important to keep in mind though that this is just a parsed AST from the tokens - the whole transformation runs before any type checking and we don&#39;t have any way to identify actual Rust types. We only see what&#39;s in the source code.&lt;/p&gt;
&lt;p&gt;For example in our macro we expect that the annotated function returns with a &lt;code&gt;Result&lt;/code&gt; type and we need to look into this type because we will use the success and error types in separate places in the generated code.&lt;/p&gt;
&lt;p&gt;We cannot do this in a 100% reliable way. We can look for things like the result type &lt;em&gt;looks like&lt;/em&gt; a &lt;code&gt;Result&amp;lt;Out, Err&amp;gt;&lt;/code&gt;, and we may support some additional forms such as &lt;code&gt;std::result::Result&amp;lt;Out, Err&amp;gt;&lt;/code&gt;, but if the user defined a type alias and uses that, a macro that looks at the AST cannot know that it is equal to a result type. In many cases these limitations can be solved by applying type level programming - we could have a trait that extracts the success and error types of a &lt;code&gt;Result&lt;/code&gt; and is not implemented for any other type, and then generate code from the macro that uses these helper types.&lt;/p&gt;
&lt;p&gt;The current implementation of the &lt;code&gt;golem_operation&lt;/code&gt; macro does not do this for determining the result types, so it has this limitation that it only works if you use the &quot;standard&quot; way of writing &lt;code&gt;Result&amp;lt;Out, Err&amp;gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This looks like the following:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; result_type&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ty&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Type&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Type&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Type&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      match&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ty&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;        Type&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Group&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;group&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; result_type&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;group&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;elem&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;        Type&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Paren&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;paren&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; result_type&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;paren&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;elem&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;        Type&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Path&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;type_path&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;					let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; idents&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; type_path&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;path&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;segments&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;iter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;segment&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; segment&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;ident&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;collect&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;_&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;          if&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; idents&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; vec!&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Result&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ... some more cases&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; last_segment&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; type_path&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;path&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;segments&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;last&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;unwrap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            let&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; syn&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;PathArguments&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;AngleBracketed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;generics&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;last_segment&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;arguments &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; return&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            if&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; generics&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;args&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;len&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; !=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 2&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;              return&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            let&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; syn&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;GenericArgument&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Type&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;success_type&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;generics&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;args&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;              return&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            let&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; syn&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;GenericArgument&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Type&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;err_type&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;generics&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;args&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;              return&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;						}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;            Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;success_type&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; err_type&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ... other cases returning None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once we have all the information we need - the function&#39;s name, its parameters, the successful and failed result types, all in &lt;code&gt;syn&lt;/code&gt; AST nodes, we can generate the additional code that we can return in the end as the new token stream.&lt;/p&gt;
&lt;p&gt;To generate token stream we use the &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/quote/latest/quote/&quot;&gt;quote library&lt;/a&gt;. This library provides the &lt;code&gt;quote!&lt;/code&gt; macro, which itself generates a &lt;code&gt;TokenStream&lt;/code&gt; . (Although it is not the same &lt;code&gt;TokenStream&lt;/code&gt; as the one we need to return from the macro. The macro requires &lt;code&gt;proc_macro::TokenStream&lt;/code&gt; and &lt;code&gt;quote!&lt;/code&gt; returns &lt;code&gt;proc_macro2::TokenStream&lt;/code&gt;. Fortunately it can be simply converted with &lt;code&gt;.into()&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;We write a single &lt;code&gt;quote!&lt;/code&gt; for producing the result of the macro:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; quote!&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ast&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  trait&lt;/span&gt;&lt;span&gt; #&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;traitname&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;fnsig&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; golem_rust&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Transaction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; #&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;traitname&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;fnsig&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;      self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;execute&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;        golem_rust&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;operation&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-1&quot;&gt;          |&lt;/span&gt;&lt;span&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;input_pattern&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            #&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;fnname&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;input_args&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-1&quot;&gt;          |&lt;/span&gt;&lt;span&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;compensation_pattern&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            #&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;compensate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;              #&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;compensation&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;              (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;op_result&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;              (&lt;/span&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;compensation_args&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            )&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map_err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; err&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        (&lt;/span&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;input_args&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;result&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;into&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; proc_macro2::TokenStream to proc_macro::TokenStream&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All the parts prefixed with &lt;code&gt;#&lt;/code&gt; are references to rust variables outside of the quote, and they can be (and usually are) various &lt;code&gt;syn&lt;/code&gt; AST nodes or raw token streams.&lt;/p&gt;
&lt;p&gt;There is a special syntax for interpolating sequences of values. The case used in the above example is when you write &lt;code&gt;#(#var), *&lt;/code&gt;. This means that &lt;code&gt;var&lt;/code&gt; is expected to be an iterable variable (in our case it will be &lt;code&gt;Vec&amp;lt;_&amp;gt;&lt;/code&gt; usually) and it interpolates each elements by inserting extra tokens, defined between &lt;code&gt;)&lt;/code&gt; and &lt;code&gt;*&lt;/code&gt;, between these elements. So this example would insert a comma and a space between the elements.&lt;/p&gt;
&lt;p&gt;The above defined &lt;code&gt;quote&lt;/code&gt; is a template that matches what we wanted to generate. All that&#39;s needed is to define all these variables holding dynamic parts of the generated code. The &lt;code&gt;#ast&lt;/code&gt; variable itself is the parsed function - so the first line of the quote just makes sure the original definition is part of the result.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;#succ&lt;/code&gt; and &lt;code&gt;#err&lt;/code&gt; types are extracted with the &lt;code&gt;result_type&lt;/code&gt; helper function as described above. The others are just defined by either transforming and cloning AST nodes, or using &lt;code&gt;quote!&lt;/code&gt; to generate sub token streams.&lt;/p&gt;
&lt;p&gt;Let&#39;s see a few examples!&lt;/p&gt;
&lt;p&gt;The new trait&#39;s name has to be an &lt;code&gt;Ident&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; fnname&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; fnsig&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;ident&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; traitname&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Ident&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;fnname&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_pascal_case&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; fnsig&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;ident&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;span&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we use the &lt;code&gt;to_pascal_case&lt;/code&gt; extension method provided by the &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/heck/latest/heck/&quot;&gt;heck crate&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Another example is the signature of the function that&#39;s inside the trait. It is &lt;em&gt;almost&lt;/em&gt; the same as the annotated feature, but it has to have a &lt;code&gt;self&lt;/code&gt; parameter as the first parameter of it, that&#39;s how it becomes an extension method on the transaction.&lt;/p&gt;
&lt;p&gt;We can do this by cloning the annotated function&#39;s signature and just adding a new parameter:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; fnsig&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ast&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;sig&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;fnsig&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;inputs&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;insert&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; parse_quote!&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that &lt;code&gt;parse_quote!&lt;/code&gt; immediately parses the token stream generated by quote back to a &lt;code&gt;syn&lt;/code&gt; AST node.&lt;/p&gt;
&lt;h4 id=&quot;compensation-function-shapes&quot;&gt;Compensation function shapes&lt;/h4&gt;
&lt;p&gt;The last interesting bit is how the macro supports compensation functions of different shapes. What we support right now, is the following.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The compensation function has no parameters at all&lt;/li&gt;
&lt;li&gt;The compensation function takes the output of the action but not the inputs&lt;/li&gt;
&lt;li&gt;The compensation function takes the output and all the inputs&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With the account creation example this means all of these are valid:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;golem_operation&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;compensation&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;delete_account&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; create_account&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;username&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;AccountId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; DomainError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; delete_account&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; DomainError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; delete_account&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;account_id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AccountId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; DomainError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; delete_account&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;account_id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AccountId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; username&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; DomainError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If we could have the AST of &lt;code&gt;delete_account&lt;/code&gt; from the macro, it would be easy to decide which shape we have - we would not even need to worry about not having actual types because we could just compare the parameter list and result type tokens of the two functions to be able to decide which way to go.&lt;/p&gt;
&lt;p&gt;Unfortunately our macro is on the &lt;code&gt;create_account&lt;/code&gt; function and there is no way to access anything else about &lt;code&gt;delete_account&lt;/code&gt; from it than the &lt;code&gt;compensation=delete_account&lt;/code&gt; part which we passed as an attribute parameter.&lt;/p&gt;
&lt;p&gt;Before solving this problem let&#39;s see how we can get the &lt;em&gt;name&lt;/em&gt; of the compensation function, at least:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; args&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; parse_macro_input!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Punctuated&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Meta&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; syn&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Token!&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;parse_terminated&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; compensation&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; arg&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; args&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  if&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; let&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Meta&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;NameValue&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name_value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; arg&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name_value&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;path&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get_ident&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;unwrap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name_value&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    if&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;compensation&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;      compensation&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We parse the macro&#39;s input into a list of &lt;code&gt;Meta&lt;/code&gt; nodes, and look for the &lt;code&gt;NameValue&lt;/code&gt; cases representing the attribute arguments having the &lt;code&gt;x=y&lt;/code&gt; form. If the key is &lt;code&gt;compensation&lt;/code&gt; we store the value, which has the type &lt;code&gt;Expr&lt;/code&gt; (expression AST node) and we can interpolate this expression node directly in the quoted code to get our function name.&lt;/p&gt;
&lt;p&gt;Let&#39;s go back to the primary problem - how can we generate code that invokes this function which can have three different shapes, if we cannot know which one it is?&lt;/p&gt;
&lt;p&gt;First we define a &lt;strong&gt;trait&lt;/strong&gt; that abstracts this problem for us:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CompensationFunction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;In&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; call&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; In&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This always has the same shape - we just pass both the results and the inputs to it, and the trait&#39;s implementation can decide to use any of these parameters to actually call the compensation function or not.&lt;/p&gt;
&lt;p&gt;We can define a function that takes an arbitrary value &lt;code&gt;T&lt;/code&gt; for which we have an implementation of this trait, and just call it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; call_compensation_function&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;In&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CompensationFunction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;In&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    result&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; In&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    f&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;call&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;result&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this, we can simply generate code from the macro that passes &lt;strong&gt;the actual compensation function&lt;/strong&gt; to the &lt;code&gt;f&lt;/code&gt; parameter of &lt;code&gt;call_compensation_function&lt;/code&gt;, and always pass both the result and the input!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;call_compensation_function&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  delete_account&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  op_result&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;username&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To make this work we need instances of &lt;code&gt;CompensationFunction&lt;/code&gt; for arbitrary function types.&lt;/p&gt;
&lt;p&gt;Let&#39;s try to define it for the function with no parameters (the first supported shape):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CompensationFunction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    F&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; FnOnce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; call&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        _result&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        _input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        self&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;        Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is not the final implementation as we will see soon. If we try to write an implementation for the second shape - where we only use the result and not the input, we immediately run into a problem:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CompensationFunction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    F&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; FnOnce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;      //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The error is about &lt;strong&gt;conflicting implementations&lt;/strong&gt; of our trait:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;error[E0119]: conflicting implementations of trait `CompensationFunction&amp;lt;(), (), _&amp;gt;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  --&amp;gt; golem-rust/src/transaction/compfn.rs:45:1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;31 | / impl&amp;lt;F, Err&amp;gt; CompensationFunction&amp;lt;(), (), Err&amp;gt; for F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;32 | | where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;33 | |     F: FnOnce() -&amp;gt; Result&amp;lt;(), Err&amp;gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   | |___________________________________- first implementation here&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;45 | / impl&amp;lt;F, Out, Err&amp;gt; CompensationFunction&amp;lt;(), Out, Err&amp;gt; for F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;46 | | where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;47 | |     F: FnOnce(Out) -&amp;gt; Result&amp;lt;(), Err&amp;gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   | |______________________________________^ conflicting implementation&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These. two trait implementations &lt;strong&gt;overlap&lt;/strong&gt;. Although it is not obvious at first glance why the two are overlapping, what happens is all the types involved in the overlap check can be unified:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The trait&#39;s parameters -
&lt;ul&gt;
&lt;li&gt;the first is &lt;code&gt;()&lt;/code&gt; in both cases&lt;/li&gt;
&lt;li&gt;The second is &lt;code&gt;()&lt;/code&gt; vs &lt;code&gt;Out&lt;/code&gt;. Nothing prevents &lt;code&gt;Out&lt;/code&gt; to be &lt;code&gt;()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The third can be anything in both cases&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The type we implement the trait for
&lt;ul&gt;
&lt;li&gt;This is the confusing part - as we have two different function type signatures in the two cases! But these are only type bounds. We say we implement &lt;code&gt;CompensationFunction&lt;/code&gt; for a type &lt;code&gt;F&lt;/code&gt; which implements the trait &lt;code&gt;FnOnce() ...&lt;/code&gt;. The problem is that in theory there can be a type that implements both these function traits, so this is not preventing the overlap either.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is something &lt;a rel=&quot;external&quot; href=&quot;https://github.com/rust-lang/rfcs/blob/master/text/1210-impl-specialization.md&quot;&gt;specialization&lt;/a&gt; would solve but that is currently an unstable compiler feature.&lt;/p&gt;
&lt;p&gt;If at least one of the above types could not be unified, we would not have an overlap, so that&#39;s what we have to do. The simplest way to do so is to stop having unconstrained types in the trait&#39;s type parameters such as &lt;code&gt;In&lt;/code&gt; and &lt;code&gt;Out&lt;/code&gt; and &lt;code&gt;Err&lt;/code&gt; (Actually &lt;code&gt;Err&lt;/code&gt; should not be affected by this, but I applied the same technique to all parameters at once in the library. This is something that could be potentially simplified in the future.).&lt;/p&gt;
&lt;p&gt;So we just have to have a type parameter that can contain an arbitrary input or output type, but does not unify with &lt;code&gt;()&lt;/code&gt;. We can do that by wrapping the output type in a tuple:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CompensationFunction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    F&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; FnOnce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here instead of &lt;code&gt;Out&lt;/code&gt; we use &lt;code&gt;(Out,)&lt;/code&gt; which is a 1-tuple wrapping our output type. This no longer unifies with &lt;code&gt;()&lt;/code&gt; so the compiler error is solved!&lt;/p&gt;
&lt;p&gt;We can imagine additional trait implementations for one or more input parameters:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CompensationFunction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    F&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; FnOnce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CompensationFunction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    F&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; FnOnce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Two more problems to solve before we are done!&lt;/p&gt;
&lt;p&gt;The first problem occurs when we try to use this mechanism for the first to compensation function shapes - when the result, or the result and the input are not used by the function.&lt;/p&gt;
&lt;p&gt;The problem is that these trait implementations bind the &lt;code&gt;In&lt;/code&gt; and/or &lt;code&gt;Out&lt;/code&gt; types to &lt;code&gt;()&lt;/code&gt; in these cases, which means that our &lt;code&gt;call&lt;/code&gt; function will use the unit type for these parameters. For example for &lt;code&gt;delete_account&lt;/code&gt; which does not takes the input parameters, it would have the following types if we replace the generic parameters with the inferred ones:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; call_compensation_function&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; FnOnce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;AccountId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; DomainError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    result&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; AccountId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; DomainError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And our macro will call it like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;call_compensation_function&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  delete_account&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  op_result&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;username&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; email&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This of course will not compile, because we pass &lt;code&gt;(&amp;amp;str, &amp;amp;str)&lt;/code&gt; in place of a &lt;code&gt;()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Let&#39;s take a step back, and change our &lt;code&gt;CompensationFunction&lt;/code&gt; trait:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CompensationFunction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;In&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; call&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;    self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    result&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TupleOrUnit&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TupleOrUnit&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;In&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Instead of directly taking &lt;code&gt;Out&lt;/code&gt; and &lt;code&gt;In&lt;/code&gt; in the parameters we now accept &lt;strong&gt;anything that implements TupleOrUnit&lt;/strong&gt; for the given type.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;TupleOrUnit&lt;/code&gt; is just a special conversion trait:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TupleOrUnit&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; into&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What makes it special and what makes it solve our problem is what instances we have for it.&lt;/p&gt;
&lt;p&gt;First of all we say that &lt;strong&gt;anything can be converted to unit&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TupleOrUnit&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; into&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we use the same trick to avoid overlapping instances, and we say that 1-tuple, 2-tuple, etc. can be converted to itself only:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T1&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TupleOrUnit&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; into&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        self&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T2&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TupleOrUnit&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; into&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;        self&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this we achieved that the &lt;code&gt;call_compensation_function&lt;/code&gt; function is still type safe - it requires us to pass the proper &lt;code&gt;Out&lt;/code&gt; and &lt;code&gt;In&lt;/code&gt; types - but in the special case when either of these types are unit, it allows us to pass an arbitrary value instead of an actual &lt;code&gt;()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This makes our macro complete.&lt;/p&gt;
&lt;p&gt;The last thing to solve is to have enough instances of these two type classes - &lt;code&gt;CompensationFunction&lt;/code&gt; and &lt;code&gt;TupleOrUnit&lt;/code&gt; so our library works with more than 1 or 2 parameters. Writing them by hand is an option but we can easily generate them with another macro!&lt;/p&gt;
&lt;p&gt;This time we don&#39;t have to write a procedural macro - we can use a &lt;strong&gt;declarative macro&lt;/strong&gt;s which are simpler, and they can be defined inline in the same module where we define these types.&lt;/p&gt;
&lt;p&gt;Let&#39;s start with &lt;code&gt;TupleOrUnit&lt;/code&gt; as it is a bit simpler. We use the &lt;a rel=&quot;external&quot; href=&quot;https://doc.rust-lang.org/reference/macros-by-example.html&quot;&gt;macro_rules&lt;/a&gt; macro which is basically a pattern match with a special syntax - you can match on what is passed to the macro, and generate code with interpolation similar to the &lt;code&gt;quote!&lt;/code&gt; macro - but using &lt;code&gt;$&lt;/code&gt; instead of &lt;code&gt;#&lt;/code&gt; as the interpolation symbol. The following definition defines an instance of &lt;code&gt;TupleOrUnit&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;macro_rules!&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; tuple_or_unit&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ty&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ident&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TupleOrUnit&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ty&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ty&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; into&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ty&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;                self&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We have a single case of our pattern match, which matches a &lt;strong&gt;comma-separated list of identifiers&lt;/strong&gt;. We can refer to this list of identifiers as &lt;code&gt;ty&lt;/code&gt;. Then we use the same syntax for interpolating sequences into the code as we have seen already in our procedural macro and just generate the instance.&lt;/p&gt;
&lt;p&gt;We can call this macro with a list of type parameters (which are all &lt;em&gt;identifiers&lt;/em&gt;):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;tuple_or_unit!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&#39;s do the same for generating &lt;code&gt;CompensationFunction&lt;/code&gt; instances:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;macro_rules!&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; compensation_function&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ty&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ident&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; $&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CompensationFunction&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;            F&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; FnOnce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; $&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; call&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;                self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                out&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TupleOrUnit&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;                input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TupleOrUnit&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            )&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;                #&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;allow&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;non_snake_case&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                let&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt; $&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ty&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;+&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;into&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                let&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; out&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;into&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;                self&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; $&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map_err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;err&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The only interesting part here is how we access the components of our tuple.&lt;/p&gt;
&lt;p&gt;Let&#39;s imagine we pass &lt;code&gt;T1, T2, T3&lt;/code&gt; as arguments to this macro, so &lt;code&gt;ty&lt;/code&gt; is a sequence of three identifiers. We can interpolate this comma separated list into the type parameter part (&lt;code&gt;impl&amp;lt;F, $($ty),*, Out, Err&amp;gt;&lt;/code&gt;) without any problems but this is still just a list of identifiers - and when we call our compensation function (&lt;code&gt;self&lt;/code&gt;), we have to access the individual elements of this tuple and pass them to the function as separate parameters.&lt;/p&gt;
&lt;p&gt;We could write it by hand like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; call&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; out&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TupleOrUnit&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; TupleOrUnit&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Err&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; out&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; out&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;into&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;into&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;  self&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It is possible to generate a list of accessors like this from a procedural macro, but not in a declarative one - we only have &lt;code&gt;ty&lt;/code&gt; to work with. We can instead &lt;strong&gt;destructure&lt;/strong&gt; the tuple and we can actually reuse the list of identifiers to do so!&lt;/p&gt;
&lt;p&gt;In the above macro code, this can be seen as:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;allow&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;non_snake_case&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt; $&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ty&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;+&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;into&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; $&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This translates to&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;allow&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;non_snake_case&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;let&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;into&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The error mapping is only necessary because currently the error typed is also wrapped into a tuple - this could enable additional function shapes where the compensation function never fails, for example, but it is not implemented yet.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;The library described here is open source and is available &lt;a rel=&quot;external&quot; href=&quot;https://github.com/golemcloud/golem-rust&quot;&gt;on GitHub&lt;/a&gt; and published &lt;a rel=&quot;external&quot; href=&quot;https://crates.io/crates/golem-rust&quot;&gt;to crates.io&lt;/a&gt;. Documentation and examples will soon be added to &lt;a rel=&quot;external&quot; href=&quot;https://learn.golem.cloud/docs/intro&quot;&gt;Golem&#39;s learn pages&lt;/a&gt;. And of course this is just a first version I hope to see grow based on user feedback.&lt;/p&gt;
&lt;p&gt;We also plan to have similar higher-level wrapper libraries for Golem&#39;s features for the other supported languages - everything Golem provides is exposed through the WASM Component Model so any language supporting that have immediate access to the building blocks. All remains is writing idiomatic wrappers on top of them for each language.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Worker to Worker communication in Golem</title>
        <published>2024-03-08T00:00:00+00:00</published>
        <updated>2024-03-08T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/w2w-communication-golem/"/>
        <id>https://blog.vigoo.dev/posts/w2w-communication-golem/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/w2w-communication-golem/">&lt;p&gt;This article was originally posted at &lt;a rel=&quot;external&quot; href=&quot;https://www.golem.cloud/post/worker-to-worker-communication&quot;&gt;the Golem Cloud Blog&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Golem Cloud&#39;s first developer preview &lt;a rel=&quot;external&quot; href=&quot;https://www.golem.cloud/post/unveiling-golem-cloud&quot;&gt;has been unveiled in August&lt;/a&gt;, and just a month ago, we released &lt;a rel=&quot;external&quot; href=&quot;https://www.golem.cloud/post/golem-goes-open-source&quot;&gt;an open-source version of Golem&lt;/a&gt;. Workers, the fundamental primitive in Golem, expose a typed interface that can be invoked through the REST API or the command line tools, but until today, calling a worker from &lt;em&gt;another worker&lt;/em&gt; was neither easy nor type-safe.&lt;/p&gt;
&lt;p&gt;With the latest release of Golem and the &lt;code&gt;golem-cli&lt;/code&gt; tool, we finally have a first-class, typed way to invoke one worker from another, using any of the supported guest languages!&lt;/p&gt;
&lt;h2 id=&quot;golem-wasm-rpc&quot;&gt;Golem WASM RPC&lt;/h2&gt;
&lt;p&gt;Golem&#39;s new worker to worker communication feature consists of two major layers:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A low-level, dynamic worker invocation API exposed as a Golem &lt;strong&gt;host function&lt;/strong&gt; to all workers. This interface is not type safe. Rather, it matches the capabilities of the external REST API, allowing a worker to invoke any method on any other worker with any parameters. However, it avoids the overhead of setting up an HTTP connection and will be optimized in the future.&lt;/li&gt;
&lt;li&gt;The ability to generate &lt;strong&gt;stubs&lt;/strong&gt; for having a completely type-safe, language-independent remote worker invocation for any supported language having a WIT-based binding generator.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With the new stub generator commands integrated into Golem&#39;s command line tool (&lt;code&gt;golem-cli&lt;/code&gt;) worker to worker communication is now a simple and fully type-safe experience.&lt;/p&gt;
&lt;h2 id=&quot;a-full-example&quot;&gt;A full example&lt;/h2&gt;
&lt;p&gt;To demonstrate how this new feature works, we will take one of the first Golem example projects, the &lt;strong&gt;shopping cart&lt;/strong&gt;, and extend it with worker-to-worker communication. The original shopping-cart project defines a worker for each shopping cart of an online web store, with exported functions to add items to the cart and eventually check out and finish the shopping process.&lt;/p&gt;
&lt;p&gt;In this example, we introduce a second &lt;strong&gt;worker template&lt;/strong&gt;, one that will be used to create a single &lt;strong&gt;worker&lt;/strong&gt; for each online shopper. This worker will keep a log of all the purchases of the user it belongs to. We will extend the shopping cart&#39;s &lt;code&gt;checkout&lt;/code&gt; function with a remote worker invocation to add a new entry to the account&#39;s purchase log.&lt;/p&gt;
&lt;p&gt;First, let&#39;s make sure we have the latest version of &lt;code&gt;golem-cli&lt;/code&gt;, if using the open-source Golem version, or &lt;code&gt;golem-cloud-cli&lt;/code&gt;, if using the hosted version. It must have the new &lt;code&gt;stubgen&lt;/code&gt; subcommand, to check let&#39;s run &lt;code&gt;golem-cli stubgen --help&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;WASM&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; RPC&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; stub&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; generator&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Usage:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem-cli&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; stubgen&lt;/span&gt;&lt;span&gt; [OPTIONS&lt;/span&gt;&lt;span&gt;] &lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;COMMAND&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Commands:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  generate&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;              Generate&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Rust&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; RPC&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; stub&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; crate&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; WASM&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  build&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;                 Build&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; an&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; RPC&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; stub&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; WASM&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  add-stub-dependency&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;   Adds&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; generated&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; stub&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; as&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; dependency&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; another&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; WASM&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  compose&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;               Compose&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; WASM&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; generated&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; stub&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; WASM&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  initialize-workspace&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  Initializes&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Golem-specific&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; cargo-make&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; configuration&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Cargo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; workspace&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; automatically&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; generating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; stubs&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; and&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; composing&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; results&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  help&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;                  Print&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; this&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; message&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; or&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; the&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; help&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; of&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; the&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; given&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; subcommand&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;s&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Options:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  -v,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-verbose...&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  Increase&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; logging&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; verbosity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  -q,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-quiet...&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    Decrease&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; logging&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; verbosity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  -h,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-help&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;        Print&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; help&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;preparing-the-example&quot;&gt;Preparing the example&lt;/h3&gt;
&lt;p&gt;We are going to create two different &lt;strong&gt;Golem templates&lt;/strong&gt;, and have the source codes of both of them in a single &lt;strong&gt;Cargo workspace&lt;/strong&gt;. This is not required—they could live in completely separate places—but it allows using our built-in cargo-make support, which currently gives us the best possible developer experience for worker-to-worker communication.&lt;/p&gt;
&lt;p&gt;First, let&#39;s use the &lt;code&gt;golem-cli new&lt;/code&gt; command to take the &lt;strong&gt;shopping-cart example&lt;/strong&gt; and generate a new template source from it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem-cli&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-example&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; rust-shopping-cart&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-template-name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; shopping-cart-rpc&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;See&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; the&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; documentation&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; about&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; installing&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; common&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; tooling:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; https://golem.cloud/learn/rust&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Compile&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; the&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Rust&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; cargo-component:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  cargo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; build&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-release&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;The&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; target/wasm32-wasi/release/shopping_cart_rpc.wasm&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; is&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; ready&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; be&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; used&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Golem!&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;shopping-cart-rpc&lt;/code&gt; directory now contains a single Rust crate, which can be compiled to WASM using &lt;code&gt;cargo component build&lt;/code&gt;. We need two different WASMs (two Golem templates) so as a first step, we convert the generated Cargo project to a &lt;a rel=&quot;external&quot; href=&quot;https://doc.rust-lang.org/book/ch14-03-cargo-workspaces.html&quot;&gt;&lt;strong&gt;cargo workspace&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;First, create two sub-directories for the two templates we will use:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; mkdir&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;pv&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; shopping-cart&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;shopping-cart&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; mkdir&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;pv&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; purchase-history&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;purchase-history&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then, move the generated shopping cart source code into the &lt;code&gt;shopping-cart&lt;/code&gt; subdirectory:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; mv&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; src&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; shopping-cart&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;src&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; shopping-cart/src&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; mv&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; wit&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; shopping-cart&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;wit&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; shopping-cart/wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; mv&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Cargo.toml&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; shopping-cart&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Cargo.toml&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; shopping-cart/Cargo.toml&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can copy the whole contents of the &lt;code&gt;shopping-cart&lt;/code&gt; directory to the &lt;code&gt;purchase-history&lt;/code&gt; directory too:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; cp&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;rv&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; shopping-cart/&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; purchase-history&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;shopping-cart/Cargo.toml&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; purchase-history/Cargo.toml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;shopping-cart/src&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; purchase-history/src&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;shopping-cart/src/lib.rs&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; purchase-history/src/lib.rs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;shopping-cart/wit&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; purchase-history/wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;shopping-cart/wit/shopping-cart-rpc.wit&lt;/span&gt;&lt;span&gt; -&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; purchase-history/wit/shopping-cart-rpc.wit&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we create a new &lt;code&gt;Cargo.toml&lt;/code&gt; file in the root, pointing to the two sub-projects:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;toml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;workspace&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;resolver&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;members&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;shopping-cart&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;purchase-history&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next, modify the &lt;code&gt;name&lt;/code&gt; property in both sub-project&#39;s &lt;code&gt;Cargo.toml&lt;/code&gt;. In &lt;code&gt;shopping-cart/Cargo.toml&lt;/code&gt;, it should be:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;toml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;shopping-cart&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;while in the other&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;toml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;name&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;purchase-history&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It&#39;s also recommended that you rename the WIT file in both the &lt;code&gt;wit&lt;/code&gt; directories to a file name that corresponds to the given sub-project&#39;s name, but it does not have any effect on the compilation—it just makes working on the source code easier.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; mv&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; shopping-cart/wit/shopping-cart-rpc.wit&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; shopping-cart/wit/shopping-cart.wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; mv&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; purchase-history/wit/shopping-cart-rpc.wit&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; purchase-history/wit/purchase-history.wit&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At this point running &lt;code&gt;cargo component build&lt;/code&gt; in the root will compile both identical sub-projects, creating two different WASM files (but both containing the shopping cart implementation for now):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; cargo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; build&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    Creating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /Users/vigoo/projects/demo/shopping-cart-rpc/target/wasm32-wasi/debug/purchase_history.wasm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    Creating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /Users/vigoo/projects/demo/shopping-cart-rpc/target/wasm32-wasi/debug/shopping_cart.wasm&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;implementing-the-purchase-history-template&quot;&gt;Implementing the purchase history template&lt;/h3&gt;
&lt;p&gt;Before talking about &lt;em&gt;worker-to-worker communication&lt;/em&gt;, let&#39;s just implement a simple version of the &lt;strong&gt;purchase history template&lt;/strong&gt;. Each worker of this template will correspond to a &lt;strong&gt;user&lt;/strong&gt; of the system, the worker name being equal to the user&#39;s identifier. We only need two exported functions, one for recording a purchase, and one for getting all the previous purchases.&lt;/p&gt;
&lt;p&gt;Let&#39;s completely replace &lt;code&gt;purchase-history/wit/purchase-history.wit&lt;/code&gt; with the following interface definition:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;wit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; shopping&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;purchase-history&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; api&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  record&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; product-item&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    product-id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    price&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; float32&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    quantity&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u32&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  record&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; order&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    order-id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    items&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;product-item&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    total&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; float32&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    timestamp&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u64&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  add&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;order&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;order&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; order&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  get&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;orders&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;order&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;world&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; purchase-history&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  export&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; api&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our &lt;code&gt;product-item&lt;/code&gt; and &lt;code&gt;order&lt;/code&gt; types are the same that we have in the shopping-cart WIT. In a next step, we will remove them from the shopping-cart WIT, and import them from this component&#39;s interface definition!&lt;/p&gt;
&lt;p&gt;Running &lt;code&gt;cargo component build&lt;/code&gt; now will print a couple of errors, as we did not update the &lt;code&gt;purchase-history&lt;/code&gt; module&#39;s Rust source code yet:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; cargo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; build&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;error[E0433]:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; failed&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; resolve:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; could&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; not&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; find&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; `&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span&gt; in &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;exports&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; --&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; purchase-history/src/lib.rs:3:31&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;3&lt;/span&gt;&lt;span&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; use&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; crate::bindings::exports::golem::template::api::&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;*&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;                               ^^^^^&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; could&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; not&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; find&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; `&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;golem&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span&gt; in &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;exports&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A simple implementation of this can be the following code replacing the existing &lt;code&gt;lib.rs&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mod&lt;/span&gt;&lt;span&gt; bindings&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;bindings&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;exports&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;shopping&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;purchase_history&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;api&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Component&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    orders&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Order&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; STATE&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    orders&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; with_state&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; FnOnce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; unsafe&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; f&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; STATE&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Guest&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Component&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; add_order&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;order&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Order&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        with_state&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;orders&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;push&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;order&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_orders&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Order&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;        with_state&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;            state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;orders&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this, &lt;code&gt;cargo component build&lt;/code&gt; now compiles the new &lt;code&gt;purchase_history.wasm&lt;/code&gt; for us.&lt;/p&gt;
&lt;h3 id=&quot;worker-to-worker-communication&quot;&gt;Worker to worker communication&lt;/h3&gt;
&lt;p&gt;At this point, the only outstanding task in our example is to &lt;strong&gt;invoke the appropriate purchase history worker&lt;/strong&gt; in the &lt;code&gt;checkout&lt;/code&gt; implementation of the shopping cart.&lt;/p&gt;
&lt;p&gt;To find all the available options for doing this, check the &lt;a rel=&quot;external&quot; href=&quot;https://learn.golem.cloud/docs/rpc&quot;&gt;Worker-to-Worker communication&#39;s documentation&lt;/a&gt;. In this example, we have both the target (the purchase history) and the caller (the shopping cart) in &lt;strong&gt;the same cargo workspace&lt;/strong&gt;, so we can use Golem&#39;s &lt;a rel=&quot;external&quot; href=&quot;https://github.com/sagiegurari/cargo-make&quot;&gt;cargo-make&lt;/a&gt; based solution for enabling communication between the different sub-projects of the workspace.&lt;/p&gt;
&lt;p&gt;Let&#39;s initialize this using &lt;code&gt;golem-cli&lt;/code&gt; (or &lt;code&gt;golem-cloud-cli&lt;/code&gt;):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; golem-cli&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; stubgen&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; initialize-workspace&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-targets&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; purchase-history&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;-callers&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; shopping-cart&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Writing&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; cargo-make&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Makefile&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/Users/vigoo/projects/demo/shopping-cart-rpc/Makefile.toml&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Generating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; initial&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; stub&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; purchase-history&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Generating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; stub&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; WIT&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /Users/vigoo/projects/demo/shopping-cart-rpc/purchase-history-stub/wit/_stub.wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Copying&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; root&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; package&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; shopping:purchasehistory&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;  .&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /Users/vigoo/projects/demo/shopping-cart-rpc/purchase-history/wit/purchase-history.wit&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /Users/vigoo/projects/demo/shopping-cart-rpc/purchase-history-stub/wit/deps/shopping_purchasehistory/purchase-history.wit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Writing&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; wasm-rpc.wit&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /Users/vigoo/projects/demo/shopping-cart-rpc/purchase-history-stub/wit/deps/wasm-rpc&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Generating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Cargo.toml&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /Users/vigoo/projects/demo/shopping-cart-rpc/purchase-history-stub/Cargo.toml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Generating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; stub&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; source&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /Users/vigoo/projects/demo/shopping-cart-rpc/purchase-history-stub/src/lib.rs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Writing&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; updated&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Cargo.toml&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/Users/vigoo/projects/demo/shopping-cart-rpc/Cargo.toml&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As a next step, we check if the generated artifacts work, by running &lt;strong&gt;cargo make&lt;/strong&gt; to execute the full build flow. It contains custom steps invoking &lt;code&gt;golem-cli&lt;/code&gt; to implement the typed worker-to-worker communication.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; cargo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; make&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; build-flow&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    Creating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /Users/vigoo/projects/demo/shopping-cart-rpc/target/wasm32-wasi/debug/purchase_history.wasm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    Creating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /Users/vigoo/projects/demo/shopping-cart-rpc/target/wasm32-wasi/debug/shopping_cart.wasm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    Creating&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /Users/vigoo/projects/demo/shopping-cart-rpc/target/wasm32-wasi/debug/purchase_history_stub.wasm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;cargo-make&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; INFO - Execute Command: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;wasm-rpc-stubgen&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;compose&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;--source-wasm&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;target/wasm32-wasi/debug/shopping_cart.wasm&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;--stub-wasm&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;target/wasm32-wasi/debug/purchase_history_stub.wasm&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;--dest-wasm&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;target/wasm32-wasi/debug/shopping_cart_composed.wasm&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Error:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; no&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; dependencies&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; of&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; `&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;target/wasm32-wasi/debug/shopping_cart.wasm&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; were&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; found&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Don&#39;t worry about the failure at the end - it will be fixed in the next step.&lt;/p&gt;
&lt;p&gt;There are several changes in our workspace after running this command:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We have a &lt;code&gt;Makefile.toml&lt;/code&gt; file describing custom build tasks related to worker to worker communication&lt;/li&gt;
&lt;li&gt;We have a completely new sub-project called &lt;code&gt;purchase-history-stub&lt;/code&gt; which is added to the Cargo workspace&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;shopping-cart/wit/deps&lt;/code&gt; directory now contains three dependencies: the original purchase history module, the generated stub interface, and the general purpose &lt;code&gt;wasm-rpc&lt;/code&gt; package.&lt;/li&gt;
&lt;li&gt;These dependencies are also registered in &lt;code&gt;shopping-cart/Cargo.toml&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Before further explaining what these generated stubs are, let&#39;s finish our example. We need to modify the &lt;strong&gt;shopping cart&lt;/strong&gt; template&#39;s interface definition (&lt;code&gt;shopping-cart/wit/shopping-cart.wit&lt;/code&gt;) to import the generated stub, and to reuse the data types defined for the purchase history template instead of redefining them.&lt;/p&gt;
&lt;p&gt;The updated WIT file would look like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;wit&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; shopping&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;cart&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; api&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; shopping&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;purchase-history&lt;/span&gt;&lt;span&gt;/&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;api&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;product-item&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  use&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; shopping&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;purchase-history&lt;/span&gt;&lt;span&gt;/&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;api&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;order&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  record&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; order-confirmation&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    order-id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  variant&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; checkout-result&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    error&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    success&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;order-confirmation&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  initialize&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;cart&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;user-id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  add&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;item&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;item&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; product-item&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  remove&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;item&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;product-id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  update&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-item&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;quantity&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;product-id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; string&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; quantity&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; u32&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  checkout&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; checkout-result&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  get&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-cart&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;contents&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; func&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; list&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;product-item&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;world&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; shopping-cart&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  import&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; shopping&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;purchase-history-stub&lt;/span&gt;&lt;span&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;stub-purchase-history&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  export&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; api&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are three changes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We renamed the package from the default &lt;code&gt;golem:template&lt;/code&gt; to &lt;code&gt;shopping:cart&lt;/code&gt; to make it more consistent with the other packages&lt;/li&gt;
&lt;li&gt;We deleted the definition of &lt;code&gt;product-item&lt;/code&gt; and &lt;code&gt;order&lt;/code&gt;, and instead importing them from the &lt;code&gt;shopping:purchase-history&lt;/code&gt; package.&lt;/li&gt;
&lt;li&gt;We added the &lt;code&gt;import&lt;/code&gt; statement in the &lt;code&gt;world&lt;/code&gt;, which loads the generated &lt;strong&gt;stub&lt;/strong&gt; into the template&#39;s world, so we can call it from the Rust code to initiate remote calls to the &lt;code&gt;purchase-history&lt;/code&gt; workers.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Because of the change of the package name, we have to update the import in &lt;code&gt;lib.rs&lt;/code&gt; :&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;bindings&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;exports&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;shopping&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;cart&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;api&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The only remaining step is to extend the &lt;code&gt;checkout&lt;/code&gt; function with the remote worker invocation!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;bindings&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;shopping&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;purchase_history&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;api&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Order&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;bindings&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;shopping&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;purchase_history_stub&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;stub_purchase_history&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;bindings&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;golem&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;rpc&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;types&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Uri&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; checkout&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; CheckoutResult&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    dispatch_order&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Defining the order to be saved in history&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; order&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Order&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        items&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;items&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        order_id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; order_id&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        timestamp&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;time&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;SystemTime&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;now&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;duration_since&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;time&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;SystemTime&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;UNIX_EPOCH&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;unwrap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;as_secs&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        total&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;items&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;iter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; item&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;price &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; item&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;quantity &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;as&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; f32&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;sum&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Constructing the remote worker&amp;#39;s URI&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; template_id&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;  		std&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;env&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;var&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;PURCHASE_HISTORY_TEMPLATE_ID&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  			.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;expect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;PURCHASE_HISTORY_TEMPLATE_ID not set&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; uri&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Uri&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; format!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;worker://&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;template_id&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; state&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;user_id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Connecdting to the remote worker and invoking it&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; history&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; stub_purchase_history&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Api&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;uri&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    history&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;add_order&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;order&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With all these changes, running &lt;code&gt;cargo make&lt;/code&gt; again will succeed:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; cargo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; make&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; build-flow&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Writing&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; composed&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; component&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;target/wasm32-wasi/debug/shopping_cart_composed.wasm&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;cargo-make&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; INFO - Build Done in 7.38 seconds.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We first created the &lt;code&gt;Order&lt;/code&gt; value to be saved in the remote purchase history. Then we get an &lt;strong&gt;environment variable&lt;/strong&gt; to figure out the Golem &lt;em&gt;template-id&lt;/em&gt; of the purchase history template. This is something we need to record when uploading the template to Golem, and set it to all shopping cart worker&#39;s when creating them. The remote URI consists of the template identifier and the &lt;em&gt;worker name&lt;/em&gt;, and in our example the worker name is the same as the &lt;strong&gt;user id&lt;/strong&gt; that the shopping cart belongs to. This guarantees that we will have a distinct purchase history worker for each user.&lt;/p&gt;
&lt;p&gt;When we have the URI, we just instantiate the &lt;strong&gt;generated stub&lt;/strong&gt; for by passing the remote worker&#39;s URI—and we get an interface that corresponds to the remote worker&#39;s exported interface! This way we can just call &lt;code&gt;add_order&lt;/code&gt; on it, passing the constructed order value.&lt;/p&gt;
&lt;p&gt;Everything else is handled by Golem. If this was the first order of the user, a new purchase history worker is created. Otherwise, the existing worker will be targeted, which is likely already in a suspended state, not actively in any worker executor&#39;s memory. Golem restores the worker&#39;s state and invokes the &lt;code&gt;add_order&lt;/code&gt; function on them, which adds the new order to the list of orders for that user, in a fully durable way, without the need for a database.&lt;/p&gt;
&lt;h3 id=&quot;how-does-it-work&quot;&gt;How does it work?&lt;/h3&gt;
&lt;p&gt;The generated cargo-make makefile just wraps a couple of &lt;code&gt;golem-cli stubgen&lt;/code&gt; commands.&lt;/p&gt;
&lt;p&gt;First, &lt;code&gt;stubgen generate&lt;/code&gt; creates a new Rust crate for each &lt;strong&gt;target&lt;/strong&gt; that has a similar interface as the original worker, but all the exported functions and interfaces are wrapped in a resource, which has to be instantiated with a &lt;strong&gt;worker URI&lt;/strong&gt;. This generated crate can be compiled to a WASM file (or &lt;code&gt;stubgen build&lt;/code&gt; can do that automatically) and it also contains a &lt;strong&gt;WIT&lt;/strong&gt; file describing this interface.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;stubgen add-stub-dependency&lt;/code&gt; command takes this generated interface specification and &lt;strong&gt;adds it&lt;/strong&gt; to an other worker&#39;s &lt;code&gt;wit&lt;/code&gt; folder—making it a &lt;em&gt;dependency&lt;/em&gt; of that worker. So the caller worker is not depending directly on the target worker, it depends on the &lt;strong&gt;generated stub&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;If we compile this caller worker to WASM, it will not only require host functions provided by Golem (such as the WASI interfaces or Golem specific APIs) but it will also require an &lt;strong&gt;implementation&lt;/strong&gt; of the stub interface. That&#39;s where the generated Rust crate comes into the picture—its compiled WASM &lt;strong&gt;implements&lt;/strong&gt; (exports) the stub interface while the caller WASM &lt;strong&gt;requires&lt;/strong&gt; (imports) it. WASM components can be composed so by combining the two we can get a result WASM that no longer tries to import the stub interface—it is going to be wired within the component—only the other dependencies the original modules had.&lt;/p&gt;
&lt;p&gt;One way to do this composition is to use &lt;code&gt;wasm-tools compose&lt;/code&gt;, but it is more convenient to use &lt;code&gt;golem-cli&lt;/code&gt; (or &lt;code&gt;golem-cloud-cli&lt;/code&gt;)&#39;s built-in command for it, called &lt;code&gt;stubgen compose&lt;/code&gt;. This is the last step the generated cargo-make file performs when running the &lt;code&gt;build-flow&lt;/code&gt; task.&lt;/p&gt;
&lt;p&gt;The following diagram demonstrates how the component&#39;s in the example are interacting with each other:&lt;/p&gt;
&lt;img src=&quot;/images/w2w-comm.webp&quot; alt=&quot;Diagram of worker-to-worker communication in Golem: composed WASM templates deployed as workers exchanging WASM RPC&quot; width=&quot;1234&quot; height=&quot;833&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;We have seen how the new Golem tools enable simple, fully-typed communication between &lt;strong&gt;workers&lt;/strong&gt;. Although the above demonstrated &lt;code&gt;cargo-make&lt;/code&gt;-based build is Rust specific, the other &lt;code&gt;stubgen&lt;/code&gt; commands are not: they can be used with any language that has WIT binding generator support (see &lt;a rel=&quot;external&quot; href=&quot;https://learn.golem.cloud/docs/building-templates/tier-2&quot;&gt;Golem&#39;s Tier 2 languages&lt;/a&gt;)—Rust, C, Go, JavaScript, Python and Scala.js.&lt;/p&gt;
&lt;p&gt;The remote calls are not only simple to use, they are also efficient, and they get translated to direct function calls when the source and the target workers are running on the same &lt;strong&gt;worker executor&lt;/strong&gt;. They are also fully durable, as all other external interaction running on Golem. This means we don&#39;t have to worry about failures when calling remote workers. Additionally, Golem applies retry policies in case of transient failures, and it makes sure that a remote invocation only happens once.&lt;/p&gt;
&lt;p&gt;This feature is ready to use both in the &lt;a rel=&quot;external&quot; href=&quot;http://github.com/golemcloud/golem&quot;&gt;open source&lt;/a&gt; and the &lt;a rel=&quot;external&quot; href=&quot;https://www.golem.cloud/&quot;&gt;cloud version&lt;/a&gt;.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>desert part 1 - features</title>
        <published>2024-02-19T00:00:00+00:00</published>
        <updated>2024-02-19T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/desert-1/"/>
        <id>https://blog.vigoo.dev/posts/desert-1/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/desert-1/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;This is the &lt;strong&gt;first part&lt;/strong&gt; of a series of blog posts about my serialization library, &lt;a rel=&quot;external&quot; href=&quot;https://vigoo.github.io/desert&quot;&gt;desert&lt;/a&gt;. I also gave an overview of this library on Functional Scala 2022 - you can check the &lt;a href=&quot;https://blog.vigoo.dev/posts/desert-1/@posts/funscala2022-talk.md&quot;&gt;talk on YouTube if interested&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In this post I&#39;m going to give an overview of the features this serialization library provides, and then going to dive into the details of how it supports evolving data types.&lt;/p&gt;
&lt;h2 id=&quot;where-is-it-coming-from&quot;&gt;Where is it coming from?&lt;/h2&gt;
&lt;p&gt;The idea of creating &lt;code&gt;desert&lt;/code&gt; came after some serious disappointment in our previously chosen serialization library. It was used for serialization of both persistent Akka actors and for the distributed actor messages, and it turned out that just by updating the Scala version from 2.12 to 2.13 completely broke our serialization format.&lt;/p&gt;
&lt;p&gt;None of the alternatives looked good enough to me - I wanted something that is code first and fits well to our functional Scala style. Support for multiple platforms or programming languages were not a requirement.&lt;/p&gt;
&lt;p&gt;So I started thinking about what would a perfect serialization library look like, at least for our use cases? It was something that has first-class support for ADTs, for Scala&#39;s collection libraries (I don&#39;t want to see Scala lists serialized via Java reflection ever again!), with a focus of supporting evolution of the serialized data types. We &lt;em&gt;knew&lt;/em&gt; that our persisted data and actor messages will change over time, and we had to be able to survive these changes without any downtime.&lt;/p&gt;
&lt;h2 id=&quot;features&quot;&gt;Features&lt;/h2&gt;
&lt;p&gt;Let&#39;s just go through all the features provided by the library before we talk about how exactly it supports these kind of changes in the serialized data structures.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;desert&lt;/code&gt; is a Scala library. As probably expected, it captures the core concept of binary serialization though a simple &lt;code&gt;trait&lt;/code&gt; called &lt;code&gt;BinaryCodec[T]&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinarySerializer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; serialize&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; context&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SerializationContext&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; contramap&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;U&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; U&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinarySerializer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;U&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; contramapOrFail&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;U&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; U&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Either&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DesertFailure&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinarySerializer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;U&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryDeserializer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; deserialize&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; ctx&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DeserializationContext&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; T&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; map&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;U&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; U&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryDeserializer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;U&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; mapOrFail&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;U&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Either&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DesertFailure&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;U&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryDeserializer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;U&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinarySerializer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryDeserializer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These &lt;code&gt;BinaryCodec&lt;/code&gt; instances should be made implicitly available for each type we need to serialize. There are multiple ways to create an instance of a binary codec:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;There are many built-in codecs for primitive types, standard collections, date-time classes, etc.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;contramap&lt;/code&gt; operators can be used to construct new codecs from existing ones&lt;/li&gt;
&lt;li&gt;There is a codec derivation macro for ADTs (case classes and sealed traits / enums)&lt;/li&gt;
&lt;li&gt;Custom implementation can directly read/write the binary data and access some of the built-in features like the type registry, references, string deduplication and compression&lt;/li&gt;
&lt;li&gt;It is also possible to define these custom implementations in a more functional way on top of &lt;code&gt;ZPure&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Under the hood there is a simple &lt;code&gt;BinaryInput&lt;/code&gt; / &lt;code&gt;BinaryOutput&lt;/code&gt; abstraction which is extensible, by default implemented for Java &lt;code&gt;InputStream&lt;/code&gt; and &lt;code&gt;OutputStream&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;On the lowest level, in addition to having an interface for serializing primitive types we also have support for &lt;strong&gt;variable length integer encoding&lt;/strong&gt; and for gzip &lt;strong&gt;compression&lt;/strong&gt;. Custom codecs can also use the built-in &lt;strong&gt;string deduplication&lt;/strong&gt; feature, and encode cyclic graphs using support for storing &lt;strong&gt;references&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Sometimes you want to serialize only a part of your data structure - a real-world example we had was having a set of &lt;em&gt;typed actor messages&lt;/em&gt; where only a subset of the cases were designed to be used between different nodes. Some cases were only used locally, and in those we would store things that are not serializable at all - for example open websocket connection handles. This is supported by &lt;code&gt;desert&lt;/code&gt; by having the concept of both &lt;strong&gt;transient fields&lt;/strong&gt; and &lt;strong&gt;transient constructors&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;What if a field is not an ADT but contains a reference to an arbitrary type with a given interface? Or if we don&#39;t know the root type of a message, only a set of possible types which are otherwise unrelated? The library provides a &lt;strong&gt;type registry&lt;/strong&gt; for this purpose. Every type registered into this will have an associated identifier, and in places where we don&#39;t know the exact type, we can use these to get the codec by it&#39;s unique ID from the type registry.&lt;/p&gt;
&lt;p&gt;On the top level &lt;code&gt;desert&lt;/code&gt; also comes with a set of &lt;strong&gt;integration modules&lt;/strong&gt;. The following modules are available at the time of writing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;desert-akka&lt;/code&gt; provides helper functions to serialize from/to &lt;code&gt;ByteString&lt;/code&gt;, provides codecs for both typed and untyped &lt;code&gt;ActorRef&lt;/code&gt;s, and provides an implementation of Akka&#39;s &lt;code&gt;Serializer&lt;/code&gt; interface.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;desert-cats&lt;/code&gt; adds codecs for &lt;code&gt;Validation&lt;/code&gt;, &lt;code&gt;NonEmptyList&lt;/code&gt;, &lt;code&gt;NonEmptySet&lt;/code&gt; and &lt;code&gt;NonEmptyMap&lt;/code&gt; from the &lt;a rel=&quot;external&quot; href=&quot;https://typelevel.org/cats/&quot;&gt;cats library&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;desert-cats-effect&lt;/code&gt; gives a &lt;a rel=&quot;external&quot; href=&quot;https://typelevel.org/cats-effect/&quot;&gt;cats-effect&lt;/a&gt; &lt;code&gt;IO&lt;/code&gt; version of the top level serialization and deserialization functions&lt;/li&gt;
&lt;li&gt;&lt;code&gt;desert-zio&lt;/code&gt; provides &lt;code&gt;ZIO&lt;/code&gt; version of the top level serialization and deserialization functions and adds codec and helper functions to work with &lt;code&gt;Chunk&lt;/code&gt;s,&lt;/li&gt;
&lt;li&gt;&lt;code&gt;desert-zio-prelude&lt;/code&gt; provides a more functional interface for defining custom codecs, as well as having built-in codecs for&lt;/li&gt;
&lt;li&gt;&lt;code&gt;desert-shardcake&lt;/code&gt; provides easy integration within the &lt;a rel=&quot;external&quot; href=&quot;https://devsisters.github.io/shardcake/&quot;&gt;Shardcake&lt;/a&gt; library&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are two more modules which implement the same core functionality, &lt;strong&gt;codec derivation&lt;/strong&gt;, with different tradeoffs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;desert-shapeless&lt;/code&gt; is a &lt;a rel=&quot;external&quot; href=&quot;https://github.com/milessabin/shapeless&quot;&gt;shapeless&lt;/a&gt; based codec deriver, the original implementation of &lt;code&gt;desert&lt;/code&gt;&#39;s derivation logic. It only works for &lt;strong&gt;Scala 2&lt;/strong&gt; but it has no additional requirements.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;desert-zio-scheme&lt;/code&gt; is an alternative implementation of the same codec derivation, built on the &lt;code&gt;Deriver&lt;/code&gt; feature of &lt;a rel=&quot;external&quot; href=&quot;https://zio.dev/zio-schema/&quot;&gt;zio-schema&lt;/a&gt;. This works both with &lt;strong&gt;Scala 2&lt;/strong&gt; and &lt;strong&gt;Scala 3&lt;/strong&gt;, and supposed to provide better compile-time error messages, but requires to derive an implicit &lt;code&gt;Schema&lt;/code&gt; for each serialized type beside the binary codec.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I wrote a &lt;a href=&quot;https://blog.vigoo.dev/posts/desert-1/@posts/zio-schema-deriving.md&quot;&gt;detailed post about typeclass derivation&lt;/a&gt; a few months ago.&lt;/p&gt;
&lt;h2 id=&quot;data-evolution&quot;&gt;Data evolution&lt;/h2&gt;
&lt;p&gt;Let&#39;s see in details what it means that &lt;code&gt;desert&lt;/code&gt; supports &lt;em&gt;evolving&lt;/em&gt; data structures.&lt;/p&gt;
&lt;h3 id=&quot;primitives-vs-newtype-wrappers&quot;&gt;Primitives vs newtype wrappers&lt;/h3&gt;
&lt;p&gt;Let&#39;s start with a simple example: we are serializing a single &lt;code&gt;Int&lt;/code&gt;. The default codec just uses the fixed width 32-bit representation of the integer:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; x&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 100&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;results in:&lt;/p&gt;
&lt;table style=&quot;border-collapse: initial; border: 0px; width: auto; color: black&quot;&gt;
  &lt;tr&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;100&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;Imagine that later we decide that &lt;code&gt;Int&lt;/code&gt; is just too generic, and what we have here is in fact a &lt;code&gt;Coordinate&lt;/code&gt;. We can define a a newtype wrapper like the following:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AnyVal&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and then define the binary codec either by using &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;contramap&lt;/code&gt; on the integer codec, or by using the &lt;code&gt;deriveForWrapper&lt;/code&gt; macro:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Coordinate&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; codec&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DeriveBinaryCodec&lt;/span&gt;&lt;span&gt;.deriveForWrapper&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The binary representation of a &lt;code&gt;Coordinate&lt;/code&gt; will be exactly the same as for an &lt;code&gt;Int&lt;/code&gt;, so we are still fully backward and forward compatible regarding our serialization format:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; x&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Coordinate&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;100&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;results in:&lt;/p&gt;
&lt;table style=&quot;border-collapse: initial; border: 0px; width: auto; color: black&quot;&gt;
  &lt;tr&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;100&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;h3 id=&quot;collections&quot;&gt;Collections&lt;/h3&gt;
&lt;p&gt;First let&#39;s see what happens if we try to serialize a pair of coordinates:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; xy&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;results in:&lt;/p&gt;
&lt;table style=&quot;border-collapse: initial; border: 0px; width: auto; color: black&quot;&gt;
  &lt;tr&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(147, 154, 231); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;1&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;2&lt;/td&gt;    
  &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;the binary representation starts with a &lt;code&gt;0&lt;/code&gt;, which is an &lt;em&gt;ADT header&lt;/em&gt;. We will talk about it later. The rest of the data is just a flat representation of the two coordinates, taking in total 9 bytes.&lt;/p&gt;
&lt;p&gt;Now we start storing arrays of these coordinates:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; coordinates&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Array&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Array&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;4&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;6&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Arrays are serialized simply by writing the length of the array as a variable-length integer and then serializing all elements.&lt;/p&gt;
&lt;table style=&quot;border-collapse: initial; border: 0px; width: auto; color: black&quot;&gt;
  &lt;tr&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(147, 154, 231); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;6&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;    
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;1&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;2&lt;/td&gt; 
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;    
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;3&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;4&lt;/td&gt; 
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;    
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;5&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;6&lt;/td&gt;    
  &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;The variable-length integer encoding of &lt;code&gt;3&lt;/code&gt; is &lt;code&gt;6&lt;/code&gt;, and that is simply followed by the three 9-byte long serialized representation of the coordinate pairs.&lt;/p&gt;
&lt;p&gt;What if we decide we don&#39;t want to use &lt;code&gt;Array&lt;/code&gt; but ZIO&#39;s &lt;code&gt;Chunk&lt;/code&gt; instead? Or if we realize our data model is more precise if we talk about a &lt;em&gt;set&lt;/em&gt; of coordinate pairs? Nothing! Desert uses the same encoding for all collection types, allowing us to always choose the best data type without being worried about breaking the serialization format. In some collections, such as linked lists, there is no way to know the number of elements without iterating through the whole data set. Desert supports these collection types by writing &lt;code&gt;-1&lt;/code&gt; as the number of elements, and then prefixing each element with a single byte where &lt;code&gt;1&lt;/code&gt; represents we have a next element and &lt;code&gt;0&lt;/code&gt; that we don&#39;t. This is actually exactly the same binary format as a series of &lt;code&gt;Option[T]&lt;/code&gt; values where the first and only &lt;code&gt;None&lt;/code&gt; represents the end of the sequence.&lt;/p&gt;
&lt;h3 id=&quot;records&quot;&gt;Records&lt;/h3&gt;
&lt;p&gt;Maybe using tuples of coordinates was a good idea in the beginning but as our data model evolves we want to introduced a named record type instead:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;x&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Coordinate&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;y&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Coordinate&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can use &lt;code&gt;desert&lt;/code&gt;&#39;s codec derivation feature to get a binary codec for this type:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; schema&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Point&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DeriveSchema&lt;/span&gt;&lt;span&gt;.gen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; codec&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Point&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DerivedBinaryCodec&lt;/span&gt;&lt;span&gt;.derive&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When using &lt;code&gt;desert-zio-scheme&lt;/code&gt; we also need to derive a &lt;code&gt;Schema&lt;/code&gt; instance - this is not required when using the &lt;code&gt;desert-shapeless&lt;/code&gt; version of the codec derivation.&lt;/p&gt;
&lt;p&gt;Let&#39;s see how &lt;code&gt;desert&lt;/code&gt; serializes an instance of this &lt;code&gt;Point&lt;/code&gt; type:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; pt&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;results in:&lt;/p&gt;
&lt;table style=&quot;border-collapse: initial; border: 0px; width: auto; color: black&quot;&gt;
  &lt;tr&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(147, 154, 231); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;1&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;2&lt;/td&gt;    
  &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;This is exactly the same as the tuple&#39;s binary representation was, which probably isn&#39;t a big surprise as they are structurally equivalent. Still this is an important property as it allows us to replace any tuple with an equivalent record type and keeping the binary format exactly the same!&lt;/p&gt;
&lt;p&gt;If we have to change a record&#39;s type, we can only change any of its fields if that field&#39;s new type has a compatible binary representation with the old one. All the cases described in this post are valid data evolution steps. Beside those there are a few special type of changes &lt;code&gt;desert&lt;/code&gt; supports for records. Let&#39;s see!s&lt;/p&gt;
&lt;h3 id=&quot;adding-a-field&quot;&gt;Adding a field&lt;/h3&gt;
&lt;p&gt;As a next step let&#39;s imagine our data type requires a new field. Let&#39;s add a &lt;code&gt;z&lt;/code&gt; coordinate to our point:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;x&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Coordinate&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;y&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Coordinate&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;z&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Coordinate&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; codec&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Point&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DerivedBinaryCodec&lt;/span&gt;&lt;span&gt;.derive &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; pt&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Serializing this &lt;code&gt;pt&lt;/code&gt; value results in:&lt;/p&gt;
&lt;table style=&quot;border-collapse: initial; border: 0px; width: auto; color: black&quot;&gt;
  &lt;tr&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(147, 154, 231); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;1&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;2&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;3&lt;/td&gt;    
  &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;If we try to read this value with the &lt;em&gt;deserializer&lt;/em&gt; of our original &lt;code&gt;Point&lt;/code&gt; type, it will read &lt;code&gt;Point(Coordinate(1), Coordinate(2))&lt;/code&gt;, but the next deserialized value will be corrupt as the input stream will point to the beginning of the &lt;code&gt;0, 0, 0, 3&lt;/code&gt; value. Similarly, if we would try to read a binary serialized with the old &lt;code&gt;Point&lt;/code&gt; &lt;em&gt;serializer&lt;/em&gt;, it would read the next four bytes from the data stream which, if even exists, belongs to some other serialized element.&lt;/p&gt;
&lt;p&gt;The solution for this in &lt;code&gt;desert&lt;/code&gt; is to &lt;strong&gt;explicitly document data evolution&lt;/strong&gt;. This is done by listing each modification in an &lt;em&gt;attribute&lt;/em&gt; called &lt;code&gt;evolutionSteps&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;evolutionSteps&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;FieldAdded&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;x&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Coordinate&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;y&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Coordinate&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;z&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Coordinate&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; codec&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Point&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DerivedBinaryCodec&lt;/span&gt;&lt;span&gt;.derive &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; pt&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this annotation, we mark &lt;code&gt;z&lt;/code&gt; as a newly added field, and provide a &lt;em&gt;default value&lt;/em&gt; for it which will be used in cases when reading an old version of the serialized data which did not have this field yet. Every time we change the data type we record the change as a new element in this attribute. There are other supported evolution step types as we will see soon.&lt;/p&gt;
&lt;p&gt;But first let&#39;s see what changes in the binary representation of &lt;code&gt;Point&lt;/code&gt; now that we added this attribute!&lt;/p&gt;
&lt;table style=&quot;border-collapse: initial; border: 0px; width: auto; color: black&quot;&gt;
  &lt;tr&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(147, 154, 231); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;1&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;16&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;8&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;1&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;2&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;3&lt;/td&gt;    
  &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;Now that we have an &lt;em&gt;evolution step&lt;/em&gt; the first byte, which was always &lt;code&gt;0&lt;/code&gt; before, becomes &lt;code&gt;1&lt;/code&gt;. Every evolution step increases this value, which is interpreted as the type&#39;s &lt;em&gt;version&lt;/em&gt;. For each ADT which has a version other than 0, this first version byte is followed by a list of the binary encoding of the evolution steps. Here the &lt;code&gt;16&lt;/code&gt; is the variable-length encoding of the value &lt;code&gt;8&lt;/code&gt;, which is the length of the &quot;version 0&quot; part of the data type. This is followed by &lt;code&gt;8&lt;/code&gt; which is just the variable-length encoding of the value &lt;code&gt;4&lt;/code&gt;, and it represents the &lt;em&gt;field added&lt;/em&gt; evolution step, encoding the newly added field&#39;s size.&lt;/p&gt;
&lt;p&gt;With this format when the &lt;em&gt;old&lt;/em&gt; deserializer reads the point, it knows it needs to skip additional 4 bytes after reading the &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; coordinates. Also when the &lt;em&gt;new&lt;/em&gt; deserializer encounters an old point, that binary data will begin with &lt;code&gt;0&lt;/code&gt;, so the deserializer is aware that it&#39;s an older version and can set the deserialized value&#39;s &lt;code&gt;z&lt;/code&gt; coordinate to the provided default.&lt;/p&gt;
&lt;p&gt;By documenting the data type change we get full forward and backward compatibility in this case. The cost is that instead of &lt;code&gt;13&lt;/code&gt; bytes, now each &lt;code&gt;Point&lt;/code&gt; takes &lt;code&gt;15&lt;/code&gt; bytes.&lt;/p&gt;
&lt;h3 id=&quot;making-a-field-optional&quot;&gt;Making a field optional&lt;/h3&gt;
&lt;p&gt;Another special data type change is making an existing field optional. Staying with the previous example we could change our &lt;code&gt;Point&lt;/code&gt; type like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;evolutionSteps&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  FieldAdded&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  FieldMadeOptional&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;x&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Coordinate&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;y&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Coordinate&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;z&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; codec&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Point&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DerivedBinaryCodec&lt;/span&gt;&lt;span&gt;.derive &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; pt&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Coordinate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;None&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This of course can no longer guarantee full forward and backward compatibility - but it can be useful as an intermediate step in getting rid of some unused parts of the data model, while still being able to access it when it&#39;s available from older serialized data.&lt;/p&gt;
&lt;p&gt;This evolution step is represented by a variable-length integer &lt;code&gt;-1&lt;/code&gt; in the ADT header. All positive values are representing the &lt;em&gt;field added&lt;/em&gt; case, with the actual value containing the size of the added field. -1 is a special marker for field removed, and it is followed by another variable-length integer encoding the field position which has been made optional. Then serializing the &lt;code&gt;Option&lt;/code&gt; field, the integer gets prefixed by a &lt;code&gt;1&lt;/code&gt; if the value was &lt;code&gt;Some&lt;/code&gt;, or the whole option is serialized as a &lt;code&gt;0&lt;/code&gt; if it was &lt;code&gt;None&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The total serialized record of the above example would look like this:&lt;/p&gt;
&lt;table style=&quot;border-collapse: initial; border: 0px; width: auto; color: black&quot;&gt;
  &lt;tr&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(147, 154, 231); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;2&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;16&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;2&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(60, 200, 150); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;1&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(60, 200, 150); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;1&lt;/td&gt;    
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(154, 231, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;1&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 154, 147); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;2&lt;/td&gt;
    &lt;td style=&quot;border: 1px solid; padding: 6px; text-align: center; background-color: rgb(231, 147, 200); margin: 0px; border-spacing: 1px; font-family: monospace; font-weight: normal&quot;&gt;0&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;The first byte is now &lt;code&gt;2&lt;/code&gt; as we have two evolution steps. The next one still defines that the original part of the data is 8 bytes long, the third byte shows that this time the new &lt;em&gt;z&lt;/em&gt; field is taking only 1 byte (as it was set to &lt;code&gt;None&lt;/code&gt;). The header is now containing two more bytes, as described above: the first &lt;code&gt;1&lt;/code&gt; means a field has been made optional, and the second points to the field.&lt;/p&gt;
&lt;p&gt;This can be still loaded by the very first point serializer (or even as the coordinate pair tuple), as everything after the first two coordinates would be skipped. It can also be loaded as a &lt;code&gt;Point&lt;/code&gt; with non-optional z coordinate, but only if the serialized data is a &lt;code&gt;Some&lt;/code&gt;. So in the above example it would lead to a deserialization error. The change is fully backward compatible so our latest deserializer can still load all the variants we have seen before.&lt;/p&gt;
&lt;h3 id=&quot;removing-a-field&quot;&gt;Removing a field&lt;/h3&gt;
&lt;p&gt;The final special data evolution step supported by the library is &lt;em&gt;removing&lt;/em&gt; a field completely. This is more limited than the previous ones though - backward compatibility is easy, newer versions of the deserializer just have to skip the removed fields which they can easily do. But forward compatibility is only possible if the removed field was an &lt;strong&gt;option field&lt;/strong&gt; - that&#39;s the only type &lt;code&gt;desert&lt;/code&gt; can automatically provide a default value, &lt;code&gt;None&lt;/code&gt; for.&lt;/p&gt;
&lt;p&gt;The binary header for removing a field needs to store the actual &lt;em&gt;field name&lt;/em&gt; because it cannot otherwise identify the field which is not actually in the rest of the data set. To make this more space-efficient, &lt;code&gt;desert&lt;/code&gt; uses string deduplication and only needs to serialize the actual field name once.&lt;/p&gt;
&lt;h3 id=&quot;sum-types&quot;&gt;Sum types&lt;/h3&gt;
&lt;p&gt;Scala 2 sealed trait hierarchies and Scala 3 enums are simply serialized with the same techniques mentioned above, but with a &lt;em&gt;constructor ID&lt;/em&gt; serialized as a prefix to the binary. Constructor identifiers are associated in order - as the constructors appear in the source code. This means that adding new constructors is backward and forward compatible, as long as they are added as the &lt;em&gt;last&lt;/em&gt; constructor. Otherwise the identifiers will be rearranged and binary compatibility breaks.&lt;/p&gt;
&lt;h3 id=&quot;transients&quot;&gt;Transients&lt;/h3&gt;
&lt;p&gt;It is possible to make a previously non-transient field transient and maintain binary compatibility. The rules are the same as for &lt;em&gt;removing&lt;/em&gt; a field.&lt;/p&gt;
&lt;h3 id=&quot;type-registry&quot;&gt;Type registry&lt;/h3&gt;
&lt;p&gt;As mentioned earlier, a &lt;em&gt;type registry&lt;/em&gt; can be used to associate identifiers to types, and then serialize arbitrary values using these identifiers. Maintaining the stability of this mapping is also very important when evolving data types. What if we want to delete a type which was added to the type registry because we never want to use it again, and we already migrated our serialized data and we are sure we will never encounter that ID again during deserialization?&lt;/p&gt;
&lt;p&gt;We still cannot just simply remove the entry from the type registry, because it will break all the following identifiers as they get assigned sequentially. The library has a solution for this - it is possible to registry empty placeholders where we previously had an actual type - it will maintain the identifier order, but will lead to a runtime error when that identifier is encountered during deserialization.&lt;/p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;
&lt;p&gt;In this post I summarized the key features of the &lt;code&gt;desert&lt;/code&gt; serialization library, and explained in detail how it supports changes into the data model while trying to keep maximal backward and forward compatibility.&lt;/p&gt;
&lt;p&gt;In the next post I will show how the same library can be implemented for &lt;strong&gt;Rust&lt;/strong&gt;, how the Scala solution maps into different concepts in the other language and what difficulties I&#39;ve encountered during the migration process.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>[Video] Beyond OpenAPI @ Functional Scala 2023</title>
        <published>2024-01-21T00:00:00+00:00</published>
        <updated>2024-01-21T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/funscala2023-talk/"/>
        <id>https://blog.vigoo.dev/posts/funscala2023-talk/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/funscala2023-talk/">&lt;p&gt;My talk at &lt;a rel=&quot;external&quot; href=&quot;https://www.functionalscala.com/&quot;&gt;Functional Scala 2023&lt;/a&gt; about my experience with generating client libraries from OpenAPI specifications, and an alternative code-first approach using &lt;a rel=&quot;external&quot; href=&quot;https://github.com/zio/zio-http/&quot;&gt;ZIO Http&lt;/a&gt;.&lt;/p&gt;
&lt;iframe width=&quot;800&quot; height=&quot;450&quot; src=&quot;https://www.youtube.com/embed/wwKs37GVubg?si=dMEqTmjUgGBhYB38&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share&quot; allowfullscreen&gt;&lt;/iframe&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Type class derivation with ZIO Schema</title>
        <published>2023-12-02T00:00:00+00:00</published>
        <updated>2023-12-02T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/zio-schema-deriving/"/>
        <id>https://blog.vigoo.dev/posts/zio-schema-deriving/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/zio-schema-deriving/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Making the compiler to automatically &lt;em&gt;derive&lt;/em&gt; implementations of a type class for your custom algebraic data types is a common technique in programming languages. Haskell, for example, has built-in syntax for it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; Literal&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; StringLit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;             |&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; BoolLit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;               deriving&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;Show&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and Rust is using macros instantiated by &lt;em&gt;annotations&lt;/em&gt; to do the same:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;deriving&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;enum&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Literal&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  StringLit&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;  BoolLit&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;bool&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Scala 3 has its own syntax for deriving type classes:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;enum&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Literal&lt;/span&gt;&lt;span&gt; deriving &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Show&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; StringLit&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BoolLit&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Boolean&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;but the more traditional way that works with Scala 2 as well is to define an implicit in the type&#39;s companion object by an explicit macro invocation:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;sealed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Literal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Literal&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; StringLit&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Literal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BoolLit&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Literal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; show&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Show&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Literal&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DeriveShow&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Literal&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All these examples from different languages are common in a way that in order to automatically generate an implementation for an arbitrary type we need to be able to gather information about these types as (compilation-) runtime values, and to generate new code fragments (or actual abstract syntax tree) which then takes part of to the compilation, producing the same result as writing the implementation by hand.&lt;/p&gt;
&lt;p&gt;This means using some kind of macro, depending on which programming language we use. But writing these macros is never easy, and in some cases can be very different from the usual way of writing code - so in each programming language people are writing &lt;em&gt;libraries&lt;/em&gt; helping type class derivation in one way or the other.&lt;/p&gt;
&lt;p&gt;In this post I will show a library like that for Scala, the &lt;code&gt;Deriver&lt;/code&gt; feature of &lt;a rel=&quot;external&quot; href=&quot;https://zio.dev/zio-schema/&quot;&gt;ZIO Schema&lt;/a&gt; that I added at the end of last year (2022). But before that let&#39;s see a real world example and what alternatives we had.&lt;/p&gt;
&lt;h2 id=&quot;example&quot;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://vigoo.github.io/desert/&quot;&gt;Desert&lt;/a&gt; is a Scala serialization library I wrote in 2020. Not surprisingly in the core of Desert is a &lt;em&gt;trait&lt;/em&gt; that describes serialization and deserailization of a type &lt;code&gt;T&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinarySerializer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryDeserializer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinarySerializer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; serialize&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; context&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SerializationContext&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryDeserializer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; deserialize&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; ctx&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DeserializationContext&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; T&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Although we can implement these traits manually, in order to take advantage of Desert&#39;s type evolution capabilities, for complex types like &lt;em&gt;case classes&lt;/em&gt; or &lt;em&gt;enums&lt;/em&gt; we want the user to be able to write something like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;x&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;y&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;z&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; codec&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Point&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DerivedBinaryCodec&lt;/span&gt;&lt;span&gt;.derive&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;alternatives&quot;&gt;Alternatives&lt;/h2&gt;
&lt;h3 id=&quot;scala-3-mirrors&quot;&gt;Scala 3 mirrors&lt;/h3&gt;
&lt;p&gt;First of all, &lt;strong&gt;Scala 3&lt;/strong&gt; has some built-in support for implementing derivation macros using its &lt;code&gt;Mirror&lt;/code&gt; type, explained in the &lt;a rel=&quot;external&quot; href=&quot;https://docs.scala-lang.org/scala3/reference/contextual/derivation.html&quot;&gt;official documentation&lt;/a&gt;. We can see a simple example of this technique &lt;a rel=&quot;external&quot; href=&quot;https://github.com/zio/zio/blob/series%2F2.x/test-magnolia/shared/src/main/scala-3/zio/test/magnolia/DeriveGen.scala&quot;&gt;in the ZIO codebase&lt;/a&gt; where I have implemented a deriving mechanism for the &lt;code&gt;Gen[R, A]&lt;/code&gt; trait which is Scala 3 specific. (The Scala 2 version is using the Magnolia library, introduced below, which did not have a Scala 3 version back then). The &lt;code&gt;Mirror&lt;/code&gt; values are summoned by the compiler and they provide the type information:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;inline&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; gen&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;using&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; m&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Mirror&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Of&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DeriveGen&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DeriveGen&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; derive&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Gen&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; elemInstances&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; summonAll&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;m.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;MirroredElemTypes&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      inline&lt;/span&gt;&lt;span&gt; m &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;match&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; s&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Mirror&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;SumOf&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;     =&amp;gt;&lt;/span&gt;&lt;span&gt; genSum&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;s, elemInstances&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; p&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Mirror&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProductOf&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; genProduct&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;p, elemInstances&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As this function is an &lt;a rel=&quot;external&quot; href=&quot;https://docs.scala-lang.org/scala3/reference/metaprogramming/inline.html&quot;&gt;inline function&lt;/a&gt;, it gets evaluated compile time, using this summoned &lt;code&gt;Mirror&lt;/code&gt; value to produce an implementation of &lt;code&gt;Gen[Any, T]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This is a little low level and requires knowledge of inline functions and things like &lt;code&gt;summonAll&lt;/code&gt; etc., but otherwise a relatively easy way to solve the type class derivation problem. But it is Scala 3 only.&lt;/p&gt;
&lt;p&gt;Back in 2020 when I wrote the first version of Desert, there was no Scala 3 at all, and the three main way to do this were&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;writing a (Scala 2) macro by hand&lt;/li&gt;
&lt;li&gt;using &lt;a rel=&quot;external&quot; href=&quot;https://github.com/milessabin/shapeless&quot;&gt;Shapeless&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;using &lt;a rel=&quot;external&quot; href=&quot;https://github.com/softwaremill/magnolia&quot;&gt;Magnolia&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;scala-2-macros&quot;&gt;Scala 2 macros&lt;/h3&gt;
&lt;p&gt;Writing a custom derivation logic with Scala 2 macros is not easy, but it is completely possible. It starts by defining a &lt;a rel=&quot;external&quot; href=&quot;https://www.scala-lang.org/api/2.13.12/scala-reflect/scala/reflect/macros/whitebox/Context.html&quot;&gt;whitebox macro&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Derive&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; derive&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; macro deriveImpl&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; deriveImpl&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; c.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WeakTypeTag&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;    c&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; whitebox.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Context&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; c.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Tree&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    import&lt;/span&gt;&lt;span&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;universe&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The job of &lt;code&gt;deriveImpl&lt;/code&gt; is to examine the type of &lt;code&gt;A&lt;/code&gt; and generate a &lt;code&gt;Tree&lt;/code&gt; that represents the implementation of the &lt;code&gt;BinaryCodec&lt;/code&gt; trait for &lt;code&gt;A&lt;/code&gt;. We can start by getting a &lt;code&gt;Type&lt;/code&gt; value for &lt;code&gt;A&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; tpe&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Type&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; weakTypeOf&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and then use that to get all kind of information about this type. For example to check if it is a &lt;em&gt;case class&lt;/em&gt;, we could write&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; isCaseClass&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;tpe&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Type&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Boolean&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; tpe.typeSymbol.asClass.isCaseClass&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and then try to collect all the fields of that case class:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; fields&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; tpe.decls.sorted.collect &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; p&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TermSymbol&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; if&lt;/span&gt;&lt;span&gt; p.isCaseAccessor &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; !&lt;/span&gt;&lt;span&gt;p.isMethod &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; p&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As we can see this is a very direct and low level way to work with the types, much harder then the &lt;code&gt;Mirror&lt;/code&gt; type we used for Scala 3. Once we gathered all the necessary information for generating the derived type class, we can use &lt;em&gt;quotes&lt;/em&gt; to construct fragments of Scala AST:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; fieldSerializationStatements&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; codec&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; q&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;$&lt;/span&gt;&lt;span&gt;tpe&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; T&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; S&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; U&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;$&lt;/span&gt;&lt;span&gt;fieldSerializationStatements&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the end, this quoted &lt;code&gt;codec&lt;/code&gt; value is a &lt;code&gt;Tree&lt;/code&gt; which we can return from the macro.&lt;/p&gt;
&lt;h3 id=&quot;shapeless&quot;&gt;Shapeless&lt;/h3&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://github.com/milessabin/shapeless&quot;&gt;Shapeless&lt;/a&gt; is a library for &lt;em&gt;type level programming&lt;/em&gt; in Scala 2 (and there is a &lt;a rel=&quot;external&quot; href=&quot;https://github.com/typelevel/shapeless-3&quot;&gt;new version&lt;/a&gt; for Scala 3 too). It provides things like type-level heterogeneous lists and all of operations on them, and it also defines &lt;em&gt;macros&lt;/em&gt; that can convert an arbitrary case class into a &lt;em&gt;generic representation&lt;/em&gt;, which is essentially a type level list containing all the fields. Similarly it can convert an arbitrary sum type (sealed trait in Scala 2) to a generic representation of coproducts. For example the &lt;code&gt;Point&lt;/code&gt; case class we used in an earlier example would be represented like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;x&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;y&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;z&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; point&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; genericPoint&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt; ::&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt; ::&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt; ::&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HNil&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; type&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;  1&lt;/span&gt;&lt;span&gt; ::&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 2&lt;/span&gt;&lt;span&gt; ::&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 3&lt;/span&gt;&lt;span&gt; ::&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HNil&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; labelledGenericPoint&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; type too complex to show here&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; -&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; ::&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; -&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; ::&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; -&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; ::&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HNil&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; value&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In connection with type class derivation the idea is that by using Shapeless we no longer have to write macros to extract type information for our types - we can work with these generic representations instead using advanced type level programming techniques. So the complexity of writing macros is replaced with the complexity of doing type level computation.&lt;/p&gt;
&lt;p&gt;Let&#39;s see how it would look like. First we start by creating a &lt;code&gt;derive&lt;/code&gt; method that gets the type we are deriving the codec for as a type parameter:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; derive&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This &lt;code&gt;T&lt;/code&gt; is an arbitrary type, for example our &lt;code&gt;Point&lt;/code&gt; structure. In order to get its generic representation provided by Shapeless we have to start using type level techniques, by introducing new type parameters for the things we want to calculate (as types) and implicits to drive these computations. The following version, when compiles, will &quot;calculate&quot; the generic representation of &lt;code&gt;T&lt;/code&gt; as the type parameter &lt;code&gt;H&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; derive&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;H&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; gen&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LabelledGeneric&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;H&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; serialize&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; context&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SerializationContext&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; h&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; H&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; gen.to&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; generic representation of (value: T)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;      //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is not that hard yet but we need to recursively summon implicit codecs for our fields, so we can&#39;t just use this &lt;code&gt;H&lt;/code&gt; value to go through all the fields in a traditional way - we need to traverse it on the type level.&lt;/p&gt;
&lt;p&gt;To do that we need to write our own type level computations implemented as implicit instances for &lt;code&gt;HNil&lt;/code&gt; and &lt;code&gt;::&lt;/code&gt; etc. The serialization part of the codec would look something like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; hnilSerializer&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinarySerializer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;HNil&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinarySerializer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;HNil&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; serialize&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HNil&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; context&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SerializationContext&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;      //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; no (more) fields&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; hlistSerializer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;K&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Symbol&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;H&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HList&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  witness&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Witness&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;K&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; type level extraction of the field&amp;#39;s name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  headSerializer&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinarySerializer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;H&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; type class summoning for the field&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  tailSerializer&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinarySerializer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; hlist recursion&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinarySerializer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;FieldType&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;K&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;H&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; ::&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Similar methods have to be implemented for coproducts too, and also in the codec example we would have to simultaneously derive the serializer &lt;em&gt;and&lt;/em&gt; the deserializer. A real implementation would also require access to the &lt;em&gt;annotations&lt;/em&gt; of various fields to drive the serialization logic, which requires more and more type level calculations and complicates these type signatures.&lt;/p&gt;
&lt;p&gt;I did chose to use Shapeless in the first version of Desert, and the real &lt;code&gt;derive&lt;/code&gt; method has the following signature:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; derive&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;H&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Ks&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HList&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Trs&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HList&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Trcs&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HList&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;KsTrs&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HList&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TH&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      gen&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LabelledGeneric&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;H&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      keys&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Lazy&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Symbols&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;H&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Ks&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      transientAnnotations&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Annotations&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;transientField, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Trs&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      transientConstructorAnnotations&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Annotations&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;transientConstructor, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Trcs&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      taggedTransients&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TagTransients&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;H&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Trs&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Trcs&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TH&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      zip&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Zip&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Ks&lt;/span&gt;&lt;span&gt; ::&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Trs&lt;/span&gt;&lt;span&gt; ::&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HNil&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;KsTrs&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      toList&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ToTraversable&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;KsTrs&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;List&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Symbol&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;transientField&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      serializationPlan&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Lazy&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;SerializationPlan&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TH&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      deserializationPlan&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Lazy&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DeserializationPlan&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TH&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      toConstructorMap&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Lazy&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ToConstructorMap&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TH&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      classTag&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ClassTag&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Although this works, there are many problems with this approach. All these type and implicit resolutions can make the compilation quite slow, the code is very complex and hard to understand or modify, and most importantly error messages will be a nightmare. A user trying to derive a type class for our serialization library should not get an error that complains about not being able to find an implicit value of &lt;code&gt;Zip.Aux&lt;/code&gt; for a weird type that does not even fit on one screen!&lt;/p&gt;
&lt;h3 id=&quot;magnolia&quot;&gt;Magnolia&lt;/h3&gt;
&lt;p&gt;The &lt;a rel=&quot;external&quot; href=&quot;https://github.com/softwaremill/magnolia&quot;&gt;Magnolia&lt;/a&gt; library provides a much more friendly solution for deriving type classes for algebraic data types - it moves the whole problem into the value space by hiding the necessary macros. The derivation implementation for a given type class then only requires defining two functions (one for working with products, one for working with coproducts) that are regular Scala functions getting a &quot;context&quot; value and producing an instance of the derived type class. The context value contains type information - for example the name and type of all the fields of a case class - and also contains an &lt;em&gt;instance&lt;/em&gt; of the derived type class for each of these inner elements.&lt;/p&gt;
&lt;p&gt;To write a Magnolia based deriver you have to create an &lt;code&gt;object&lt;/code&gt; with a &lt;code&gt;join&lt;/code&gt; and a &lt;code&gt;split&lt;/code&gt; method and a &lt;code&gt;Typeclass&lt;/code&gt; type:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodecDerivation&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Typeclass&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; join&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;ctx&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CaseClass&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;BinaryCodec&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; serialize&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; context&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SerializationContext&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;parameter &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; ctx.parameters&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;          //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; recursively serialize the fields&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          parameter.typeclass.serialize&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;parameter.dereference&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; split&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;ctx&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SealedTrait&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;BinaryCodec&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; gen&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; macro &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Magnolia&lt;/span&gt;&lt;span&gt;.gen&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There is a Magnolia version for Scala 3 too, which is although quite similar, it is not source compatible with the Scala 2 version, leading to the need to define these derivations twice in cross-compiled projects.&lt;/p&gt;
&lt;h2 id=&quot;why-not-magnolia&quot;&gt;Why not Magnolia?&lt;/h2&gt;
&lt;p&gt;Magnolia already existed when I wrote the first version of Desert, but I could not use it because of two reasons. In that early version of the library the derivation had to take a user defined list of &lt;em&gt;evolution steps&lt;/em&gt;, so the actual codec definitions looked something like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; codec&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Point&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;.derive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;FieldAdded&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It was not clear how could I pass these parameters to Magnolia context - with Shapeless it was not a problem because it is possible to simply pass them as a parameter to the &lt;code&gt;derive&lt;/code&gt; function that &quot;starts&quot; the type level computation.&lt;/p&gt;
&lt;p&gt;This requirement no longer exists though, as in recent versions the &lt;em&gt;evolution steps&lt;/em&gt; are defined by attributes, which are fully supported by Magnolia as well:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;evolutionSteps&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;FieldAdded&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Point&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;x&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;y&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;z&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The second reason was a much more important limitation in Magnolia that still exists - it is not possible to shortcut the derivation tree. Desert has &lt;em&gt;transient field&lt;/em&gt; and &lt;em&gt;transient constructor&lt;/em&gt; support. For those fields and constructors which are marked as transient we don&#39;t want to, and cannot define codec instances. They can be things like open files, streams, actor references, sockets etc. Even though Magnolia only instantiates the type class instances when they are accessed, the derivation fails if there are types in the tree that does not have an instance. This issue is &lt;a rel=&quot;external&quot; href=&quot;https://github.com/softwaremill/magnolia/issues/297&quot;&gt;tracked here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;There was one more decision I did not like regarding Magnolia - the decision to have an incompatible Scala 3 version. I believe it was a big missed opportunity to seamlessly support cross-compiled type class derivation code.&lt;/p&gt;
&lt;h2 id=&quot;zio-schema-based-derivation&quot;&gt;ZIO Schema based derivation&lt;/h2&gt;
&lt;p&gt;All these issues lead to writing a new derivation library - as part of the &lt;a rel=&quot;external&quot; href=&quot;https://zio.dev/zio-schema/&quot;&gt;ZIO Schema&lt;/a&gt; project. It was first released in version &lt;a rel=&quot;external&quot; href=&quot;https://github.com/zio/zio-schema/releases/tag/v0.3.0&quot;&gt;v0.3.0&lt;/a&gt; in November of 2022.&lt;/p&gt;
&lt;p&gt;From the previously demonstrated type class derivation techniques the closest to ZIO Schema&#39;s deriver is Magnolia. On the other hand it does supports the transient field use case, and it is fully cross-compilation compatible between Scala 2 and Scala 3.&lt;/p&gt;
&lt;p&gt;To implement type class derivation based on ZIO Schema you need to implement a trait called &lt;code&gt;Deriver&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Deriver&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; deriveRecord&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;    record&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Record&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;    fields&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WrappedF&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;    summoned&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; more deriveXXX methods to impelment&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This looks similar to Magnolia&#39;s &lt;code&gt;join&lt;/code&gt; method but has some significant differences. The first thing to notice is that we get a &lt;code&gt;Schema.Record&lt;/code&gt; value describing our case class. This is one of the cases of the core data type &lt;code&gt;Schema[T]&lt;/code&gt; which describes Scala data types and provides a lot of features to work with them. So having a &lt;code&gt;Schema[A]&lt;/code&gt; is a requirement to derive an &lt;code&gt;F[A]&lt;/code&gt; with &lt;code&gt;Deriver&lt;/code&gt; - but luckily ZIO schema has derivation support for Schema itself.&lt;/p&gt;
&lt;p&gt;The second thing to notice is that &lt;code&gt;Schema[A]&lt;/code&gt; itself does not know anything about type class derivation and especially about the actual &lt;code&gt;F&lt;/code&gt; type class that is being derived, so the second parameter of &lt;code&gt;deriveRecord&lt;/code&gt; is a collection of potentially derived instances of our derived type class for each field. &lt;code&gt;WrappedF&lt;/code&gt; is just making this lazy so if we decide we don&#39;t need instances for (some of) the fields they won&#39;t be traversed (they still need to have a &lt;code&gt;Schema&lt;/code&gt; though - but it can even be a &lt;code&gt;Schema.fail&lt;/code&gt; for things not representable by ZIO Schema - it will be fine if we never touch them by unwrapping the &lt;code&gt;WrappedF&lt;/code&gt; value).&lt;/p&gt;
&lt;p&gt;The third parameter is also interesting as it provides full control to the developer to choose between the summoned implicit and the derivation logic. If your &lt;code&gt;deriveRecord&lt;/code&gt; is called for a record type &lt;code&gt;A&lt;/code&gt; and there is already an implicit &lt;code&gt;F[A]&lt;/code&gt; that the compiler can find (for example defined in &lt;code&gt;A&lt;/code&gt;&#39;s companion object), it will be passed in the &lt;code&gt;summoned&lt;/code&gt; parameter to &lt;code&gt;deriveRecord&lt;/code&gt;. The usual logic is to choose the summoned value when it is available and only derive an instance when there isn&#39;t any. By calling &lt;code&gt;.autoAcceptSummoned&lt;/code&gt; on our &lt;code&gt;Deriver&lt;/code&gt; class we can automatically enable this behavior - in this case &lt;code&gt;deriveRecord&lt;/code&gt; will only be called for the cases where &lt;code&gt;summoned&lt;/code&gt; was &lt;code&gt;None&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Another method we have on &lt;code&gt;Deriver&lt;/code&gt; is &lt;code&gt;.cached&lt;/code&gt; which stores the generated type class instances in a concurrent hash map shared between the macro invocations.&lt;/p&gt;
&lt;p&gt;Our ZIO Schema based Desert codec derivation is defined using these modifiers:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DerivedBinaryCodec&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  lazy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; deriver&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodecDeriver&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;.cached.autoAcceptSummoned&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodecDeriver&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Deriver&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;BinaryCodec&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As ZIO Schema is not only describing records and enums but also primitive types, tuples, and special cases like &lt;code&gt;Option&lt;/code&gt; and &lt;code&gt;Either&lt;/code&gt; and collection types, the deriver has to support all these.&lt;/p&gt;
&lt;p&gt;The minimum set of methods to implement is &lt;code&gt;deriveRecord&lt;/code&gt;, &lt;code&gt;deriveEnum&lt;/code&gt;, &lt;code&gt;derivePrimitive&lt;/code&gt;, &lt;code&gt;deriveOption&lt;/code&gt;, &lt;code&gt;deriveSequence&lt;/code&gt;, &lt;code&gt;deriveMap&lt;/code&gt; and &lt;code&gt;deriveTransformedRecord&lt;/code&gt;. In addition to that we can also override &lt;code&gt;deriveEither&lt;/code&gt;, &lt;code&gt;deriveSet&lt;/code&gt; and &lt;code&gt;deriveTupleN&lt;/code&gt; (1-22) to handle these cases specially.&lt;/p&gt;
&lt;p&gt;In case of Desert the &lt;code&gt;deriveRecord&lt;/code&gt; and &lt;code&gt;deriveEnum&lt;/code&gt; are calling to the implementation of the same data-evolution aware binary format that was previously implemented using Shapeless, but this time it is automatically supporting Scala 2 and Scala 3 the same time. The &lt;code&gt;derivePrimitive&lt;/code&gt; is just choosing from predefined &lt;code&gt;BinaryCodec&lt;/code&gt; instances based on the primitive&#39;s type:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; derivePrimitive&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  st&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; StandardType&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  summoned&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  st &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;match&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; StandardType&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;UnitType&lt;/span&gt;&lt;span&gt;           =&amp;gt;&lt;/span&gt;&lt;span&gt; unitCodec&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; StandardType&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;StringType&lt;/span&gt;&lt;span&gt;         =&amp;gt;&lt;/span&gt;&lt;span&gt; stringCodec&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; StandardType&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;BoolType&lt;/span&gt;&lt;span&gt;           =&amp;gt;&lt;/span&gt;&lt;span&gt; booleanCodec&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; StandardType&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ByteType&lt;/span&gt;&lt;span&gt;           =&amp;gt;&lt;/span&gt;&lt;span&gt; byteCodec&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Same applies for option, either, sequence etc - it is just a mapping to the library&#39;s own definition of these binary codecs.&lt;/p&gt;
&lt;p&gt;Under the hood &lt;code&gt;Deriver&lt;/code&gt; is a macro (implemented separately both for Scala 2 and Scala 3) that traverses the types simultaneously with the provided &lt;code&gt;Schema&lt;/code&gt; (so it does not need to regenerate those) and maps these informations into calls through the &lt;code&gt;Deriver&lt;/code&gt; interface. The whole process is initiated by calling the &lt;code&gt;derive&lt;/code&gt; method on our &lt;code&gt;Deriver&lt;/code&gt;, which is the entry point of these macros, so it has a different looking (but source-code compatible) definition for Scala 2 and Scala 3:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Scala 3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;inline&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; derive&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; schema&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Scala 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; derive&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;deriver&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Deriver&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; schema&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; macro deriveImpl&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These are compatible if you are directly calling them: so you can write&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; binaryCodecDeriver&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Deriver&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;BinaryCodec&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; pointCodec&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Point&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; binaryCodecDeriver.derive&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Point&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or even:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodecDeriver&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Deriver&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;BinaryCodec&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; pointCodec&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Point&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodecDeriver&lt;/span&gt;&lt;span&gt;.derive&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Point&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But if you want to wrap this derive call you have to be aware that they are macro calls, and they have to be wrapped by (version-specific) macros. This is what Desert is doing - as shown before, it uses the &lt;code&gt;cached&lt;/code&gt; and &lt;code&gt;autoAcceptSummoned&lt;/code&gt; modifiers to create a deriver, but still exposes a simple &lt;code&gt;derive&lt;/code&gt; method through an &lt;code&gt;object&lt;/code&gt;. To do so it needs to wrap the inner deriver macro with its own macro like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Scala 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DerivedBinaryCodecVersionSpecific&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; deriver&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Deriver&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;BinaryCodec&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; derive&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; schema&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    macro &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DerivedBinaryCodecVersionSpecific&lt;/span&gt;&lt;span&gt;.deriveImpl&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DerivedBinaryCodecVersionSpecific&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; deriveImpl&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; c.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WeakTypeTag&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      c&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; whitebox.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Context&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      schema&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; c.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Expr&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; c.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Tree&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      import&lt;/span&gt;&lt;span&gt; c&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;universe&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; tpe&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; weakTypeOf&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      q&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;D&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; $&lt;/span&gt;&lt;span&gt;tpe&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;D&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;$&lt;/span&gt;&lt;span&gt;schema&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Scala 3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DerivedBinaryCodecVersionSpecific&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  lazy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; deriver&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Deriver&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;BinaryCodec&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  inline&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; derive&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; schema&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; BinaryCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Derive&lt;/span&gt;&lt;span&gt;.derive&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;BinaryCodec&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DerivedBinaryCodec&lt;/span&gt;&lt;span&gt;.deriver&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;We have a new alternative for deriving type class instances from type information, based on ZIO Schema. You may want to use it if you want to have a single deriver source code for both Scala 2 and Scala 3, if you need more flexibility than what Magnolia provides, or if you are already using ZIO Schema in your project.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Generating a Rust client library for ZIO Http endpoints</title>
        <published>2023-09-07T00:00:00+00:00</published>
        <updated>2023-09-07T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/generating-a-rust-client-library-for-zio-http-endpoints/"/>
        <id>https://blog.vigoo.dev/posts/generating-a-rust-client-library-for-zio-http-endpoints/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/generating-a-rust-client-library-for-zio-http-endpoints/">&lt;p&gt;We at &lt;a rel=&quot;external&quot; href=&quot;https://golem.cloud&quot;&gt;Golem Cloud&lt;/a&gt; built our first developer preview on top of the ZIO ecosystem, including &lt;a rel=&quot;external&quot; href=&quot;https://github.io/zio/zio-http&quot;&gt;ZIO Http&lt;/a&gt; for defining and implementing our server&#39;s REST API. By using &lt;strong&gt;ZIO Http&lt;/strong&gt; we immediately had the ability to call our endpoints using endpoint &lt;strong&gt;client&lt;/strong&gt;s, which allowed us to develop the first version of Golem&#39;s &lt;strong&gt;CLI tool&lt;/strong&gt; very rapidly.&lt;/p&gt;
&lt;p&gt;Although very convenient for development, &lt;em&gt;using&lt;/em&gt; a CLI tool built with Scala for the JVM is not a pleasant experience for the users due to the slow startup time. One possible solution is to compile to native using &lt;a rel=&quot;external&quot; href=&quot;https://www.graalvm.org/22.0/reference-manual/native-image/&quot;&gt;GraalVM Native Image&lt;/a&gt; but it is very hard to set up and even when it works, it is extremely fragile - further changes to the code or updated dependencies can break it causing unexpected extra maintenance cost. After some initial experiments we dropped this idea - and instead chose to reimplement the CLI using &lt;strong&gt;Rust&lt;/strong&gt; - a language being a much better fit for command line tools, and also already an important technology in our Golem stack.&lt;/p&gt;
&lt;h2 id=&quot;zio-http&quot;&gt;ZIO Http&lt;/h2&gt;
&lt;p&gt;If we rewrite &lt;code&gt;golem-cli&lt;/code&gt; to Rust, we lose the convenience of using  &lt;strong&gt;endpoint definitions&lt;/strong&gt; (written in Scala with ZIO Http, the ones we have for implementing the server) for calling our API, and we would also lose all the &lt;strong&gt;types&lt;/strong&gt; used in these APIs as they are all defined as Scala case classes and enums. Just to have more context, let&#39;s take a look at one of the endpoints!&lt;/p&gt;
&lt;p&gt;A ZIO Http &lt;strong&gt;endpoint&lt;/strong&gt; is just a definition of a single endpoint of a HTTP API, describing the routing as well the inputs and outputs of it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; getWorkerMetadata&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Endpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;GET&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;v1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;templates&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; /&lt;/span&gt;&lt;span&gt; rawTemplateId &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;workers&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; /&lt;/span&gt;&lt;span&gt; workerName&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .header&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Auth&lt;/span&gt;&lt;span&gt;.tokenSecret&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .outErrorCodec&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;errorCodec&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .out&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WorkerMetadata&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; ??&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Doc&lt;/span&gt;&lt;span&gt;.p&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Get the current worker status and metadata&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&#39;s see what we have here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the endpoint is reached by sending a &lt;strong&gt;GET&lt;/strong&gt; request&lt;/li&gt;
&lt;li&gt;the request &lt;strong&gt;path&lt;/strong&gt; consists of some static segments as well as the &lt;em&gt;template id&lt;/em&gt; and the &lt;em&gt;worker name&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;it also requires an &lt;strong&gt;authorization header&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;we define the kind of errors it can return&lt;/li&gt;
&lt;li&gt;and finally it defines that the response&#39;s &lt;strong&gt;body&lt;/strong&gt; will contain a JSON representation (default in ZIO Http) of a type called &lt;code&gt;WorkerMetadata&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;What are &lt;code&gt;rawTemplateId&lt;/code&gt; and &lt;code&gt;workerName&lt;/code&gt;? These are so called &lt;strong&gt;path codecs&lt;/strong&gt;, defined in a common place so they can be reused in multiple endpoints. They allow us to have dynamic parts of the request path mapped to specific types - so when we implement the endpoint (or call it in a client) we don&#39;t have to pass strings and we can directly work with the business domain types, in this case &lt;code&gt;RawTemplateId&lt;/code&gt; and &lt;code&gt;WorkerName&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The simplest way to define path codecs is to &lt;strong&gt;transform&lt;/strong&gt; an existing one:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; workerName&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PathCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WorkerName&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;worker-name&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.transformOrFailLeft&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WorkerName&lt;/span&gt;&lt;span&gt;.make&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.toErrorEither, _.value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here the &lt;code&gt;make&lt;/code&gt; function is a &lt;strong&gt;ZIO Prelude&lt;/strong&gt; &lt;a rel=&quot;external&quot; href=&quot;https://zio.github.io/zio-prelude/docs/functionaldatatypes/validation&quot;&gt;&lt;code&gt;Validation&lt;/code&gt;&lt;/a&gt; which we have to convert to an &lt;code&gt;Either&lt;/code&gt; for the transform function. Validations can contain more than one failures, as opposed to &lt;code&gt;Either&lt;/code&gt;s, which allows us to compose them in a way that we can keep multiple errors instead of immediately returning with the first failure.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;tokenSecret&lt;/code&gt; is similar, but it is a &lt;code&gt;HeaderCodec&lt;/code&gt; describing what type of header it is and how the value of the given header should be mapped to a specific type (a token, in this case).&lt;/p&gt;
&lt;p&gt;What is &lt;code&gt;WorkerMetadata&lt;/code&gt; and how does ZIO Http know how to produce a JSON from it?&lt;/p&gt;
&lt;p&gt;It&#39;s just a simple &lt;em&gt;case class&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; WorkerMetadata&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  workerId&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ComponentInstanceId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  accountId&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AccountId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  args&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  env&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Map&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  status&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; InstanceStatus&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  templateVersion&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  retryCount&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But with an implicit &lt;strong&gt;derived&lt;/strong&gt; &lt;strong&gt;ZIO Schema&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; WorkerMetadata&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; schema&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WorkerMetadata&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DeriveSchema&lt;/span&gt;&lt;span&gt;.gen&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WorkerMetadata&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We will talk more about ZIO Schema below - for now all we need to know is it describes the structure of Scala types, and this information can be used to serialize data into various formats, including JSON.&lt;/p&gt;
&lt;p&gt;Once we have our endpoints defined like this, we can do several things with them - they are just data describing what an endpoint looks like!&lt;/p&gt;
&lt;h3 id=&quot;implementing-an-endpoint&quot;&gt;Implementing an endpoint&lt;/h3&gt;
&lt;p&gt;When developing a &lt;em&gt;server&lt;/em&gt;, the most important thing to do with an endpoint is to &lt;strong&gt;implement&lt;/strong&gt; it. Implementing an endpoint looks like the following:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; getWorkerMetadataImpl&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    getWorkerMetadata.implement &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      Handler&lt;/span&gt;&lt;span&gt;.fromFunctionZIO &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;rawTemplateId, workerName, authTokenId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ... ZIO program returning a WorkerMetadata&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;em&gt;type&lt;/em&gt; of &lt;code&gt;getWorkerMetadataImpl&lt;/code&gt; is &lt;code&gt;Route&lt;/code&gt; - it is no longer just a description of what an endpoint looks like, it defines a specific HTTP route and its associated &lt;em&gt;request handler&lt;/em&gt;, implemented by a ZIO effect (remember that ZIO effects are also values - we &lt;em&gt;describe&lt;/em&gt; what we need to do when a request comes in, but executing it will be the responsibility of the server implementation).&lt;/p&gt;
&lt;p&gt;The nice thing about ZIO Http endpoints is that they are completely type safe. I&#39;ve hidden the type signature in the previous code snippets but actually &lt;code&gt;getWorkerMetadata&lt;/code&gt; has the type:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Endpoint&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RawTemplateId&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WorkerName&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RawTemplateId&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WorkerName&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TokenSecret&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    WorkerEndpointError&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    WorkerMetadata&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here the &lt;em&gt;second&lt;/em&gt; type parameter defines the &lt;strong&gt;input&lt;/strong&gt; of the request handler and the &lt;em&gt;forth&lt;/em&gt; type parameter defines the &lt;strong&gt;output&lt;/strong&gt; the server constructs the response from.&lt;/p&gt;
&lt;p&gt;With these types, we really just have to implement a (ZIO) function from the input to the output:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RawTemplateId&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WorkerName&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TokenSecret&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WorkerEndpointError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WorkerMetadata&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and this is exactly what we pass to &lt;code&gt;Handler.fromFunctionZIO&lt;/code&gt; in the above example.&lt;/p&gt;
&lt;h3 id=&quot;calling-an-endpoint&quot;&gt;Calling an endpoint&lt;/h3&gt;
&lt;p&gt;The same endpoint values can also be used to make requests to our API from clients such as &lt;code&gt;golem-cli&lt;/code&gt;. Taking advantage of the same type safe representation we can just call &lt;code&gt;apply&lt;/code&gt; on the endpoint definition passing its input as a parameter to get an &lt;strong&gt;invocation&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; invocation&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; getInstanceMetadata&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;rawTemplateId, workerName, token&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;this invocation can be &lt;strong&gt;executed&lt;/strong&gt; to perform the actual request using an &lt;code&gt;EndpointExecutor&lt;/code&gt; which can be easily constructed from a ZIO Http &lt;code&gt;Client&lt;/code&gt; and some other parameters like the URL of the remote server:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;executor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;invocation&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.flatMap &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; workerMetadata &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;the-task&quot;&gt;The task&lt;/h2&gt;
&lt;p&gt;So can we do anything to keep this convenient way of calling our endpoints when migrating the CLI to Rust? At the time of writing we already had more than 60 endpoints, with many complex types used in them - defining them by hand in Rust, and keeping the Scala and Rust code in sync sounds like a nightmare.&lt;/p&gt;
&lt;p&gt;The ideal case would be to have something like this in Rust:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;async_trait&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Worker&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_worker_metadata&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; template_id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;TemplateId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; worker_name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;WorkerName&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; authorization&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Token&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;WorkerMetadata&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; WorkerError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;with an implementation that just requires the same amount of configuration as the Scala endpoint executor (server URL, etc), and all the referenced types like &lt;code&gt;WorkerMetadata&lt;/code&gt; would be an exact clone of the Scala types just in Rust.&lt;/p&gt;
&lt;p&gt;Fortunately we can have (almost) this by taking advantage of the declarative nature of ZIO Http and ZIO Schema!&lt;/p&gt;
&lt;p&gt;In the rest of this post we will see how we can &lt;strong&gt;generate Rust code&lt;/strong&gt; using a combination of ZIO libraries to automatically have all our type definitions and client implementation ready to use from the Rust version of &lt;code&gt;golem-cli&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-building-blocks&quot;&gt;The building blocks&lt;/h2&gt;
&lt;p&gt;We want to generate from an arbitrary set of ZIO Http &lt;code&gt;Endpoint&lt;/code&gt; definitions a &lt;strong&gt;Rust crate&lt;/strong&gt; ready to be compiled, published and used. We will take advantage of the following libraries:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://zio.dev/zio-http/&quot;&gt;ZIO Http&lt;/a&gt; as the source of &lt;strong&gt;endpoint&lt;/strong&gt; definitions&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://zio.dev/zio-schema/&quot;&gt;ZIO Schema&lt;/a&gt; for observing the &lt;strong&gt;type&lt;/strong&gt; definitions&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://zio.dev/zio-parser/&quot;&gt;ZIO Parser&lt;/a&gt; because it has a composable &lt;strong&gt;printer&lt;/strong&gt; concept&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://zio.dev/zio-nio/&quot;&gt;ZIO NIO&lt;/a&gt; for working with the &lt;strong&gt;filesystem&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://zio.dev/zio-prelude/&quot;&gt;ZIO Prelude&lt;/a&gt; for implementing the stateful endpoint/type discovery in a purely functional way&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;generating-rust-code&quot;&gt;Generating Rust code&lt;/h2&gt;
&lt;p&gt;Let&#39;s start with the actual source code generation. This is something that can be done in many different ways - one extreme could be to just concatenate strings (or use a &lt;code&gt;StringBuilder&lt;/code&gt;) while the other is to build a full real Rust &lt;em&gt;AST&lt;/em&gt; and pretty print that. I had a &lt;a href=&quot;https://blog.vigoo.dev/posts/funscala2021-talk/&quot;&gt;talk on Function Scala 2021 about the topic&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For this task I chose a technique which is somewhere in the middle and provides some extent of composability while also allowing use to do just the amount of abstraction we want to. The idea is that we define a &lt;em&gt;Rust code generator model&lt;/em&gt; which does not have to strictly follow the actual generated language&#39;s concepts, and then define a pretty printer for this model. This way we only have to model the subset of the language we need for the code generator, and we can keep simplifications or even complete string fragments in it if that makes our life easier.&lt;/p&gt;
&lt;p&gt;Let&#39;s see how this works with some examples!&lt;/p&gt;
&lt;p&gt;We will have to generate &lt;em&gt;type definitions&lt;/em&gt; so we can define a Scala &lt;em&gt;enum&lt;/em&gt; describing what kind of type definitions we want to generate:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;enum&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustDef&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TypeAlias&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Name&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;typ&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustType&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;derives&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustType&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Newtype&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Name&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;typ&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustType&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;derives&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustType&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Struct&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Name&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;fields&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustDef&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Field&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;derives&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustType&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;isPublic&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Boolean&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Enum&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Name&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;cases&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustDef&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;derives&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustType&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Impl&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;tpe&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustType&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;functions&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustDef&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ImplTrait&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;implemented&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustType&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;forType&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustType&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;functions&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustDef&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Function&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Name&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;parameters&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustDef&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Parameter&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;returnType&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustType&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;body&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;isPublic&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Boolean&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can make this as convenient to use as we want, for example adding constructors like:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; struct&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Name&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;fields&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Field&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustDef&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;Name&lt;/code&gt; is an opaque string type with extension methods to convert between various cases like pascal case, snake case, etc. &lt;code&gt;RustType&lt;/code&gt; is a similar &lt;em&gt;enum&lt;/em&gt; to &lt;code&gt;RustDef&lt;/code&gt;, containing all the different type descriptions we will have to use. But it is definitely not how a proper Rust parser would define what a type is - for example we can have a &lt;code&gt;RustType.Option&lt;/code&gt; as a shortcut for wrapping a Rust type in Rust&#39;s own option type, just because it makes our code generator simpler to write.&lt;/p&gt;
&lt;p&gt;So once we have this model (which in practice evolves together with the code generator, usually starting with a few simple case classes) we can use &lt;strong&gt;ZIO Parser&lt;/strong&gt;&#39;s printer feature to define composable elements constructing Rust source code.&lt;/p&gt;
&lt;p&gt;We start by defining a module and a type alias for our printer:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Rust&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Rust&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Printer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Char&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and then just define building blocks - what these building blocks are depends completely on us, and the only thing it affects is how well you can compose them. Having very small building blocks may reduce the readability of the code generator, but using too large chunks reduces their composability and makes it harder to change or refactor.&lt;/p&gt;
&lt;p&gt;We can define some short aliases for often used characters or string fragments:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; gt&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Rust&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Printer&lt;/span&gt;&lt;span&gt;.print&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; lt&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Rust&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Printer&lt;/span&gt;&lt;span&gt;.print&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; bracketed&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;inner&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Rust&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Rust&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  lt &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;~&lt;/span&gt;&lt;span&gt; inner &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;~&lt;/span&gt;&lt;span&gt; gt&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and we have to define &lt;code&gt;Rust&lt;/code&gt; printers for each of our model types. For example for the &lt;code&gt;RustType&lt;/code&gt; enum it could be something like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; typename&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Rust&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustType&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Printer&lt;/span&gt;&lt;span&gt;.byValue&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustType&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Primitive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;             =&amp;gt;&lt;/span&gt;&lt;span&gt; str&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustType&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Option&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;inner&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;               =&amp;gt;&lt;/span&gt;&lt;span&gt; typename&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustType&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Primitive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ~&lt;/span&gt;&lt;span&gt; bracketed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;typename&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;inner&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustType&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;inner&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;                  =&amp;gt;&lt;/span&gt;&lt;span&gt; typename&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustType&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Primitive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Vec&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ~&lt;/span&gt;&lt;span&gt; bracketed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;typename&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;inner&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustType&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;SelectFromModule&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;path, typ&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Printer&lt;/span&gt;&lt;span&gt;.anyString.repeatWithSep&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;dcolon&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;path&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ~&lt;/span&gt;&lt;span&gt; dcolon &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;~&lt;/span&gt;&lt;span&gt; typename&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;typ&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustType&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Parametric&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;name, params&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    str&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ~&lt;/span&gt;&lt;span&gt; bracketed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;typename.repeatWithSep&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;comma&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;params&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can see that &lt;code&gt;typename&lt;/code&gt; uses itself to recursively generate inner type names, for example when generating type parameters of tuple members. It also demonstrates that we can extract patterns such as &lt;code&gt;bracketed&lt;/code&gt; to simplify our printer definitions and eliminate repetition.&lt;/p&gt;
&lt;p&gt;Another nice feature we get by using a general purpose printer library like ZIO Parser is that we can use the built-in combinators to get printers for new types. One example is the sequential composition of printers. For example the following fragment:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; str&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;pub &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ~&lt;/span&gt;&lt;span&gt; name &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;~&lt;/span&gt;&lt;span&gt; str&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ~&lt;/span&gt;&lt;span&gt; typename&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;would have the type &lt;code&gt;Rust[(Name, RustType)]&lt;/code&gt; and we can even make that a printer of a case class like:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PublicField&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Name&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;typ&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustType&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; p2&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; p.from&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PublicField&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where &lt;code&gt;p2&lt;/code&gt; will have the type &lt;code&gt;Rust[PublicField&lt;/code&gt;].&lt;/p&gt;
&lt;p&gt;Another very useful combinator is &lt;strong&gt;repetition&lt;/strong&gt;. For example if we have a printer for an enum&#39;s case:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; enumCase&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Rust&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustDef&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;we can simply use one of the repetition combinators to make a printer for a &lt;em&gt;list of enum cases&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; enumCases&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Rust&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustDef&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; enumCase.&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;*&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or as in the &lt;code&gt;typename&lt;/code&gt; example above:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;typename.repeatWithSep&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;comma&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;to have a &lt;code&gt;Rust[Chunk[RustType]]&lt;/code&gt; that inserts a comma between each element when printed.&lt;/p&gt;
&lt;h2 id=&quot;inspecting-the-scala-types&quot;&gt;Inspecting the Scala types&lt;/h2&gt;
&lt;p&gt;As we have seen the &lt;em&gt;endpoint DSL&lt;/em&gt; uses &lt;strong&gt;ZIO Schema&lt;/strong&gt; to capture information about the types being used in the endpoints (usually as request or response bodies, serialized into JSON). We can use the same information to generate &lt;strong&gt;Rust types&lt;/strong&gt; from our Scala types!&lt;/p&gt;
&lt;p&gt;The core data type defined by the ZIO Schema library is called &lt;code&gt;Schema&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;sealed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Schema describes the structure of a Scala type &lt;code&gt;A&lt;/code&gt; in a way we can inspect it from regular Scala code. Let&#39;s imagine we have &lt;code&gt;Schema[WorkerMetadata]&lt;/code&gt; coming from our endpoint definition and we have to generate an equivalent Rust &lt;code&gt;struct&lt;/code&gt; with the same field names and field types.&lt;/p&gt;
&lt;p&gt;The first thing to notice is that type definitions are recursive. Unless &lt;code&gt;WorkerMetadata&lt;/code&gt; only contains fields of &lt;em&gt;primitive types&lt;/em&gt; such as integer or string, our job does not end with generating a single Rust struct - we need to recursively generate all the other types &lt;code&gt;WorkerMetadata&lt;/code&gt; is depending on! To capture this fact let&#39;s introduce a type that represents everything we have to extract from a single (or a set of) schemas in order to generate Rust types from them:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustModel&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  typeRefs&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Map&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustType&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  definitions&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustDef&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  requiredCrates&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Set&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Crate&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We have &lt;code&gt;typeRefs&lt;/code&gt; which associates a &lt;code&gt;RustType&lt;/code&gt; with a schema so we can use it in future steps of our code generator to refer to a generated type in our Rust codebase. We have a list of &lt;code&gt;RustDef&lt;/code&gt; values which are the generated type definitions, ready to be printed with out &lt;code&gt;Rust&lt;/code&gt; pretty printer. And finally we can also gather a set of required extra rust &lt;em&gt;crates&lt;/em&gt;, because some of the types considered &lt;em&gt;primitive types&lt;/em&gt; by ZIO Schema are not having proper representations in the Rust standard library, only in external crates. Examples are UUIDs and various date/time types.&lt;/p&gt;
&lt;p&gt;So our job now is to write a function of&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; fromSchemas&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;schemas&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Seq&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Either&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustModel&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;Either&lt;/code&gt; result type is used to indicate failures. Even if we write a transformation that can produce from any &lt;code&gt;Schema&lt;/code&gt; a proper &lt;code&gt;RustModel&lt;/code&gt;, we always have to have an error result when working with ZIO Schema because it has an explicit failure case called &lt;code&gt;Schema.Fail&lt;/code&gt;. If we process a schema and end up with a &lt;code&gt;Fail&lt;/code&gt; node, we can&#39;t do anything else than fail our code generator.&lt;/p&gt;
&lt;p&gt;There are many important details to consider when implementing this function, but let&#39;s just see first what the actual &lt;code&gt;Schema&lt;/code&gt; type looks like. When we have a value of &lt;code&gt;Schema[?]&lt;/code&gt; we can pattern match on it and implement the following cases:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Schema.Primitive&lt;/code&gt; describes a primitive type - there are a lot of primitive types defined by ZIO Schema&#39;s &lt;code&gt;StandardType&lt;/code&gt; enum&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Schema.Enum&lt;/code&gt; describes a type with multiple cases (a &lt;em&gt;sum type&lt;/em&gt;) such as a &lt;code&gt;sealed trait&lt;/code&gt; or &lt;code&gt;enum&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Schema.Record&lt;/code&gt; describes a type with multiple fields (a &lt;em&gt;product type&lt;/em&gt;) such as a &lt;code&gt;case class&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Schema.Map&lt;/code&gt; represents a &lt;em&gt;map&lt;/em&gt; with a key and value type&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Schema.Sequence&lt;/code&gt; represents a &lt;em&gt;sequence&lt;/em&gt; of items of a given element type&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Schema.Set&lt;/code&gt; is a &lt;em&gt;set&lt;/em&gt; of items of a given element type&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Schema.Optional&lt;/code&gt; represents an &lt;em&gt;optional&lt;/em&gt; type (like an &lt;code&gt;Option[T]&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Schema.Either&lt;/code&gt; is a special case of sum types representing either one or the other type (like an &lt;code&gt;Either[A, B]&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Schema.Lazy&lt;/code&gt; is used to safely encode recursive types, it contains a function that evaluates into an inner &lt;code&gt;Schema&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Schema.Dynamic&lt;/code&gt; represents a type that is dynamic - like a &lt;code&gt;JSON&lt;/code&gt; value&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Schema.Transform&lt;/code&gt; assigns a transformation function that converts a &lt;em&gt;value&lt;/em&gt; of a type represented by the schema to a value of some other type. As we have no way to inspect these functions (they are compiled Scala functions) in our code generator, this is not very interesting for us now.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Schema.Fail&lt;/code&gt; as already mentioned represents a failure in describing the data type&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When traversing a &lt;code&gt;Schema&lt;/code&gt; recursively (for any reason), it is important to keep in mind that it &lt;em&gt;can&lt;/em&gt; encode recursive types! A simple example is a binary tree:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Tree&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;label&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; A&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;left&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Tree&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;right&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Tree&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can construct a &lt;code&gt;Schema[Tree[A]]&lt;/code&gt; if we have a &lt;code&gt;Schema[A]&lt;/code&gt;. This will be something like (pseudo-code):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;lazy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; tree&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Tree&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Record&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Field&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Field&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Optional&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Lazy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; tree&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Field&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Optional&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Lazy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; tree&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If we are not prepared for recursive types we can easily get into an endless loop (or stack overflow) when processing these schemas.&lt;/p&gt;
&lt;p&gt;This is just one example of things to keep track of while converting a schema into a set of Rust definitions. If fields refer to the self type we want to use &lt;code&gt;Box&lt;/code&gt; so to put them on the heap. We also need to keep track of if everything within a generated type derives &lt;code&gt;Ord&lt;/code&gt; and &lt;code&gt;Hash&lt;/code&gt; - and if yes, we should derive an instance for the same type classes for our generated type as well.&lt;/p&gt;
&lt;p&gt;My preferred way to implement such recursive stateful transformation functions is to use &lt;strong&gt;ZIO Prelude&lt;/strong&gt;&#39;s &lt;code&gt;ZPure&lt;/code&gt; type. It&#39;s type definition looks a little scary:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;sealed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZPure&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;W&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;S1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;S2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;E&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;ZPure&lt;/code&gt; describes a &lt;em&gt;purely functional computation&lt;/em&gt; which can:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Emit log entries of type &lt;code&gt;W&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Works with an inital state of type &lt;code&gt;S1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Results in a final state of type &lt;code&gt;S2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Has access to some context of type &lt;code&gt;R&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Can fail with a value of &lt;code&gt;E&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Or succeed with a value of &lt;code&gt;A&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this case we need the state, failure and result types only, but we could also take advantage of &lt;code&gt;W&lt;/code&gt; to log debug information within our schema transformation function.&lt;/p&gt;
&lt;p&gt;To make it easier to work with &lt;code&gt;ZPure&lt;/code&gt; we can introduce a &lt;em&gt;type alias&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Fx&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZPure&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;State&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;State&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where &lt;code&gt;State&lt;/code&gt; is our own &lt;em&gt;case class&lt;/em&gt; containing everything we need:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  typeRefs&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Map&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustType&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  definitions&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustDef&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  requiredCrates&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Set&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Crate&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  processed&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Set&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  stack&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  nameTypeIdMap&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Map&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Name&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Set&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TypeId&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  schemaCaps&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Map&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Capabilities&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We won&#39;t get into the details of the state type here, but I&#39;m showing some fragments to get a feeling of working with &lt;code&gt;ZPure&lt;/code&gt; values.&lt;/p&gt;
&lt;p&gt;Some helper functions to manipulate the state can make our code much easier to read:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; getState&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Fx&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;State&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZPure&lt;/span&gt;&lt;span&gt;.get&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;State&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; updateState&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Fx&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZPure&lt;/span&gt;&lt;span&gt;.update&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;State&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;State&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;f&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For example we can use &lt;code&gt;updateState&lt;/code&gt;  to manipulate the &lt;code&gt;stack&lt;/code&gt; field of the state around another computation - before running it, we add a schema to the stack, after that we remove it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; stacked&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;schema&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Fx&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Fx&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  updateState&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;s &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; s.copy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;stack &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; s.stack &lt;/span&gt;&lt;span&gt;:+&lt;/span&gt;&lt;span&gt; schema&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .zipRight&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;f&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .zipLeft&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;updateState&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;s &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; s.copy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;stack &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; s.stack.dropRight&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This allows us to decide whether we have to wrap a generated field&#39;s type in &lt;code&gt;Box&lt;/code&gt; in the rust code:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; boxIfNeeded&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;schema&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Fx&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustType&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  for&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    state &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; getState&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    backRef &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; state.stack.contains&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;schema&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    rustType &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; getRustType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;schema&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; if&lt;/span&gt;&lt;span&gt; backRef &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustType&lt;/span&gt;&lt;span&gt;.box&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;rustType&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span&gt; rustType&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By looking into &lt;code&gt;state.stack&lt;/code&gt; we can decide if we are dealing with a recursive type or not, and make our decision regarding boxing the field.&lt;/p&gt;
&lt;p&gt;Another example is to guard against infinite recursion when traversing the schema definition, as I explained before. We can define a helper function that just keeps track of all the visited schemas and shortcuts the computation if something has already been seen:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; ifNotProcessed&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Fx&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Fx&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  getState.flatMap&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; state &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    if&lt;/span&gt;&lt;span&gt; state.processed.contains&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; then&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZPure&lt;/span&gt;&lt;span&gt;.unit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    else&lt;/span&gt;&lt;span&gt; updateState&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_.copy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;processed &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; state.processed &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span&gt; value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.zipRight&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;f&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Putting all these smaller combinators together we have an easy-to-read core recursive transformation function for converting the schema:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;schema&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Fx&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  ifNotProcessed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;schema&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    getRustType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;schema&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.flatMap&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; typeRef &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      stacked&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;schema&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        schema &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;match&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;          //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the end to run a &lt;code&gt;Fx[A]&lt;/code&gt; all we need to do is to provide an initial state:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;processSchema.provideState&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;State&lt;/span&gt;&lt;span&gt;.empty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.runEither&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;inspecting-the-endpoints&quot;&gt;Inspecting the endpoints&lt;/h2&gt;
&lt;p&gt;We generated Rust code for all our types but we still need to generate HTTP clients. The basic idea is the same as what we have seen so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Traversing the &lt;code&gt;Endpoint&lt;/code&gt; data structure for each endpoint we have&lt;/li&gt;
&lt;li&gt;Generate some intermediate model&lt;/li&gt;
&lt;li&gt;Pretty print this model to Rust code&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The conversion once again is recursive, can fail, and requires keeping track of various things, so we can use &lt;code&gt;ZPure&lt;/code&gt; to implement it. Not repeating the same details, in this section we will talk about what exactly the endpoint descriptions look like and what we have be aware of when trying to process them.&lt;/p&gt;
&lt;p&gt;The first problem to solve is that currently ZIO Http does not have a concept of multiple endpoints. We are not composing &lt;code&gt;Endpoint&lt;/code&gt; values into an API, instead we first &lt;strong&gt;implement&lt;/strong&gt; them to get &lt;code&gt;Route&lt;/code&gt; values and compose those. We can no longer inspect the endpoint definitions from the composed routes, so unfortunately we have to repeat ourselves and somehow compose our set of endpoints for our code generator.&lt;/p&gt;
&lt;p&gt;First we can define a &lt;code&gt;RustEndpoint&lt;/code&gt; class, similar to the &lt;code&gt;RustModel&lt;/code&gt; earlier, containing all the necessary information to generate Rust code for a &lt;strong&gt;single endpoint&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;We can construct it with a function:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustEndpoint&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; fromEndpoint&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PathInput&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Input&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Output&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Middleware&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; EndpointMiddleware&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;      endpoint&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Endpoint&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PathInput&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Input&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Output&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Middleware&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Either&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustEndpoint&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The second thing to notice: endpoints do not have a name! If we look back to our initial example of &lt;code&gt;getWorkerMetadata&lt;/code&gt;, it did not have a unique name except the Scala value it was assigned to. But we can&#39;t observe that in our code generator (without writing a macro) so here we have chosen to just get a name as a string next to the definition.&lt;/p&gt;
&lt;p&gt;Then we can define a &lt;strong&gt;collection&lt;/strong&gt; of &lt;code&gt;RustEndpoint&lt;/code&gt;s:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustEndpoints&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Name&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;originalEndpoints&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustEndpoint&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and define a &lt;code&gt;++&lt;/code&gt; operator between &lt;code&gt;RustEndpoint&lt;/code&gt; and &lt;code&gt;RustEndpoints&lt;/code&gt;. In the end we can use these to define APIs like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    for&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      getDefaultProject &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; fromEndpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;getDefaultProject&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProjectEndpoints&lt;/span&gt;&lt;span&gt;.getDefaultProject&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      getProjects       &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; fromEndpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;getProjects&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProjectEndpoints&lt;/span&gt;&lt;span&gt;.getProjects&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      postProject       &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; fromEndpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;postProject&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProjectEndpoints&lt;/span&gt;&lt;span&gt;.postProject&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      getProject        &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; fromEndpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;getProject&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProjectEndpoints&lt;/span&gt;&lt;span&gt;.getProject&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      deleteProject     &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; fromEndpoint&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;deleteProject&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProjectEndpoints&lt;/span&gt;&lt;span&gt;.deleteProject&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    yield&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;getDefaultProject &lt;/span&gt;&lt;span&gt;++&lt;/span&gt;&lt;span&gt; getProjects &lt;/span&gt;&lt;span&gt;++&lt;/span&gt;&lt;span&gt; postProject &lt;/span&gt;&lt;span&gt;++&lt;/span&gt;&lt;span&gt; getProject &lt;/span&gt;&lt;span&gt;++&lt;/span&gt;&lt;span&gt; deleteProject&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.named&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Project&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The collection of endpoints also have a name (&lt;code&gt;&quot;Project&quot;&lt;/code&gt;). In the code generator we can use these to have a separate &lt;strong&gt;client&lt;/strong&gt; (trait and implementation) for each of these groups of endpoints.&lt;/p&gt;
&lt;p&gt;When processing a single endpoint, we need to process the following parts of data:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Inputs (&lt;code&gt;endpoint.input&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Outputs (&lt;code&gt;endpoint.output&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Errors (&lt;code&gt;endpoint.error&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Everything we need is encoded in one of these three fields of an endpoint, and all three are built on the same abstraction called &lt;code&gt;HttpCodec&lt;/code&gt;. Still there is a significant difference in what we want to do with inputs versus what we want to do with outputs and errors, so we can write two different traversals for gathering all the necessary information from them.&lt;/p&gt;
&lt;h3 id=&quot;inputs&quot;&gt;Inputs&lt;/h3&gt;
&lt;p&gt;When gathering information from the inputs, we are going to run into the following cases:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;HttpCodec.Combine&lt;/code&gt; means we have two different inputs; we need both, so we have to process both inner codecs sequentially, both extending our conversion function&#39;s state.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;HttpCodec.Content&lt;/code&gt; describes a &lt;strong&gt;request body&lt;/strong&gt;. Here we have a &lt;code&gt;Schema&lt;/code&gt; of our request body type and we can use the previously generated schema-to-rust type mapping to know how to refer to the generated rust type in our client code. It is important that in case there are &lt;strong&gt;multiple content codecs&lt;/strong&gt;, that means the endpoint receives a &lt;code&gt;multipart/form-data&lt;/code&gt; body, while if there is only one codec, it accepts an &lt;code&gt;application/json&lt;/code&gt; representation of that.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;HttpCodec.ContentStream&lt;/code&gt; represents a body containing a stream of a given element type. We can model this as just a &lt;code&gt;Vec&amp;lt;A&amp;gt;&lt;/code&gt; in the Rust side, but there is one special case here - if the element is a &lt;code&gt;Byte&lt;/code&gt;, ZIO Http expects a simple byte stream of type &lt;code&gt;application/octet-stream&lt;/code&gt; instead of a JSON-encoded array of bytes.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;HttpCodec.Fallback&lt;/code&gt; this represents the case when we should either use the first codec, &lt;em&gt;or&lt;/em&gt; the second. A special case is when the &lt;code&gt;right&lt;/code&gt; value of &lt;code&gt;Fallback&lt;/code&gt; is &lt;code&gt;HttpCodec.Empty&lt;/code&gt;. This is how ZIO Http represents optional inputs! We have to handle this specially in our code generator to mark some of the input parameters of the generated API as optional parameters. We don&#39;t support currently the other cases (when &lt;code&gt;right&lt;/code&gt; is not empty) as it is not frequently used and was not required for the &lt;em&gt;Golem API&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;HttpCodec.Header&lt;/code&gt; means we need to send a &lt;em&gt;header&lt;/em&gt; in the request, which can be a static (value described by the endpoint) or dynamic one (where we need to add an extra parameter to the generated function to get a value of the header). There are a couple of different primitive types supported for the value, such as string, numbers, UUIDs.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;HttpCodec.Method&lt;/code&gt; defines the method to be used for calling the endpoint&lt;/li&gt;
&lt;li&gt;&lt;code&gt;HttpCodec.Path&lt;/code&gt; describes the request path, which consists of a sequence of static and dynamic segments - for the dynamic segments the generated API need to have exposed function parameters of the appropriate type&lt;/li&gt;
&lt;li&gt;&lt;code&gt;HttpCodec.Query&lt;/code&gt; similar to the header codec defines query parameters to be sent&lt;/li&gt;
&lt;li&gt;&lt;code&gt;HttpCodec.TransformOrFail&lt;/code&gt; transforms a value with a Scala function - the same case as with &lt;code&gt;Schema.Transform&lt;/code&gt;. We cannot use the Scala function in our code generator so we just need to ignore this and go to the inner codec.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;HttpCodec.Annotated&lt;/code&gt; attaches additional information to the codecs that we are currently not using, but it could be used to get documentation strings and include them in the generated code as comments, for example.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;outputs&quot;&gt;Outputs&lt;/h3&gt;
&lt;p&gt;For outputs we are dealing with the same &lt;code&gt;HttpCodec&lt;/code&gt; type but there are some significant differences:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We can ignore &lt;code&gt;Path&lt;/code&gt;, &lt;code&gt;Method&lt;/code&gt;, &lt;code&gt;Query&lt;/code&gt; as they have no meaning for outputs&lt;/li&gt;
&lt;li&gt;We could look for &lt;em&gt;output headers&lt;/em&gt; but currently we ignore them&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Fallback&lt;/code&gt; on the other hand needs to be properly handled for outputs (errors, especially) because this is how the different error responses are encoded.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Status&lt;/code&gt; is combined with &lt;code&gt;Content&lt;/code&gt; in these &lt;code&gt;Fallback&lt;/code&gt; nodes to describe cases. This complicates the code generator because we need to record &quot;possible outputs&quot; which are only added as real output once we are sure we will not get any other piece of information for them.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To understand the error fallback handling better, let&#39;s take a look at how it is defined in one of Golem&#39;s endpoint groups:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; errorCodec&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HttpCodec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;HttpCodecType&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Status&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HttpCodecType&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Content&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LimitsEndpointError&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  HttpCodec&lt;/span&gt;&lt;span&gt;.enumeration&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LimitsEndpointError&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    HttpCodec&lt;/span&gt;&lt;span&gt;.error&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LimitsEndpointError&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unauthorized&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Status&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unauthorized&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    HttpCodec&lt;/span&gt;&lt;span&gt;.error&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LimitsEndpointError&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ArgValidationError&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Status&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;BadRequest&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    HttpCodec&lt;/span&gt;&lt;span&gt;.error&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LimitsEndpointError&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LimitExceeded&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Status&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Forbidden&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    HttpCodec&lt;/span&gt;&lt;span&gt;.error&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LimitsEndpointError&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;InternalError&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Status&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;InternalServerError&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This leads to a series of nested &lt;code&gt;HttpCodec.Fallback&lt;/code&gt;, &lt;code&gt;HttpCodec.Combine&lt;/code&gt;, &lt;code&gt;HttpCodec.Status&lt;/code&gt; and &lt;code&gt;HttpCodec.Content&lt;/code&gt; nodes. When processing them we first add values of possible outputs:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PossibleOutput&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;tpe&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustType&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;status&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Status&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;isError&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Boolean&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;schema&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Schema&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and once we have fully processed one branch of a &lt;code&gt;Fallback&lt;/code&gt;, we finalize these possible outputs and make them real outputs. The way these different error cases are mapped into different case classes of a a single error type (&lt;code&gt;LimitsEndpointError&lt;/code&gt;) also complicates things. When we reach a &lt;code&gt;HttpCodec.Content&lt;/code&gt; referencing  &lt;code&gt;Schema[LimitsEndpointError.LimitExceeded&lt;/code&gt;] for example, all we see is a &lt;code&gt;Schema.Record&lt;/code&gt; - and not the parent enum! For this reason in the code generator we are explicitly defining the error ADT type:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; fromEndpoint&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RustEndpoint&lt;/span&gt;&lt;span&gt;.withKnownErrorAdt&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LimitsEndpointError&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.zio&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and we detect if all cases are subtypes of this error ADT and generate the client code according to that.&lt;/p&gt;
&lt;h3 id=&quot;the-rust-client&quot;&gt;The Rust client&lt;/h3&gt;
&lt;p&gt;It is time to take a look at what the output of all this looks like. In this section we will examine some parts of the generated Rust code.&lt;/p&gt;
&lt;p&gt;Let&#39;s take a look at the &lt;strong&gt;Projects API&lt;/strong&gt;. We have generated a &lt;code&gt;trait&lt;/code&gt; for all the endpoints belonging to it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;async_trait&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;async_trait&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Project&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_default_project&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; authorization&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;model&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Project&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; ProjectError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_projects&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; project_name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; authorization&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;model&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Project&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; ProjectError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; post_project&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; field0&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;model&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;ProjectDataRequest&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; authorization&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;model&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Project&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; ProjectError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_project&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; project_id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; authorization&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;model&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Project&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; ProjectError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; delete_project&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; project_id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; authorization&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; ProjectError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is quite close to our original goal! One significant difference is that some type information is lost: &lt;code&gt;project_id&lt;/code&gt; was &lt;code&gt;ProjectId&lt;/code&gt; in Scala, and &lt;code&gt;authorization&lt;/code&gt; was &lt;code&gt;TokenSecret&lt;/code&gt; etc. Unfortunately with the current version of ZIO Schema these newtypes (or Scala 3 opaque types) are represented as primitive types transformed by a function. As explained earlier, we can&#39;t inspect the transformation function so all we can do is to use the underlying primitive type&#39;s schema here. This can be solved by introducing the concept of newtypes into ZIO Schema.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;ProjectError&lt;/code&gt; is a client specific generated &lt;code&gt;enum&lt;/code&gt; which can represent a mix of internal errors (such as not being able to call the endpoint) as well as the endpoint-specific domain errors:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; enum&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProjectError&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    RequestFailure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;reqwest&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Error&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    InvalidHeaderValue&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;reqwest&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;header&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;InvalidHeaderValue&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;    UnexpectedStatus&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;reqwest&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;StatusCode&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    Status404&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        message&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    Status403&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        error&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    Status400&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        errors&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    Status500&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        error&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    Status401&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        message&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So why are these per-status-code error types inlined here instead of generating the error ADT as a Rust &lt;code&gt;enum&lt;/code&gt; and using that? The reason is a difference between Scala and Rust: we have a single error ADT in Scala and we can still use its &lt;em&gt;cases&lt;/em&gt; directly in the endpoint definition:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;sealed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProjectEndpointError&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProjectEndpointError&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ArgValidation&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;errors&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProjectEndpointError&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;HttpCodec&lt;/span&gt;&lt;span&gt;.error&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProjectEndpointError&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ArgValidation&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Status&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;BadRequest&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We &lt;em&gt;do&lt;/em&gt; generate the corresponding &lt;code&gt;ProjectEndpointError&lt;/code&gt; enum in Rust:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Clone&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; PartialEq&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Eq&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Hash&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Ord&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; PartialOrd&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt; serde&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Serialize&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt; serde&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Deserialize&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; enum&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProjectEndpointError&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;    ArgValidation&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;        errors&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;but we cannot use &lt;code&gt;ProjectEndpointError::ArgValidation&lt;/code&gt; as a type in the above &lt;code&gt;ProjectError&lt;/code&gt; enum. And we cannot safely do something like &lt;code&gt;Either[ClientError, ProjectEndpointError]&lt;/code&gt; because in the endpoint DSL we just have a sequence of status code - error case pairs. There is no guarantee that one enum case is only used once in that mapping, or that every case is used at least once. For this reason the mapping from &lt;code&gt;ProjectError&lt;/code&gt; to &lt;code&gt;ProjectEndpointError&lt;/code&gt; is generated as a transformation function:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; ProjectError&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; to_project_endpoint_error&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;model&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;ProjectEndpointError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    match&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;      ProjectError&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Status400&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; errors&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;model&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;ProjectEndpointError&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;ArgValidation&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; errors&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; errors&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;      //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For each client trait we also generate a &lt;strong&gt;live implementation&lt;/strong&gt;, represented by a struct containing configuration for the client:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Clone&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Debug&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProjectLive&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    pub&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; base_url&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; reqwest&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Url&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    pub&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; allow_insecure&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; bool&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And the implementation of the client trait for these live structs are just using &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/reqwest/latest/reqwest/&quot;&gt;&lt;code&gt;reqwest&lt;/code&gt;&lt;/a&gt; (a HTTP client library for Rust) to construct the request from the input parameters exactly the way the endpoint definition described:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get_project&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; project_id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; authorization&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Project&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt; ProjectError&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; url&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;base_url&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  url&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;set_path&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;format!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;v1/projects/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;project_id&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; headers&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; reqwest&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;header&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;HeaderMap&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; builder&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; reqwest&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Client&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;builder&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; client&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; builder&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;build&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; client&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;url&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;headers&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;headers&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;send&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;await&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  match&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;status&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;as_u16&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;    200&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; body&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;json&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;crate&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;model&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Project&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;await&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;body&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;    404&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      let&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; body&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;json&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;ProjectEndpointErrorNotFoundPayload&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;await&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;ProjectError&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-10&quot;&gt;Status404&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; message&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; body&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;message &lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;putting-it-all-together&quot;&gt;Putting it all together&lt;/h2&gt;
&lt;p&gt;At this point we have seen how &lt;em&gt;ZIO Http&lt;/em&gt; describes endpoints, how &lt;em&gt;ZIO Schema&lt;/em&gt; encodes Scala types, how we can use &lt;em&gt;ZIO Parser&lt;/em&gt; to have composable printers and how &lt;em&gt;ZIO Prelude&lt;/em&gt; can help with working with state in a purely functional code. The only thing remaining is to wire everything together and define an easy to use function that, when executed, creates all the required &lt;em&gt;Rust files&lt;/em&gt; ready to be compiled.&lt;/p&gt;
&lt;p&gt;We can create a class for this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ClientCrateGenerator&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;version&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;description&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;homepage&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;endpoints&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustEndpoints&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here &lt;code&gt;endpoints&lt;/code&gt; is a collection of a &lt;strong&gt;group of endpoints&lt;/strong&gt;, as it was shown earlier. So first you can use &lt;code&gt;RustEndpoint.fromEither&lt;/code&gt; and &lt;code&gt;++&lt;/code&gt; to create a &lt;code&gt;RustEndpoints&lt;/code&gt; value for each API you have, and then generate a client for all of those in one run with this class.&lt;/p&gt;
&lt;p&gt;The first thing to do is collect &lt;em&gt;all&lt;/em&gt; the referenced &lt;code&gt;Schema&lt;/code&gt; from all the endpoints:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; allSchemas&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; endpoints.map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_.endpoints.toSet.flatMap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_.referredSchemas&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.reduce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_ union _&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we define a ZIO function (it is an effectful function, manipulating the filesystem!) to generate the files:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; generate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;targetDirectory&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Path&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Throwable&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  for&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    clientModel &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;.fromEither&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RustModel&lt;/span&gt;&lt;span&gt;.fromSchemas&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;allSchemas.toSeq&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                      .mapError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;err &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RuntimeException&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; g&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; $&lt;/span&gt;&lt;span&gt;err&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    cargoFile &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; targetDirectory &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Cargo.toml&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    srcDir &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; targetDirectory &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    libFile &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; srcDir &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;lib.rs&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    modelFile &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; srcDir &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;model.rs&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    requiredCrates &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; clientModel.requiredCrates union endpoints.map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_.requiredCrates&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.reduce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_ union _&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Files&lt;/span&gt;&lt;span&gt;.createDirectories&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;targetDirectory&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Files&lt;/span&gt;&lt;span&gt;.createDirectories&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;srcDir&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; writeCargo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;cargoFile, requiredCrates&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; writeLib&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;libFile&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; writeModel&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;modelFile, clientModel.definitions&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;.foreachDiscard&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;endpoints&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; endpoints &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;           val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; clientFile&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; srcDir &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;${&lt;/span&gt;&lt;span&gt;endpoints.name.toSnakeCase&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           writeClient&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;clientFile, endpoints&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  yield&lt;/span&gt;&lt;span&gt; ()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The steps are straightforward:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Create a &lt;code&gt;RustModel&lt;/code&gt; using all the collected &lt;code&gt;Schema[?]&lt;/code&gt; values&lt;/li&gt;
&lt;li&gt;Create all the required directories&lt;/li&gt;
&lt;li&gt;Write a &lt;em&gt;cargo file&lt;/em&gt; - having all the dependencies and other metadata required to compile the Rust project&lt;/li&gt;
&lt;li&gt;Write a &lt;em&gt;lib file&lt;/em&gt; - this is just a series of &lt;code&gt;pub mod xyz;&lt;/code&gt; lines, defining the generated modules which are put in different fiels&lt;/li&gt;
&lt;li&gt;Write all the generated Rust types into a &lt;code&gt;model.rs&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;For each endpoint group create a &lt;code&gt;xyz.rs&lt;/code&gt; module containing the client trait and implementation&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For working with the file system - creating directories, writing data into files, we can use the [&lt;a rel=&quot;external&quot; href=&quot;https://zio.dev/zio-nio/&quot;&gt;ZIO NIO&lt;/a&gt;] library providing ZIO wrapprers for all these functionalities.&lt;/p&gt;
&lt;h3 id=&quot;links&quot;&gt;Links&lt;/h3&gt;
&lt;p&gt;Finally, some links:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;code generator&lt;/strong&gt; is open source and available at https://github.com/vigoo/zio-http-rust - the code and the repository itself is not documented at the moment, except by this blog post.&lt;/li&gt;
&lt;li&gt;The generated &lt;strong&gt;Golem client for Rust&lt;/strong&gt; is published as a crate to https://crates.io/crates/golem-client&lt;/li&gt;
&lt;li&gt;The new &lt;strong&gt;Golem CLI&lt;/strong&gt;, using the generated client, is also open sourced and can be found at https://github.com/golemcloud/golem-cli&lt;/li&gt;
&lt;li&gt;Finally you can learn more about &lt;strong&gt;Golem&lt;/strong&gt; itself at https://www.golem.cloud&lt;/li&gt;
&lt;/ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>[Video] Introducing ZIO Flow @ ZIO World 2023</title>
        <published>2023-09-06T00:00:00+00:00</published>
        <updated>2023-09-06T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/introducing-zio-flow/"/>
        <id>https://blog.vigoo.dev/posts/introducing-zio-flow/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/introducing-zio-flow/">&lt;p&gt;My short talk at &lt;a rel=&quot;external&quot; href=&quot;https://www.zioworld.com/&quot;&gt;ZIO World 2023&lt;/a&gt; about the  &lt;a rel=&quot;external&quot; href=&quot;https://zio.dev/zio-flow/&quot;&gt;zio-flow library&lt;/a&gt;.&lt;/p&gt;
&lt;iframe width=&quot;800&quot; height=&quot;450&quot; src=&quot;https://www.youtube.com/embed/ujJuFd6Vvfc?si=bsh3b7f-LXFVP_v_&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share&quot; allowfullscreen&gt;&lt;/iframe&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>[Video] Binary Serialization Of Evolving Data Types @ Functional Scala 2022</title>
        <published>2022-12-01T00:00:00+00:00</published>
        <updated>2022-12-01T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/funscala2022-talk/"/>
        <id>https://blog.vigoo.dev/posts/funscala2022-talk/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/funscala2022-talk/">&lt;p&gt;My talk at &lt;a rel=&quot;external&quot; href=&quot;https://www.functionalscala.com/&quot;&gt;Functional Scala 2022&lt;/a&gt; about the binary serialization library &lt;a rel=&quot;external&quot; href=&quot;https://vigoo.github.io/desert/&quot;&gt;desert&lt;/a&gt;.&lt;/p&gt;
&lt;iframe width=&quot;800&quot; height=&quot;450&quot; src=&quot;https://www.youtube.com/embed/Y2KopYpjZ3Y&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share&quot; allowfullscreen&gt;&lt;/iframe&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>ZIO Kafka with transactions - a debugging story</title>
        <published>2022-06-15T00:00:00+00:00</published>
        <updated>2022-06-15T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/zio-kafka-debugging-story/"/>
        <id>https://blog.vigoo.dev/posts/zio-kafka-debugging-story/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/zio-kafka-debugging-story/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;With one of our clients, we were working on a chain of services responsible for processing some logs coming from a Kafka topic, partition them by some properties like user and date, infer and aggregate the log schema and eventually store the partitioned data in a different format. The details of this use case are not important for understanding this post, in which I&#39;m going to explain the recent changes to &lt;a rel=&quot;external&quot; href=&quot;https://ziverge.com/blog/introduction-to-zio-kafka&quot;&gt;ZIO Kafka&lt;/a&gt;, how was it implemented and how did we know it&#39;s not perfect, and the long story of investigation that finally resulted in a fix making this new feature usable in production.&lt;/p&gt;
&lt;p&gt;We only have to know about the first component of this data pipeline, which is a zio-kafka service:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Consumes it&#39;s source topic. Each record in this topic consists one or more log entries for a given user. The kafka topic&#39;s partitions are not aligned with our target partition (of user/date), all kafka partitions may contain data from all users.&lt;/li&gt;
&lt;li&gt;The service partitions the source data per user/date/hour and writes the log entries into Avro files in the local file system&lt;/li&gt;
&lt;li&gt;It also computes and aggregates a log schema in memory for each of these files&lt;/li&gt;
&lt;li&gt;It is using Kafka transactions to achieve &lt;a rel=&quot;external&quot; href=&quot;https://www.baeldung.com/kafka-exactly-once&quot;&gt;exactly-once delivery&lt;/a&gt;. This means that the processed records are not committed when they are written to the Avro files - there is a periodic event triggered every 30 seconds and at each rebalance that uploads the Avro files to S3, and &lt;em&gt;then&lt;/em&gt; it emits Kafka messages to downstream containing references to the uploaded files and their aggregated schema, and it commits all the offsets of all the input Kafka topic&#39;s &lt;em&gt;transactionally&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;img src=&quot;/images/blog-zio-kafka-debugging-1.webp&quot; alt=&quot;Diagram of the ZIO Kafka service decoding uploads, storing files in S3 and transactionally committing offsets&quot; width=&quot;960&quot; height=&quot;425&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;h2 id=&quot;stream-restarting-mode-in-zio-kafka&quot;&gt;Stream restarting mode in zio-kafka&lt;/h2&gt;
&lt;p&gt;When we first implemented this using zio-kafka and started to test it we have seen a lot of errors like&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Transiting to abortable error state due to org.apache.kafka.clients.consumer.CommitFailedException: Transaction offset Commit failed due to consumer group metadata mismatch: Specified group generation id is not valid.&quot;}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Group generation ID&lt;/em&gt; is a counter that gets incremented at each rebalance. The problem was that zio-kafka by default provides a continuous stream for partitions that survives rebalances. So we have a single stream per Kafka partition and after a rebalance we end up with some of them revoked and their streams stopped, some new streams created, but the ones that remained assigned are not going to be recreated.&lt;/p&gt;
&lt;img src=&quot;/images/blog-zio-kafka-debugging-2.webp&quot; alt=&quot;Kafka partitions with messages assigned to consumer generations across a rebalance&quot; width=&quot;701&quot; height=&quot;538&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;This works fine without using transactions, but it means your stream can contain messages from multiple generations. I first tried to solve this by detecting generation switches downstream but quickly realized this cannot work. It&#39;s too late to commit the previous generation when there are already records from the new generation; we have to do it before the rebalance finishes.&lt;/p&gt;
&lt;p&gt;To solve this I introduced a new &lt;em&gt;mode&lt;/em&gt; in zio-kafka back in February 2022, with &lt;a rel=&quot;external&quot; href=&quot;https://github.com/zio/zio-kafka/pull/427&quot;&gt;this pull request&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This adds a new mode to zio-kafka&#39;s core run loop which guarantees that every rebalance stops all the partition streams and create new ones every time.&lt;/p&gt;
&lt;img src=&quot;/images/blog-zio-kafka-debugging-3.webp&quot; alt=&quot;Rebalance diagram showing a stale message from the old generation arriving after partition reassignment&quot; width=&quot;714&quot; height=&quot;538&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;With this approach the library user can build the following logic on top of the &quot;stream of partition streams&quot; API of zio-kafka:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Get the next set of partition streams&lt;/li&gt;
&lt;li&gt;Merge and drain them all&lt;/li&gt;
&lt;li&gt;Perform a &lt;em&gt;flush&lt;/em&gt; - upload and commit everything before start working on the new set of streams&lt;/li&gt;
&lt;li&gt;Repeat&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This alone is still not enough - we have to block the rebalancing until we are done with the committing otherwise we would still get the invalid generation ID error.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;onRevoke&lt;/code&gt; and &lt;code&gt;onAssigned&lt;/code&gt; callbacks from the underlying Java Kafka library are working in a way that they block the rebalance process so that&#39;s the place where we can finish every processing for the revoked partitions. This extension point is provided by zio-kafka too but it&#39;s completely detached from the streaming API so I have introduced a rebalance event queue with with some promises and timeouts to coordinate this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In &lt;code&gt;onRevoke&lt;/code&gt; we publish a rebalance event and wait until it gets processed.&lt;/li&gt;
&lt;li&gt;Because the new run loop mode is guaranteed to terminate all streams on rebalance (which &lt;em&gt;is&lt;/em&gt; already happening, as we are in &lt;code&gt;onRevoke&lt;/code&gt;) we can be sure that eventually the main consumer stream&#39;s current stage - that drains the previous generation&#39;s partition streams will finish soon&lt;/li&gt;
&lt;li&gt;and then it performs the rotation and fulfills the promise in the rebalance event.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With these changes our service started to work - but we had to know if it works correctly.&lt;/p&gt;
&lt;h2 id=&quot;qos-tests&quot;&gt;QoS tests&lt;/h2&gt;
&lt;p&gt;We implemented a QoS test running on Spark which periodically checks that we are not loosing any data with our new pipeline.&lt;/p&gt;
&lt;p&gt;Our log entries have associated unique identifiers coming from upstream - so what we can do in this test is to consume an hour amount of log records from the same Kafka topic our service is consuming from, and read all the Avro files produced in that period (with some padding of course to have some tolerance for lag) and then see if there are any missing records in our output.&lt;/p&gt;
&lt;p&gt;Another source of truth for the investigation was an older system doing something similar, resulting in the same input being available as archived CSV files in some cases. Comparing the archived CSV files with the archived Avro files I could verify that the QoS test itself works correctly, by checking that both methods report the same set of missing records.&lt;/p&gt;
&lt;p&gt;What we learned from these tests was that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;there is data loss&lt;/li&gt;
&lt;li&gt;the data loss is related to rebalances&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To understand it&#39;s related to rebalances I was comparing failing QoS reports from several hours, figured out the ingestion time for some of the missing log records within these hours, and checked our service and infrastructure logs around that time. Every time there was a rebalance near the reported errors.&lt;/p&gt;
&lt;h2 id=&quot;additional-tests&quot;&gt;Additional tests&lt;/h2&gt;
&lt;p&gt;During the investigation I added some additional debug features and logs to the system.&lt;/p&gt;
&lt;p&gt;One of them is an extra verification step, enabled only temporarily in our development cluster, that&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;aggregates all the log identifiers at the earliest point - as soon as they got in the zio-kafka partition stream&lt;/li&gt;
&lt;li&gt;after uploading the Avro files and committing the records, it re-downloads all the files from S3 and checks if they got all the log identifiers that they should.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This never reported any error so based on that I considered the flow &lt;em&gt;after&lt;/em&gt; zio-kafka correct.&lt;/p&gt;
&lt;p&gt;We also have a lot of debug logs coming from the Java Kafka library, from zio-kafka and from our service to help understanding the issue:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;After each rebalance, the Java library logs the offset it&#39;s starting to read from&lt;/li&gt;
&lt;li&gt;When committing I&#39;m logging the minimum and maximum offset contained by the committed and uploaded Avro files per kafka partition&lt;/li&gt;
&lt;li&gt;All streams creation and termination are logged&lt;/li&gt;
&lt;li&gt;If records within a partition stream are skipping an offset (this was never logged actually)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I wrote a test app that reads our service&#39;s logs from a given period, logged from all the Kubernetes pods it&#39;s running on, and runs a state machine that verifies that all the logged offsets from the different pods are in sync. It fails in two cases:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When a pod &lt;em&gt;resets its offsets&lt;/em&gt; to something that was previously seen in the logs and there is a gap&lt;/li&gt;
&lt;li&gt;When a pod &lt;em&gt;rotates&lt;/em&gt; a kafka without it got assigned to that pod first (so if multiple pods would somehow consume the same partition which Kafka prevents)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I tried for long to write integration tests using embedded Kafka (similar to how it&#39;s done in zio-kafka&#39;s test suite) that reproduces the data loss issue, without any luck. In all my simulated cases everything works perfectly.&lt;/p&gt;
&lt;h2 id=&quot;theories-and-fixes&quot;&gt;Theories and fixes&lt;/h2&gt;
&lt;p&gt;From logs from the time ranges where the data loss is reported from, these additional checks were not showing any discrepancies.&lt;/p&gt;
&lt;p&gt;This could only mean two things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All the kafka/zio-kafka level is correct but we are still loosing data in our service-specific logic, somewhere in writing to Avro-s and uploading to S3.&lt;/li&gt;
&lt;li&gt;On Kafka level everything is fine but somehow zio-kafka does not pass all the records to our service&#39;s logic&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I trusted the validation mode I described earlier (the one that re-downloads the data) so I ruled out the first option.&lt;/p&gt;
&lt;h2 id=&quot;zio-kafka-internals&quot;&gt;zio-kafka internals&lt;/h2&gt;
&lt;p&gt;Before discussing the fixes I tried to make in zio-kafka, first let&#39;s talk about how the library works.&lt;/p&gt;
&lt;p&gt;The zio-kafka library wraps the Java library for Kafka and provides a ZIO Stream interface for consuming the records. As I mentioned earlier, it creates a separate stream for each kafka partition assigned to the consumer. The primary operation on the Java interface is called &lt;code&gt;poll&lt;/code&gt;. This method is responsible for fetching data for all the subscribed partitions for a given timeout. Another important property is that in case of rebalancing, the &lt;code&gt;poll&lt;/code&gt; is blocked until the rebalancing completes, and it calls the already mentioned revoked/assigned callbacks in this blocked state.&lt;/p&gt;
&lt;p&gt;Another thing it has to support is back pressure. We don&#39;t want this &lt;code&gt;poll&lt;/code&gt; to fetch more and more data for partitions where we did process the previous records yet. In other words, upstream demand in our ZIO Streams must control what partitions we &lt;code&gt;poll&lt;/code&gt;. In the Java level this is controlled by pausing and resuming individual partitions.&lt;/p&gt;
&lt;p&gt;So let&#39;s see a summary of how the consumer streams work:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Each partition stream is a repeated ZIO effect that enqueues a &lt;code&gt;Request&lt;/code&gt; in a &lt;em&gt;queue&lt;/em&gt; and then waits for the promise contained in this request to be fulfilled. The promise will contain a chunk of records fetched from Kafka if everything went well.&lt;/li&gt;
&lt;li&gt;There is a single (per consumer) &lt;em&gt;run loop&lt;/em&gt; which periodically calls &lt;code&gt;poll&lt;/code&gt;. Before calling it, it pauses/resumes partitions based on which partitions has at least one &lt;code&gt;Request&lt;/code&gt; since the last &lt;code&gt;poll&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This, as ZIO streams are pull based, implements the back pressure semantics mentioned earlier.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There is a similar mechanism for gathering commit requests and then performing them as part of the &lt;em&gt;run loop&lt;/em&gt; but in our use case that is not used - the transactional producer is independent of this mechanism.&lt;/p&gt;
&lt;p&gt;There is one more concept which is very important for to understand the problem: &lt;em&gt;buffered records&lt;/em&gt;. Imagine that we are consuming five partitions, &lt;code&gt;1 .. 5&lt;/code&gt; and only have a request (downstream pull) for partition &lt;code&gt;1&lt;/code&gt;. This means we are pausing &lt;code&gt;2 .. 5&lt;/code&gt; and do a &lt;code&gt;poll&lt;/code&gt; but what if the resulting record set contains records from other partitions? There could be multiple reason for this (and some of them may not be possible in practice), for example there could be some data already buffered within the Java library for the paused partitions, or maybe a rebalance assigns some new partitions which are not paused yet (as we don&#39;t know we are going to get them) resulting in immediately fetching some data for them.&lt;/p&gt;
&lt;p&gt;The library handles these cases in a simple way: it &lt;em&gt;buffers&lt;/em&gt; these records which were not requested in a per-partition map, and when a partition is pulled next time, it will not only give the records returned by &lt;code&gt;poll&lt;/code&gt; to the request&#39;s promise, but also all the buffered ones, prepended to the new set of records.&lt;/p&gt;
&lt;p&gt;Another important detail for this investigation is that we don&#39;t care about graceful shutdown, or if records got lost during shutdown. This is also very interesting in general, but our service is not trying to finish writing and uploading all data during shutdown, it simply ignores the partial data and quits without committing them so they get reprocessed as soon as possible in another consumer.&lt;/p&gt;
&lt;p&gt;What happens during rebalancing? Let&#39;s forget the default mode of zio-kafka for this discussion and focus on the new mode which &lt;em&gt;restarts&lt;/em&gt; all the partition streams every time.&lt;/p&gt;
&lt;p&gt;We don&#39;t know in advance that a rebalance will happen, it happens during the call to &lt;code&gt;poll&lt;/code&gt;. The method in the &lt;em&gt;run loop&lt;/em&gt; that contains this logic is called &lt;code&gt;handlePoll&lt;/code&gt; and does roughly the following (in our case):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;store the current state (containing the current streams, requests, buffered records etc) in a ref&lt;/li&gt;
&lt;li&gt;pause/resume partitions based on the current requests, as described earlier&lt;/li&gt;
&lt;li&gt;call &lt;code&gt;poll&lt;/code&gt;
&lt;ul&gt;
&lt;li&gt;during &lt;code&gt;poll&lt;/code&gt; in the revoked callback we end all partition streams. This means they get an interrupt signal and they stop. As I mentioned earlier, in this mode the consumer merges the partition streams and drain them; this is the other side of it, interrupting all the streams so we know that eventually this merged stream will also stop.&lt;/li&gt;
&lt;li&gt;dropping all the buffered records, but first adding them to a &lt;em&gt;drain queue&lt;/em&gt; (this is a fix that was not part of the original implementation). It is now guaranteed that the partition streams will get the remaining buffered elements before they stop.&lt;/li&gt;
&lt;li&gt;storing the fact of the rebalancing, so the rest of &lt;code&gt;handlePoll&lt;/code&gt; knows about it when &lt;code&gt;poll&lt;/code&gt; returns.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;once &lt;code&gt;poll&lt;/code&gt; returned, buffer all records for all unrequested partitions. this is another place where a fix was made, currently we treat &lt;em&gt;all&lt;/em&gt; records unrequested in case of a rebalancing, because all the streams were restarted, so the original requests were made by the previous set of streams; fulfilling them would loose data because the new streams are not waiting for the same promises.&lt;/li&gt;
&lt;li&gt;the next step would be to fulfill all the requests that we can by using the combination of buffered records and the &lt;code&gt;poll&lt;/code&gt; result. But we had a rebalance and dropped all the requests! So this step must not do anything.&lt;/li&gt;
&lt;li&gt;finally we start new streams for each assigned partition&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So based on all this, and the theory that the commits/offsets are all correct but somehow data is lost between the Java library and the service logic, the primary suspect was the &lt;em&gt;buffered records&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Let&#39;s see what fixes and changes I made, in time order:&lt;/p&gt;
&lt;h2 id=&quot;fix-attempt-1&quot;&gt;Fix attempt 1&lt;/h2&gt;
&lt;p&gt;The first time I suspected buffered records are behind the issue I realized that when we end &lt;em&gt;all&lt;/em&gt; partition streams during rebalancing, we loose the buffered records. This is not a problem if those partitions are really revoked - it means there was no demand for those partitions, so it&#39;s just that some records were read ahead and now they get dropped and will be reprocessed on another consumer.&lt;/p&gt;
&lt;p&gt;But if the same partition is &quot;reassigned&quot; to the same consumer, this could be a data loss! The reason is that there is an internal state in Kafka which is a per-consumer, per-partition &lt;em&gt;position&lt;/em&gt;. In this case this position would point to &lt;em&gt;after&lt;/em&gt; the buffered records, so the next &lt;code&gt;poll&lt;/code&gt; will get the next records and the previously buffered ones will not be prepended as usual because the revocation clears the buffer.&lt;/p&gt;
&lt;p&gt;Note that this whole problem would not exist if the reassigned partitions get &lt;em&gt;reseted&lt;/em&gt; to the last committed offset after rebalancing. I don&#39;t think this is the case, only when a new partition is assigned to a consumer with no previous position.&lt;/p&gt;
&lt;p&gt;My first fix was passing the buffered records to the user-defined revoke handler so it could write the remaining records to the Avro files before uploading them. This was just a quick test, as it does not really fit into the API of zio-kafka.&lt;/p&gt;
&lt;h2 id=&quot;fix-attempt-2&quot;&gt;Fix attempt 2&lt;/h2&gt;
&lt;p&gt;After playing with the first fix for a while I thought it solved the issue but it was just not reproducing - it is not completely clear why, probably I missed some test results.&lt;/p&gt;
&lt;p&gt;But I wrote a second version of the same fix, this time by adding the remaining buffered elements to the end of the partition streams before they stop, instead of explicitly passing them to the revoke handler.&lt;/p&gt;
&lt;p&gt;This should work exactly the same but handles the problem transparently.&lt;/p&gt;
&lt;h2 id=&quot;fix-attempt-3&quot;&gt;Fix attempt 3&lt;/h2&gt;
&lt;p&gt;After some more testing it was clear that the QoS tests were still showing data loss. The investigation continued and the next problem I have found was that in &lt;code&gt;handlePoll&lt;/code&gt; after a rebalance we were not storing the buffered records anymore in this &quot;restarting streams&quot; mode. I did not catch this in the first fix attempts I was focusing on dealing with the buffered records at the &lt;em&gt;end&lt;/em&gt; of the revoked streams.&lt;/p&gt;
&lt;p&gt;What does it mean it was not storing the buffered records? In &lt;code&gt;handlePoll&lt;/code&gt; there is a series of state manipulation functions and the buffered records map is part of this state. The logic here is quite complicated and it very much depends on whether we are running the consumer in &lt;em&gt;normal&lt;/em&gt; or &lt;em&gt;stream restarting&lt;/em&gt; mode. The problem was that for some reason after a rebalance (in the new mode only) this buffered records field was cleared instead of preserving records from before the rebalance.&lt;/p&gt;
&lt;h2 id=&quot;fix-attempt-4&quot;&gt;Fix attempt 4&lt;/h2&gt;
&lt;p&gt;Very soon turned out that my previous fix was not doing anything, because there was one more problem in the state handling in &lt;code&gt;handlePoll&lt;/code&gt;. As I wrote, it bufferes only those records which were not &lt;em&gt;requested&lt;/em&gt;. For those partitions which have a request, it fulfills these requests with the new records instead. When the reassigned partitions are not restarted during rebalancing (as in the &lt;em&gt;normal mode&lt;/em&gt;) this is OK but for us, as we are creating new streams, the old requests must be dropped and not taken into account when deciding which records to buffer.&lt;/p&gt;
&lt;p&gt;In other words, in &lt;em&gt;restarting streams mode&lt;/em&gt; we have to buffer all records after a rebalance.&lt;/p&gt;
&lt;h2 id=&quot;fix-attempt-5&quot;&gt;Fix attempt 5&lt;/h2&gt;
&lt;p&gt;I was very confident about the previous fix but something was still not OK, the test continued to report data loss. After several code reviews and discussions, I realized that it is not guaranteed that the &lt;code&gt;onRevoked&lt;/code&gt; and &lt;code&gt;onAssigned&lt;/code&gt; callbacks are called within a single &lt;code&gt;poll&lt;/code&gt;! My code was not prepared for this (the original zio-kafka code was, actually, but I did not realize this for a long time).&lt;/p&gt;
&lt;p&gt;First of all I had to change the way how the rebalance callbacks are passing information to the poll handler. The previously added rebalance event (which was a simple case class) was changed to be either &lt;code&gt;Revoked&lt;/code&gt;, &lt;code&gt;Assigned&lt;/code&gt; or &lt;code&gt;RevokedAndAssigned&lt;/code&gt; and I made sure that for each case all the run loop state variables are modified correctly.&lt;/p&gt;
&lt;p&gt;Immediately after deploying this, I saw evidence in the logs that indeed the revoked and assigned callbacks are called separately, so the fix was definitely needed. The only problem was that I did not really understand how could this cause data loss, and by doing some rebalancing tests it turned out that the problem still exists.&lt;/p&gt;
&lt;h2 id=&quot;fix-attempt-6&quot;&gt;Fix attempt 6&lt;/h2&gt;
&lt;p&gt;One more thing I added in the previous attempt was a log in a place that was suspicious to me and I did not care about it earlier. When adding requests to the run loop - these are added to the run loop&#39;s command queue when a partition stream tries to pull, completely asynchronous to the run loop itself - it was checking if currently the run loop is in the middle of a rebalancing. So in case the rebalancing takes multiple &lt;code&gt;poll&lt;/code&gt;s, as we have seen, it is possible that between the &lt;code&gt;onRevoked&lt;/code&gt; and &lt;code&gt;onAssigned&lt;/code&gt; events we get some new requests from the streams.&lt;/p&gt;
&lt;p&gt;In the restart-streams mode all partition streams are interrupted on the revoke event, and no new streams are created until the assigned event. This means that these requests can &lt;em&gt;only&lt;/em&gt; come from the previous streams so they should be ignored. But what zio-kafka was doing was to add these requests to the run loop&#39;s pending requests. This is correct behavior in its normal mode, because on rebalance some of the streams survive it and their requests can be still fulfilled.&lt;/p&gt;
&lt;p&gt;But in our case it is incorrect, because after the assignment is done and some records are fetched by &lt;code&gt;poll&lt;/code&gt;, these pending requests get fulfilled with them, &quot;stealing&quot; the records from the new partition streams!&lt;/p&gt;
&lt;p&gt;At this point I really felt like this was the last missing piece of the puzzle.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;And it was!&lt;/p&gt;
&lt;p&gt;The final set of fixes are published &lt;a rel=&quot;external&quot; href=&quot;https://github.com/zio/zio-kafka/pull/473&quot;&gt;in this pull request&lt;/a&gt;. The service and its tests are running perfectly since more than 10 days, proving that it is correct.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>[Video] ZIO Parser @ ZIO World 2022</title>
        <published>2022-03-11T00:00:00+00:00</published>
        <updated>2022-03-11T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/zioworld-talk/"/>
        <id>https://blog.vigoo.dev/posts/zioworld-talk/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/zioworld-talk/">&lt;p&gt;My talk at &lt;a rel=&quot;external&quot; href=&quot;https://zioworld.com/&quot;&gt;ZIO World 2022&lt;/a&gt; introducing &lt;a rel=&quot;external&quot; href=&quot;https://github.com/zio/zio-parser&quot;&gt;ZIO Parser&lt;/a&gt;&lt;/p&gt;
&lt;iframe width=&quot;800&quot; height=&quot;450&quot; src=&quot;https://www.youtube.com/embed/IG6SmKPPamY&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;/iframe&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>[Video] Generating Libraries @ Functional Scala 2021</title>
        <published>2021-12-03T00:00:00+00:00</published>
        <updated>2021-12-03T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/funscala2021-talk/"/>
        <id>https://blog.vigoo.dev/posts/funscala2021-talk/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/funscala2021-talk/">&lt;p&gt;My talk at &lt;a rel=&quot;external&quot; href=&quot;https://www.functionalscala.com/&quot;&gt;Functional Scala 2021&lt;/a&gt; about generating libraries in Scala:&lt;/p&gt;
&lt;iframe width=&quot;800&quot; height=&quot;450&quot; src=&quot;https://www.youtube.com/embed/HCPTmytex3U&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;/iframe&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Writing kubectl plugins with ZIO K8s</title>
        <published>2021-03-07T00:00:00+00:00</published>
        <updated>2021-03-07T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/zio-k8s-plugins/"/>
        <id>https://blog.vigoo.dev/posts/zio-k8s-plugins/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/zio-k8s-plugins/">&lt;p&gt;Originally posted &lt;a rel=&quot;external&quot; href=&quot;https://ziverge.com/blog/zio-k8s-kubectl-plugin&quot;&gt;at the Ziverge blog&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Andrea Peruffo recently published &lt;a rel=&quot;external&quot; href=&quot;https://www.lightbend.com/blog/writing-kubectl-plugins-with-scala-or-java-with-fabric8-kubernetes-client-on-graalvm?utm_campaign=Oktopost-BLG+-+Writing+Kubectl+plugins+in+Java+or+Scala&quot;&gt;a blog post on the Lightbend blog&lt;/a&gt; about how they migrated a &lt;code&gt;kubectl&lt;/code&gt; plugin from Golang to Scala using the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/fabric8io/kubernetes-client&quot;&gt;Fabric8&lt;/a&gt; Kubernetes client and a few Scala libraries. This is a perfect use case for the &lt;a rel=&quot;external&quot; href=&quot;https://coralogix.github.io/zio-k8s/&quot;&gt;zio-k8s library&lt;/a&gt; announced &lt;a rel=&quot;external&quot; href=&quot;https://coralogix.com/log-analytics-blog/the-coralogix-operator-a-tale-of-zio-and-kubernetes/&quot;&gt;two weeks ago&lt;/a&gt;, so we decided to write this post demonstrating how to implement the same example using the ZIO ecosystem.&lt;/p&gt;
&lt;p&gt;We are going to implement the same example, originally described in the &lt;a rel=&quot;external&quot; href=&quot;https://dev.to/ikwattro/write-a-kubectl-plugin-in-java-with-jbang-and-fabric8-566&quot;&gt;Write a kubectl plugin in Java with JBang and fabric8&lt;/a&gt; article, using the following libraries:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://zio.dev/&quot;&gt;ZIO&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://coralogix.github.io/zio-k8s/&quot;&gt;ZIO K8s&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://zio.github.io/zio-logging/&quot;&gt;ZIO Logging&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://vigoo.github.io/clipp/docs/&quot;&gt;clipp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://sttp.softwaremill.com/en/latest/&quot;&gt;sttp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://circe.github.io/circe/&quot;&gt;circe&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The source code of the example &lt;a rel=&quot;external&quot; href=&quot;https://github.com/zivergetech/zio-k8s-kubectl-plugin-example&quot;&gt;can be found here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The linked blog post does a great job in explaining the benefits and difficulties of compiling to native image with GraalVM so we are not going to repeat it here. Instead, we will focus on how the implementation looks in the functional Scala world.&lt;/p&gt;
&lt;p&gt;The example has to implement two &lt;em&gt;kubectl commands&lt;/em&gt;: &lt;code&gt;version&lt;/code&gt; to print its own version and &lt;code&gt;list&lt;/code&gt; to list information about &lt;em&gt;all Pods of the Kubernetes cluster&lt;/em&gt; in either ASCII table, JSON or YAML format.&lt;/p&gt;
&lt;h3 id=&quot;cli-parameters&quot;&gt;CLI parameters&lt;/h3&gt;
&lt;p&gt;Let&#39;s start with defining these command line options with the &lt;a rel=&quot;external&quot; href=&quot;https://vigoo.github.io/clipp/docs/&quot;&gt;clipp&lt;/a&gt; library!&lt;/p&gt;
&lt;p&gt;First, we define the data structures that describe our parameters:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;sealed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Format&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Format&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Default&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Format&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Json&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Format&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Yaml&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Format&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;sealed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Command&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Command&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ListPods&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;format&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Format&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Command&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Version&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Command&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Parameters&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;verbose&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Boolean&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;command&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Command&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When parsing the arguments (passed as an array of strings), we need to either produce a &lt;code&gt;Parameters&lt;/code&gt; value or fail and print some usage information.&lt;/p&gt;
&lt;p&gt;With &lt;code&gt;clipp&lt;/code&gt;, this is done by defining a parameter parser using its parser DSL in a &lt;em&gt;for comprehension&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; spec&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    _           &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; metadata&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;kubectl lp&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    verbose     &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; flag&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Verbose logging&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;verbose&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    commandName &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; command&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    command     &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      commandName &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;match&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          pure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Command&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Version&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;    =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;          for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            specifiedFormat &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; optional &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                namedParameter&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Format&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;                                  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Output format&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;                                  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;default|json|yaml&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;                                  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;                                  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                              }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            format           &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; specifiedFormat.getOrElse&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Format&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Default&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Command&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ListPods&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;format&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Parameters&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;verbose, command&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As we can see, it is possible to make decisions in the parser based on the previously parsed values, so each &lt;em&gt;command&lt;/em&gt; can have a different set of arguments. In order to parse the possible &lt;em&gt;output formats&lt;/em&gt;, we also implement the &lt;code&gt;ParameterParser&lt;/code&gt; type class for &lt;code&gt;Format&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; parameterParser&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ParameterParser&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Format&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ParameterParser&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Format&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; parse&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Either&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Format&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      value.toLowerCase &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;match&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Right&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Format&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Default&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;    =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Right&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Format&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Json&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;yaml&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;    =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Right&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Format&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Yaml&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span&gt; _         &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Left&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; f&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;$&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; u&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; example&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Format&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Format&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Default&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is all we need to bootstrap our command line application. The following main function parses the arguments and provides the parsed &lt;code&gt;Parameters&lt;/code&gt; value to the &lt;code&gt;ZIO&lt;/code&gt; program:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;args&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; URIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;zio.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ZEnv&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ExitCode&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; clippConfig&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; config.fromArgsWithUsageInfo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;args, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Parameters&lt;/span&gt;&lt;span&gt;.spec&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  runWithParameters&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .provideCustomLayer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;clippConfig&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .catchAll &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; _&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ParserFailure&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;.succeed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ExitCode&lt;/span&gt;&lt;span&gt;.failure&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; runWithParameters&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ZEnv&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ClippConfig&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Parameters&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ExitCode&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;working-with-kubernetes&quot;&gt;Working with Kubernetes&lt;/h3&gt;
&lt;p&gt;In &lt;code&gt;runWithParameters&lt;/code&gt;, we have everything needed to initialize the logging and Kubernetes modules and perform the actual command. Before talking about the initialization though, let&#39;s take a look at how we can list the pods!&lt;/p&gt;
&lt;p&gt;We define a data type holding all the information we want to report about each pod:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PodInfo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;namespace&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;status&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;message&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The task now is to fetch &lt;em&gt;all pods&lt;/em&gt; from Kubernetes and construct &lt;code&gt;PodInfo&lt;/code&gt; values. In &lt;code&gt;zio-k8s&lt;/code&gt; &lt;em&gt;getting a list of pods&lt;/em&gt; is defined as a &lt;strong&gt;ZIO Stream&lt;/strong&gt;, which under the hood sends multiple HTTP requests to Kubernetes taking advantage of its &lt;em&gt;pagination&lt;/em&gt; capability. In this &lt;em&gt;stream&lt;/em&gt; each element will be a &lt;code&gt;Pod&lt;/code&gt; and we can start processing them one by one as soon they arrive over the wire. This way the implementation of the &lt;code&gt;list&lt;/code&gt; command can be something like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;format&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Format&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; log.debug&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Executing the list command&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; pods&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            .getAll&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;namespace &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            .mapM&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;toModel&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            .run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;reports.sink&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;format&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            .catchAll &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; k8sFailure &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;              console.putStrLnErr&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; g&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; $&lt;/span&gt;&lt;span&gt;k8sFailure&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span&gt; ()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&#39;s take a look at each line!&lt;/p&gt;
&lt;p&gt;First, &lt;code&gt;log.debug&lt;/code&gt; uses the &lt;em&gt;ZIO logging&lt;/em&gt; library. We are going to initialize logging in a way that these messages only appear if the &lt;code&gt;--verbose&lt;/code&gt; option was enabled.&lt;/p&gt;
&lt;p&gt;Then &lt;code&gt;pods.getAll&lt;/code&gt; is the ZIO Stream provided by the &lt;em&gt;ZIO K8s&lt;/em&gt; library. Not providing a specific namespace means that we are getting pods from &lt;em&gt;all&lt;/em&gt; namespaces.&lt;/p&gt;
&lt;p&gt;With &lt;code&gt;mapM(toModel)&lt;/code&gt; we transform each &lt;code&gt;Pod&lt;/code&gt; in the stream to our &lt;code&gt;PodInfo&lt;/code&gt; data structure.&lt;/p&gt;
&lt;p&gt;Finally we &lt;code&gt;run&lt;/code&gt; the stream into a &lt;em&gt;sink&lt;/em&gt; that is responsible for displaying the &lt;code&gt;PodInfo&lt;/code&gt; structures with the specific &lt;em&gt;output format&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;Pod&lt;/code&gt; objects returned in the stream are simple &lt;em&gt;case classes&lt;/em&gt; containing all the information available for the given resource. Most of the fields of these case classes are &lt;em&gt;optional&lt;/em&gt; though, even though we can be sure that in our case each pod would have a name, a namespace and a status. To make working with these data structures easier within a set of expectations, they feature &lt;em&gt;getter methods&lt;/em&gt; that are ZIO functions either returning the field&#39;s value, or failing if they are not specified. With these we can implement &lt;code&gt;toModel&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; toModel&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;pod&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Pod&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;K8sFailure&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PodInfo&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      metadata  &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; pod.getMetadata&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      name      &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; metadata.getName&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      namespace &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; metadata.getNamespace&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      status    &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; pod.getStatus&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      phase     &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; status.getPhase&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      message    &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; status.message.getOrElse&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PodInfo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;name, namespace, phase, message&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;An alternative would be to just store the optional values in &lt;code&gt;PodInfo&lt;/code&gt; and handle their absence in the &lt;em&gt;report sink&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Let&#39;s talk about the &lt;em&gt;type&lt;/em&gt; of the above defined &lt;code&gt;run&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Pods&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Console&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Logging&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The ZIO &lt;em&gt;environment&lt;/em&gt; precisely specifies the modules used by our &lt;code&gt;run&lt;/code&gt; function:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Module&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;Pods&lt;/code&gt;&lt;/td&gt;&lt;td&gt;for accessing K8s pods&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;Console&lt;/code&gt;&lt;/td&gt;&lt;td&gt;for printing &lt;em&gt;errors&lt;/em&gt; on the standard error channel with &lt;code&gt;putStrLnErr&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;Logging&lt;/code&gt;&lt;/td&gt;&lt;td&gt;for emitting some debug logs&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The error type is &lt;code&gt;Nothing&lt;/code&gt; because it can never fail - all errors are catched and displayed for the user within the run function.&lt;/p&gt;
&lt;h3 id=&quot;initialization&quot;&gt;Initialization&lt;/h3&gt;
&lt;p&gt;Now we can see that in order to run the &lt;code&gt;list&lt;/code&gt; command in &lt;code&gt;runWithParameters&lt;/code&gt;, we must &lt;em&gt;provide&lt;/em&gt; &lt;code&gt;Pods&lt;/code&gt; and &lt;code&gt;Logging&lt;/code&gt; modules to our implementation (&lt;code&gt;Console&lt;/code&gt; is part of the default environment and does not need to be provided).&lt;/p&gt;
&lt;p&gt;These modules are described by &lt;em&gt;ZIO Layers&lt;/em&gt; which can be composed together to provide the &lt;em&gt;environment&lt;/em&gt; for running our ZIO program. In this case we need to define a &lt;em&gt;logging layer&lt;/em&gt; and a &lt;em&gt;kubernetes pods client&lt;/em&gt; layer and then compose the two for our &lt;code&gt;list&lt;/code&gt; implementation.&lt;/p&gt;
&lt;p&gt;Let&#39;s start with logging:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; configuredLogging&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;verbose&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Boolean&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZLayer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Clock&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Logging&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; logLevel&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;verbose&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LogLevel&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Trace&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LogLevel&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Info&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Logging&lt;/span&gt;&lt;span&gt;.consoleErr&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;logLevel&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span&gt; initializeSlf4jBridge&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We create a simple ZIO console logger that will print lines to the standard error channel; the enabled log level is determined by the &lt;code&gt;verbose&lt;/code&gt; command line argument. As this logger writes to the console and also prints timestamps, our logging layer &lt;em&gt;requires&lt;/em&gt; &lt;code&gt;Console with Clock&lt;/code&gt; to be able to build a &lt;code&gt;Logging&lt;/code&gt; module. Enabling the &lt;em&gt;SLF4j bridge&lt;/em&gt; guarantees that logs coming from third party libraries will also get logged through ZIO logging. In our example this means that when we enable verbose logging, our &lt;code&gt;kubectl&lt;/code&gt; plugin will log the HTTP requests made by the Kubernetes library!&lt;/p&gt;
&lt;p&gt;The second layer we must define constructs a &lt;code&gt;Pods&lt;/code&gt; module:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; pods&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; k8sDefault &lt;/span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Pods&lt;/span&gt;&lt;span&gt;.live&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By using &lt;code&gt;k8sDefault&lt;/code&gt; we ask &lt;code&gt;zio-k8s&lt;/code&gt; to use the &lt;em&gt;default configuration chain&lt;/em&gt;, which first tries to load the &lt;code&gt;kubeconfig&lt;/code&gt; and use the active &lt;em&gt;context&lt;/em&gt; stored in it. This is exactly what &lt;code&gt;kubectl&lt;/code&gt; does, so it is the perfect choice when writing a &lt;code&gt;kubectl&lt;/code&gt; plugin. Other variants provide more flexibility such as loading custom configuration with the &lt;a rel=&quot;external&quot; href=&quot;https://zio.github.io/zio-config/&quot;&gt;ZIO Config&lt;/a&gt; library. Once we have a &lt;em&gt;k8s configuration&lt;/em&gt; we just feed it to the set of resource modules we need. In this example we only need to access pods. In more complex applications this would be something like &lt;code&gt;k8sDefault &amp;gt;&amp;gt;&amp;gt; (Pods.live ++ Deployments.live ++ ...)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;With both layers defined, we can now provide them to our command implementation:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;runCommand&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;parameters.command&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .provideCustomLayer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;logging &lt;/span&gt;&lt;span&gt;++&lt;/span&gt;&lt;span&gt; pods&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;output&quot;&gt;Output&lt;/h3&gt;
&lt;p&gt;The last thing missing is the &lt;em&gt;report sink&lt;/em&gt; that we are running the stream of pods into. We are going to define three different sinks for the three output types.&lt;/p&gt;
&lt;p&gt;Let&#39;s start with JSON!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sink&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Encoder&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZSink&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Console&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  ZSink&lt;/span&gt;&lt;span&gt;.foreach &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;item&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    console.putStrLn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;item.asJson.printWith&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Printer&lt;/span&gt;&lt;span&gt;.spaces2SortKeys&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The JSON sink requires &lt;code&gt;Console&lt;/code&gt; and then for each element &lt;code&gt;T&lt;/code&gt; it converts it to JSON and pretty prints it to console. Note that this is going to be a JSON document per each line. We could easily define a different sink that collects each element and produces a single valid JSON array of them:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; arraySink&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Encoder&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZSink&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Console&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    ZSink&lt;/span&gt;&lt;span&gt;.collectAll.flatMap &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;items&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      ZSink&lt;/span&gt;&lt;span&gt;.fromEffect &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        console.putStrLn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Json&lt;/span&gt;&lt;span&gt;.arr&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;items.map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_.asJson&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; _*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.printWith&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Printer&lt;/span&gt;&lt;span&gt;.spaces2SortKeys&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;T&lt;/code&gt; type paramter in our example will always be &lt;code&gt;PodInfo&lt;/code&gt;. By requiring it to have an implementation of circe&#39;s &lt;code&gt;Encoder&lt;/code&gt; type class we can call &lt;code&gt;.asJson&lt;/code&gt; on instances of &lt;code&gt;T&lt;/code&gt;, encoding it into a JSON object. We can &lt;em&gt;derive&lt;/em&gt; these encoders automatically:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; encoder&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Encoder&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PodInfo&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; deriveEncoder&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Producing YAML output is exactly the same except of first converting the JSON model to YAML with &lt;code&gt;asJson.asYaml&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The third output format option is to generate ASCII tables. We implement that with the same Java library as the original post, called &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vdmeer/asciitable&quot;&gt;&lt;code&gt;asciitable&lt;/code&gt;&lt;/a&gt;. In order to separate the specification of how to convert a &lt;code&gt;PodInfo&lt;/code&gt; to a table from the sink implementation, we can define our own type class similar to the JSON &lt;code&gt;Encoder&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Tabular&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    /**&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Initializes a table by setting properties and adding header rows&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;      */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; createTableRenderer&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZManaged&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AsciiTable&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    /**&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Adds a single item of type T to the table created with &lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-3 z-d-i&quot;&gt;createTableRenderer()&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;]]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;      */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; addRow&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;table&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AsciiTable&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;item&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    /**&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Adds the table&amp;#39;s footer and renders it to a string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;      */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; renderTable&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;table&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AsciiTable&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can implement this for &lt;code&gt;PodInfo&lt;/code&gt; and then use a generic sink for printing the result table, similar to the previous examples:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; sink&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; tabular&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Tabular&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZSink&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Console&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  ZSink&lt;/span&gt;&lt;span&gt;.managed&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Console&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AsciiTable&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;tabular.createTableRenderer&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    table &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; initialize the table&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      ZSink&lt;/span&gt;&lt;span&gt;.foreach&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;tabular.addRow&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;table&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;lt;*&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; add each row&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      printResultTable&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;table&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; print the result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; printResultTable&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;  table&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AsciiTable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; tabular&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Tabular&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZSink&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Console&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  ZSink&lt;/span&gt;&lt;span&gt;.fromEffect &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    tabular&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .renderTable&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;table&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .flatMap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;str &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; console.putStrLn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;trying-it-out&quot;&gt;Trying it out&lt;/h3&gt;
&lt;p&gt;With the report sinks implemenented we have everything ready to try out our new &lt;code&gt;kubectl&lt;/code&gt; plugin!&lt;/p&gt;
&lt;p&gt;We can compile the example to &lt;em&gt;native image&lt;/em&gt; and copy the resulting image to a location on the &lt;code&gt;PATH&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;sbt nativeImage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cp target/native-image/kubectl-lp ~/bin&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then use &lt;code&gt;kubectl lp&lt;/code&gt; to access our custom functions:&lt;/p&gt;
&lt;img src=&quot;/images/blog-ziok8s-kubectlplugin.webp&quot; alt=&quot;The kubectl lp plugin printing usage, version and a table of Kubernetes resources&quot; width=&quot;1108&quot; height=&quot;1092&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>The Coralogix Operator: A Tale of ZIO and Kubernetes</title>
        <published>2021-02-16T00:00:00+00:00</published>
        <updated>2021-02-16T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/zio-k8s/"/>
        <id>https://blog.vigoo.dev/posts/zio-k8s/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/zio-k8s/">&lt;p&gt;My blog post &lt;a rel=&quot;external&quot; href=&quot;https://coralogix.com/blog/the-coralogix-operator-a-tale-of-zio-and-kubernetes/&quot;&gt;published at the Coralogix blog&lt;/a&gt; about using &lt;a rel=&quot;external&quot; href=&quot;https://coralogix.github.io/zio-k8s/&quot;&gt;zio-k8s&lt;/a&gt; for writing operators.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>ZIO-AWS with ZIO Query</title>
        <published>2020-11-01T00:00:00+00:00</published>
        <updated>2020-11-01T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/zioaws-zioquery/"/>
        <id>https://blog.vigoo.dev/posts/zioaws-zioquery/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/zioaws-zioquery/">&lt;p&gt;A few years ago I wrote a &lt;a href=&quot;https://blog.vigoo.dev/posts/aws-rate-limits-prezidig/&quot;&gt;post&lt;/a&gt; about how I refactored one of our internal tools at &lt;a rel=&quot;external&quot; href=&quot;https://prezi.com&quot;&gt;Prezi&lt;/a&gt;. This command line tool was able to discover a set of AWS resources and present them in a nice human readable way. The primary motivation at that time was to introduce circuit breaking to survive AWS API rate limits.&lt;/p&gt;
&lt;p&gt;I have recently published a set of libraries, &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/zio-aws&quot;&gt;&lt;strong&gt;zio-aws&lt;/strong&gt;&lt;/a&gt;, and thought it would be interesting to rewrite this tool on top of it, and use this opportunity to try out &lt;a rel=&quot;external&quot; href=&quot;https://zio.github.io/zio-query/&quot;&gt;&lt;strong&gt;ZIO Query&lt;/strong&gt;&lt;/a&gt; on a real-world example. In this post I&#39;m going to show step by step how to build an efficient and easily extensible query tool with the help of &lt;em&gt;ZIO&lt;/em&gt; libraries. The full source can be found &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/aws-query&quot;&gt;on GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-task&quot;&gt;The task&lt;/h2&gt;
&lt;p&gt;The CLI tool we build will get an arbitrary string as an input, and search for it in various AWS resources. Once it has a match, it has to traverse a graph of these resources and finally pretty-print all the gathered information to the console.&lt;/p&gt;
&lt;img src=&quot;/images/awsquery-1.webp&quot; alt=&quot;ZIO Query walking the ECS resource tree in a terminal: clusters, services, task definitions and instances&quot; width=&quot;780&quot; height=&quot;505&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;The provided input could mean any of the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;An &lt;strong&gt;EC2&lt;/strong&gt; &lt;em&gt;instance ID&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;ELB&lt;/strong&gt; (load balancer)&#39;s &lt;em&gt;name&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;ElasticBeanstalk&lt;/strong&gt; &lt;em&gt;environment name&lt;/em&gt; or &lt;em&gt;ID&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;ElasticBeanstalk&lt;/strong&gt; &lt;em&gt;application name&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;ASG&lt;/strong&gt; (auto-scaling group) &lt;em&gt;ID&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For the level of detail to be reported I copied the original tool. This means finding all the related resources in the above sets (plus among &lt;em&gt;launch configurations&lt;/em&gt;) but only include a single &lt;em&gt;EC2 instance&lt;/em&gt; in the output if it was explicitly queried. So for example if the search term matches an &lt;em&gt;ELB&lt;/em&gt; that belongs to an &lt;em&gt;ElasticBeanstalk environment&lt;/em&gt;, the report will contain the &lt;em&gt;EB app&lt;/em&gt; and all its other environments as well, but won&#39;t show individual instances. This choice does not affect the design and could be easily changed or extended with additional resource types.&lt;/p&gt;
&lt;h2 id=&quot;aws-client&quot;&gt;AWS client&lt;/h2&gt;
&lt;p&gt;For querying the above mentioned resources, we have to call four different AWS services. The &lt;code&gt;zio-aws&lt;/code&gt; project adds a streaming ZIO wrapper for &lt;em&gt;all&lt;/em&gt; the libraries in &lt;a rel=&quot;external&quot; href=&quot;https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/welcome.html&quot;&gt;AWS Java SDK v2&lt;/a&gt;, each published as separate artifact:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;libraryDependencies &lt;/span&gt;&lt;span&gt;++=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Seq&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;io.github.vigoo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; %%&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;zio-aws-autoscaling&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; %&lt;/span&gt;&lt;span&gt; zioAwsVersion,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;io.github.vigoo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; %%&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;zio-aws-ec2&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; %&lt;/span&gt;&lt;span&gt; zioAwsVersion,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;io.github.vigoo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; %%&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;zio-aws-elasticloadbalancing&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; %&lt;/span&gt;&lt;span&gt; zioAwsVersion,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;io.github.vigoo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; %%&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;zio-aws-elasticbeanstalk&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; %&lt;/span&gt;&lt;span&gt; zioAwsVersion&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;io.github.vigoo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; %%&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;zio-aws-netty&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; %&lt;/span&gt;&lt;span&gt; zioAwsVersion,    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In addition to loading the necessary client libraries, we also need one of the &lt;em&gt;http implementations&lt;/em&gt;, in this case I chose the default &lt;em&gt;Netty&lt;/em&gt;. Other possibilities are &lt;em&gt;akka-http&lt;/em&gt; and &lt;em&gt;http4s&lt;/em&gt;. If your application already uses one of these for other HTTP communications you may want to use them to share their configuration and pools.&lt;/p&gt;
&lt;p&gt;The client libraries have a &lt;code&gt;ZStream&lt;/code&gt; API for all the operations that either support streaming (like for example S3 download/upload) or pagination, and &lt;code&gt;ZIO&lt;/code&gt; wrapper for non-streaming simple operations. Instead of using the Java SDK&#39;s builders, the requests are described by &lt;em&gt;case classes&lt;/em&gt;, and the &lt;em&gt;result&lt;/em&gt; types have convenience accessors to handle the nullable results.&lt;/p&gt;
&lt;p&gt;Let&#39;s see some examples!&lt;/p&gt;
&lt;p&gt;We can get information about &lt;em&gt;EB applications&lt;/em&gt; with the &lt;em&gt;ElasticBeanstalk&lt;/em&gt; API&#39;s &lt;a rel=&quot;external&quot; href=&quot;https://docs.aws.amazon.com/elasticbeanstalk/latest/api/API_DescribeApplications.html&quot;&gt;&lt;code&gt;DescribeApplications&lt;/code&gt; operation&lt;/a&gt;. This is defined like the following in &lt;code&gt;zio-aws-elasticbeanstalk&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; describeApplications&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;request&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DescribeApplicationsRequest&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ElasticBeanstalk&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DescribeApplicationsResponse&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ReadOnly&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ApplicationName&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DescribeApplicationsRequest&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;applicationNames&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Iterable&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ApplicationName&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DescribeApplicationsResponse&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;applications&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Iterable&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ApplicationDescription&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DescribeApplicationsResponse&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ReadOnly&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; editable&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DescribeApplicationsResponse&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; applicationsValue&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ApplicationDescription&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ReadOnly&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; applications&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ApplicationDescription&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ReadOnly&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A few things to notice here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The client function requires the &lt;code&gt;ElasticBeanstalk&lt;/code&gt; module. We will see how to set up the dependencies in the &lt;em&gt;Putting all together&lt;/em&gt; section.&lt;/li&gt;
&lt;li&gt;The primitive types defined by the AWS schema are currently simple type aliases. In the future they will be probably replaced by &lt;a rel=&quot;external&quot; href=&quot;https://github.com/zio/zio-prelude&quot;&gt;zio-prelude&lt;/a&gt;&#39;s &lt;em&gt;newtypes&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Each wrapper type has a &lt;code&gt;ReadOnly&lt;/code&gt; trait and a &lt;em&gt;case class&lt;/em&gt;. The case classes are used as input, and the read-only interfaces as outputs. This way the result provided by the Java SDK can be accessed directly and it only has to be rewrapped in the case class if it is passed to another call as input.&lt;/li&gt;
&lt;li&gt;In many cases the AWS SDK describes fields as optional even if in normal circumstances it would never be &lt;code&gt;None&lt;/code&gt;. To make it more convenient to work with these, the &lt;code&gt;ReadOnly&lt;/code&gt; interface contains &lt;em&gt;accessor functions&lt;/em&gt; which fail with &lt;code&gt;FieldIsNone&lt;/code&gt; in case the field did not have any value. The pure optional values can be accessed with the &lt;code&gt;xxxValue&lt;/code&gt; variants. See &lt;code&gt;applications&lt;/code&gt; and &lt;code&gt;applicationsValue&lt;/code&gt; in the above example.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For operations support pagination, the wrapper functions return a stream. The actual first AWS call happens when the stream is first pulled. An example for this that we have to use in this application is the &lt;em&gt;EC2&lt;/em&gt; API&#39;s &lt;a rel=&quot;external&quot; href=&quot;https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeInstances.html&quot;&gt;&lt;code&gt;DescribeInstances&lt;/code&gt; operation&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; describeInstances&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;request&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DescribeInstancesRequest&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZStream&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Ec2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Reservation&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ReadOnly&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The pagination can be controlled by setting the &lt;code&gt;MaxResults&lt;/code&gt; property in &lt;code&gt;DescribeInstancesRequest&lt;/code&gt;. For the user of the &lt;code&gt;describeInstances&lt;/code&gt; function this is completely transparent, the returned stream will gather all the results, possibly by performing multiple AWS requests.&lt;/p&gt;
&lt;h2 id=&quot;queries&quot;&gt;Queries&lt;/h2&gt;
&lt;p&gt;We could implement the resource discovery directly using the low level AWS wrappers described above, using ZIO&#39;s tools to achieve concurrency. There are several things to consider though:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We don&#39;t know what resource we are looking for, so we should start multiple queries in parallel to find a match as soon as possible&lt;/li&gt;
&lt;li&gt;Some queries return additional data that could be reused later. For example it is not possible to search for an ELB by a instance ID contained by it; for that we have to query &lt;em&gt;all&lt;/em&gt; load balancers and check the members on client side.&lt;/li&gt;
&lt;li&gt;There are AWS operations that support querying multiple entities, for example by providing a list of IDs to look for&lt;/li&gt;
&lt;li&gt;We should minimize the number of calls to AWS, both for performance reasons, and to avoid getting rate limited&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We can achieve all this by expressing our AWS queries with a higher level abstraction, delegating the execution to a library called &lt;a rel=&quot;external&quot; href=&quot;https://zio.github.io/zio-query/&quot;&gt;ZIO Query&lt;/a&gt;. This library let us define composable &lt;em&gt;queries&lt;/em&gt; to arbitrary &lt;em&gt;data sources&lt;/em&gt;, and it automatically provides &lt;em&gt;pipelining&lt;/em&gt;, &lt;em&gt;batching&lt;/em&gt; and &lt;em&gt;caching&lt;/em&gt;. A perfect match for the problem we have to solve here.&lt;/p&gt;
&lt;p&gt;To be able to cache results that became available as a side effect of a query, we need a &lt;a rel=&quot;external&quot; href=&quot;https://github.com/zio/zio-query/pull/105&quot;&gt;recent improvement&lt;/a&gt; that is not published yet, so &lt;code&gt;aws-query&lt;/code&gt; currently uses a snapshot release of &lt;code&gt;zio-query&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;libraryDependencies &lt;/span&gt;&lt;span&gt;+=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;dev.zio&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; %%&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;zio-query&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;0.2.5+12-c41557f7-SNAPSHOT&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first step is to define custom &lt;em&gt;data sources&lt;/em&gt;. Data sources must implement a function &lt;code&gt;runAll&lt;/code&gt; with the following signature:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; runAll&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;requests&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;CompletedRequestMap&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here &lt;code&gt;A&lt;/code&gt; is the &lt;em&gt;request type&lt;/em&gt; specific to a given data source (extending &lt;code&gt;Request[E, A]&lt;/code&gt;, and the returned &lt;code&gt;CompletedRequestMap&lt;/code&gt; will store an &lt;code&gt;Either[E, A]&lt;/code&gt; result for each request. The two nested chunks model sequential and parallel execution: the requests in the inner chunks can be executed in parallel, while these batches contained by the outer chunk must be performed sequentially. In practice we won&#39;t implement this method but use &lt;code&gt;DataSource.Batched&lt;/code&gt; that is a simplified version that can perform requests in parallel but does not make further optimizations on the requests to be performed sequentially.&lt;/p&gt;
&lt;p&gt;What should belong to one data source?  It could be a single data source for all the AWS queries, or one per service, or one per resource type. The best choice in this case is to have one for each resource type, for the following reasons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;There are no opportunities to do any cross-resource-type caching. For example when we are querying EC2 instances, we won&#39;t fetch auto scaling groups as a side effect.&lt;/li&gt;
&lt;li&gt;If all requests are about the same data type, implementing the data source is much simpler&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&#39;s see a simple example. EC2 instances can be queried by &lt;em&gt;instance ID&lt;/em&gt; with the &lt;a rel=&quot;external&quot; href=&quot;https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeInstances.html&quot;&gt;&lt;code&gt;DescribeInstances&lt;/code&gt;&lt;/a&gt; operation, and it supports querying for multiple IDs in a single request. We first define a &lt;em&gt;request type&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; GetEc2Instance&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; InstanceId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Request&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Instance&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ReadOnly&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then the data source:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; ec2InstancesDataSource&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DataSource&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Logging&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Ec2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;GetEc2Instance&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  DataSource&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Batched&lt;/span&gt;&lt;span&gt;.make&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ec2&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;requests&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;GetEc2Instance&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    import&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AwsDataSource&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      result &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; ec2.describeInstances&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DescribeInstancesRequest&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;instanceIds &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;requests.map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_.id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .mapM&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_.instances&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .flatMap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;instances &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZStream&lt;/span&gt;&lt;span&gt;.fromIterable&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;instances&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .foldM&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;CompletedRequestMap&lt;/span&gt;&lt;span&gt;.empty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;resultMap, item&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;          for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            instanceId &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; item.instanceId&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span&gt; resultMap.insert&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;GetEc2Instance&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;instanceId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Right&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;item&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .recordFailures&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;DescribeInstances&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, requests&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here &lt;code&gt;requests&lt;/code&gt; holds a set of &lt;code&gt;GetEc2Instance&lt;/code&gt; requests to be performed in parallel. We can simply do this by taking all the &lt;em&gt;instance IDs&lt;/em&gt; from these requests and performing a single &lt;code&gt;describeInstances&lt;/code&gt; AWS call. The result, as I explained before, is a &lt;code&gt;ZStream&lt;/code&gt; of instances. We have to construct a &lt;code&gt;CompletedRequestMap&lt;/code&gt; holding one entry for each request in &lt;code&gt;requests&lt;/code&gt;. To do this we &lt;code&gt;foldM&lt;/code&gt; the stream, using the &lt;code&gt;instanceId&lt;/code&gt; accessor function to reconstruct the request value for each item in the result stream.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;.recordFailures&lt;/code&gt; function is a helper extension method defined in &lt;code&gt;AwsDataSource&lt;/code&gt;. It catches all errors and produces a &lt;code&gt;CompletedRequestMap&lt;/code&gt; where all requested items are recorded as failures:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; recordFailures&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;description&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;                      requests&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Iterable&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Request&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;CompletedRequestMap&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  f.catchAll &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; error &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    log.error&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;$&lt;/span&gt;&lt;span&gt;description&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; f&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; $&lt;/span&gt;&lt;span&gt;error&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; *&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      ZIO&lt;/span&gt;&lt;span&gt;.succeed &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        requests.foldLeft&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;CompletedRequestMap&lt;/span&gt;&lt;span&gt;.empty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;resultMap, req&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          resultMap.insert&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;req&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Left&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;error&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is necessary because the data source requires a function of type &lt;code&gt;Chunk[A] =&amp;gt; ZIO[R, Nothing, CompletedRequestMap]&lt;/code&gt; that cannot fail.&lt;/p&gt;
&lt;p&gt;With the data source defined, we can define primitive &lt;em&gt;queries&lt;/em&gt; on it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; getEc2Instance&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; InstanceId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZQuery&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Logging&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Ec2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Instance&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ReadOnly&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  ZQuery&lt;/span&gt;&lt;span&gt;.fromRequest&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;GetEc2Instance&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ec2InstancesDataSource&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A more complex example is &lt;code&gt;ebEnvDataSource&lt;/code&gt;, the data source of &lt;em&gt;ElasticBeanstalk environments&lt;/em&gt;. For this resource, we have different request types:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;sealed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; EbEnvRequest&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Request&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; GetEnvironmentByName&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; EnvironmentName&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; EbEnvRequest&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;EnvironmentDescription&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ReadOnly&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; GetEnvironmentById&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; EnvironmentId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; EbEnvRequest&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;EnvironmentDescription&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ReadOnly&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; GetEnvironmentByApplicationName&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ApplicationName&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; EbEnvRequest&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;EnvironmentDescription&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ReadOnly&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the data source implementation we get a &lt;code&gt;Chunk&lt;/code&gt; of &lt;code&gt;EbEnvRequest&lt;/code&gt; to be performed in parallel. We start it by separating it per request type:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; byName&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; requests.collect &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; GetEnvironmentByName&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; name &lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; byId&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; requests.collect &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; GetEnvironmentById&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; id &lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; byAppName&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; requests.collect &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; GetEnvironmentByApplicationName&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; name &lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then for each of these collections, if not empty, we can perform a &lt;code&gt;describeEnvironments&lt;/code&gt; AWS call and then fold the result stream to create partial &lt;code&gt;CompletedRequestMap&lt;/code&gt; values. What is interesting here is that if we already queried an environment by either name or id or it&#39;s application name, we already know both its identifier and name, so we can store additional items in &lt;code&gt;CompletedRequestMap&lt;/code&gt; that will be cached and reused in future queries. For example this is how the query by-id gets processed:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;resultMap &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; elasticbeanstalk&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .describeEnvironments&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DescribeEnvironmentsRequest&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;environmentIds &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;byId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .foldM&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;initialResultMap&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;resultMap, item&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      name &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; item.environmentName&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      id &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; item.environmentId&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span&gt; resultMap&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .insert&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;GetEnvironmentById&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Right&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;item&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .insert&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;GetEnvironmentByName&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Right&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;item&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .recordFailures&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;DescribeEnvironmentRequest(id)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, byId.map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;GetEnvironmentById&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For all three request types we describe the computation to create a partial &lt;code&gt;CompletedRequestMap&lt;/code&gt; for them. Then we can implement the data source by executing these (maximum) three queries in parallel and combining the results:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;byNameResultMap&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .zipWithPar&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;byIdResultMap&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_ &lt;/span&gt;&lt;span&gt;++&lt;/span&gt;&lt;span&gt; _&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .zipWithPar&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;byAppNameResultMap&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_ &lt;/span&gt;&lt;span&gt;++&lt;/span&gt;&lt;span&gt; _&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are some cases where being able to query &lt;em&gt;all&lt;/em&gt; instances of a given resource is also a requirement. An example is &lt;em&gt;load balancers&lt;/em&gt;, where the only way to find if an ELB contains a given &lt;em&gt;EC2 instance&lt;/em&gt; is to query &lt;em&gt;all&lt;/em&gt; ELBs and check their members. There are a few more cases that require a very similar implementation, so it makes sense extracting it to a common place. We define an &lt;code&gt;AllOrPerItem&lt;/code&gt; trait that defines the specifics per use case:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AllOrPerItem&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Req&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Item&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; isGetAll&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;request&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Req&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Boolean&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; isPerItem&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;request&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Req&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Boolean&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; allReq&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Req&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; itemToReq&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;item&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Item&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Req&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; getAll&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZStream&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Item&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; getSome&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;reqs&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Set&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Req&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZStream&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Item&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; processAdditionalRequests&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;requests&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Req&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;                                partialResult&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CompletedRequestMap&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;CompletedRequestMap&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    ZIO&lt;/span&gt;&lt;span&gt;.succeed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;partialResult&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By implementing these one-liners the actual data source implementation can be a shared code defined in &lt;code&gt;AllOrPerItem.make&lt;/code&gt;. It&#39;s very similar to the examples already seen. If any of the requests is the &lt;em&gt;get all request&lt;/em&gt;, that&#39;s the only thing to be performed, and all the result items will be cached. Otherwise a single batched request is made.&lt;/p&gt;
&lt;p&gt;These primitive &lt;code&gt;ZQuery&lt;/code&gt;s then can be composed to more complex queries. For example the following code:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  instance &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; ec2query.getEc2Instance&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;instanceId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  imageId &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZQuery&lt;/span&gt;&lt;span&gt;.fromEffect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;instance.imageId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  imgElb &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;ec2query.getImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;imageId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;lt;&amp;amp;&amp;gt;&lt;/span&gt;&lt;span&gt; elbquery.loadBalancerOf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;instanceId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;/span&gt;&lt;span&gt;image, elb&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; imgElb&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  elbReport &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; optionally&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;elb&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;getElbReport&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  result &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span&gt; result    &lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is part of the definition of a query of type &lt;code&gt;ZQuery[QueryEnv, AwsError, LinkedReport[Ec2InstanceKey, Ec2InstanceReport]]&lt;/code&gt;. We will talk about &lt;code&gt;QueryEnv&lt;/code&gt; and &lt;code&gt;LinkedReport&lt;/code&gt; later, for now it&#39;s enough to understand that this is a more complex query that provides an &lt;em&gt;EC2 instance report&lt;/em&gt;; the data type that will be used to render the human-readable output. The query first gets an EC2 instance by &lt;em&gt;instance ID&lt;/em&gt;. Then with &lt;code&gt;ZQuery.fromEffect&lt;/code&gt; we lift a &lt;code&gt;ZIO&lt;/code&gt; effect to the query. In this case this is a &lt;code&gt;zio-aws&lt;/code&gt; &lt;em&gt;accessor function&lt;/em&gt; that fails if &lt;code&gt;imageId&lt;/code&gt; is &lt;code&gt;None&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;By this we express that we &lt;em&gt;expect&lt;/em&gt; that &lt;code&gt;imageId&lt;/code&gt; is always specified, and if not, we fail the &lt;em&gt;whole query&lt;/em&gt;. Then we use &lt;code&gt;&amp;lt;&amp;amp;&amp;gt;&lt;/code&gt; (it&#39;s alias is &lt;code&gt;zipPar&lt;/code&gt;) to perform two queries &lt;strong&gt;in parallel&lt;/strong&gt;: getting an EC2 image and finding the load balancer containing the instance. Once both queries are finished, we optionally generate a &lt;em&gt;load balancer report&lt;/em&gt; (if we have found an ELB link) and then we construct the result.&lt;/p&gt;
&lt;p&gt;Here &lt;code&gt;optionally&lt;/code&gt; is a simple helper function that makes our query more readable. It could have been written as &lt;code&gt;elb.fold(ZQuery.none)(getElbReport)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Another useful combinator on &lt;code&gt;ZQuery&lt;/code&gt; is &lt;code&gt;collectAllPar&lt;/code&gt; that runs a subquery on each item of a collection in parallel:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  elbNames &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZQuery&lt;/span&gt;&lt;span&gt;.fromEffect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;asg.loadBalancerNames&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  result &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZQuery&lt;/span&gt;&lt;span&gt;.collectAllPar&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;elbNames.map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;name &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; elbquery.getLoadBalancer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;gt;&amp;gt;=&lt;/span&gt;&lt;span&gt; getElbReport&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As I mentioned earlier, we have no way to know what resource we are looking for (in fact we could for example detect EC2 &lt;em&gt;instance IDs&lt;/em&gt; by a pattern but let&#39;s ignore that for now). So on top level we simply start _all the possible queries &lt;strong&gt;at once&lt;/strong&gt; and let print all the non-failing ones:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  renderers &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZQuery&lt;/span&gt;&lt;span&gt;.collectAllPar&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;possibleQueries&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.run&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;.foreach_&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;renderers.flatten&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;identity&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span&gt; ()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Where &lt;code&gt;possibleQueries&lt;/code&gt; is a where we list all the queries we want to support, tied to the &lt;em&gt;renderer&lt;/em&gt; to show it on the console.&lt;/p&gt;
&lt;h2 id=&quot;report-cache&quot;&gt;Report cache&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;ZIO Query&lt;/em&gt; solves caching and optimizes the requests on the AWS resource level, but we still have a problem. The queries form a cyclic graph. For example an &lt;em&gt;EC2 instance&lt;/em&gt; holds a link to its &lt;em&gt;load balancer&lt;/em&gt;, that holds a link to the &lt;em&gt;EB environment&lt;/em&gt; it is defined in. The environment refers back to the ELB, and it also links to the &lt;em&gt;EB app&lt;/em&gt; and the application has again links to all the &lt;em&gt;environments&lt;/em&gt; it contains.&lt;/p&gt;
&lt;p&gt;We want to collect all these resources exactly once, and there is a chance that parallel queries reach to the same resource. To solve this we can add an extra &lt;em&gt;caching layer&lt;/em&gt; on top of &lt;em&gt;ZIO Query&lt;/em&gt;. Let&#39;s define this caching layer as a ZIO &lt;em&gt;module&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ReportCache&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Service&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; storeIfNew&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Report&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;reportKey&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ReportKey&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;                                query&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZQuery&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZQuery&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Boolean&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; retrieve&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Report&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;key&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ReportKey&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;storeIfNew&lt;/code&gt; function is a &lt;em&gt;query&lt;/em&gt;, to be used in high level queries to shortcut cycles in case a given report is already stored in the cache. We can define a helper function &lt;code&gt;cached&lt;/code&gt; like the following:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;protected&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; cached&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ReportCache&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Logging&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;B&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Report&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;K&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ReportKey&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; A&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;keyFn&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; A&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;K&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;query&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; K&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZQuery&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;B&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZQuery&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LinkedReport&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;K&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;B&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    key &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZQuery&lt;/span&gt;&lt;span&gt;.fromEffect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;keyFn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;input&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    env &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZQuery&lt;/span&gt;&lt;span&gt;.environment&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; storeIfNew&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      key,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      query&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;key&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.provide&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;env &lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;provided environment&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LinkedReport&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;K&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;B&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;key&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we can use it in queries like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; getEbAppReport&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ApplicationName&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZQuery&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;QueryEnv&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LinkedReport&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;EbAppKey&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;EbAppReport&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  cached&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;name &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;.succeed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;EbAppKey&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;key&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; EbAppKey&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;     //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&#39;s see in detail how this works!&lt;/p&gt;
&lt;p&gt;First of all, we define the following types:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LinkedReport&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;K&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ReportKey&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Report&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;key&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; K&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;sealed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ReportKey&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Ec2InstanceKey&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;instanceId&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; InstanceId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ReportKey&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;sealed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Report&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Ec2InstanceReport&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;instanceId&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; ec2.model.primitives.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;InstanceId&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;                                   //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;                                   elb&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LinkedReport&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ElbKey&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ElbReport&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                  )&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Report&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In &lt;code&gt;cached&lt;/code&gt;, we provide a &lt;code&gt;keyFn&lt;/code&gt; that is an effectful function to extract the &lt;code&gt;ReportKey&lt;/code&gt; from the arbitrary input that can be the key itself, or an already fetched resource. Then we call the &lt;code&gt;ReportCache&lt;/code&gt; module&#39;s &lt;code&gt;storeIfNew&lt;/code&gt; query and return a &lt;code&gt;LinkedReport&lt;/code&gt;. A &lt;em&gt;linked report&lt;/em&gt; is just a wrapper around a report key, it is the type to be used in &lt;code&gt;Report&lt;/code&gt; types to refer to each other. We store the cyclic resource graph by using these report keys and the cache&#39;s &lt;code&gt;retrieve&lt;/code&gt; function to resolve the references on demand.&lt;/p&gt;
&lt;p&gt;One thing to notice is the &lt;code&gt;.provide&lt;/code&gt; in the code of &lt;code&gt;cached&lt;/code&gt;. The report cache does not know about the environments needed for the queries it caches the results of; the &lt;code&gt;query&lt;/code&gt; parameter of &lt;code&gt;storeIfNew&lt;/code&gt; has the type &lt;code&gt;ZQuery[Any, AwsError, A]&lt;/code&gt;. For this reason &lt;code&gt;cached&lt;/code&gt; eliminates the environment of its inner query by getting it and calling &lt;code&gt;.provide(env)&lt;/code&gt; before passing it to the cache.&lt;/p&gt;
&lt;p&gt;The report cache itself can be implemented with &lt;a rel=&quot;external&quot; href=&quot;https://zio.dev/docs/datatypes/datatypes_stm&quot;&gt;&lt;em&gt;STM&lt;/em&gt;&lt;/a&gt;. First we create a &lt;a rel=&quot;external&quot; href=&quot;https://zio.dev/docs/datatypes/datatypes_tmap&quot;&gt;&lt;code&gt;TMap&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cache &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TMap&lt;/span&gt;&lt;span&gt;.empty&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ReportKey&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Promise&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Report&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.commit&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We want to store the fact that a query &lt;em&gt;has been started&lt;/em&gt; for a given report key. This can be modelled with a &lt;code&gt;Promise&lt;/code&gt; that eventually gets a &lt;code&gt;Report&lt;/code&gt; value. With this &lt;code&gt;TMap&lt;/code&gt; structure, the &lt;code&gt;storeIfNew&lt;/code&gt; function can be defined as:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; storeIfNew&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Report&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;reportKey&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ReportKey&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;                                     query&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZQuery&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZQuery&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Boolean&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  ZQuery&lt;/span&gt;&lt;span&gt;.fromEffect &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      promise &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Promise&lt;/span&gt;&lt;span&gt;.make&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Report&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      finalQuery &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; cache.get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;reportKey&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.flatMap &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;report&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;          //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; replacing the query with the cached value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;          ZSTM&lt;/span&gt;&lt;span&gt;.succeed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ZQuery&lt;/span&gt;&lt;span&gt;.succeed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;false&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;          //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; replacing the query with the cached value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          cache.put&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;reportKey, promise&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.map &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; _ &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            query.foldM&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;              failure &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZQuery&lt;/span&gt;&lt;span&gt;.fromEffect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;promise.fail&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;failure&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; *&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZQuery&lt;/span&gt;&lt;span&gt;.fail&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;failure&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;              success &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZQuery&lt;/span&gt;&lt;span&gt;.fromEffect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;promise.succeed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;success&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;span&gt;.commit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span&gt; finalQuery&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span&gt;.flatMap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;identity&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This may seem simple but actually we are combining three different layers of abstraction here!&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The whole thing is a &lt;em&gt;query&lt;/em&gt;. But we first run a &lt;em&gt;ZIO effect&lt;/em&gt; that &lt;strong&gt;produces&lt;/strong&gt; a query, and then execute that result query (in &lt;code&gt;.flatMap(identity)&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;In the effect we create a promise that might be used or not, depending on the outcome of the transaction. Then we do &lt;code&gt;cache.get&lt;/code&gt; which is an &lt;em&gt;STM transaction&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;In the transaction we produce a &lt;code&gt;ZQuery&lt;/code&gt; value that is either returning a simple &lt;code&gt;false&lt;/code&gt; value if the report was already cached, or we store the already created promise in the map and return the query that constructs the report as the &lt;em&gt;result&lt;/em&gt; of the transaction.&lt;/li&gt;
&lt;li&gt;As it is an &lt;em&gt;STM transaction&lt;/em&gt; it may be retried multiple times but eventually it returns with a query that is either a NOP or calculates the &lt;em&gt;report&lt;/em&gt; &lt;strong&gt;and&lt;/strong&gt; sets the promise in the end.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The other function of &lt;code&gt;ReportCache&lt;/code&gt;, &lt;code&gt;retrieve&lt;/code&gt; will be used when traversing the gathered &lt;em&gt;reports&lt;/em&gt; to follow the &lt;code&gt;LinkedReport&lt;/code&gt; links. It is simply a combination of getting an item from the &lt;code&gt;TMap&lt;/code&gt; and then waiting for the stored promise.&lt;/p&gt;
&lt;h2 id=&quot;throttling&quot;&gt;Throttling&lt;/h2&gt;
&lt;p&gt;The original implementation of this tool did not control the amount and rate of AWS requests in any way, and a few years ago API rate limits made it somewhat unusable. As I explained &lt;a href=&quot;https://blog.vigoo.dev/posts/aws-rate-limits-prezidig/&quot;&gt;in a previous post&lt;/a&gt;, I solved it by centralizing the calls to AWS then adding &lt;em&gt;circuit breaking and retry&lt;/em&gt; to handle the &lt;em&gt;throttling errors&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;In this new implementation &lt;em&gt;ZIO Query&lt;/em&gt; &#39;s batching feature already reduces the load but AWS has a global rate limit that can be reached any time, regardless of the actual request rate provided by this application. So how could we handle this with &lt;code&gt;zio-aws&lt;/code&gt; and ZIO Query?&lt;/p&gt;
&lt;p&gt;There is useful ZIO library called &lt;a rel=&quot;external&quot; href=&quot;https://www.vroste.nl/rezilience/&quot;&gt;rezilience&lt;/a&gt; that defines utilities to express circuit breaking, retries, rate limiting and other similar policies. With this library we can create a policy that detects &lt;code&gt;AwsError&lt;/code&gt;s representing throttling failures:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; throttlingPolicy&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZManaged&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Random&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Clock&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Logging&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Policy&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    cb &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CircuitBreaker&lt;/span&gt;&lt;span&gt;.make&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      trippingStrategy &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TrippingStrategy&lt;/span&gt;&lt;span&gt;.failureCount&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      resetPolicy &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Retry&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Schedules&lt;/span&gt;&lt;span&gt;.exponentialBackoff&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;min &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span&gt;.second, max &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span&gt;.minute&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      isFailure &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; GenericAwsError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;error&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AwsServiceException&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; if&lt;/span&gt;&lt;span&gt; error.isThrottlingException &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    retry &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Retry&lt;/span&gt;&lt;span&gt;.make&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;min &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span&gt;.second, max &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span&gt;.minute&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    retryComposable &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; retry.widen&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PolicyError&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Policy&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WrappedError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;e&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; e &lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span&gt; cb.toPolicy compose retryComposable.toPolicy&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will open a circuit breaker in case of throttling errors, and retry the operation with exponential back-off.&lt;/p&gt;
&lt;p&gt;These policies can be applied to &lt;code&gt;ZIO&lt;/code&gt; effects. What we really need is to apply a policy like this to &lt;em&gt;all&lt;/em&gt; AWS call. It should be the actual call to the underlying &lt;em&gt;AWS Java SDK&lt;/em&gt;, not on the &lt;code&gt;zio-aws&lt;/code&gt; wrapper level, because for example a streaming API function may produce multiple AWS requests.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;zio-aws&lt;/code&gt; library supports applying &lt;code&gt;AwsCallAspect&lt;/code&gt;s on the &lt;em&gt;AWS service client layers&lt;/em&gt; to modify the underlying SDK calls. This is exactly what we need to apply the throttling policy to all calls! What&#39;s even better, by creating a single &lt;code&gt;throttlingPolicy&lt;/code&gt; and applying it to all the service layers (&lt;code&gt;ec2&lt;/code&gt;, &lt;code&gt;elasticloadbalancing&lt;/code&gt;, &lt;code&gt;elasticbeanstalk&lt;/code&gt; and &lt;code&gt;autoscaling&lt;/code&gt;) they will share a common circuit breaker that matches the situation perfectly as the AWS API rate limiting is applied to globally to all services.&lt;/p&gt;
&lt;p&gt;An AWS call aspect has the following form:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; throttling&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AwsCallAspect&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; apply&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Described&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, aspects.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Described&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    policy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;f&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.mapError &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Policy&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WrappedError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;e&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; e&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Policy&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;BulkheadRejection&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AwsError&lt;/span&gt;&lt;span&gt;.fromThrowable&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RuntimeException&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Policy&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;CircuitBreakerOpen&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AwsError&lt;/span&gt;&lt;span&gt;.fromThrowable&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RuntimeException&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;W&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Another simple example could be logging all AWS requests:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; callLogging&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AwsCallAspect&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Logging&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AwsCallAspect&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Logging&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;   override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; apply&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R1&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Logging&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Described&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AwsError&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Described&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     f.flatMap &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span&gt; r&lt;/span&gt;&lt;span&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Described&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_, description&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       log.info&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;${&lt;/span&gt;&lt;span&gt;description.service&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;${&lt;/span&gt;&lt;span&gt;description.operation&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.as&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;r&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These aspects can be applied to a &lt;code&gt;zio-aws&lt;/code&gt; &lt;code&gt;ZLayer&lt;/code&gt; directly, such as:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ec2.live &lt;/span&gt;&lt;span&gt;@@&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;throttling &lt;/span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span&gt; callLogging&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;rendering&quot;&gt;Rendering&lt;/h2&gt;
&lt;p&gt;With the queries and report cache ready the last missing building block is &lt;em&gt;rendering&lt;/em&gt; the gathered reports. We implement it in its own ZIO module with the following interface:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Rendering&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Service&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; renderEc2Instance&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;report&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LinkedReport&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Ec2InstanceKey&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Ec2InstanceReport&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; renderElb&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;report&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LinkedReport&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ElbKey&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ElbReport&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;context&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; renderAsg&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;report&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LinkedReport&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AsgKey&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AsgReport&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; renderEbEnv&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;report&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LinkedReport&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;EbEnvKey&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;EbEnvReport&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; renderEbApp&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;report&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LinkedReport&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;EbAppKey&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;EbAppReport&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The live implementation of course needs access to &lt;code&gt;ReportCache&lt;/code&gt; and writes the report out to &lt;code&gt;Console&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; live&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZLayer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ReportCache&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Rendering&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We need two main things to implement report rendering:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A way to pretty-print reports to the console&lt;/li&gt;
&lt;li&gt;We have to track which report was already rendered to be able to traverse the cyclic result graph&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To track the already printed reports we can simply create a &lt;code&gt;Ref&lt;/code&gt; holding a set of visited &lt;code&gt;ReportKey&lt;/code&gt;s:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; State&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;alreadyVisited&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Set&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ReportKey&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;alreadyVisited &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Ref&lt;/span&gt;&lt;span&gt;.make&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;State&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Set&lt;/span&gt;&lt;span&gt;.empty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For pretty printing the reports there are several possibilities. Eventually we want to call &lt;code&gt;console.putStr&lt;/code&gt; to write to the console. The original implementation of this tool used a string templating engine to define the output. Instead of doing that we can write a pretty-printing DSL to define our output in Scala. Take a look at the following example:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ifNotVisitedYet&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;report&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; env &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  sectionHeader&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Beanstalk/Env&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;lt;-&amp;gt;&lt;/span&gt;&lt;span&gt; highlighted&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;env.name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;lt;-&amp;gt;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    details&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;env.id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;lt;-&amp;gt;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    normal&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; ${&lt;/span&gt;&lt;span&gt;env.appName&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; \\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  indented &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    keyword&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;AWS Console&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;lt;:&amp;gt;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      link&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;${&lt;/span&gt;&lt;span&gt;env.region&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;${&lt;/span&gt;&lt;span&gt;env.appName&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;${&lt;/span&gt;&lt;span&gt;env.id&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; \\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    keyword&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Health&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;lt;:&amp;gt;&lt;/span&gt;&lt;span&gt; highlighted&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;env.health.toString&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; \\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    keyword&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Currently running version&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;lt;:&amp;gt;&lt;/span&gt;&lt;span&gt; normal&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;env.version&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; \\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    normal&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;${&lt;/span&gt;&lt;span&gt;env.asgs.size&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;G&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; ${&lt;/span&gt;&lt;span&gt;env.instanceCount&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; ${&lt;/span&gt;&lt;span&gt;env.elbs.size&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; E&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; \\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    env.elbs.foreach_&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;elb&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;None&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; \\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    env.asgs.foreach_&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;asg&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; \\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ebApp&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;env.app&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can see here a couple of functions and operators, all created to the specific task of printing &lt;em&gt;AWS resource reports&lt;/em&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;ifNotYetVisitedYet&lt;/code&gt; must somehow interact with the &lt;code&gt;Ref&lt;/code&gt; we defined above&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;-&amp;gt;&lt;/code&gt; concatenates two texts with a space&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;:&amp;gt;&lt;/code&gt; concatenates two texts with a colon and a space&lt;/li&gt;
&lt;li&gt;&lt;code&gt;\\&lt;/code&gt; concatenates two texts with a newline&lt;/li&gt;
&lt;li&gt;&lt;code&gt;keyword&lt;/code&gt;, &lt;code&gt;link&lt;/code&gt;, &lt;code&gt;normal&lt;/code&gt;, &lt;code&gt;highlighted&lt;/code&gt; etc. add styling to the given text&lt;/li&gt;
&lt;li&gt;&lt;code&gt;foreach_&lt;/code&gt; is coming from &lt;code&gt;zio-prelude&lt;/code&gt;-s &lt;code&gt;Traversable&lt;/code&gt;. We will see why is it used soon.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We could define these styling functions as &lt;code&gt;ZIO&lt;/code&gt; effects and the helper operators as general extension methods on &lt;code&gt;ZIO&lt;/code&gt;. Then we could store required state (for example for indentation) in a &lt;code&gt;Ref&lt;/code&gt; for example. This works but we can do better. By defining our own monadic data type &lt;code&gt;Print[A]&lt;/code&gt; we get the following advantages:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It is more type safe. The pretty printing operators will be only applicable to pretty printing functions, not to arbitrary ZIO effects&lt;/li&gt;
&lt;li&gt;Pretty printing state gets completely hidden from the pretty printing definitions&lt;/li&gt;
&lt;li&gt;We can easily do some optimizations such as collapsing multiple newlines into one, which makes rendering optional lines more convenient&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So let&#39;s define a data type to represent pretty printing:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;sealed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintPure&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;a&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; A&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintS&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;s&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintModified&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;s&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;modifiers&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintNL&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintIndented&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;p&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintFlatMap&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;B&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;a&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; A&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;B&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;B&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintEffect&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;PrintPure&lt;/code&gt; and &lt;code&gt;PrintFlatMap&lt;/code&gt; can be used to implement &lt;code&gt;zio-prelude&lt;/code&gt;s type classes:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; print&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Covariant&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Print&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IdentityFlatten&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Print&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IdentityBoth&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Print&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; map&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;B&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; A&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; B&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;B&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fa &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintFlatMap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;fa, &lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;a&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; A&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintPure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;f&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; any&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    PrintPure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; flatten&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;ffa&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    PrintFlatMap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ffa, &lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;fa&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; fa&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; both&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;B&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;fa&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;fb&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;B&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;B&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    PrintFlatMap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;fa, &lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;a&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; A&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;b&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; B&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;a, b&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;fb&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What are these type classes providing to us?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Covariant&lt;/code&gt; basically gives us &lt;code&gt;map&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;IdentityFlatten&lt;/code&gt; means that the data type can be &quot;flattened&quot; associatively and has an identity element. This gives us &lt;code&gt;flatten&lt;/code&gt; and &lt;code&gt;flatMap&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;IdentityBoth&lt;/code&gt; means we have an associative binary operator to combine two values. This enables syntax like &lt;code&gt;&amp;lt;*&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Having this we can define primitive pretty printing operators like:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; normal&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;text&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintS&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;text&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; space&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintS&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintOps&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;-&amp;gt;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;B&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;next&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;B&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;B&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    self &lt;/span&gt;&lt;span&gt;*&amp;gt;&lt;/span&gt;&lt;span&gt; space &lt;/span&gt;&lt;span&gt;*&amp;gt;&lt;/span&gt;&lt;span&gt; next&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we can use the syntax provided by &lt;code&gt;zio-prelude&lt;/code&gt; to compose these pretty printer values. The only thing remaining is to provide a transformation of &lt;code&gt;Print[A]&lt;/code&gt; to &lt;code&gt;UIO[A]&lt;/code&gt;. This is where we can hide the pretty printer state and can handle special rules like collapsing newlines:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrettyConsole&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  protected&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; console&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Console&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Service&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrettyState&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;indentation&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;afterNL&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Boolean&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; printFlatMap&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;B&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;a&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; A&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;B&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;state&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrettyState&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;B&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PrettyState&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      r1 &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; runImpl&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;a, state&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      r2 &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; runImpl&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;f&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;r1._1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, r1._2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span&gt; r2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; runImpl&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;p&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;state&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrettyState&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PrettyState&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    p &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;match&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintPure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;.succeed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;a, state&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintS&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;.when&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;state.afterNL&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;console.putStr&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;state.indentation&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; *&amp;gt;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        console.putStr&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.as&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;, state.copy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;afterNL &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; false&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintModified&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;s, modifiers&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;.when&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;state.afterNL&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;console.putStr&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;state.indentation&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; *&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        console.putStr&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;${&lt;/span&gt;&lt;span&gt;modifiers&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;$&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;$&lt;/span&gt;&lt;span&gt;RESET&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.as&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;, state.copy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;afterNL &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; false&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintNL&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;state.afterNL&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;.succeed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;, state&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        else&lt;/span&gt;&lt;span&gt; console.putStrLn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.as&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;, state.copy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;afterNL &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintIndented&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;f&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; runImpl&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;f, state.copy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;indentation &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; state.indentation &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.map &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;a, s&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          (&lt;/span&gt;&lt;span&gt;a, s.copy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;indentation &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; state.indentation&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintFlatMap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;a, f&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; printFlatMap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;a, f, state&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PrintEffect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;f&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; f.map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_, state&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;p&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Print&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; runImpl&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;p, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PrettyState&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, afterNL &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; false&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_._1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A couple of things to notice here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;PrettyState&lt;/code&gt; holds the indentation and a flag that is true when the last print was a &lt;em&gt;new line&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;runImpl&lt;/code&gt; gets the state as input and has the capability to modify it, by returning the modified state together with the computation&#39;s result&lt;/li&gt;
&lt;li&gt;there is a &lt;code&gt;PrintEffect&lt;/code&gt; constructor that allows lifting arbitrary &lt;code&gt;ZIO&lt;/code&gt; effects to the pretty printer. This is needed for interacting with the &lt;code&gt;Ref&lt;/code&gt; that holds the record of already printed reports.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;putting-all-together&quot;&gt;Putting all together&lt;/h2&gt;
&lt;p&gt;Putting all this together means getting command line arguments, setting up the AWS client libraries, the report cache and the rendering modules and running the top level queries.&lt;/p&gt;
&lt;p&gt;To parse the command line arguments we can use my &lt;a rel=&quot;external&quot; href=&quot;https://vigoo.github.io/clipp/docs/&quot;&gt;clipp library&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Parameters&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;verbose&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Boolean&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;                            searchInput&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;                            region&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; paramSpec&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; metadata&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;aws-query&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;search for AWS infrastructure resources&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  verbose &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; flag&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Verbose logging&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;verbose&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  searchInput &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; parameter&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Search input&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;NAME_OR_ID&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  region &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; optional &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; namedParameter&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;AWS region&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;REGION&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;region&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Parameters&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;verbose, searchInput, region.getOrElse&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;us-east-1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; params&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; clipp.zioapi.config.fromArgsWithUsageInfo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;args, paramSpec&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;verbose&lt;/code&gt; flag is used to set up logging. We use &lt;a rel=&quot;external&quot; href=&quot;https://zio.github.io/zio-logging/&quot;&gt;zio-logging&lt;/a&gt; with SLF4j support (to be able to see logs from the underlying AWS Java SDK) with lo4j2 backend. In order to control the log level by the command line &lt;code&gt;verbose&lt;/code&gt; flag, instead of the usual XML-based configuration for log4j2 we define a ZIO &lt;em&gt;layer&lt;/em&gt; that&#39;s only purpose is to perform the configuration programmatically:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; log4j2Configuration&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZLayer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Has&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ClippConfig&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Service&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Parameters&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Throwable&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Has&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Log4jConfiguration&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  ZLayer&lt;/span&gt;&lt;span&gt;.fromServiceM&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ClippConfig&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Service&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Parameters&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Throwable&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Log4jConfiguration&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; params &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    ZIO&lt;/span&gt;&lt;span&gt;.effect &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; builder&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ConfigurationBuilderFactory&lt;/span&gt;&lt;span&gt;.newConfigurationBuilder&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;       &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;      //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      Configurator&lt;/span&gt;&lt;span&gt;.initialize&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;builder.build&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      Log4jConfiguration&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This way the root logger&#39;s level can depend on the &lt;code&gt;Parameters&lt;/code&gt; parsed by &lt;code&gt;clipp&lt;/code&gt;. Composing this layer with &lt;code&gt;zio-logger&lt;/code&gt;s &lt;code&gt;Slf4jLogger&lt;/code&gt; gives us a working &lt;code&gt;Logging&lt;/code&gt; layer:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; logging&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; log4j2Configuration &lt;/span&gt;&lt;span&gt;&amp;gt;+&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Slf4jLogger&lt;/span&gt;&lt;span&gt;.make &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;_, message&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; message &lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By bootstrapping the parameters and the logging we can run our main application like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  result &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; awsQuery&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .provideCustomLayer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;params &lt;/span&gt;&lt;span&gt;&amp;gt;+&amp;gt;&lt;/span&gt;&lt;span&gt; logging&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .catchAll &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; _ &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;.succeed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ExitCode&lt;/span&gt;&lt;span&gt;.failure&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;.effect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LogManager&lt;/span&gt;&lt;span&gt;.shutdown&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.orDie&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;clipp&lt;/code&gt; parser will print detailed usage info in case it fails, and other runtime errors are logged, so we can simply catch all errors and exit with a failure on top level.&lt;/p&gt;
&lt;p&gt;In &lt;code&gt;awsQuery&lt;/code&gt; we create all the other layers necessary for running the queries. First we need to create the &lt;em&gt;throttling policy&lt;/em&gt; that is used by all the AWS service clients as I explained above:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; awsQuery&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Random&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Clock&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Console&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Logging&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ClippConfig&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Parameters&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ExitCode&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    throttlingPolicy.use &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; policy &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt;      &lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;zio-aws&lt;/code&gt; library uses &lt;a rel=&quot;external&quot; href=&quot;https://zio.github.io/zio-config/&quot;&gt;ZIO Config&lt;/a&gt; for configuration. This means we need a &lt;code&gt;ZConfig[CommonAwsConfig]&lt;/code&gt; to construct the &lt;code&gt;AwsConfig&lt;/code&gt; layer:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; commonConfig&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZLayer&lt;/span&gt;&lt;span&gt;.succeed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;CommonAwsConfig&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  region &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Region&lt;/span&gt;&lt;span&gt;.of&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;params.region&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  credentialsProvider &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DefaultCredentialsProvider&lt;/span&gt;&lt;span&gt;.create&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  endpointOverride &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  commonClientConfig &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; awsCore&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;netty.default &lt;/span&gt;&lt;span&gt;++&lt;/span&gt;&lt;span&gt; commonConfig&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span&gt; core.config.configured&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;AwsConfig&lt;/code&gt; layer combines the configuration with a selected HTTP backend. In our case this is the &lt;em&gt;Netty&lt;/em&gt; backend, using its default configuration.&lt;/p&gt;
&lt;p&gt;Then we define the per-service client layers, applying the throttling and call logging &lt;em&gt;aspects&lt;/em&gt; as I described before:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; awsClients&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  ec2.live &lt;/span&gt;&lt;span&gt;@@&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;throttling &lt;/span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span&gt; callLogging&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; ++&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  elasticloadbalancing.live &lt;/span&gt;&lt;span&gt;@@&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;throttling &lt;/span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span&gt; callLogging&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; ++&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  elasticbeanstalk.live &lt;/span&gt;&lt;span&gt;@@&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;throttling &lt;/span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span&gt; callLogging&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; ++&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  autoscaling.live &lt;/span&gt;&lt;span&gt;@@&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;throttling &lt;/span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span&gt; callLogging&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To produce the final layer, we feed the logging and the &lt;code&gt;AwsConfig&lt;/code&gt; layers to the client layers, and add the &lt;code&gt;ReportCache&lt;/code&gt; and &lt;code&gt;Render&lt;/code&gt; implementations:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; finalLayer&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ZLayer&lt;/span&gt;&lt;span&gt;.service&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Logger&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; ++&lt;/span&gt;&lt;span&gt; awsCore&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span&gt; awsClients&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; ++&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Console&lt;/span&gt;&lt;span&gt;.any &lt;/span&gt;&lt;span&gt;++&lt;/span&gt;&lt;span&gt; cache.live&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;gt;+&amp;gt;&lt;/span&gt;&lt;span&gt; render.live&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This has the environment &lt;code&gt;ClippConfig[Parameters] with Console with Logging with ReportCache with Rendering with AllServices&lt;/code&gt; where&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AllServices&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Ec2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ElasticLoadBalancing&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ElasticBeanstalk&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AutoScaling&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;We reimplemented the tool to query AWS resources using functional programming techniques, built on top of ZIO libraries. By separating the execution from the problem specification we get an easily readable and maintainable code that can be easily extended with new queries or reports without having to thing about how caching and concurrency is implemented under the hood. We can rate limit AWS requests without touching the actual queries, and take advantage of batching AWS operations while keeping the query logic simple and unaware of this optimization.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Code generation in ZIO-AWS</title>
        <published>2020-09-23T00:00:00+00:00</published>
        <updated>2020-09-23T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/zioaws-code-generation/"/>
        <id>https://blog.vigoo.dev/posts/zioaws-code-generation/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/zioaws-code-generation/">&lt;p&gt;I have recently published a set of libraries, &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/zio-aws&quot;&gt;&lt;strong&gt;zio-aws&lt;/strong&gt;&lt;/a&gt;, aiming to provide a better interface for working with &lt;em&gt;AWS services&lt;/em&gt; from &lt;a rel=&quot;external&quot; href=&quot;https://zio.dev/&quot;&gt;ZIO&lt;/a&gt; applications. For more information about how the ZIO &lt;em&gt;interface&lt;/em&gt; works and how to get started with these libraries, read the repository&#39;s README. In this post, I will focus on how these libraries are generated from the schema provided by the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/aws/aws-sdk-java-v2&quot;&gt;AWS Java SDK v2&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;generating-code&quot;&gt;Generating code&lt;/h2&gt;
&lt;p&gt;I wanted to cover &lt;em&gt;all&lt;/em&gt; AWS services at once. This means client libraries for more than 200 services, so the only possible approach was to &lt;em&gt;generate&lt;/em&gt; these libraries on top of a small hand-written core.&lt;/p&gt;
&lt;h3 id=&quot;schema&quot;&gt;Schema&lt;/h3&gt;
&lt;p&gt;The first thing we need for generating code is a source schema. This is the model that we use to create the source code from. It is usually constructed by some kind of DSL or more directly described by a JSON or YAML or similar data model. In the case of &lt;strong&gt;zio-aws&lt;/strong&gt; this was already defined in the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/aws/aws-sdk-java-v2&quot;&gt;AWS Java SDK v2&lt;/a&gt; project. The way it works is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;There is a &lt;code&gt;codegen&lt;/code&gt; project, published in the &lt;code&gt;software.amazon.awssdk&lt;/code&gt; group among the client libraries, that contains the Java classes used for generating the Java SDK itself. This contains the data model classes for parsing the actual schema as well.&lt;/li&gt;
&lt;li&gt;In the AWS Java SDK v2 repository, the schema is located in the subdirectory called &lt;a rel=&quot;external&quot; href=&quot;https://github.com/aws/aws-sdk-java-v2/tree/master/services&quot;&gt;&lt;code&gt;services&lt;/code&gt;&lt;/a&gt;. There is a directory for each AWS service and it contains among other things some relevant &lt;em&gt;JSON&lt;/em&gt; schema files:
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;service-2.json&lt;/code&gt; is the main schema of the service, describing the data structures and operations&lt;/li&gt;
&lt;li&gt;&lt;code&gt;paginators-1.json&lt;/code&gt; describes the operations that the Java SDK creates a &lt;em&gt;paginator interface&lt;/em&gt; for&lt;/li&gt;
&lt;li&gt;&lt;code&gt;customization.config&lt;/code&gt; contains extra information, including changes to be applied on top of the service model&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Fortunately, these are also embedded in the generated &lt;em&gt;AWS Java SDK&lt;/em&gt; libraries as resources, so getting &lt;em&gt;all client libraries&lt;/em&gt; on the classpath gives us an easy way to get the corresponding schemas as well&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I decided to use the low-level data classes from the AWS &lt;code&gt;codegen&lt;/code&gt; library to parse these files and using that build a higher-level model that can be then used as an input for the &lt;em&gt;code generator&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;This is encapsulated in a &lt;em&gt;ZIO layer&lt;/em&gt; called &lt;code&gt;Loader&lt;/code&gt;, which has two functions:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; findModels&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Blocking&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Throwable&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Set&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ModelId&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; loadCodegenModel&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ModelId&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Blocking&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Throwable&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;C2jModels&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first one, &lt;code&gt;findModels&lt;/code&gt; uses the &lt;code&gt;ClassLoader&lt;/code&gt; to enumerate all &lt;code&gt;codegen-resources&lt;/code&gt; folders on the &lt;em&gt;classpath&lt;/em&gt; and just returns a set of &lt;code&gt;ModelId&lt;/code&gt;s. &lt;code&gt;ModelId&lt;/code&gt; is a pair of a model name (such as &lt;code&gt;s3&lt;/code&gt;) and an optional submodule name (for example &lt;code&gt;dynamodb:dynamodbstreams&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Then for each detected model we can load it with the &lt;code&gt;loadCodegenModel&lt;/code&gt; function, &lt;code&gt;C2jModels&lt;/code&gt; is a class from the AWS &lt;code&gt;codegen&lt;/code&gt; library.&lt;/p&gt;
&lt;p&gt;Figuring out how to interpret these data structures, and how to map them to the generated Java API was the hardest part, but it&#39;s out of scope for this post. Our next topic here is how we generate code from our &lt;em&gt;model&lt;/em&gt;.&lt;/p&gt;
&lt;h3 id=&quot;scalameta&quot;&gt;Scalameta&lt;/h3&gt;
&lt;p&gt;There are several possibilities to generate source code and I tried many of them during the past years. Let&#39;s see some examples:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Using a general-purpose text template engine. An example we used at &lt;a rel=&quot;external&quot; href=&quot;https://prezi.com&quot;&gt;Prezi&lt;/a&gt; is the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/bkiers/Liqp&quot;&gt;Java implementation of the Liquid templating engine&lt;/a&gt;. Another example is the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/OpenAPITools/openapi-generator&quot;&gt;OpenAPI generator project&lt;/a&gt; that uses &lt;a rel=&quot;external&quot; href=&quot;https://mustache.github.io/&quot;&gt;Mustache&lt;/a&gt; templates to generate server and client code from OpenAPI specifications.&lt;/li&gt;
&lt;li&gt;Generating from code with some general-purpose pretty-printing library. With this approach, we are using the pretty-printer library&#39;s composability features to create source code building blocks, and map the code generator model to these constructs. It is easier to express complex logic in this case, as we don&#39;t have to encode it in a limited dynamic template model. On the other hand, reading the code generator&#39;s source and imagining the output is not easy, and nothing enforces that the pretty-printer building blocks are actually creating valid source code.&lt;/li&gt;
&lt;li&gt;If the target language has an AST with a pretty-printing feature, we can map the model to the AST directly and just pretty print at the end. With this, we get a much more efficient development cycle, as the generated code is at least guaranteed to be syntactically correct. But the AST can be far from how the target language&#39;s textual representation looks like, which makes it difficult to read and write this code.&lt;/li&gt;
&lt;li&gt;With a library that supports building ASTs with &lt;em&gt;quasiquotes&lt;/em&gt;, we can build the AST fragments with a syntax that is very close to the generated target language. For &lt;em&gt;Scala&lt;/em&gt;, a library that supports this and is used in a lot of tooling projects is &lt;a rel=&quot;external&quot; href=&quot;https://scalameta.org/&quot;&gt;Scalameta&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I wanted to try using &lt;em&gt;Scalameta&lt;/em&gt; ever since I met Devon Stewart and he mentioned how he uses it in &lt;a rel=&quot;external&quot; href=&quot;https://github.com/twilio/guardrail/&quot;&gt;guardrail&lt;/a&gt;. Finally, this was a perfect use case to do so!&lt;/p&gt;
&lt;p&gt;To get an understanding of what kind of Scala language constructs can be built with &lt;em&gt;quasiquotes&lt;/em&gt; with &lt;em&gt;Scalameta&lt;/em&gt;, check &lt;a rel=&quot;external&quot; href=&quot;https://scalameta.org/docs/trees/quasiquotes.html&quot;&gt;the list of them in the official documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;We get a good mix of both worlds with this. It is possible to express complex template logic in real code, creating higher-level constructs, taking advantage of the full power of Scala. On the other hand, the actual &lt;em&gt;quasiquoted&lt;/em&gt; fragments are still close to the code generator&#39;s target language (which is in this case also Scala).&lt;/p&gt;
&lt;p&gt;Let&#39;s see a short example of this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; generateMap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;m&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Model&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;GeneratorContext&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;GeneratorFailure&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ModelWrapper&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    keyModel &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;m.shape.getMapKeyType.getShape&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    valueModel &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;m.shape.getMapValueType.getShape&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    keyT &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TypeMapping&lt;/span&gt;&lt;span&gt;.toWrappedType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;keyModel&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    valueT &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TypeMapping&lt;/span&gt;&lt;span&gt;.toWrappedType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;valueModel&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ModelWrapper&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    code &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; List&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; ${&lt;/span&gt;&lt;span&gt;m.asType&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; M&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;$&lt;/span&gt;&lt;span&gt;keyT&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; $&lt;/span&gt;&lt;span&gt;valueT&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For each &lt;em&gt;AWS&lt;/em&gt; service-specific &lt;em&gt;model type&lt;/em&gt; we generate some kind of wrapper code into the ZIO service client library. This is done by processing the schema model to an intermediate format where for each such wrapper, we have a &lt;code&gt;ModelWrapper&lt;/code&gt; value that already has the &lt;em&gt;Scalameta AST&lt;/em&gt; for that particular wrapper. The above code fragment creates this for &lt;em&gt;map types&lt;/em&gt;, which is a simple type alias for a Scala &lt;code&gt;Map&lt;/code&gt;. It&#39;s a &lt;code&gt;ZIO&lt;/code&gt; function, taking advantage of passing around the context in the &lt;em&gt;environment&lt;/em&gt; and safely handling generator failures, while the actual generated code part in the &lt;code&gt;q&quot;&quot;&quot;...&quot;&quot;&quot;&lt;/code&gt; remained quite readable.&lt;/p&gt;
&lt;p&gt;Then the whole &lt;em&gt;model package&lt;/em&gt; can be expressed like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  primitiveModels &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;.foreach&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;primitiveModels.toList.sortBy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_.name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;generateModel&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  models &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;.foreach&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;complexModels.toList.sortBy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_.name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;generateModel&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; q&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; $&lt;/span&gt;&lt;span&gt;fullPkgName&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;            i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;            i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; j&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;            i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; z&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; Z&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;            i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;$&lt;/span&gt;&lt;span&gt;parentModuleImport&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;            p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;              o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;                .&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;${&lt;/span&gt;&lt;span&gt;primitiveModels.flatMap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_.code&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;              }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;              .&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;${&lt;/span&gt;&lt;span&gt;models.flatMap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_.code&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;            }&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This can be then &lt;em&gt;pretty printed&lt;/em&gt; simply with&lt;code&gt;.toString&lt;/code&gt; and saved to a &lt;code&gt;.scala&lt;/code&gt; file.&lt;/p&gt;
&lt;h2 id=&quot;building-the-libraries&quot;&gt;Building the libraries&lt;/h2&gt;
&lt;p&gt;We have a way to collect the service models and generate source code from that, but we still have to use that generated code somehow. In &lt;code&gt;zio-aws&lt;/code&gt; the goal was to generate a separate &lt;em&gt;client library&lt;/em&gt; for each AWS service. At the time of writing, there were &lt;strong&gt;235&lt;/strong&gt; such services. The generated libraries have to be built and published to &lt;em&gt;Sonatype&lt;/em&gt;.&lt;/p&gt;
&lt;h3 id=&quot;first-version&quot;&gt;First version&lt;/h3&gt;
&lt;p&gt;In the first version I simply wired together the above described &lt;code&gt;loader&lt;/code&gt; and &lt;code&gt;generator&lt;/code&gt; module into a &lt;code&gt;ZIO&lt;/code&gt; &lt;em&gt;command line&lt;/em&gt; app, using &lt;a rel=&quot;external&quot; href=&quot;https://vigoo.github.io/clipp/docs/&quot;&gt;clipp&lt;/a&gt; for command line parsing. It&#39;s &lt;code&gt;main&lt;/code&gt; was really just something like the following:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; app&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  svcs &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; config.parameters&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Parameters&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_.serviceList&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  ids &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; svcs &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;match&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ids&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;.succeed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ids.toSet&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; loader.findModels&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;.mapError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ReflectionError&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;.foreachPar&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ids&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; id &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      model &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; loader.loadCodegenModel&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.mapError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ReflectionError&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; generator.generateServiceCode&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;id, model&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.mapError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;GeneratorError&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span&gt; ()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; generator.generateBuildSbt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ids&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.mapError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;GeneratorError&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; generator.copyCoreProject&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;.mapError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;GeneratorError&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExitCode&lt;/span&gt;&lt;span&gt;.success&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; cfg&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; config.fromArgsWithUsageInfo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;args, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Parameters&lt;/span&gt;&lt;span&gt;.spec&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.mapError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ParserError&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; modules&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; loader.live &lt;/span&gt;&lt;span&gt;++&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;cfg &lt;/span&gt;&lt;span&gt;&amp;gt;+&amp;gt;&lt;/span&gt;&lt;span&gt; generator.live&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;app.provideCustomLayer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;modules&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then created a &lt;em&gt;multi-module&lt;/em&gt; &lt;code&gt;sbt&lt;/code&gt; project with the following modules:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;zio-aws-codegen&lt;/code&gt; the CLI code generator we were talking about so far&lt;/li&gt;
&lt;li&gt;&lt;code&gt;zio-aws-core&lt;/code&gt; holding the common part of all AWS service wrapper libraries. This contains things like how to translate AWS pagination into &lt;code&gt;ZStream&lt;/code&gt; etc.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;zio-aws-akka-http&lt;/code&gt;, &lt;code&gt;zio-aws-http4s&lt;/code&gt; and &lt;code&gt;zio-aws-netty&lt;/code&gt; are the supported &lt;em&gt;HTTP layers&lt;/em&gt;, all depend on &lt;code&gt;zio-aws-core&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I also created a first &lt;em&gt;example&lt;/em&gt; project in a separate &lt;code&gt;sbt&lt;/code&gt; project, that demonstrated the use of some of the generated AWS client libraries. With this primitive setup, building everything from scratch and running the example took the following steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;sbt compile&lt;/code&gt; the root project&lt;/li&gt;
&lt;li&gt;manually running &lt;code&gt;zio-aws-codegen&lt;/code&gt; to generate &lt;em&gt;all client libs at once&lt;/em&gt; to a separate directory, with a corresponding &lt;code&gt;build.sbt&lt;/code&gt; including all these projects in a single &lt;code&gt;sbt&lt;/code&gt; project&lt;/li&gt;
&lt;li&gt;&lt;code&gt;sbt publishLocal&lt;/code&gt; in the generated &lt;code&gt;sbt&lt;/code&gt; project&lt;/li&gt;
&lt;li&gt;&lt;code&gt;sbt run&lt;/code&gt; in the &lt;em&gt;examples&lt;/em&gt; project&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For the second, manual step I created some &lt;em&gt;custom sbt tasks&lt;/em&gt; called &lt;code&gt;generateAll&lt;/code&gt;, &lt;code&gt;buildAll&lt;/code&gt;, and &lt;code&gt;publishLocalAll&lt;/code&gt;, that downloaded an &lt;code&gt;sbt-launch-*.jar&lt;/code&gt; and used it to run the code generator and fork an &lt;code&gt;sbt&lt;/code&gt; to build the generated project.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;generateAll&lt;/code&gt; task was quite simple:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;generateAll &lt;/span&gt;&lt;span&gt;:=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Def&lt;/span&gt;&lt;span&gt;.taskDyn &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; root&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; baseDirectory.value.getAbsolutePath&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Def&lt;/span&gt;&lt;span&gt;.task &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span&gt;codegen &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Compile&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; /&lt;/span&gt;&lt;span&gt; run&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.toTask&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; ${&lt;/span&gt;&lt;span&gt;root&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; ${&lt;/span&gt;&lt;span&gt;root&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; $&lt;/span&gt;&lt;span&gt;zioAwsVersion&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; $&lt;/span&gt;&lt;span&gt;zioVersion&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; $&lt;/span&gt;&lt;span&gt;zioReactiveStreamsInteropVersion&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;.value&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Launching a second &lt;code&gt;sbt&lt;/code&gt; took more effort:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;buildAll &lt;/span&gt;&lt;span&gt;:=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Def&lt;/span&gt;&lt;span&gt;.taskDyn &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; _&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; generateAll.value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; generatedRoot&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; baseDirectory.value &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;generated&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; launcherVersion&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; sbtVersion.value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; launcher&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;$&lt;/span&gt;&lt;span&gt;launcherVersion&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; launcherFile&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; generatedRoot &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;/&lt;/span&gt;&lt;span&gt; launcher&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Def&lt;/span&gt;&lt;span&gt;.task&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;!&lt;/span&gt;&lt;span&gt;launcherFile.exists&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; u&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; url&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;$&lt;/span&gt;&lt;span&gt;launcherVersion&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;$&lt;/span&gt;&lt;span&gt;launcherVersion&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      sbt.io.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Using&lt;/span&gt;&lt;span&gt;.urlInputStream&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;u&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; inputStream &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;        IO&lt;/span&gt;&lt;span&gt;.transfer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;inputStream, launcherFile&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; fork&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ForkRun&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ForkOptions&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      .withWorkingDirectory&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;generatedRoot&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fork.run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;xsbt.boot.Boot&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      classpath &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; launcherFile &lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Nil&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      options &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; ::&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Nil&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      log &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; streams.value.log&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;.value&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With these extra tasks, I released the first version of the library manually, but there was a lot of annoying difficulties:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Having to switch between various &lt;code&gt;sbt&lt;/code&gt; projects&lt;/li&gt;
&lt;li&gt;The need to &lt;code&gt;publishLocal&lt;/code&gt; the generated artifacts in order to build the examples, or any kind of integration tests that I planned to add&lt;/li&gt;
&lt;li&gt;The only way to build only those client libraries that are needed for the examples/tests was to build and publish them manually, as this dependency was not tracked at all between the unrelated &lt;code&gt;sbt&lt;/code&gt; projects&lt;/li&gt;
&lt;li&gt;Because the generated &lt;code&gt;sbt&lt;/code&gt; project could not refer to the outer &lt;code&gt;zio-aws-core&lt;/code&gt; project, it has to be copied into the generated project in the code generator step&lt;/li&gt;
&lt;li&gt;Building and publishing all the &lt;strong&gt;235&lt;/strong&gt; projects at once required about &lt;strong&gt;16Gb&lt;/strong&gt; memory and hours of compilation time. It was too big to run on any of the (freely available) CI systems.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;proper-solution&quot;&gt;Proper solution&lt;/h3&gt;
&lt;p&gt;When I mentioned this, &lt;em&gt;Itamar Ravid&lt;/em&gt; recommended trying to make it an &lt;em&gt;sbt code generator&lt;/em&gt;. &lt;code&gt;sbt&lt;/code&gt; has built-in support for generating source code, as described &lt;a rel=&quot;external&quot; href=&quot;https://www.scala-sbt.org/1.0/docs/Howto-Generating-Files.html&quot;&gt;on it&#39;s documentation page&lt;/a&gt;. This alone though would not be enough to cover our use case, as in &lt;code&gt;zio-aws&lt;/code&gt; even the &lt;em&gt;set of projects&lt;/em&gt; is dynamic and comes from the enumeration of schema models. Fortunately, there is support for that in too, through the &lt;code&gt;extraProjects&lt;/code&gt; property of &lt;code&gt;sbt&lt;/code&gt; &lt;em&gt;plugins&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;With these two tools, the new project layout became the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;zio-aws-codegen&lt;/code&gt; is an sbt &lt;strong&gt;plugin&lt;/strong&gt;, having it&#39;s own &lt;code&gt;sbt&lt;/code&gt; project in a subdirectory&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;zio-aws-core&lt;/code&gt; and the HTTP libraries are all in the top-level project as before&lt;/li&gt;
&lt;li&gt;examples and integration tests are also part of the top-level project&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;zio-aws-codegen&lt;/code&gt; plugin is referenced using a &lt;code&gt;ProjectRef&lt;/code&gt; from the outer project&lt;/li&gt;
&lt;li&gt;the plugin adds all the &lt;em&gt;AWS service client wrapper libraries&lt;/em&gt; to the top-level project&lt;/li&gt;
&lt;li&gt;these projects generate their source on-demand&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this setup, it is possible to build any subset of the generated libraries without the need to process and compile all of them, so it needs much less memory. It is also much simpler to run tests or build examples on top of them, as the test and example projects can directly depend on the generated libraries as &lt;code&gt;sbt&lt;/code&gt; submodules. And even developing the &lt;em&gt;code generator&lt;/em&gt; itself is convenient - although for editing it, it has to be opened as in a separate IDE session, but otherwise, &lt;code&gt;sbt reload&lt;/code&gt; on the top level project automatically recompiles the plugin when needed.&lt;/p&gt;
&lt;p&gt;Let&#39;s see piece by piece how we can achieve this!&lt;/p&gt;
&lt;h4 id=&quot;project-as-a-source-dependency&quot;&gt;Project as a source dependency&lt;/h4&gt;
&lt;p&gt;The first thing I wanted to do is having the &lt;code&gt;zio-aws-codegen&lt;/code&gt; project converted to an &lt;code&gt;sbt&lt;/code&gt; plugin, but still having it in the same repository and be able to use it without having to install to a local repository. Although the whole code generator code could have been added to the top level &lt;code&gt;sbt&lt;/code&gt; project&#39;s &lt;code&gt;project&lt;/code&gt; source, I wanted to keep it as a separate module to be able to publish it as a library or a CLI tool in the future if needed.&lt;/p&gt;
&lt;p&gt;This can be achieved by putting it in a subdirectory of the top level project, with a separate &lt;code&gt;build.sbt&lt;/code&gt; that contains the&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;sbtPlugin &lt;/span&gt;&lt;span&gt;:=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(beside the usual ones). Then it can be referenced in the top level project&#39;s &lt;code&gt;project/plugins.sbt&lt;/code&gt; in the following way:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;lazy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; codegen&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; project&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .in&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;file&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .dependsOn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProjectRef&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;file&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;../zio-aws-codegen&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;zio-aws-codegen&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and enabled in the &lt;code&gt;build.sbt&lt;/code&gt; as&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;enablePlugins&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ZioAwsCodegenPlugin&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;dynamically-generating-projects&quot;&gt;Dynamically generating projects&lt;/h4&gt;
&lt;p&gt;To generate the subprojects dynamically, we need the &lt;code&gt;Set[ModelId]&lt;/code&gt; coming from the &lt;code&gt;loader&lt;/code&gt; module. It is a &lt;code&gt;ZIO&lt;/code&gt; module, so from the &lt;code&gt;sbt&lt;/code&gt; plugin we have to use &lt;code&gt;Runtime.default.unsafeRun&lt;/code&gt; to execute it.&lt;/p&gt;
&lt;p&gt;As the code generator project is now an &lt;code&gt;sbt&lt;/code&gt; plugin, all the &lt;code&gt;sbt&lt;/code&gt; data structures are directly available, so we can just write a function that maps the &lt;code&gt;ModelId&lt;/code&gt;s to &lt;code&gt;Project&lt;/code&gt;s:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;protected&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; generateSbtSubprojects&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;ids&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Set&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ModelId&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Seq&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Project&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; ???&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;One interesting part here is that some of the subprojects are depending on each other. This happens with AWS service &lt;em&gt;submodules&lt;/em&gt;, indicated by the second parameter of &lt;code&gt;ModelId&lt;/code&gt;. An example is &lt;code&gt;dynamodbstreams&lt;/code&gt; that depends on &lt;code&gt;dynamodb&lt;/code&gt;. When creating the &lt;code&gt;Project&lt;/code&gt; values, we have to be able to &lt;code&gt;dependOn&lt;/code&gt; on some other already generated projects, and they have to be generated in the correct order to do so.&lt;/p&gt;
&lt;p&gt;We could do a full topological sort, but it is not necessary, here we know that the maximum depth of dependencies is 1, so it is enough to put the submodules at the end of the sequence:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; map&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; ids&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .toSeq&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .sortWith &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;a, b&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; aIsDependent&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; a.subModuleName &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;match&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; if&lt;/span&gt;&lt;span&gt; value &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;!=&lt;/span&gt;&lt;span&gt; a.name &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span&gt; _ &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; bIsDependent&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; b.subModuleName &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;match&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; if&lt;/span&gt;&lt;span&gt; value &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;!=&lt;/span&gt;&lt;span&gt; b.name &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span&gt; _ &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    bIsDependent &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;||&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;!&lt;/span&gt;&lt;span&gt;aIsDependent &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; a.toString &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; b.toString&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then in order to be able get the dependencies, we do a &lt;em&gt;fold&lt;/em&gt; on the ordered &lt;code&gt;ModelId&lt;/code&gt;s:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .foldLeft&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Map&lt;/span&gt;&lt;span&gt;.empty&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ModelId&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Project&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;mapping, id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;      //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; deps&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; id.subModule &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;match&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; if&lt;/span&gt;&lt;span&gt; value &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;!=&lt;/span&gt;&lt;span&gt; id.name &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;          Seq&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ClasspathDependency&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LocalProject&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;zio-aws-core&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;None&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;              ClasspathDependency&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;mapping&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ModelId&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;id.name, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;id.name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;None&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span&gt; _ &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;          Seq&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ClasspathDependency&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LocalProject&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;zio-aws-core&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;None&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;span&gt;      &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; project&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Project&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;fullName, file&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;generated&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; /&lt;/span&gt;&lt;span&gt; name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .settings&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          libraryDependencies &lt;/span&gt;&lt;span&gt;+=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;software.amazon.awssdk&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; %&lt;/span&gt;&lt;span&gt; id.name &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;%&lt;/span&gt;&lt;span&gt; awsLibraryVersion.value,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;          //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .dependsOn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;deps&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; _*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      mapping.updated&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;id, project&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To make it easier to work with the generated projects, we also create a project named &lt;code&gt;all&lt;/code&gt; that aggregates all the ones generated above.&lt;/p&gt;
&lt;h4 id=&quot;applying-settings-to-the-generated-projects&quot;&gt;Applying settings to the generated projects&lt;/h4&gt;
&lt;p&gt;The code generator only sets the basic settings for the generated projects: name, path and dependencies. We need a lot more, setting organization and version, all the publishing options, controlling the Scala version, etc.&lt;/p&gt;
&lt;p&gt;I decided to keep these settings outside of the code generator plugin, in the top-level &lt;code&gt;sbt&lt;/code&gt; project. By creating an &lt;code&gt;AutoPlugin&lt;/code&gt; end enabling it for all projects, we can inject all the common settings for both the hand-written and the generated projects:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Common&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AutoPlugin&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; autoImport&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; scala212Version&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;2.12.12&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; scala213Version&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;2.13.3&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  import&lt;/span&gt;&lt;span&gt; autoImport&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; trigger&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; allRequirements&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; requires&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Sonatype&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; lazy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; projectSettings&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Seq&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      scalaVersion &lt;/span&gt;&lt;span&gt;:=&lt;/span&gt;&lt;span&gt; scala213Version,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      crossScalaVersions &lt;/span&gt;&lt;span&gt;:=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; List&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;scala212Version, scala213Version&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;      //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;source-generator-task&quot;&gt;Source generator task&lt;/h4&gt;
&lt;p&gt;At this point, we could also add the already existing &lt;em&gt;source code generation&lt;/em&gt; to the initialization of the plugin, and just generate all the subproject&#39;s all source files every time the &lt;code&gt;sbt&lt;/code&gt; project is loaded. With this number of generated projects though, it would have been a very big startup overhead and would not allow us to split the build (at least not the code generation part) on CI, to solve the memory and build time issues.&lt;/p&gt;
&lt;p&gt;As &lt;code&gt;sbt&lt;/code&gt; has built-in support for defining &lt;em&gt;source generator tasks&lt;/em&gt;, we can do much better!&lt;/p&gt;
&lt;p&gt;Instead of generating the source codes in one step, we define a &lt;code&gt;generateSources&lt;/code&gt; task and add it to each &lt;em&gt;generated subproject&lt;/em&gt; as a &lt;em&gt;source generator&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Compile&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; /&lt;/span&gt;&lt;span&gt; sourceGenerators &lt;/span&gt;&lt;span&gt;+=&lt;/span&gt;&lt;span&gt; generateSources.taskValue,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;awsLibraryId &lt;/span&gt;&lt;span&gt;:=&lt;/span&gt;&lt;span&gt; id.toString&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;awsLibraryId&lt;/code&gt; is a custom property that we the &lt;code&gt;generateSources&lt;/code&gt; task can use to determine which schema to use for the code generation.&lt;/p&gt;
&lt;p&gt;The first part of this task is to gather the information from the project it got applied on, including the custom &lt;code&gt;awsLibraryId&lt;/code&gt; property:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;lazy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; generateSources&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Def&lt;/span&gt;&lt;span&gt;.task &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; log&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; streams.value.log&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; idStr&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; awsLibraryId.value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; id&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ModelId&lt;/span&gt;&lt;span&gt;.parse&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;idStr&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; match&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Left&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;failure&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; sys.error&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;failure&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Right&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; targetRoot&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;sourceManaged in &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Compile&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; travisSrc&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; travisSource.value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; travisDst&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; travisTarget.value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; parallelJobs&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; travisParallelJobs.value&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;From these, we create a &lt;code&gt;Parameters&lt;/code&gt; data structure to pass to the &lt;code&gt;generator&lt;/code&gt; module. This is what we used to construct with &lt;code&gt;clipp&lt;/code&gt; from CLI arguments:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; params&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Parameters&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      targetRoot &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Path&lt;/span&gt;&lt;span&gt;.fromJava&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;targetRoot.toPath&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      travisSource &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Path&lt;/span&gt;&lt;span&gt;.fromJava&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;travisSrc.toPath&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      travisTarget &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Path&lt;/span&gt;&lt;span&gt;.fromJava&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;travisDst.toPath&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      parallelTravisJobs &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; parallelJobs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And finally, construct the &lt;code&gt;ZIO&lt;/code&gt; environment, load a &lt;strong&gt;single&lt;/strong&gt; schema model, and generate the library&#39;s source code:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    zio.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Runtime&lt;/span&gt;&lt;span&gt;.default.unsafeRun &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; cfg&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZLayer&lt;/span&gt;&lt;span&gt;.succeed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;params&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; env&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; loader.live &lt;/span&gt;&lt;span&gt;++&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;cfg &lt;/span&gt;&lt;span&gt;&amp;gt;+&amp;gt;&lt;/span&gt;&lt;span&gt; generator.live&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; task&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;.effect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;log.info&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;G&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; f&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; $&lt;/span&gt;&lt;span&gt;id&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          model &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; loader.loadCodegenModel&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          files &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; generator.generateServiceCode&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;id, model&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span&gt; files.toSeq&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      task.provideCustomLayer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;env&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.catchAll &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; generatorError &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;        ZIO&lt;/span&gt;&lt;span&gt;.effect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;log.error&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; g&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; f&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; ${&lt;/span&gt;&lt;span&gt;generatorError&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.as&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Seq&lt;/span&gt;&lt;span&gt;.empty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;generateServiceCode&lt;/code&gt; function returns a &lt;code&gt;Set[File]&lt;/code&gt; value containing all the generated source files. This is the result of the &lt;em&gt;source generator task&lt;/em&gt;, and &lt;code&gt;sbt&lt;/code&gt; uses this information to add the generated files to the compilation.&lt;/p&gt;
&lt;h4 id=&quot;referencing-the-generated-projects&quot;&gt;Referencing the generated projects&lt;/h4&gt;
&lt;p&gt;When defining downstream projects in the &lt;code&gt;build.sbt&lt;/code&gt;, such as integration tests and other examples, we have to refer to the generated projects somehow. There is no value of type &lt;code&gt;Project&lt;/code&gt; in scope to do so, but we can do it easily by name using &lt;code&gt;LocalProject&lt;/code&gt;. The following example shows how the &lt;code&gt;example1&lt;/code&gt; subproject does this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;lazy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; example1&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Project&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;example1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, file&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;examples&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;example1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .dependsOn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    core,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    http4s,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    netty,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    LocalProject&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;zio-aws-elasticbeanstalk&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    LocalProject&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;zio-aws-ec2&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;parallel-build-on-travis-ci&quot;&gt;Parallel build on Travis CI&lt;/h4&gt;
&lt;p&gt;The last thing that I wanted to solve is building the full &lt;code&gt;zio-aws&lt;/code&gt; suite on a CI. I am using &lt;a rel=&quot;external&quot; href=&quot;https://travis-ci.org/&quot;&gt;Travis CI&lt;/a&gt; for my private projects, so that&#39;s what I built it for. The idea is to split the set of &lt;em&gt;service client libraries&lt;/em&gt; to chunks and create &lt;a rel=&quot;external&quot; href=&quot;https://docs.travis-ci.com/user/build-matrix/&quot;&gt;build matrix&lt;/a&gt; to run those in parallel. The tricky part is that the set of generated service libraries is dynamic, collected by the code generator.&lt;/p&gt;
&lt;p&gt;To solve this, I started to generate the &lt;code&gt;.travis.yml&lt;/code&gt;  build descriptor as well. The &lt;em&gt;hand-written&lt;/em&gt; part has been moved to &lt;code&gt;.travis.base.yml&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;anguage&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;cala&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ervices&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ocker&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;cala&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 2.12.12&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 2.13.3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ache&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  d&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;irectories&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; $&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;HOME/.cache/coursier&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; $&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;HOME/.ivy2/cache&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; $&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;HOME/.sbt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;nv&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; C&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;OMMANDS=&amp;quot;clean zio-aws-core/test zio-aws-akka-http/test zio-aws-http4s/test zio-aws-netty/test&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; C&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;OMMANDS=&amp;quot;clean examples/compile&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; C&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;OMMANDS=&amp;quot;clean integtests/test&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;efore_install&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;f [ &amp;quot;$COMMANDS&amp;quot; = &amp;quot;clean integtests/test&amp;quot; ]; then docker pull localstack/localstack; fi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;f [ &amp;quot;$COMMANDS&amp;quot; = &amp;quot;clean integtests/test&amp;quot; ]; then docker run -d -p 4566:4566 --env SERVICES=s3,dynamodb --env START_WEB=0 localstack/localstack; fi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;cript&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;bt ++$TRAVIS_SCALA_VERSION -jvm-opts travis/jvmopts $COMMANDS&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I use the &lt;code&gt;COMMANDS&lt;/code&gt; environment variable to define the parallel sets of &lt;code&gt;sbt&lt;/code&gt; commands here. There are three predefined sets: building &lt;code&gt;zio-aws-core&lt;/code&gt; and the HTTP implementations, building the &lt;em&gt;example projects&lt;/em&gt; and running the &lt;em&gt;integration test&lt;/em&gt;. The last two involve generating actual service client code and building them - but only the few that are necessary, so it is not an issue to do that redundantly.&lt;/p&gt;
&lt;p&gt;The real &lt;code&gt;.travis.yml&lt;/code&gt; file is then generated by running a task &lt;em&gt;manually&lt;/em&gt;, &lt;code&gt;sbt generateTravisYaml&lt;/code&gt;. It is implemented in the &lt;code&gt;zio-aws-codegen&lt;/code&gt; plugin and it loads the &lt;code&gt;.travis.base.yml&lt;/code&gt; file and extends the &lt;code&gt;env&lt;/code&gt; section with a set of &lt;code&gt;COMMANDS&lt;/code&gt; variants, each compiling a subset of the generated subprojects.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Travis CI can now build &lt;code&gt;zio-aws&lt;/code&gt; and run its integration tests. A build runs for hours, but it is stable, and consists of 22 parallel jobs to build all the libraries for both Scala 2.12 and 2.13. At the same time, developing the code generator and the other subprojects and tests became really convenient.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>prox part 4 - simplified redesign</title>
        <published>2020-08-03T00:00:00+00:00</published>
        <updated>2020-08-03T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/prox-4-simplify/"/>
        <id>https://blog.vigoo.dev/posts/prox-4-simplify/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/prox-4-simplify/">&lt;h2 id=&quot;blog-post-series&quot;&gt;Blog post series&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.vigoo.dev/posts/prox-1-types/&quot;&gt;Part 1 - type level programming&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.vigoo.dev/posts/prox-2-io-akkastreams/&quot;&gt;Part 2 - akka streams with cats effect&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.vigoo.dev/posts/prox-3-zio/&quot;&gt;Part 3 - effect abstraction and ZIO&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.vigoo.dev/posts/prox-4-simplify/&quot;&gt;Part 4 - simplified redesign&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;intro&quot;&gt;Intro&lt;/h2&gt;
&lt;p&gt;In &lt;a href=&quot;https://blog.vigoo.dev/posts/prox-4-simplify/2019-02-10-prox-1-types.html&quot;&gt;Part 1&lt;/a&gt; I described how the advanced type level programming techniques can be used to describe the execution of system processes. It was both a good playground to experiment with these and the result has been proven useful as we started to use it in more and more production systems and test environments at &lt;a rel=&quot;external&quot; href=&quot;https://prezi.com&quot;&gt;Prezi&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;On the other hand as I mentioned at the end of the first post, there is a tradeoff. These techniques made the original version of &lt;em&gt;prox&lt;/em&gt; very hard to maintain and improve, and the error messages library users got by small mistakes were really hard to understand.&lt;/p&gt;
&lt;p&gt;Last December (in 2019) I redesigned the library to be simpler and easier to use by making some compromises. Let&#39;s discover how!&lt;/p&gt;
&lt;h2 id=&quot;a-single-process&quot;&gt;A single process&lt;/h2&gt;
&lt;p&gt;We start completely from scratch and try to design the library with the same functionality but with simplicity in mind. The code snippets shown here are not necessarily the final, current state of the traits and objects of the library, but some intermediate steps so we see the thought process.&lt;/p&gt;
&lt;p&gt;First let&#39;s focus on defining a &lt;strong&gt;single process&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; command&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; arguments&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; workingDirectory&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Path&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; environmentVariables&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Map&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; removedEnvironmentVariables&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Set&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Without deciding already how it will be implemented, we know we need these information to be able to launch the process alone. And how to execute it? Let&#39;s separate it completely:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessResult&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; exitCode&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExitCode&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessRunner&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; start&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;process&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Resource&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Fiber&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProcessResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I decided that better integration with the IO library (&lt;a rel=&quot;external&quot; href=&quot;https://typelevel.org/cats-effect/&quot;&gt;cats-effect&lt;/a&gt; in this case) is also a goal of the redesign, so for starter modelled the &lt;em&gt;running process&lt;/em&gt; as a cancellable fiber resulting in &lt;code&gt;ProcessResult&lt;/code&gt;, where cancellation means &lt;strong&gt;terminating&lt;/strong&gt; the process. At this stage of the redesign I worked directly with &lt;code&gt;IO&lt;/code&gt; instead of the &lt;em&gt;IO typeclasses&lt;/em&gt; and later replaced it like I described in &lt;a href=&quot;https://blog.vigoo.dev/posts/prox-4-simplify/2019-08-13-prox-3-zio.html&quot;&gt;the previous post&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Let&#39;s see how a simple runner implementation would look like:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span&gt; java&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;lang&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Process&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; JvmProcess&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; JVMProcessRunner&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; contextShift&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextShift&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessRunner&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  import&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; JVMProcessRunner&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; start&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;process&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Resource&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Fiber&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProcessResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; builder&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; withEnvironmentVariables&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;process,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      withWorkingDirectory&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;process,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessBuilder&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;process.command &lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt; process.arguments&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.asJava&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; start&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;.delay&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; JVMRunningProcess&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;builder.start&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.bracketCase &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; runningProcess &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      runningProcess.waitForExit&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;_, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Completed&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;        IO&lt;/span&gt;&lt;span&gt;.unit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;_, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Error&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;reason&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;        IO&lt;/span&gt;&lt;span&gt;.raiseError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;reason&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;runningProcess, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Canceled&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        runningProcess.terminate&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt; &amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;.unit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;.start&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Resource&lt;/span&gt;&lt;span&gt;.make&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;start&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_.cancel&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here &lt;code&gt;withEnvironmentVariables&lt;/code&gt; and &lt;code&gt;withWorkingDirectories&lt;/code&gt; are just helper functions around the JVM &lt;em&gt;process builder&lt;/em&gt;. The more important part is the &lt;em&gt;cancelation&lt;/em&gt; and that we expose it as a &lt;em&gt;resource&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;First we wrap the started JVM process in a &lt;code&gt;JVMRunningProcess&lt;/code&gt; class which really just wraps some of it&#39;s operations in IO operations:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SimpleProcessResult&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; exitCode&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExitCode&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessResult&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; JVMRunningProcess&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; nativeProcess&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; JvmProcess&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RunningProcess&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; isAlive&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Boolean&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;.delay&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;nativeProcess.isAlive&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; kill&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProcessResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;.delay&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;nativeProcess.destroyForcibly&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;gt;&amp;gt;&lt;/span&gt;&lt;span&gt; waitForExit&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; terminate&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProcessResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;.delay&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;nativeProcess.destroy&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;gt;&amp;gt;&lt;/span&gt;&lt;span&gt; waitForExit&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; waitForExit&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProcessResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      exitCode &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;.delay&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;nativeProcess.waitFor&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SimpleProcessResult&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ExitCode&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;exitCode&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we wrap the &lt;em&gt;starting of the process&lt;/em&gt; with &lt;code&gt;bracketCase&lt;/code&gt;, specifying the two cases:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;On normal execution, we &lt;code&gt;waitForExit&lt;/code&gt; for the process to stop and create the &lt;code&gt;ProcessResult&lt;/code&gt; as the result of the bracketed IO operation.&lt;/li&gt;
&lt;li&gt;In the release case, if JVM thrown an exception it is raised to the IO level&lt;/li&gt;
&lt;li&gt;And if it got &lt;em&gt;canceled&lt;/em&gt;, we &lt;code&gt;terminate&lt;/code&gt; the process&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This way the IO cancelation interface gets a simple way to wait for or terminate an executed process. By calling &lt;code&gt;.start&lt;/code&gt; on this bracketed IO operation we move it to a concurrent &lt;em&gt;fiber&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Finally we wrap it in a &lt;code&gt;Resource&lt;/code&gt;, so if the user code starting the process got canceled, it &lt;em&gt;releases the resource&lt;/em&gt; too that ends up &lt;em&gt;terminating&lt;/em&gt; the process, leaving no process leaks. This is something that was missing from the earlier versions of the library.&lt;/p&gt;
&lt;p&gt;To make starting processes more convenient we can create an &lt;strong&gt;extension method&lt;/strong&gt; on the &lt;code&gt;Process&lt;/code&gt; trait:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessOps&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; process&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AnyVal&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; start&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; runner&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessRunner&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Resource&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Fiber&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProcessResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    runner.start&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;process&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;redirection&quot;&gt;Redirection&lt;/h2&gt;
&lt;p&gt;The next step was to implement input/output/error &lt;em&gt;redirection&lt;/em&gt;. In the original &lt;em&gt;prox&lt;/em&gt; library we had two important features, both implemented with type level techniques:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Allow redirection only once per channel&lt;/li&gt;
&lt;li&gt;The redirection source or target was a type class with &lt;em&gt;dependent result types&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To keep the type signatures simpler I decided to work around these by sacrificing some genericity and terseness. Let&#39;s start by defining an interface for &lt;strong&gt;redirecting process output&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectableOutput&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;P&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; connectOutput&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutputRedirection&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;target&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; R&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; outputRedirectionType&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutputRedirectionType&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; P&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is not &lt;em&gt;very&lt;/em&gt; much different than the output redirection operator in the previous &lt;em&gt;prox&lt;/em&gt; versions:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NewOut&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NewOutResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Result&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, _, _, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Redirected&lt;/span&gt;&lt;span&gt;, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;to&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; To&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;     contextOf&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextOf&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;     target&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CanBeProcessOutputTarget&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NewOut&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NewOutResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;     redirectOutput&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectOutput&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NewOut&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NewOutResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Result&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;One of the primary differences is that we don&#39;t allow arbitrary targets just by requiring a &lt;code&gt;CanBeProcessOutput&lt;/code&gt; type class. Instead we can only connect the output to a value of &lt;code&gt;OutputRedirection&lt;/code&gt; which is an ADT:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;sealed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutputRedirection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; StdOut&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutputRedirection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutputFile&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;path&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Path&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;append&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Boolean&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutputRedirection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutputStream&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OR&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;pipe&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Pipe&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;runner&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Stream&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OR&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;chunkSize&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 8192&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutputRedirection&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We still need a type level calculation to extract the result type of the &lt;code&gt;OutputStream&lt;/code&gt; case (which is the &lt;code&gt;OR&lt;/code&gt; type parameter). This extracted by the following trait with the help of the &lt;code&gt;Aux&lt;/code&gt; pattern:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutputRedirectionType&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; runner&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;of&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; R&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;nativeProcess&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; JvmProcess&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;blocker&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Blocker&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;contextShift&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextShift&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The important difference from earlier versions of the library is that this remains completely an implementation detail. &lt;code&gt;OutputRedirectionType&lt;/code&gt; is implemented for all three cases of the &lt;code&gt;OutputRedirection&lt;/code&gt; type and &lt;code&gt;connectOutput&lt;/code&gt; is not even used in the default use cases, only when implementing redirection for something custom.&lt;/p&gt;
&lt;p&gt;Instead the &lt;code&gt;RedirectableOutput&lt;/code&gt; trait itself defines a set of operators and named function versions for redirecting to different targets. With this we loose a general-purpose, type class managed way to redirect to &lt;em&gt;anything&lt;/em&gt; but improve a lot on the usability of the library. All these functions are easily discoverable from the IDE and there would not be any weird implicit resolution errors.&lt;/p&gt;
&lt;p&gt;Let&#39;s see some examples of these functions:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectableOutput&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;P&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;sink&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Pipe&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; P&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; toSink&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;sink&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; toSink&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;sink&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Pipe&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; P&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    connectOutput&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OutputStream&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;sink, &lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;s&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Stream&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; s.compile.drain&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;gt;#&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Monoid&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;pipe&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Pipe&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; P&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; toFoldMonoid&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;pipe&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; toFoldMonoid&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Monoid&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;pipe&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Pipe&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; P&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    connectOutput&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OutputStream&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;pipe, &lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;s&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Stream&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; s.compile.foldMonoid&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;path&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Path&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; P&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; appendToFile&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;path&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; appendToFile&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;path&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Path&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; P&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    connectOutput&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OutputFile&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;path, append &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All of them are just using the &lt;code&gt;connectOutput&lt;/code&gt; function so implementations of the &lt;code&gt;RedirectableOutput&lt;/code&gt; trait need to define that single function to get this capability.&lt;/p&gt;
&lt;p&gt;Note that &lt;code&gt;connectOutput&lt;/code&gt; has a return type of &lt;code&gt;P[O]&lt;/code&gt; instead of being just &lt;code&gt;Process&lt;/code&gt;. This is important for multiple reasons.&lt;/p&gt;
&lt;p&gt;First, in order to actually &lt;em&gt;execute&lt;/em&gt; the output streams, we need to store it somehow in the &lt;code&gt;Process&lt;/code&gt; data type itself. For this reason we add a type parameter to the &lt;code&gt;Process&lt;/code&gt; trait representing the &lt;em&gt;output type&lt;/em&gt; and store the &lt;em&gt;output stream runner function&lt;/em&gt; itself in it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; outputRedirection&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutputRedirection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; runOutputStream&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;JvmProcess&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Blocker&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ContextShift&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that &lt;code&gt;runOutputStream&lt;/code&gt; is actually the &lt;code&gt;OutputRedirectiontype.runner&lt;/code&gt; function, got from the &quot;hidden&quot; type level operation and stored in the process data structure. With this, the &lt;em&gt;process runner&lt;/em&gt; can be extended to pass the started JVM process to this function that sets up the redirection, and then store the result of type &lt;code&gt;O&lt;/code&gt; in &lt;code&gt;ProcessResult[O]&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; start&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;process&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;blocker&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Blocker&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Resource&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Fiber&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProcessResult&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ... process builder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; outputRedirect&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; process.outputRedirection &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;match&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; StdOut&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessBuilder&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Redirect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;INHERIT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutputFile&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;path&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessBuilder&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Redirect&lt;/span&gt;&lt;span&gt;.to&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;path.toFile&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutputStream&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_, _, _&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessBuilder&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Redirect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PIPE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  builder.redirectOutput&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;outputRedirect&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; startProcess&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    nativeProcess &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;.delay&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;builder.start&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    runningOutput &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; process.runOutputStream&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;nativeProcess, blocker, contextShift&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.start&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; JVMRunningProcess&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;nativeProcess, runningOutput&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ... bracketCase, start, Resource.make&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It is also important that this &lt;code&gt;RedirectableOutput&lt;/code&gt; trait is not something all process has: it is a &lt;strong&gt;capability&lt;/strong&gt;, and only processes with unbound output should implement it. This is the new encoding of fixing the three channels of a process. Instead of having three type parameters with &lt;em&gt;phantom types&lt;/em&gt;, now we have a combination of capability traits mixed with the &lt;code&gt;Process&lt;/code&gt; trait, constraining what kind of redirections we can do. As this is not something unbounded and have relatively small number of cases, I chose to implement the combinations by hand, designing it in a way to minimize the redundancy in these implementation classes. This means, in total &lt;strong&gt;8&lt;/strong&gt; classes representing the combinations of bound input, output and error.&lt;/p&gt;
&lt;p&gt;I will demonstrate this with a single example. The &lt;code&gt;Process&lt;/code&gt; constructor now returns a type with everything unbound, represented by having all the redirection capability traits:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; apply&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;command&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;arguments&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; List&lt;/span&gt;&lt;span&gt;.empty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessImpl&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    ProcessImpl&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      command,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      arguments,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      workingDirectory &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      environmentVariables &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Map&lt;/span&gt;&lt;span&gt;.empty,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      removedEnvironmentVariables &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Set&lt;/span&gt;&lt;span&gt;.empty,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      outputRedirection &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; StdOut&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      runOutputStream &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;_, _, _&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;.unit,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      errorRedirection &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; StdOut&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      runErrorStream &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;_, _, _&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;.unit,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      inputRedirection &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; StdIn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessImpl&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; command&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                         override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; arguments&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                         override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; workingDirectory&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Path&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                         override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; environmentVariables&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Map&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                         override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; removedEnvironmentVariables&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Set&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                         override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; outputRedirection&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutputRedirection&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                         override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; runOutputStream&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;java.io.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;InputStream&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Blocker&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ContextShift&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                         override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; errorRedirection&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutputRedirection&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                         override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; runErrorStream&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;java.io.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;InputStream&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Blocker&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ContextShift&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                         override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; inputRedirection&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; InputRedirection&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectableOutput&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProcessImplO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;*&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectableError&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProcessImplE&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;*&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectableInput&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProcessImplI&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; connectOutput&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutputRedirection&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;target&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; R&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; outputRedirectionType&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutputRedirectionType&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessImplO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      ProcessImplO&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        target,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        outputRedirectionType.runner&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;target&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessImplO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                             override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; runOutputStream&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;java.io.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;InputStream&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Blocker&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ContextShift&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;                             //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                            )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectableError&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProcessImplOE&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;*&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectableInput&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProcessImplIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;      //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each implementation class only has the necessary subset of type parameters &lt;code&gt;O&lt;/code&gt; and &lt;code&gt;E&lt;/code&gt; (&lt;code&gt;E&lt;/code&gt; is the error output type), and the &lt;code&gt;I&lt;/code&gt; &lt;code&gt;O&lt;/code&gt; and &lt;code&gt;E&lt;/code&gt; postfixes in the class names represent which channels are &lt;em&gt;bound&lt;/em&gt;. Each redirection leads to a different implementation class with less and less redirection &lt;em&gt;capabilities&lt;/em&gt;. &lt;code&gt;ProcessImplIOE&lt;/code&gt; is the fully bound process.&lt;/p&gt;
&lt;p&gt;This makes all the redirection operators completely type inferable and very pleasant to use for building up concrete process definitions. And we don&#39;t loose the ability to create generic function either. We can do it by requiring redirection capabilities:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; withInput&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;E&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;P&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;E&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;s&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;process&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;E&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectableInput&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;P&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; P&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; input&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Stream&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;This is a test string&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.through&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;text.utf8Encode&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  process &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; input&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we know we want to have a &lt;code&gt;Process&lt;/code&gt; with the &lt;code&gt;RedirectableInput&lt;/code&gt; capability. We also know that by binding the input we get a something without that trait, so we know the result is a process &lt;code&gt;P&lt;/code&gt; but know nothing else about its further capabilities. This is where this solution gets a bit inconvenient, if we want to chain these wrapper functions. To help with it, the library contains &lt;em&gt;type aliases&lt;/em&gt; for the whole redirection capability chain that can be used in these functions. For example:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;/**&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Process with unbound input, output and error streams &lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UnboundProcess&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectableInput&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;UnboundOEProcess&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectableOutput&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;UnboundIEProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;*&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectableError&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;UnboundIOProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;*&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;process-piping&quot;&gt;Process piping&lt;/h2&gt;
&lt;p&gt;The other major feature beside redirection that &lt;em&gt;prox&lt;/em&gt; had is &lt;strong&gt;piping processes together&lt;/strong&gt;, meaning the first process&#39; output gets redirected to the second process&#39; input. Now that we have redesigned processes and redirection capabilities, we can try to implement this on top of them.&lt;/p&gt;
&lt;p&gt;The idea is that when we construct a &lt;em&gt;process group&lt;/em&gt; from a list of &lt;code&gt;Process&lt;/code&gt; instances with the necessary redirection capabilities, this construction could set up the redirection and store the modified processes instead, then running them together. And it can reuse the &lt;code&gt;RedirectableOutput&lt;/code&gt; and &lt;code&gt;RedirectableInput&lt;/code&gt; capabilities to bind the first/last process!&lt;/p&gt;
&lt;p&gt;Let&#39;s again start by defining what we need for the &lt;em&gt;process group&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessGroup&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;E&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessLike&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; firstProcess&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Stream&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;E&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; innerProcesses&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Process&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;UnboundIProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Stream&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;E&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; lastProcess&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;UnboundIProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;E&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; originalProcesses&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;ProcessLike&lt;/code&gt; is a common base trait for &lt;code&gt;Process&lt;/code&gt; and &lt;code&gt;ProcessGroup&lt;/code&gt;. By introducing it, we can change the &lt;code&gt;RedirectableOutput&lt;/code&gt; trait&#39;s self type bounds so it works for both processes and process groups.&lt;/p&gt;
&lt;p&gt;A valid process group always have at least &lt;strong&gt;2&lt;/strong&gt; processes and they get pre-configured during the construction of the group so when they get started, their channels can be joined. This means the group members can be split into three groups:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;first process&lt;/strong&gt; has it&#39;s output redirected to a stream, but &lt;em&gt;running&lt;/em&gt; the stream just returns the stream itself; this way it can be connected to the next process&#39;s input&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;inner processes&lt;/strong&gt; are all having their output redirected in the same way, and it is also a &lt;em&gt;requirement&lt;/em&gt; that these must have their &lt;em&gt;input channel&lt;/em&gt; unbound. This is needed for the operation described above, when we plug the previous process&#39; output into the input&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;last process&lt;/strong&gt; can have its output freely redirected by the user, but it&#39;s &lt;em&gt;input&lt;/em&gt; must be unbound so the previous process can be plugged in&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We also store the &lt;em&gt;original&lt;/em&gt; process values for reasons explained later.&lt;/p&gt;
&lt;p&gt;So as we can see the piping has two stages:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;First we prepare the processes by setting up their output to return an un-executed stream&lt;/li&gt;
&lt;li&gt;And we need a process group specific start function into the &lt;code&gt;ProcessRunner&lt;/code&gt; that plugs everything together&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The first step is performed by the &lt;em&gt;pipe operator&lt;/em&gt; (&lt;code&gt;|&lt;/code&gt;), which is defined on &lt;code&gt;Process&lt;/code&gt; via an extension method to construct group of two processes, and on &lt;code&gt;ProcessGroupImpl&lt;/code&gt; to add more. For simplicity the piping operator is currently not defined on the bound process group types. So it has to be first constructed, and then the redirection set up.&lt;/p&gt;
&lt;p&gt;Let&#39;s see the one that adds one more process to a group:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; pipeInto&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;other&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;UnboundProcess&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;             channel&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Pipe&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessGroupImpl&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; pl1&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; lastProcess.connectOutput&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OutputStream&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;channel, &lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;stream&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Stream&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;.pure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;stream&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  copy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    innerProcesses &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; pl1 &lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt; innerProcesses,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    lastProcess &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; other,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    originalProcesses &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; other &lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt; originalProcesses&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; |&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;other&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;UnboundProcess&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessGroupImpl&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; pipeInto&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;other, identity&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Other than moving processes around in the &lt;code&gt;innerProcesses&lt;/code&gt; and &lt;code&gt;lastProcess&lt;/code&gt;, we also set up the &lt;strong&gt;previous last process&lt;/strong&gt;&#39;s output in the way I described:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It gets redirected to a pipe which is by default &lt;code&gt;identity&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;And it&#39;s &lt;em&gt;runner&lt;/em&gt; instead of actually running the stream, just returns the stream definition&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This way we can write a process group specific start function into the &lt;em&gt;process runner&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; startProcessGroup&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;E&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;processGroup&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessGroup&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;E&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;blocker&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Blocker&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RunningProcessGroup&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;E&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    first &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; startProcess&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;processGroup.firstProcess, blocker&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    firstOutput &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; first.runningOutput.join&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    innerResult &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;processGroup.innerProcesses.isEmpty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      IO&lt;/span&gt;&lt;span&gt;.pure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;List&lt;/span&gt;&lt;span&gt;.empty, firstOutput&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; inner&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; processGroup.innerProcesses.reverse&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      connectAndStartProcesses&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;inner.head, firstOutput, inner.tail, blocker, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;List&lt;/span&gt;&lt;span&gt;.empty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span&gt;inner, lastInput&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; innerResult&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    last &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; startProcess&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;processGroup.lastProcess.connectInput&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;InputStream&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;lastInput, flushChunks &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; false&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, blocker&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    runningProcesses &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; processGroup.originalProcesses.reverse.zip&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;first &lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt; inner&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; :+&lt;/span&gt;&lt;span&gt; last&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.toMap&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; JVMRunningProcessGroup&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;E&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;runningProcesses, last.runningOutput&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where &lt;code&gt;connectAndStartProcesses&lt;/code&gt; is a recursive function that does the same as we do with the first process:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;start it with the &lt;code&gt;startProcess&lt;/code&gt; function (this is the same function we discussed in the first section, that starts &lt;code&gt;Process&lt;/code&gt; values)&lt;/li&gt;
&lt;li&gt;then &quot;join&quot; the output fiber; this completes immediately as it is not really running the output stream just returning it&lt;/li&gt;
&lt;li&gt;we connect the &lt;em&gt;input&lt;/em&gt; of the next process to the previous process&#39; output&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;One thing we did not talk about yet is getting the &lt;strong&gt;results&lt;/strong&gt; of a process group. This is where the old implementation again used some type level techniques and returned a &lt;code&gt;RunningProcess&lt;/code&gt; value with specific per-process output and error types for each member of the group, as a &lt;code&gt;HList&lt;/code&gt; (or converted to a &lt;em&gt;tuple&lt;/em&gt;).&lt;/p&gt;
&lt;p&gt;By making the library a bit more dynamic we can drop this part too. What is that we really want to do with a running process group?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Terminating&lt;/strong&gt; the whole group together. Terminating just one part is something we does not support currently although it would not be hard to add.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Waiting&lt;/strong&gt; for all processes to stop&lt;/li&gt;
&lt;li&gt;Examining the &lt;strong&gt;exit code&lt;/strong&gt; for each member of the group&lt;/li&gt;
&lt;li&gt;Redirecting the &lt;strong&gt;error&lt;/strong&gt; channel of each process to something and getting them in the result&lt;/li&gt;
&lt;li&gt;Redirecting the &lt;strong&gt;input&lt;/strong&gt; of the group&#39;s first process&lt;/li&gt;
&lt;li&gt;Redirecting the &lt;strong&gt;output&lt;/strong&gt; of the group&#39;s last process, and getting it in the result&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The most difficult and primary reason for the &lt;code&gt;HList&lt;/code&gt; in the old version is the error redirection, as it can be done &lt;em&gt;per process&lt;/em&gt;. With some restrictions we can make a reasonable implementation though.&lt;/p&gt;
&lt;p&gt;First, we require that the processes participating in forming a &lt;em&gt;process group&lt;/em&gt; does not have their &lt;em&gt;error channel&lt;/em&gt; bound yet. Then we create a &lt;code&gt;RedirectableErrors&lt;/code&gt; capability that is very similar to the existing &lt;code&gt;RedirectableError&lt;/code&gt; trait, but provides an advanced interface through it&#39;s &lt;code&gt;customizedPerProcess&lt;/code&gt; field:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectableErrors&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;P&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessGroup&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  lazy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; customizedPerProcess&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectableErrors&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;CustomizedPerProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;P&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where the &lt;code&gt;CustomizedPerProcess&lt;/code&gt; interface contains the same redirection functions but accept a function of a &lt;code&gt;Process&lt;/code&gt; as parameter.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; errorsToSink&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;sink&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Pipe&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; P&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; vs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; errorsToSink&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;sinkFn&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Pipe&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; P&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The limitation is that for all process we need to have the same &lt;strong&gt;error result type&lt;/strong&gt; but it still gets a lot of freedom via the advanced interface: we can tag the output with the process and split their processing further in the stream.&lt;/p&gt;
&lt;p&gt;With this choice, we can finally define the result type of the process group too:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessGroupResult&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;E&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; exitCodes&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Map&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ExitCode&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; output&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; O&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; errors&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Map&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;E&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The error results and the exit codes are in a map indexed by the &lt;strong&gt;original process&lt;/strong&gt;. This is the value passed to the piping operator, the one that the user constructing the group has. That&#39;s why in the &lt;code&gt;ProcessGroup&lt;/code&gt; trait we also had to store the original process values.&lt;/p&gt;
&lt;p&gt;As the output of all the inner processes are piped to the next process, we only have to care about the last process&#39; output.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;With a full redesign and making some compromises, we get a library that has a much more readable and easier to maintain code, and an API that is discoverable by the IDE and does not produce any weird error messages on misuse.&lt;/p&gt;
&lt;p&gt;Note that in all the code snippets above I removed the &lt;em&gt;effect abstraction&lt;/em&gt; and just used &lt;code&gt;IO&lt;/code&gt; to make them simpler. The real code of course can be used with any IO library such as ZIO, just like the previous versions.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>prox part 3 - effect abstraction and ZIO</title>
        <published>2019-08-13T00:00:00+00:00</published>
        <updated>2019-08-13T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/prox-3-zio/"/>
        <id>https://blog.vigoo.dev/posts/prox-3-zio/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/prox-3-zio/">&lt;h2 id=&quot;blog-post-series&quot;&gt;Blog post series&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.vigoo.dev/posts/prox-1-types/&quot;&gt;Part 1 - type level programming&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.vigoo.dev/posts/prox-2-io-akkastreams/&quot;&gt;Part 2 - akka streams with cats effect&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.vigoo.dev/posts/prox-3-zio/&quot;&gt;Part 3 - effect abstraction and ZIO&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.vigoo.dev/posts/prox-4-simplify/&quot;&gt;Part 4 - simplified redesign&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;intro&quot;&gt;Intro&lt;/h2&gt;
&lt;p&gt;The &lt;a href=&quot;https://blog.vigoo.dev/posts/prox-3-zio/2019-02-10-prox-1-types.html&quot;&gt;first post&lt;/a&gt; introduced the &lt;em&gt;prox library&lt;/em&gt; and demonstrated the advanced type level programming techniques it uses. Then in the &lt;a href=&quot;https://blog.vigoo.dev/posts/prox-3-zio/2019-03-07-prox-2-io-akkastreams.html&quot;&gt;second part&lt;/a&gt; of this series we experimented with replacing the &lt;em&gt;streaming library&lt;/em&gt; from &lt;a rel=&quot;external&quot; href=&quot;https://fs2.io/&quot;&gt;fs2&lt;/a&gt; to &lt;a rel=&quot;external&quot; href=&quot;https://doc.akka.io/docs/akka/2.5/stream/&quot;&gt;Akka Streams&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In both cases the library used &lt;a rel=&quot;external&quot; href=&quot;https://typelevel.org/cats-effect/&quot;&gt;cats-effect&lt;/a&gt; for describing side effects. But it did not really take advantage of &lt;em&gt;cats-effect&lt;/em&gt;&#39;s effect abstraction: it explicitly defined everything to be a computation in &lt;a rel=&quot;external&quot; href=&quot;https://typelevel.org/cats-effect/datatypes/io.html&quot;&gt;&lt;code&gt;IO&lt;/code&gt;&lt;/a&gt;, cats-effect&#39;s implementation of describing effectful computations.&lt;/p&gt;
&lt;p&gt;But we can do better! By not relying on &lt;code&gt;IO&lt;/code&gt; but the various type classes the &lt;em&gt;cats-effect&lt;/em&gt; library provides we can make &lt;em&gt;prox&lt;/em&gt; work with any kind of effect library out of the box. One such example is &lt;a rel=&quot;external&quot; href=&quot;https://github.com/zio/zio&quot;&gt;ZIO&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;effect-abstraction&quot;&gt;Effect abstraction&lt;/h2&gt;
&lt;p&gt;Let&#39;s see an example of how &lt;code&gt;IO&lt;/code&gt; used to be used in the library! The following function is in the &lt;code&gt;Start&lt;/code&gt; type class, and it starts a process or piped process group:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; apply&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;process&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PN&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;dontStartOutput&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Boolean&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; false&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;blocker&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Blocker&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;         (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; contextShift&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextShift&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RunningProcesses&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can observe two things here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The function returns an effectful computation in &lt;code&gt;IO&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;An implicit &lt;em&gt;context shifter&lt;/em&gt; is needed by the implementations which are calling some streaming functions needing it.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To make it independent of the effect library implementation we have to get rid of &lt;code&gt;IO&lt;/code&gt; and use a generic type instead, let&#39;s call it &lt;code&gt;F&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; apply&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;process&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PN&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;          dontStartOutput&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Boolean&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; false&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;          blocker&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Blocker&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;         (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;          concurrent&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Concurrent&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;          contextShift&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextShift&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RunningProcesses&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Beside using &lt;code&gt;F&lt;/code&gt; instead of &lt;code&gt;IO&lt;/code&gt; everywhere we also have a new requirement, our context type (&lt;code&gt;F&lt;/code&gt;) have to have an implementation of the &lt;a rel=&quot;external&quot; href=&quot;https://typelevel.org/cats-effect/typeclasses/concurrent.html&quot;&gt;&lt;code&gt;Concurrent&lt;/code&gt;&lt;/a&gt; type class.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Cats-effect&lt;/em&gt; defines a hierarchy of type classes to deal with effectful computations. At the time of writing it looks like this:
&lt;img src=&quot;https://typelevel.org/cats-effect/img/cats-effect-3.0-hierarchy.svg&quot; alt=&quot;Diagram of the cats-effect typeclass hierarchy&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;p&gt;Read the &lt;a rel=&quot;external&quot; href=&quot;https://typelevel.org/cats-effect/typeclasses/&quot;&gt;official documentation&lt;/a&gt; for more information.&lt;/p&gt;
&lt;p&gt;Prox is based on the &lt;code&gt;ProcessNode&lt;/code&gt; type which has two implementations, a single &lt;code&gt;Process&lt;/code&gt; or a set of processes piped together to a &lt;code&gt;PipedProcess&lt;/code&gt;. Because these types store their I/O redirection within themselves, they also have to be enriched with a context type parameter.&lt;/p&gt;
&lt;p&gt;For example &lt;code&gt;Process&lt;/code&gt; will look like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OutResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ErrResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IRS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ORS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; command&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; arguments&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; workingDirectory&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Path&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; inputSource&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessInputSource&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; outputTarget&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessOutputTarget&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OutResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; errorTarget&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessErrorTarget&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ErrResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; environmentVariables&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Map&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; removedEnvironmentVariables&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Set&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IRS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ORS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The context parameter (&lt;code&gt;F&lt;/code&gt;) is needed because the &lt;em&gt;input source&lt;/em&gt; and &lt;em&gt;output target&lt;/em&gt; are all representing effectful code such as writing to the standard output, reading from a file, or passing data through concurrent streams.&lt;/p&gt;
&lt;p&gt;Let&#39;s see some examples of how the abstract types of &lt;em&gt;cats-effect&lt;/em&gt; can be used to describe the computation, when we cannot rely on &lt;code&gt;IO&lt;/code&gt; itself!&lt;/p&gt;
&lt;p&gt;The most basic operation is to &lt;em&gt;delay the execution&lt;/em&gt; of some code that does not use the effect abstractions. This is how we wrap the Java process API, for example.&lt;/p&gt;
&lt;p&gt;While with the original implementation of &lt;em&gt;prox&lt;/em&gt; it was done by using the &lt;code&gt;IO&lt;/code&gt; constructor:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    systemProcess.isAlive&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;with an arbitrary &lt;code&gt;F&lt;/code&gt; we only need to require that it has an implementation of the &lt;code&gt;Sync&lt;/code&gt; type class:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; WrappedProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Sync&lt;/span&gt;&lt;span&gt;,  &lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and then use the &lt;code&gt;delay&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Sync&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.delay &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    systemProcess.isAlive&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Similarily the &lt;code&gt;Concurrent&lt;/code&gt; type class can be used to start a concurrent computation on a &lt;em&gt;fiber&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Concurrent&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.start&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;stream.compile.toVector&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;type-level&quot;&gt;Type level&lt;/h2&gt;
&lt;p&gt;This would be it - except that we need one more thing because of the type level techniques described in the &lt;a href=&quot;https://blog.vigoo.dev/posts/prox-3-zio/2019-02-10-prox-1-types.html&quot;&gt;first post&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To understand the problem, let&#39;s see how the &lt;em&gt;output redirection&lt;/em&gt; operator works. It is implemented as an &lt;em&gt;extension method&lt;/em&gt; on the &lt;code&gt;ProcessNode&lt;/code&gt; type:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNodeOutputRedirect&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, _, _, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NotRedirected&lt;/span&gt;&lt;span&gt;, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;processNode&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PN&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NewOut&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NewOutResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Result&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, _, _, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Redirected&lt;/span&gt;&lt;span&gt;, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;to&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; To&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;     target&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CanBeProcessOutputTarget&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NewOut&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NewOutResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;     redirectOutput&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectOutput&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NewOut&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NewOutResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Result&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      redirectOutput&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;processNode, to&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This extension method basically just finds the appropriate type class implementations and then call it to alter the process node to register the output redirection:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;we are redirecting the output of &lt;code&gt;processNode&lt;/code&gt; (of type &lt;code&gt;PN&lt;/code&gt;) to &lt;code&gt;to&lt;/code&gt; (of type &lt;code&gt;To&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;target&lt;/code&gt; is the &lt;code&gt;CanBeProcessOutputTarget&lt;/code&gt; implementation, containing the actual code to set up the redirection&lt;/li&gt;
&lt;li&gt;&lt;code&gt;redirectOutput&lt;/code&gt; is the process node type specific implementation of the &lt;code&gt;RedirectOutput&lt;/code&gt; interface, knowing how to set up the redirection of a &lt;code&gt;Process&lt;/code&gt; or a &lt;code&gt;PipedProcess&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This code would compile, but we won&#39;t be able to use it. For example for the following code:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;running &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;List&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Hello world!&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span&gt; tempFile.toPath&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.start&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;blocker&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It fails with not being able to resolve the implicits correctly. The exact error of course depends much on the context but one example for the above line could be:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[error] prox/src/test/scala/io/github/vigoo/prox/ProcessSpecs.scala:95:63: diverging implicit expansion for type cats.effect.Concurrent[F]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[error] starting with method catsIorTConcurrent in object Concurrent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[error]         running &amp;lt;- (Process[IO](&amp;quot;echo&amp;quot;, List(&amp;quot;Hello world!&amp;quot;)) &amp;gt; tempFile.toPath).start(blocker)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This does not really help understanding the real problem though. As we have seen earlier, in this library the &lt;code&gt;Process&lt;/code&gt; types have to be parameterized with the context as well, because they store their redirection logic within themselves. That&#39;s why we specify it explicitly in the example to be &lt;code&gt;IO&lt;/code&gt;: &lt;code&gt;Process[IO](...)&lt;/code&gt;. What we would expect is that by tying &lt;code&gt;F[_]&lt;/code&gt; to &lt;code&gt;IO&lt;/code&gt; at the beginning, all the subsequent operations such as the &lt;code&gt;&amp;gt;&lt;/code&gt; redirection would respect this and the context gets inferred to be &lt;code&gt;IO&lt;/code&gt; everywhere in the expression.&lt;/p&gt;
&lt;p&gt;The compiler cannot do this. If we check the definition of &lt;code&gt;&amp;gt;&lt;/code&gt; again, you can see that there is no connection expressed between the type &lt;code&gt;PN&lt;/code&gt; (the actual process node type) and &lt;code&gt;F&lt;/code&gt; which is used as a type parameter for the implicit parameters.&lt;/p&gt;
&lt;p&gt;The fix is to link the two, and we have a technique exactly for this that I described earlier: the &lt;em&gt;aux pattern&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;First let&#39;s write some code that, in compile time, can &quot;extract&quot; the context type from a process node type:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextOf&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Context&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextOf&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextOf&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Context&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; apply&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, _, _, _, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; contextOf&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextOf&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; contextOf&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; contextOfProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OutResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ErrResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IRS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ORS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OutResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ErrResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IRS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ORS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextOf&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OutResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ErrResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IRS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ORS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Context&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; contextOfPipedProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  PN1&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, _, _, _, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  PN2&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, _, _, _, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  IRS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ORS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PipedProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IRS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ORS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextOf&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PipedProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IRS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ORS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Context&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Both &lt;code&gt;Process&lt;/code&gt; and &lt;code&gt;PipedProcess&lt;/code&gt; have the context as their first type parameter. By creating the &lt;code&gt;ContextOf&lt;/code&gt; type class and the corresponding &lt;code&gt;Aux&lt;/code&gt; type we can extend the &lt;code&gt;&amp;gt;&lt;/code&gt; operator to &lt;em&gt;require&lt;/em&gt; such a connection (a way to get a &lt;code&gt;F[_]&lt;/code&gt; context out of a type &lt;code&gt;PN&lt;/code&gt;) in compile time, and with the aux pattern it unifies the type parameters and the context type gets &lt;em&gt;chained&lt;/em&gt; through all the subsequent calls as we desired:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NewOut&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NewOutResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Result&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, _, _, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Redirected&lt;/span&gt;&lt;span&gt;, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;to&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; To&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;     contextOf&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextOf&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;     target&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CanBeProcessOutputTarget&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NewOut&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NewOutResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;     redirectOutput&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectOutput&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NewOut&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NewOutResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Result&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      redirectOutput&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;processNode, to&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;zio&quot;&gt;ZIO&lt;/h2&gt;
&lt;p&gt;Now that everything is in place, we can try out whether &lt;em&gt;prox&lt;/em&gt; is really working with other effect libraries such as &lt;a rel=&quot;external&quot; href=&quot;https://github.com/zio/zio&quot;&gt;ZIO&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;ZIO&lt;/em&gt; has a compatibility layer for &lt;em&gt;cats-effect&lt;/em&gt;. It&#39;s the implementation of the type classes cats-effect provides. It is in an extra library called &lt;a rel=&quot;external&quot; href=&quot;https://github.com/zio/interop-cats&quot;&gt;zio-interop-cats&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For running processes with &lt;em&gt;prox&lt;/em&gt; we can use the following variants of the &lt;code&gt;ZIO&lt;/code&gt; type:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;RIO[-R, +A]&lt;/code&gt; which is an alias for &lt;code&gt;ZIO[R, scala.Throwable, A]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;or &lt;code&gt;Task[A]&lt;/code&gt; which is an alias for &lt;code&gt;ZIO[scala.Any, scala.Throwable, A]&lt;/code&gt; if we don&#39;t take advantage of the environment parameter &lt;code&gt;R&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This in fact assuming the correct context only means switching &lt;code&gt;IO&lt;/code&gt; to &lt;code&gt;RIO&lt;/code&gt; or &lt;code&gt;Task&lt;/code&gt; in the type parameter for &lt;code&gt;Process&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span&gt; zio&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;interop&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;catz&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Blocker&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Console&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.use &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; blocker &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;      //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; console.putStrLn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Starting external process...&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      _ &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Task&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;List&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Hello world!&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span&gt; tempFile.toPath&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.start&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;blocker&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;      //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span&gt; ()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A nice way to have everything set up for this is to use the interop library&#39;s &lt;a rel=&quot;external&quot; href=&quot;https://zio.dev/docs/interop/interop_catseffect#cats-app&quot;&gt;&lt;code&gt;CatsApp&lt;/code&gt;&lt;/a&gt; trait as an entrypoint for the application.&lt;/p&gt;
&lt;p&gt;This brings all the necessary implicits in scope and requires you to implement the following function as the entry point of the application:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;args&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ZIO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Environment&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>prox part 2 - akka streams with cats effect</title>
        <published>2019-03-07T00:00:00+00:00</published>
        <updated>2019-03-07T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/prox-2-io-akkastreams/"/>
        <id>https://blog.vigoo.dev/posts/prox-2-io-akkastreams/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/prox-2-io-akkastreams/">&lt;h2 id=&quot;blog-post-series&quot;&gt;Blog post series&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.vigoo.dev/posts/prox-1-types/&quot;&gt;Part 1 - type level programming&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.vigoo.dev/posts/prox-2-io-akkastreams/&quot;&gt;Part 2 - akka streams with cats effect&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.vigoo.dev/posts/prox-3-zio/&quot;&gt;Part 3 - effect abstraction and ZIO&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.vigoo.dev/posts/prox-4-simplify/&quot;&gt;Part 4 - simplified redesign&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;intro&quot;&gt;Intro&lt;/h2&gt;
&lt;p&gt;In the previous post we have seen how &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/prox&quot;&gt;prox&lt;/a&gt; applies advanced type level programming techniques to express executing external system processes. The input and output of these processes can be connected to &lt;strong&gt;streams&lt;/strong&gt;. The current version of &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/prox&quot;&gt;prox&lt;/a&gt; uses the &lt;a rel=&quot;external&quot; href=&quot;https://fs2.io/&quot;&gt;fs2&lt;/a&gt; library to describe these streams, and &lt;a rel=&quot;external&quot; href=&quot;https://typelevel.org/cats-effect/&quot;&gt;cats-effect&lt;/a&gt; as an &lt;strong&gt;IO&lt;/strong&gt; abstraction, allowing it to separate the specification of a process pipeline from its actual execution.&lt;/p&gt;
&lt;p&gt;In this post we will keep &lt;a rel=&quot;external&quot; href=&quot;https://typelevel.org/cats-effect/&quot;&gt;cats-effect&lt;/a&gt; but replace &lt;a rel=&quot;external&quot; href=&quot;https://fs2.io/&quot;&gt;fs2&lt;/a&gt; with the stream library of the Akka toolkit, &lt;a rel=&quot;external&quot; href=&quot;https://doc.akka.io/docs/akka/2.5/stream/&quot;&gt;Akka Streams&lt;/a&gt;. This will be a hybrid solution, as Akka Streams is not using any kind of IO abstraction, unlike &lt;a rel=&quot;external&quot; href=&quot;https://fs2.io/&quot;&gt;fs2&lt;/a&gt; which is implemented on top of &lt;a rel=&quot;external&quot; href=&quot;https://typelevel.org/cats-effect/&quot;&gt;cats-effect&lt;/a&gt;. We will experiment with implementing &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/prox&quot;&gt;prox&lt;/a&gt; purely with the &lt;em&gt;Akka&lt;/em&gt; libraries in a future post.&lt;/p&gt;
&lt;h2 id=&quot;replacing-fs2-with-akka-streams&quot;&gt;Replacing fs2 with Akka Streams&lt;/h2&gt;
&lt;p&gt;We start by removing the &lt;a rel=&quot;external&quot; href=&quot;https://fs2.io/&quot;&gt;fs2&lt;/a&gt; dependency and adding &lt;em&gt;Akka Streams&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-1&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;co.fs2&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; %%&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;fs2-core&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1.0.3&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-1&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;co.fs2&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; %%&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;fs2-io&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1.0.3&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;com.typesafe.akka&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; %%&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;akka-stream&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;2.5.20&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we have to change all the &lt;em&gt;fs2&lt;/em&gt; types used in the codebase to the matching &lt;em&gt;Akka Streams&lt;/em&gt; types. The following table describe these pairs:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;fs2&lt;/th&gt;&lt;th&gt;Akka Streams&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;Stream[IO, O]&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;Source[O, Any]&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;Pipe[IO, I, O]&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;Flow[I, O, Any]&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;Sink[IO, O]&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;Sink[O, Future[Done]&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Another small difference that requires changing a lot of our functions is the &lt;em&gt;implicit context&lt;/em&gt; these streaming solutions require.&lt;/p&gt;
&lt;p&gt;With the original implementation it used to be:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;an implicit &lt;code&gt;ContextShift[IO]&lt;/code&gt; instance&lt;/li&gt;
&lt;li&gt;and an explicitly passed &lt;em&gt;blocking execution context&lt;/em&gt; of type &lt;code&gt;ExecutionContext&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We can treat the blocking execution context as part of the implicit context for &lt;em&gt;prox&lt;/em&gt; too, and could refactor the library to pass both of them wrapped together within a context object.&lt;/p&gt;
&lt;p&gt;Let&#39;s see what we need for the &lt;em&gt;Akka Streams&lt;/em&gt; based implementation!&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;an implicit &lt;code&gt;ContextShift[IO]&lt;/code&gt; is &lt;em&gt;still needed&lt;/em&gt; because we are still using &lt;code&gt;cats-effect&lt;/code&gt; as our IO abstraction&lt;/li&gt;
&lt;li&gt;The blocking execution context however was only used for passing it to &lt;em&gt;fs2&lt;/em&gt;, so we can remove that&lt;/li&gt;
&lt;li&gt;And for &lt;em&gt;Akka Streams&lt;/em&gt; we will need an execution context of type &lt;code&gt;ExecutionContext&lt;/code&gt; and also a &lt;code&gt;Materializer&lt;/code&gt;. The materializer is used by &lt;em&gt;Akka Streams&lt;/em&gt; to execute blueprints of streams. The usual implementation is &lt;code&gt;ActorMaterializer&lt;/code&gt; which does that by spawning actors implementing the stream graph.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So for example the &lt;code&gt;start&lt;/code&gt; extension method, is modified like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-1&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; start&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RP&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;blockingExecutionContext&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExecutionContext&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;               (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; start&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Start&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RP&lt;/span&gt;&lt;span&gt;, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;                contextShift&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextShift&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RP&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; start&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RP&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;               (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; start&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Start&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RP&lt;/span&gt;&lt;span&gt;, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;                contextShift&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextShift&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;                materializer&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Materializer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;                executionContext&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExecutionContext&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RP&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It turns out that there is one more minor difference that needs changes in the internal type signatures.&lt;/p&gt;
&lt;p&gt;In &lt;em&gt;Akka Streams&lt;/em&gt; byte streams are represented by not streams of element type &lt;code&gt;Byte&lt;/code&gt;. like in &lt;em&gt;fs2&lt;/em&gt;, but streams of &lt;em&gt;chunks&lt;/em&gt; called &lt;code&gt;ByteString&lt;/code&gt;s. So everywhere we used &lt;code&gt;Byte&lt;/code&gt; as element type, such as on the process boundaries, we now simply have to use &lt;code&gt;ByteStrings&lt;/code&gt;, for example:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-1&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; apply&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;from&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PN1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;to&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PN2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;via&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Pipe&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ResultProcess&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; apply&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;from&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PN1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;to&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PN2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;via&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Flow&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ByteString&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ByteString&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ResultProcess&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Another thing to notice is that &lt;em&gt;fs2&lt;/em&gt; had a type parameter for passing the &lt;code&gt;IO&lt;/code&gt; monad to run on. As I wrote earlier, &lt;em&gt;Akka Streams&lt;/em&gt; does not depend on such abstractions, so this parameter is missing. On the other hand, it has a third type parameter which is set in the above example to &lt;code&gt;Any&lt;/code&gt;. This parameter is called &lt;code&gt;Mat&lt;/code&gt; and represents the type of the value the flow will materialize to. At this point we don&#39;t care about it so we set it to &lt;code&gt;Any&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Let&#39;s take a look of the &lt;code&gt;connect&lt;/code&gt; function of the &lt;code&gt;ProcessIO&lt;/code&gt; trait. With &lt;em&gt;fs2&lt;/em&gt; the &lt;code&gt;InputStreamingSource&lt;/code&gt; is implemented like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; InputStreamingSource&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;source&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Source&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ByteString&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessInputSource&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; toRedirect&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Redirect&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Redirect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PIPE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; connect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;systemProcess&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; lang.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Process&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;blockingExecutionContext&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExecutionContext&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; contextShift&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextShift&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Stream&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        source.observe&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            io.writeOutputStream&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;                IO&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; systemProcess.getOutputStream &lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                closeAfterUse &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                blockingExecutionContext &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; blockingExecutionContext&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;stream&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Stream&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; contextShift&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextShift&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Fiber&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;        Concurrent&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.start&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;stream.compile.drain&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We have a &lt;code&gt;source&lt;/code&gt; stream and during the setup of the process graph, when the system process has been already created, we have to set up the redirection of this source stream to this process. This is separated to a &lt;code&gt;connect&lt;/code&gt; and a &lt;code&gt;run&lt;/code&gt; step:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;connect&lt;/code&gt; step creates an &lt;em&gt;fs2 stream&lt;/em&gt; that observers the source stream and sends each byte to the system process&#39;s standard input. This just &lt;strong&gt;defines&lt;/strong&gt; this stream, and returns it as a pure functional value.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;run&lt;/code&gt; step on the other hand has the result type &lt;code&gt;IO[Fiber[IO, Unit]]&lt;/code&gt;. It &lt;strong&gt;defines&lt;/strong&gt; the effect of starting a new thread and running the stream on it.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In the case of &lt;em&gt;fs2&lt;/em&gt; we can be sure that the &lt;code&gt;source.observe&lt;/code&gt; function is pure just by checking it&#39;s type signature:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; observe&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;p&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Pipe&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; F&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Concurrent&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Stream&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;F&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All side-effecting functions in &lt;em&gt;fs2&lt;/em&gt; are defined as &lt;code&gt;IO&lt;/code&gt; functions, so we simply know that this one is not among them, and that&#39;s why the &lt;code&gt;connect&lt;/code&gt; was a pure, non-&lt;code&gt;IO&lt;/code&gt; function in the original implementation. With &lt;em&gt;Akka Streams&lt;/em&gt; we don&#39;t have any information about this encoded in the type system. We use the &lt;code&gt;source.alsoTo&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; alsoTo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;that&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Graph&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;SinkShape&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Repr&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which is actually also pure (only creating a blueprint of the graph to be executed), so we can safely replace the implementation to this in the &lt;em&gt;Akka Streams&lt;/em&gt; version:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; InputStreamingSource&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;source&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Source&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ByteString&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessInputSource&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; toRedirect&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Redirect&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Redirect&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PIPE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; connect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;systemProcess&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; lang.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Process&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; contextShift&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextShift&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Source&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ByteString&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        source.alsoTo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;fromOutputStream&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; systemProcess.getOutputStream, autoFlush &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;stream&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Source&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ByteString&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; contextShift&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextShift&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;                     materializer&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Materializer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;                     executionContext&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExecutionContext&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Fiber&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;        Concurrent&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.start&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;.async &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; finish &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            stream.runWith&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Sink&lt;/span&gt;&lt;span&gt;.ignore&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.onComplete &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Success&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Done&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; finish&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Right&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Failure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;reason&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; finish&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Left&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;reason&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The implementation of &lt;code&gt;run&lt;/code&gt; above is a nice example of how we can integrate asynchronous operations not implemented with &lt;code&gt;cats-effect&lt;/code&gt; to an &lt;code&gt;IO&lt;/code&gt; based program. With &lt;code&gt;IO.async&lt;/code&gt; we define how to start the asynchronous operation (in this case running the &lt;em&gt;Akka stream&lt;/em&gt;) and we get a callback function, &lt;code&gt;finish&lt;/code&gt; to be called when the asynchronous operation ends. The stream here &lt;em&gt;materializes&lt;/em&gt; to a &lt;code&gt;Future[T]&lt;/code&gt; value, so we can use it&#39;s &lt;code&gt;onComplete&lt;/code&gt; function to notify the IO system about the finished stream. The &lt;code&gt;IO&lt;/code&gt; value returned by &lt;code&gt;IO.async&lt;/code&gt; represents the whole asynchronous operation, it returns it&#39;s final result when the callback is called, and &quot;blocks&quot; the program flow until it is done. This does not mean actually blocking a thread; but the next IO function will be executed only when it finished running (as it&#39;s type is &lt;code&gt;IO[A]&lt;/code&gt;). That is not what we need here, so we use &lt;code&gt;Concurrent[IO].start&lt;/code&gt; to put this &lt;code&gt;IO&lt;/code&gt; action on a separate &lt;em&gt;fiber&lt;/em&gt;. This way all streams involved in the process graph will be executing in parallel.&lt;/p&gt;
&lt;h3 id=&quot;calculating-the-result&quot;&gt;Calculating the result&lt;/h3&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/prox&quot;&gt;prox&lt;/a&gt; supports multiple ways to calculate a result of running a process graph:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If the target is a &lt;code&gt;Sink&lt;/code&gt;, the result type is &lt;code&gt;Unit&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;If the pipe&#39;s output is &lt;code&gt;Out&lt;/code&gt; and there is a &lt;code&gt;Monoid&lt;/code&gt; instance for &lt;code&gt;Out&lt;/code&gt;, the stream is folded into an &lt;code&gt;Out&lt;/code&gt; value&lt;/li&gt;
&lt;li&gt;Otherwise if the pipe&#39;s output is &lt;code&gt;Out&lt;/code&gt;, the result type will be &lt;code&gt;Vector[Out]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These cases can be enforced by the &lt;code&gt;Drain&lt;/code&gt;, &lt;code&gt;ToVector&lt;/code&gt; and &lt;code&gt;Fold&lt;/code&gt; wrapper classes.&lt;/p&gt;
&lt;p&gt;Let&#39;s see how we can implement them with &lt;em&gt;Akka Streams&lt;/em&gt; compared to &lt;em&gt;fs2&lt;/em&gt;.&lt;/p&gt;
&lt;h4 id=&quot;drain-sink&quot;&gt;Drain sink&lt;/h4&gt;
&lt;p&gt;The sink version was implemented like this with &lt;em&gt;fs2&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Concurrent&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.start&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;stream.compile.drain&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;.compile&lt;/code&gt; gets an interface that can be used to convert the stream to a &lt;code&gt;IO[A]&lt;/code&gt; value in multiple ways.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.drain&lt;/code&gt; is one of them. It runs the stream but ignores its elements, having a result type of &lt;code&gt;IO[Unit]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We want to run this concurrently with the other streams so we move it to a &lt;em&gt;fiber&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With &lt;em&gt;Akka Streams&lt;/em&gt; there is one big difference. In &lt;em&gt;fs2&lt;/em&gt; the sink is represented as a &lt;code&gt;Pipe[F, E, Unit]&lt;/code&gt;, so we could treat it in the same way as other stream segments. In this case the &lt;code&gt;Sink&lt;/code&gt; is not a &lt;code&gt;Flow&lt;/code&gt;, so we do a trick to keep the interface as close to the original one as possible:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;create&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;sink&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Sink&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ByteString&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutputStreamingTarget&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Flow&lt;/span&gt;&lt;span&gt;.fromFunction&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;identity&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                                                with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessOutputTarget&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ByteString&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;stream&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Source&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ByteString&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Any&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; contextShift&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ContextShift&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;                    materializer&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Materializer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;                    executionContext&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ExecutionContext&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Fiber&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;R&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Concurrent&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.start&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;.async &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; complete &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        stream.runWith&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;sink&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.onComplete &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Success&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; complete&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Right&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Failure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;reason&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; complete&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Left&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;reason&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The trick is that we create the &lt;code&gt;OutputStreamingTarget&lt;/code&gt; with an identity flow, and only use the &lt;code&gt;Sink&lt;/code&gt; when we actually run the stream, passing it to the &lt;code&gt;runWith&lt;/code&gt; function. This materializes the stream into a &lt;code&gt;Future[Done]&lt;/code&gt; value, that we can tie back to our &lt;code&gt;IO&lt;/code&gt; system with &lt;code&gt;IO.async&lt;/code&gt; as I already described it.&lt;/p&gt;
&lt;h4 id=&quot;combine-with-monoid&quot;&gt;Combine with Monoid&lt;/h4&gt;
&lt;p&gt;When the element type is a &lt;em&gt;monoid&lt;/em&gt; we can fold it into a single value. &lt;em&gt;Fs2&lt;/em&gt; directly supports this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Concurrent&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.start&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;stream.compile.foldMonoid&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Akka Streams&lt;/em&gt; does not use cats type classes, but it also has a way to &lt;em&gt;fold&lt;/em&gt; the stream, so we can easily implement it using the &lt;em&gt;monoid instance&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Concurrent&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.start&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;.async &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; complete &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    stream.runFold&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;monoid.empty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;monoid.combine&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.onComplete &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Success&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; complete&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Right&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Failure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;reason&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; complete&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Left&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;reason&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;vector-of-elements&quot;&gt;Vector of elements&lt;/h4&gt;
&lt;p&gt;Finally let&#39;s see the version that keeps all the stream elements in a vector as a result:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Concurrent&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.start&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;stream.compile.toVector&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With &lt;em&gt;Akka Streams&lt;/em&gt; we can do it by running the stream into a &lt;em&gt;sink&lt;/em&gt; created for this, &lt;code&gt;Sink.seq&lt;/code&gt;. It materializes into a &lt;code&gt;Future[Seq[T]]&lt;/code&gt; value that holds all the elements
of the executed stream:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Concurrent&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;.start&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;.async &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; complete &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    stream.runWith&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Sink&lt;/span&gt;&lt;span&gt;.seq&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.onComplete &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Success&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; complete&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Right&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;value.toVector&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Failure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;reason&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; complete&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Left&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;reason&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;testing&quot;&gt;Testing&lt;/h3&gt;
&lt;p&gt;At this point the only remaining thing is to modify the tests too. One of the more complex examples is the &lt;code&gt;customProcessPiping&lt;/code&gt; test case. With &lt;em&gt;fs2&lt;/em&gt; it takes advantage of some &lt;em&gt;text processing&lt;/em&gt; pipe elements coming with the library:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; customPipe&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Pipe&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;s&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Stream&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .through&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;text.utf8Decode&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .through&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;text.lines&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_.split&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.toVector&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;v &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; v.map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_ &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; !!!&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.mkString&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .intersperse&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .through&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;text.utf8Encode&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; proc&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;List&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;This is a test string&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            .via&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;customPipe&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            .to&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Process&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;wc&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;List&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span&gt; text.utf8Decode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are similar tools in &lt;em&gt;Akka Streams&lt;/em&gt; to express this in the &lt;code&gt;Framing&lt;/code&gt; module:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; customPipe&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Framing&lt;/span&gt;&lt;span&gt;.delimiter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      delimiter &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ByteString&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      maximumFrameLength &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 10000&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      allowTruncation &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt;.map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_.utf8String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     .map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_.split&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.toVector&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     .map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;v &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; v.map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_ &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; !!!&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.mkString&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     .intersperse&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     .map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ByteString&lt;/span&gt;&lt;span&gt;.apply&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; proc&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;List&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;This is a test string&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            .via&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;customPipe&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            .to&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Process&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;wc&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;List&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;-w&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span&gt; utf8Decode&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where &lt;code&gt;utf8Decode&lt;/code&gt; is a helper sink defined as:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; utf8Decode&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Sink&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ByteString&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Flow&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ByteString&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .reduce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_ &lt;/span&gt;&lt;span&gt;++&lt;/span&gt;&lt;span&gt; _&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_.utf8String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .toMat&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Sink&lt;/span&gt;&lt;span&gt;.head&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Keep&lt;/span&gt;&lt;span&gt;.right&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;First it concatenates the &lt;code&gt;ByteString&lt;/code&gt; chunks, then simply calls &lt;code&gt;.utf8String&lt;/code&gt; on the result.&lt;/p&gt;
&lt;h2 id=&quot;final-thoughts&quot;&gt;Final thoughts&lt;/h2&gt;
&lt;p&gt;We have seen that it is relatively easy to replace the stream library in &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/prox&quot;&gt;prox&lt;/a&gt; without changing it&#39;s interface much, if we keep &lt;a rel=&quot;external&quot; href=&quot;https://typelevel.org/cats-effect/&quot;&gt;cats-effect&lt;/a&gt; for expressing the effectful computations. The complete working example is available on the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/prox/compare/akka-streams&quot;&gt;&lt;code&gt;akka-streams&lt;/code&gt; branch&lt;/a&gt;.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>prox part 1 - type level programming</title>
        <published>2019-02-10T00:00:00+00:00</published>
        <updated>2019-02-10T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/prox-1-types/"/>
        <id>https://blog.vigoo.dev/posts/prox-1-types/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/prox-1-types/">&lt;h2 id=&quot;blog-post-series&quot;&gt;Blog post series&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.vigoo.dev/posts/prox-1-types/&quot;&gt;Part 1 - type level programming&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.vigoo.dev/posts/prox-2-io-akkastreams/&quot;&gt;Part 2 - akka streams with cats effect&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.vigoo.dev/posts/prox-3-zio/&quot;&gt;Part 3 - effect abstraction and ZIO&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.vigoo.dev/posts/prox-4-simplify/&quot;&gt;Part 4 - simplified redesign&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;intro&quot;&gt;Intro&lt;/h2&gt;
&lt;p&gt;I started writing &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/prox&quot;&gt;prox&lt;/a&gt; at the end of 2017 for two reasons. First, I never liked any of the existing solutions for running external processes and capture their input/output streams. And I just returned from the &lt;a rel=&quot;external&quot; href=&quot;https://scala.io/&quot;&gt;scala.io conference&lt;/a&gt; full of inspiration; I wanted to try out some techniques and libraries and this seemed to be a nice small project to do so.&lt;/p&gt;
&lt;p&gt;Since then, &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/prox&quot;&gt;prox&lt;/a&gt; has been proved to be useful, we are using it at &lt;a rel=&quot;external&quot; href=&quot;https://prezi.com/&quot;&gt;Prezi&lt;/a&gt; in all our Scala projects where we have to deal with external processes. The last stable version was created last October, after &lt;a rel=&quot;external&quot; href=&quot;https://typelevel.org/cats-effect/&quot;&gt;cats-effect 1.0&lt;/a&gt; and &lt;a rel=&quot;external&quot; href=&quot;https://fs2.io/&quot;&gt;fs2 1.0&lt;/a&gt; was released.&lt;/p&gt;
&lt;p&gt;This is the first part of a series of blog posts dedicated to this library. In the first one I&#39;m going to talk about &lt;a rel=&quot;external&quot; href=&quot;https://github.com/milessabin/shapeless&quot;&gt;shapeless&lt;/a&gt; and &lt;em&gt;type level programming&lt;/em&gt; techniques are used to create a strongly typed interface for starting system processes. In future posts I will explore replacing its dependencies such as using &lt;a rel=&quot;external&quot; href=&quot;https://doc.akka.io/docs/akka/2.5/stream/&quot;&gt;akka-streams&lt;/a&gt; instead of &lt;a rel=&quot;external&quot; href=&quot;https://fs2.io/&quot;&gt;fs2&lt;/a&gt; or &lt;a rel=&quot;external&quot; href=&quot;https://scalaz.github.io/scalaz-zio/&quot;&gt;ZIO&lt;/a&gt; instead of &lt;a rel=&quot;external&quot; href=&quot;https://typelevel.org/cats-effect/&quot;&gt;cats-effect&lt;/a&gt;. These different versions will be a good opportunity to do some performance comparison, and to close the series with creating a new version of the library which is easier to use in the alternative environments.&lt;/p&gt;
&lt;h2 id=&quot;limiting-redirection&quot;&gt;Limiting redirection&lt;/h2&gt;
&lt;p&gt;When I started writing the library I wanted to explore how I can express some strict constraints on the type level:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A process can have its input, output and error streams redirected, but only once&lt;/li&gt;
&lt;li&gt;Processes without redirected output can be piped to processes without a redirected input&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In prox &lt;em&gt;0.2.1&lt;/em&gt; a single system process is described by the following type:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OutResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ErrResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;              IRS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ORS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; command&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; arguments&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; workingDirectory&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Path&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; inputSource&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessInputSource&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; outputTarget&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessOutputTarget&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OutResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; errorTarget&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessErrorTarget&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ErrResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; environmentVariables&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Map&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IRS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ORS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;but let&#39;s focus first on the requirement to be able to redirect one of the streams &lt;em&gt;maximum once&lt;/em&gt;.
This is encoded by the &lt;code&gt;IRS&lt;/code&gt;, &lt;code&gt;ORS&lt;/code&gt; and &lt;code&gt;ERS&lt;/code&gt; type parameters, which are all have to be subtypes of &lt;code&gt;RedirectionState&lt;/code&gt;. &lt;code&gt;RedirectionState&lt;/code&gt; is a &lt;strong&gt;phantom type&lt;/strong&gt;; there are no values ever created of this type, it is only used in type signatures to encode whether one of the three streams are already redirected or not:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;/**&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Phantom type representing the redirection state of a process &lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;sealed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;/**&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Indicates that the given channel is not redirected yet &lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; NotRedirected&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;/**&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Indicates that the given channel has already been redirected &lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Redirected&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So for example with a simplified model of a &lt;em&gt;process&lt;/em&gt;, &lt;code&gt;Process[IRS &amp;lt;: RedirectionState, ORS &amp;lt;: RedirectionState, ERS &amp;lt;: RedirectionState]&lt;/code&gt;, using the output redirection operator &lt;code&gt;&amp;gt;&lt;/code&gt; would change the types in the following way:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; p1&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NotRedirected&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NotRedirected&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NotRedirected&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; ???&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; p2&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NotRedirected&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Redirected&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NotRedirected&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; p1 &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;home &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;out.txt&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; p3&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; p2 &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;home &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;another.txt&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; THIS MUST NOT COMPILE&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;How can we restrict the redirect function to only work on &lt;code&gt;Process[_, NotRedirected, _]&lt;/code&gt;? We can define it as an &lt;strong&gt;extension method&lt;/strong&gt; with an implicit class (once again this is a simplified version focusing only on the &lt;em&gt;redirection state&lt;/em&gt; handling):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNodeOutputRedirect&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      IRS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      ERS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      PN&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IRS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NotRedirected&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;process&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PN&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;to&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; To&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; target&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CanBeProcessOutputTarget&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IRS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Redirected&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; ???&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By forcing the &lt;code&gt;ORS&lt;/code&gt; type parameter to be &lt;code&gt;NotRedirected&lt;/code&gt; and setting it to &lt;code&gt;Redirected&lt;/code&gt; in the result type we can guarantee that this function can only be called on a process that does not have their output redirected yet. The &lt;em&gt;target&lt;/em&gt; of the redirection is extensible through the &lt;code&gt;CanBeProcessOutputTarget&lt;/code&gt; type class, as we will see later.&lt;/p&gt;
&lt;h2 id=&quot;dependent-types&quot;&gt;Dependent types&lt;/h2&gt;
&lt;p&gt;Reality is much more complicated, because of &lt;em&gt;process piping&lt;/em&gt; and because the process types encode the redirection result types too. Let&#39;s get back to our &lt;code&gt;&amp;gt;&lt;/code&gt; function and see how we could modify it so it works with piped processes too. Anyway, how is process piping encoded in this library?&lt;/p&gt;
&lt;p&gt;Two processes connected through a pipe are represented by the &lt;code&gt;PipedProcess&lt;/code&gt; class. Both &lt;code&gt;Procses&lt;/code&gt; and &lt;code&gt;PipedProcess&lt;/code&gt; implements the following trait:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;sealed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IRS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ORS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We&#39;ve already seen &lt;code&gt;Process&lt;/code&gt;. &lt;code&gt;PipedProcess&lt;/code&gt; is a bit more complicated:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PipedProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN1Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;                   PN1&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, _, _, _, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;                   PN2&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, _, _, _, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;                   IRS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ORS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; from&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PN1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; createTo&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PipeConstruction&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN1Out&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PN2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IRS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ORS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To make &lt;code&gt;&amp;gt;&lt;/code&gt; work on both, we can start by modifying its definition to work on &lt;em&gt;any&lt;/em&gt; &lt;code&gt;ProcessNode&lt;/code&gt; not just &lt;code&gt;Process&lt;/code&gt; (omitting the output type params for now):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNodeOutputRedirect&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    IRS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    ERS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    PN&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IRS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NotRedirected&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;process&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PN&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;to&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; To&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; target&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CanBeProcessOutputTarget&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IRS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Redirected&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; ???&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This has a serious problem though. The output type is &lt;code&gt;ProcessNode&lt;/code&gt; and not the &quot;real&quot; process type, which means that we lose type information and all the other dependent typed operations will not work. We have to make the result type &lt;strong&gt;depend&lt;/strong&gt; on the input!&lt;/p&gt;
&lt;p&gt;We may try to use the &lt;code&gt;RedirectionOutput&lt;/code&gt; type class like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNodeOutputRedirect&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    IRS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    ERS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    PN&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IRS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NotRedirected&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;process&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PN&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;to&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; To&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; target&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CanBeProcessOutputTarget&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;            redirectOutput&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectOutput&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; redirectOutput.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Result&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; redirectOutput&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;to&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here the result (&lt;code&gt;redirectOutput.Result&lt;/code&gt;) is a &lt;em&gt;path dependent type&lt;/em&gt;. This may work in some simple cases but have two serious issues:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It is not possible to use &lt;code&gt;redirectOutput.Result&lt;/code&gt; in the &lt;em&gt;parameter block&lt;/em&gt; of the function, so if another type class needed it as a type parameter we could not pass it.&lt;/li&gt;
&lt;li&gt;Further implicit resolutions and type level operations will quickly break as the compiler will not be able to unify the various path dependent types&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;strong&gt;Aux pattern&lt;/strong&gt;, used heavily in the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/milessabin/shapeless&quot;&gt;shapeless&lt;/a&gt; library provides a nice pattern for fixing both problems. We start by defining a &lt;em&gt;type class&lt;/em&gt; for describing the operation, in this case &lt;em&gt;redirecting the output channel of a process&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectOutput&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NotRedirected&lt;/span&gt;&lt;span&gt;, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Redirected&lt;/span&gt;&lt;span&gt;, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; apply&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;process&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PN&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;to&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; To&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; target&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CanBeProcessOutputTarget&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectOutput&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NotRedirected&lt;/span&gt;&lt;span&gt;, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Result0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;        RedirectOutput&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Result0&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ... type class instances&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The type class itself is straightforward. We have to implement it for both &lt;code&gt;Process&lt;/code&gt; and &lt;code&gt;PipedProcess&lt;/code&gt; and set the &lt;code&gt;Result&lt;/code&gt; type accordingly, then implement &lt;code&gt;apply&lt;/code&gt; that sets up the actual redirection. But what the &lt;code&gt;Aux&lt;/code&gt; type is for?&lt;/p&gt;
&lt;p&gt;It solves the problems with the &lt;em&gt;path dependent&lt;/em&gt; version if we use it like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNodeOutputRedirect&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    IRS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    ERS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    PN&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IRS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;NotRedirected&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;process&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PN&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Result&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Redirected&lt;/span&gt;&lt;span&gt;, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;to&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; To&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; target&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CanBeProcessOutputTarget&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;            redirectOutput&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectOutput&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Result&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Result&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; redirectOutput&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;to&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By lifting the &lt;code&gt;Result&lt;/code&gt; from the type class instance to a type parameter the compiler can now &quot;extract&quot; the calculated type from &lt;code&gt;redirectOutput.Result&lt;/code&gt; to the &lt;code&gt;&amp;gt;&lt;/code&gt; function&#39;s &lt;code&gt;Result&lt;/code&gt; type parameter and use it directly, both for other further type requirements or as we do here, in the result type.&lt;/p&gt;
&lt;p&gt;This is the basic pattern used for &lt;em&gt;all&lt;/em&gt; the operations in prox. You can check &lt;a rel=&quot;external&quot; href=&quot;http://gigiigig.github.io/posts/2015/09/13/aux-pattern.html&quot;&gt;Luigi&#39;s short introduction to the &lt;code&gt;Aux&lt;/code&gt; pattern&lt;/a&gt; for a more detailed explanation.&lt;/p&gt;
&lt;h2 id=&quot;starting-the-processes&quot;&gt;Starting the processes&lt;/h2&gt;
&lt;p&gt;So far we just combined purely functional data structures in a complicated way. The result value may encode the launching of several system processes that are connected via pipes to each other and possibly other streams as we will see.&lt;/p&gt;
&lt;p&gt;When we eventually decide to &lt;em&gt;start&lt;/em&gt; these processes, we need a way to observe their status, wait for them to stop, get their exit code, and to access the data sent to the output streams if they were redirected. And we need this &lt;em&gt;per process&lt;/em&gt;, while launching the whole process graph in a &lt;em&gt;single step&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;First let&#39;s model a single &lt;em&gt;running process&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RunningProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OutResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ErrResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; isAlive&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Boolean&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; waitForExit&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProcessResult&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OutResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ErrResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; terminate&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProcessResult&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OutResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ErrResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and &lt;code&gt;ProcessResult&lt;/code&gt; that represents an already &lt;em&gt;terminated process&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessResult&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OutResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ErrResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;    exitCode&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;    fullOutput&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;    fullError&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ErrResult&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we need to define a &lt;code&gt;start&lt;/code&gt; extension method on &lt;code&gt;ProcessNode&lt;/code&gt; that returns somehow one well typed &lt;code&gt;RunningProcess&lt;/code&gt; for &lt;em&gt;each&lt;/em&gt; system process that it starts.&lt;/p&gt;
&lt;p&gt;Let&#39;s forget for a second about having multiple processes piped together and just consider the single process case. For that, we would need somehing like this (the &lt;code&gt;Out&lt;/code&gt; parameter is needed only for piping so I omitted it):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; start&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RunningProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OutResult&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ErrResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can see why &lt;code&gt;Process&lt;/code&gt; has those additional type paramters. It is not enough to encode whether the output and error channels were redirected or not, we also have to encode the expected &lt;em&gt;result type&lt;/em&gt; of redirecting these. By storing these types in type parameters of &lt;code&gt;Process&lt;/code&gt; we can easily imagine that by using the pattern described in the previous section, the &lt;em&gt;result type&lt;/em&gt; can &lt;strong&gt;depend&lt;/strong&gt; on what we redirected the process to.&lt;/p&gt;
&lt;p&gt;Let&#39;s see some examples of what this means!&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Target&lt;/th&gt;&lt;th&gt;Result type&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;A file system path&lt;/td&gt;&lt;td&gt;The result type is &lt;code&gt;Unit&lt;/code&gt;, the redirection happens on OS level&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Sink&lt;/td&gt;&lt;td&gt;The result type is &lt;code&gt;Unit&lt;/code&gt;, only the sink&#39;s side effect matters&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Pipe with monoid elem type&lt;/td&gt;&lt;td&gt;The stream is folded by the monoid, the result type is &lt;code&gt;T&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Pipe with non-monoid elem type&lt;/td&gt;&lt;td&gt;The stream captures the elements in a vector, the result type is &lt;code&gt;Vector[T]&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Custom fold function&lt;/td&gt;&lt;td&gt;The result type is the function&#39;s result type&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The &lt;code&gt;CanBeProcessOutputTarget&lt;/code&gt; type class we&#39;ve seen earlier defines both the stream element type and the result type:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CanBeProcessOutputTarget&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;To&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  /**&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Output stream element type &lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Out&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  /**&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Result type of running the output stream &lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; OutResult&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; apply&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;to&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; To&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessOutputTarget&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;OutResult&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;ProcessOutputTarget&lt;/code&gt; contains the actual IO code to build the redirection of the streams, I won&#39;t get into details in this post. Note that there are similar type classes for &lt;em&gt;error&lt;/em&gt; and &lt;em&gt;input&lt;/em&gt; redirection too.&lt;/p&gt;
&lt;p&gt;For two processes piped together we have to provide &lt;em&gt;two&lt;/em&gt; &lt;code&gt;RunningProcess&lt;/code&gt; instances with the proper result type parameters. So we can see that it is not enough that the &lt;em&gt;redirection&lt;/em&gt; stores the result type in the process type, the &lt;em&gt;start&lt;/em&gt; method must be dependent typed too.&lt;/p&gt;
&lt;p&gt;One way to encode this in the type system would be something like this (simplified):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; p1&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; p2&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; p3&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Process&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; rp1&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RunningProcess&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; p1.start&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; rp2&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RunningProcess&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RunningProcess&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;p1 &lt;/span&gt;&lt;span&gt;|&lt;/span&gt;&lt;span&gt; p2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.start&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; rp3&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RunningProcess&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RunningProcess&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RunningProcess&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;p1 &lt;/span&gt;&lt;span&gt;|&lt;/span&gt;&lt;span&gt; p2 &lt;/span&gt;&lt;span&gt;|&lt;/span&gt;&lt;span&gt; p3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.start&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We encode piped processes with tuples of &lt;code&gt;RunningProcess&lt;/code&gt; and single process with a single &lt;code&gt;RunningProcess&lt;/code&gt;. To implement this we can make use of the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/milessabin/shapeless&quot;&gt;shapeless&lt;/a&gt; library&#39;s &lt;code&gt;HList&lt;/code&gt; implementation.&lt;/p&gt;
&lt;p&gt;HLists are heterogeneous lists; basically similar to a tuple, but with all the &quot;usual&quot; list-like functions implemented as dependent typed functions. It&#39;s type describes the types of all its elements, and you can split it to head/tail, append two, etc. And we can do it both on the &lt;em&gt;type level&lt;/em&gt; (computing the result type of appending two &lt;code&gt;HList&lt;/code&gt;&#39;s, for example) and on the &lt;em&gt;value leve&lt;/em&gt; (appending the two values creating a third &lt;code&gt;HList&lt;/code&gt; value).&lt;/p&gt;
&lt;p&gt;We can implement the &lt;code&gt;start&lt;/code&gt; method more easily by building a &lt;code&gt;HList&lt;/code&gt;, while still keep the desired interface as &lt;a rel=&quot;external&quot; href=&quot;https://github.com/milessabin/shapeless&quot;&gt;shapeless&lt;/a&gt; implements a conversion from &lt;code&gt;HList&lt;/code&gt; to tuples.&lt;/p&gt;
&lt;p&gt;We can define two separate &lt;em&gt;start functions&lt;/em&gt;, one producing &lt;code&gt;HList&lt;/code&gt; and another the tuples (IO releated parameters omitted):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; start&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RP&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; start&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Start&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RP&lt;/span&gt;&lt;span&gt;, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RP&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; ???&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; startHL&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RPL&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HList&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; start&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Start&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN&lt;/span&gt;&lt;span&gt;, _, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RP&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IO&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RPL&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; ???&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;Start&lt;/code&gt; type class calculates both the tupled and the &lt;code&gt;HList&lt;/code&gt; version&#39;s result type. The implementation&#39;s responsibility is to start the actual system processes and wire the streams together.&lt;/p&gt;
&lt;p&gt;The interesting part is how we use &lt;em&gt;type level calculations&lt;/em&gt; from &lt;a rel=&quot;external&quot; href=&quot;https://github.com/milessabin/shapeless&quot;&gt;shapeless&lt;/a&gt; to calculte the tuple and &lt;code&gt;HList&lt;/code&gt; types for piped processes. This is all done using the technique I described earlier, but may look a bit shocking first. Let&#39;s take a look!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; startPipedProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  PN1&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, _, _, _, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  PN2&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ProcessNode&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, _, _, _, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  IRS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ORS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RedirectionState&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  RP1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RPL1&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HList&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RP1Last&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RunningProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, _, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  RP2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RPL2&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HList&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RP2Head&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RunningProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, _, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RP2Tail&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HList&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  RPT&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RPL&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HList&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;   start1&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Start&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RP1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RPL1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;   start2&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Start&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RP2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RPL2&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;   last1&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Last&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RPL1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RP1Last&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;   rp1LastType&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RP1Last&lt;/span&gt;&lt;span&gt; &amp;lt;:&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RunningProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;, _, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;   hcons2&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IsHCons&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RPL2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RP2Head&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RP2Tail&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;   prepend&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Prepend&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RPL1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RPL2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RPL&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;   tupler&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Tupler&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RPL&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RPT&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;  Aux&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PipedProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IRS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ORS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RPT&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RPL&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Start&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PipedProcess&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Out&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Err&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PN2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IRS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ORS&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ERS&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RunningProcesses&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RPT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; type&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RunningProcessList&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RPL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;      //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The way to parse this is to follow the type level computations performed through the &lt;em&gt;Aux types&lt;/em&gt; in the implicit parameter list:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;PN1&lt;/code&gt; and &lt;code&gt;PN2&lt;/code&gt; are the types of the two processes piped together&lt;/li&gt;
&lt;li&gt;The first two implicit definition calculates the &lt;em&gt;running process tuple&lt;/em&gt; and the &lt;em&gt;running process HList&lt;/em&gt; types of these inidividual process nodes and &quot;stores&quot; the results in &lt;code&gt;RP1&lt;/code&gt;, &lt;code&gt;RPL1&lt;/code&gt;, &lt;code&gt;RP2&lt;/code&gt; and &lt;code&gt;RPL2&lt;/code&gt; type parameters. For example if the two processes pipe together are single &lt;code&gt;Process&lt;/code&gt; instances, then &lt;code&gt;RP1&lt;/code&gt; and &lt;code&gt;RP2&lt;/code&gt; would be some kind of &lt;code&gt;RunningProcess&lt;/code&gt;, and the HLists would be one element long, like &lt;code&gt;RunningProcess :: HNil&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;last1&lt;/code&gt; implicit parameter is a type level &lt;em&gt;last&lt;/em&gt; functinon on the first process&#39;s &lt;code&gt;HList&lt;/code&gt;. This is required because &lt;code&gt;PN1&lt;/code&gt; itself can also be a sequence of piped processes, and we are connecting &lt;code&gt;PN2&lt;/code&gt; to the &lt;strong&gt;last&lt;/strong&gt; of these. The &lt;code&gt;RP1Last&lt;/code&gt; type parameter becomes the &lt;em&gt;type&lt;/em&gt; of the &lt;em&gt;last running process&lt;/em&gt; of the first process node.&lt;/li&gt;
&lt;li&gt;The next line, &lt;code&gt;rp1LastType&lt;/code&gt; is an additional constraint fixing the &lt;em&gt;output stream element type&lt;/em&gt; of &lt;code&gt;RP1Last&lt;/code&gt; to &lt;code&gt;Byte&lt;/code&gt;. The piping implementation is not able to connect streams of arbitrary element types, as the &lt;em&gt;process input&lt;/em&gt; is always required to be a &lt;em&gt;byte stream&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;hcons2&lt;/code&gt; is similar to the &lt;code&gt;last1&lt;/code&gt; but here we are calculating the type level &lt;em&gt;head type&lt;/em&gt; of the &lt;code&gt;HList&lt;/code&gt; called &lt;code&gt;RPL2&lt;/code&gt;. The head will be in &lt;code&gt;RP2Head&lt;/code&gt; and the tail &lt;code&gt;HList&lt;/code&gt; in &lt;code&gt;RP2Tail&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In the &lt;code&gt;prepend&lt;/code&gt; step we concatenate &lt;code&gt;RPL1&lt;/code&gt; with &lt;code&gt;RPL2&lt;/code&gt; using the &lt;code&gt;Prepend&lt;/code&gt; operation, the result &lt;code&gt;HList&lt;/code&gt; type is in &lt;code&gt;RPL&lt;/code&gt;. This is the &lt;code&gt;HList&lt;/code&gt; representation of the piped running process.&lt;/li&gt;
&lt;li&gt;Finally we use the &lt;code&gt;Tupler&lt;/code&gt; operation to calculate the tuple type from the &lt;code&gt;HList&lt;/code&gt;, and store it in &lt;code&gt;RPT&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The compiler perform the type level calculations and we can use the result types &lt;code&gt;RPT&lt;/code&gt; and &lt;code&gt;RPL&lt;/code&gt; to actually implement the &lt;em&gt;start typeclass&lt;/em&gt;. This is the most complicated type level calculation in the library.&lt;/p&gt;
&lt;h2 id=&quot;final-thoughts&quot;&gt;Final thoughts&lt;/h2&gt;
&lt;p&gt;As we&#39;ve seen, Scala&#39;s type system can bring us quite far in expressing a dependent typed interface. On the other hand writing and reading code in this style is really hard, and if things go wrong, decoding the compiler&#39;s error messages is not an easy task either. This is a serious tradeoff that has to be considered and in many cases a more dynamic but much more readable and maintainable approach can be better.&lt;/p&gt;
&lt;p&gt;With &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/prox&quot;&gt;prox&lt;/a&gt; I explicitly wanted to explore these features of the Scala language.&lt;/p&gt;
&lt;p&gt;In the next posts we will ignore the type level parts of the library and focus on different &lt;em&gt;streaming&lt;/em&gt; and &lt;em&gt;effect&lt;/em&gt; libraries.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>AWS rate limits vs prezidig</title>
        <published>2018-09-21T00:00:00+00:00</published>
        <updated>2018-09-21T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/aws-rate-limits-prezidig/"/>
        <id>https://blog.vigoo.dev/posts/aws-rate-limits-prezidig/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/aws-rate-limits-prezidig/">&lt;p&gt;At &lt;a rel=&quot;external&quot; href=&quot;https://prezi.com&quot;&gt;Prezi&lt;/a&gt;, we have an internal tool called &lt;strong&gt;prezidig&lt;/strong&gt; for discovering AWS resources. I like it a lot so I was quite annoyed recently that it always fails with a &lt;em&gt;throttling exception&lt;/em&gt; because of our increased use of the AWS API. It made it completely unusable, so I decided to try to fix this.&lt;/p&gt;
&lt;p&gt;Then I decided to write the story in this blog post, as the steps I had to made to achieve the results I aimed for can be useful for writing maintainable, fast and safe Scala code in the future.&lt;/p&gt;
&lt;p&gt;I will describe the phases as they happened, as I did not really know anything about this codebase so the path to the success was not exactly clear immediately.&lt;/p&gt;
&lt;h2 id=&quot;wrapping-the-calls&quot;&gt;Wrapping the calls&lt;/h2&gt;
&lt;p&gt;So my initial thought was to just find the AWS API calls and wrap them in a helper function which catches the throttling error and retries with an increasing delay.&lt;/p&gt;
&lt;p&gt;I basically wrote this in the base class of all the &lt;em&gt;mirrors&lt;/em&gt; (the classes which are responsible for fetching AWS and other resource data for &lt;strong&gt;prezidig&lt;/strong&gt;):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; protected&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; byHandlingThrottling&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;awsCall&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; call&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;remainingTries&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Int&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;wait&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; FiniteDuration&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      Future&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Try&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;awsCall&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.flatMap &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Success&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;result&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;.successful&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;result&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Failure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;awsException&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AmazonServiceException&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; if&lt;/span&gt;&lt;span&gt; awsException.getErrorCode &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Throttling&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; remainingTries &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 0&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          akka.pattern.after&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;wait, actorSystem.scheduler&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            call&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;remainingTries &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 1&lt;/span&gt;&lt;span&gt;, wait &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        case&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Failure&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;reason&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;.failed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;reason&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     call&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;10&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;100&lt;/span&gt;&lt;span&gt;.millis&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; TODO: make configurable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then the only thing I had to do was to was wrapping all the existing AWS calls with this. Then I realized that this won’t be this simple, as these calls were not always asynchronous, just sometimes. To see an example, for an &lt;em&gt;ElasticBeanstalk application&lt;/em&gt;, it fetches the &lt;em&gt;application metadata&lt;/em&gt; with synchronous call, then fetches the related &lt;em&gt;EB environments&lt;/em&gt; asynchronously. The whole thing might be wrapped in another future somewhere else, but that’s a different story.&lt;/p&gt;
&lt;p&gt;While making these discoveries I also found several synchronization points, like the code waiting for some futures to complete in a blocking way. Also that the model is mutable. So… just for trying this out, I &lt;em&gt;still &lt;strong&gt;wrapped&lt;/strong&gt;&lt;/em&gt; all the AWS calls with this stuff, by converting the future back to a synchronous call by immediately blocking on it.&lt;/p&gt;
&lt;p&gt;What did I achieve with this? Well, some throttling errors were fixed, the code became extremely ugly, and I could not even wrap everything so the errors remained, and because of the tons of blocking, timeouts, etc. it was basically impossible to understand whether this would work or deadlock or just be slow.&lt;/p&gt;
&lt;p&gt;That was the point I decided to do this properly&lt;/p&gt;
&lt;h2 id=&quot;reflection&quot;&gt;Reflection&lt;/h2&gt;
&lt;p&gt;Before solving the real problem I found that the mirrors are initialized via reflection, something like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; buildMirrors&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RegionAwareAWSMirror&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; mf&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Manifest&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Seq&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Config&lt;/span&gt;&lt;span&gt;.regions.map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;region &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; mf.runtimeClass.getConstructor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;classOf&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.newInstance&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;region&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;asInstanceOf&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is something that you should avoid, as it leads to problems that are not detected by the compiler, only at runtime, every time you refactor something around these classes. There are some use cases where this may be required, like dynamically loading plugins or stuff like this, but to just have a factory for something, it is must simple to use… &lt;strong&gt;functions&lt;/strong&gt;!&lt;/p&gt;
&lt;p&gt;So I could not hold myself back and quickly changed this to:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; buildMirrors&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;A&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RegionAwareAWSMirror&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_, _&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;factory&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ActorSystem&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; A&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Config&lt;/span&gt;&lt;span&gt;.regions.map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;region &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; factory&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;region, system&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(Since then even this has disappeared, but don’t run that much forward).&lt;/p&gt;
&lt;h2 id=&quot;async-fetching&quot;&gt;Async fetching&lt;/h2&gt;
&lt;p&gt;Ok so the first obvious step was to refactor the whole fetching code in a way that it is just a chain of &lt;strong&gt;futures&lt;/strong&gt;. By making everything async in the process, the AWS calls would be simply replaceable with the throttling function above or anything more sophisticated!&lt;/p&gt;
&lt;p&gt;But I knew that I cannot safely do this while the model we are building itself is mutable - there is no way I want to debug what happens with it once all the steps are really becoming parallel!&lt;/p&gt;
&lt;h3 id=&quot;immutable-model&quot;&gt;Immutable model&lt;/h3&gt;
&lt;p&gt;I believe the following GitHub diff captures the core change of this step:&lt;/p&gt;
&lt;img src=&quot;/images/prezidig-img-1.webp&quot; alt=&quot;GitHub diff of Model.scala replacing the Future-based children API with a plain list&quot; width=&quot;800&quot; height=&quot;408&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;Of course I had to change all the subtypes of Model, and I went through the code looking for&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;var&lt;/strong&gt;s&lt;/li&gt;
&lt;li&gt;mutable collections&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;and got rid of them. Except for the caching constructs, because I planned to refactor those in the next step, so for now I left them alone.&lt;/p&gt;
&lt;h3 id=&quot;async-mirrors&quot;&gt;Async mirrors&lt;/h3&gt;
&lt;p&gt;Once I felt the model is safe enough, I went to the next big change, making everything asynchronous.&lt;/p&gt;
&lt;img src=&quot;/images/prezidig-img-2.webp&quot; alt=&quot;Diff changing the fetch method&#39;s return type from Seq[M] to Future[List[M]]&quot; width=&quot;800&quot; height=&quot;14&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;This took some hours, to be honest. But really, the core idea is only that the result must be a &lt;code&gt;Future[T]&lt;/code&gt;, not &lt;code&gt;T&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;So how do you refactor a code that was previously half synchronous, half asynchronous to achieve this? Let’s see an example! It will be the &lt;em&gt;key-pair mirror&lt;/em&gt; as it is the smallest.&lt;/p&gt;
&lt;p&gt;Originally (with my ugly wrapping in the previous step) it looked like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; protected&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; fetch&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SimpleParsedInput&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;context&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Context&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Seq&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;KeyPair&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    try&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; futureResult&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; byHandlingThrottling&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        buildClient&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AmazonEC2ClientBuilder&lt;/span&gt;&lt;span&gt;.standard&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.describeKeyPairs&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;          new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DescribeKeyPairsRequest&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;.withKeyNames&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;input.id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Await&lt;/span&gt;&lt;span&gt;.result&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;futureResult, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;10&lt;/span&gt;&lt;span&gt;.seconds&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      result.getKeyPairs.asScala.map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;info &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; KeyPair&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;info, region&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.seq&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;keypair &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; keypair.withFutureChildren&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LaunchConfigurationMirror&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;region, actorSystem&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.apply&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;context.withInput&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;keypair.description.getKeyName&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; catch&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; _&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AmazonEC2Exception&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Seq&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So as you can see fetching the key pairs by name was a synchronous request, but then the &lt;em&gt;launch configurations&lt;/em&gt; are fetched asynchronously and are being updated back the result model in a mutable way. We want to transform this function so it does not have any side effects, just performs a chain of asynchronous operations and in the end have a fully fetched &lt;em&gt;key pair&lt;/em&gt; with the related &lt;em&gt;launch configurations&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;In every case the only thing needed was a combination of &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;flatMap&lt;/code&gt; on futures, and of course the &lt;em&gt;for syntax&lt;/em&gt; can also be used to make the code more readable:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; fetchKeyPair&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;client&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AmazonEC2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;context&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Context&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;info&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; KeyPairInfo&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;KeyPair&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      launchConfigurations &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; LaunchConfigurationMirror&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;region, actorSystem&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.apply&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;context.withInput&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;info.getKeyName&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; KeyPair&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      description &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; info,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      region &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; region,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      children &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; launchConfigurations&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; protected&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; fetch&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; SimpleParsedInput&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;context&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Context&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;KeyPair&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; client&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; buildClient&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;AmazonEC2ClientBuilder&lt;/span&gt;&lt;span&gt;.standard&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    byHandlingThrottling&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;client.describeKeyPairs&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DescribeKeyPairsRequest&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;.withKeyNames&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;input.id&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.flatMap &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; result &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;      Future&lt;/span&gt;&lt;span&gt;.sequence&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        result.getKeyPairs.asScala.toList.map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;fetchKeyPair&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;client, context, _&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;.recover &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      case&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; _&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; AmazonEC2Exception&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; List&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; TODO: log?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that the &lt;code&gt;Future.sequence&lt;/code&gt; function is quite useful in these scenarios, as it makes a &lt;code&gt;Future[List[T]]&lt;/code&gt; from &lt;code&gt;List[Future[T]]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Of course the code became more verbose because of all this chaining, this is the price of this transformation. And why I don’t like to express complex logic with a chain of futures, rather with some higher level abstraction such as actors (or for this use case, streams would fit even better).&lt;/p&gt;
&lt;p&gt;But I wanted to make iterative changes, so I did this transformation on all the mirrors and eventually got a &lt;code&gt;Future[List[Model]]&lt;/code&gt; in the main function that I could await for. I also thrown out the global atomic integer that counted the running stuff for completion, as in this model the completion of the composed future should mark the end of the whole computation.&lt;/p&gt;
&lt;p&gt;So did I succeed at this point? Of course not. Actually this whole thing is a big deadlock :)&lt;/p&gt;
&lt;h2 id=&quot;caching-and-circular-references&quot;&gt;Caching and circular references&lt;/h2&gt;
&lt;p&gt;It was not immediately obvious what causes the deadlock. In a system like this it can happen in different ways. For example I knew that there are global singleton caches in the code, protected by &lt;strong&gt;locks&lt;/strong&gt;. This &lt;em&gt;could&lt;/em&gt; cause deadlocks if all the executors got blocked and no new threads can be spawned by the active executor. I did not know if this is happening, but would not have been surprised at all, as much more things were happening in parallel because of the previous refactoring step.&lt;/p&gt;
&lt;p&gt;And circular references in the huge chained future graph can also lead to this. Let’s consider this simplified example:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Cache&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;key&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Work&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; put&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;key&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;compute&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; ()&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Work&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Unit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; cache&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Cache&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; ???&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; work1&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Work&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; cache.get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;work2&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.map &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; w2 &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Work&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;H&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; $&lt;/span&gt;&lt;span&gt;w2&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; work2&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Work&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; cache.get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;work1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.map &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; w1 &lt;/span&gt;&lt;span&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Work&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;H&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; $&lt;/span&gt;&lt;span&gt;w1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cache.put&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;work1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cache.put&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;work2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;println&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Await&lt;/span&gt;&lt;span&gt;.result&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;work1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;.second&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This can never work. If you think about what &lt;strong&gt;prezidig&lt;/strong&gt; does, you will have a feeling that this happens. A lot.&lt;/p&gt;
&lt;p&gt;But let’s go in order.&lt;/p&gt;
&lt;h3 id=&quot;non-blocking-cache&quot;&gt;Non-blocking cache&lt;/h3&gt;
&lt;p&gt;First I wanted to get rid of the global, lock-protected mutable maps used as caches, and have a non-blocking implementation with more control and better performance and safety. This is the kind of job that an &lt;strong&gt;actor&lt;/strong&gt; can model nicely, so I created a &lt;em&gt;model cache actor&lt;/em&gt; that is spawned for &lt;em&gt;each mirror&lt;/em&gt; and can store and retrieve lists of AWS models for a given key.&lt;/p&gt;
&lt;p&gt;I won’t list the whole actor’s code here, let’s see the messages it consumes:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  sealed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ModelCacheMessage&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;M&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Model&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Put&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;M&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Model&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;key&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;M&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ModelCacheMessage&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;M&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; FetchFailed&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;M&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Model&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;key&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;failure&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Failure&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ModelCacheMessage&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;M&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; GetOrFetch&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;M&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Model&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;key&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;fetch&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; ()&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;M&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;respondTo&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ActorRef&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Try&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;M&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ModelCacheMessage&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;M&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; GetRefOrFetch&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;M&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Model&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;key&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;fetch&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; ()&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;M&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;respondTo&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ActorRef&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ModelRef&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;M&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ModelCacheMessage&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;M&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  final&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Dump&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;M&lt;/span&gt;&lt;span&gt; &amp;lt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Model&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;respondTo&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ActorRef&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Map&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;String&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;M&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ModelCacheMessage&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;M&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This cache itself is responsible for executing the &lt;em&gt;fetch function&lt;/em&gt; only if needed, when the value for the given key is not cached yet. It is done by using the &lt;strong&gt;pipe pattern&lt;/strong&gt;: it starts the asynchronous fetch function on a configured worker executor (which can be the actor system, or a fix thread pool, etc.) and registers an &lt;code&gt;onFinish&lt;/code&gt; callback for the future which &lt;em&gt;pipes back&lt;/em&gt; the future’s result to the actor as actor messages (&lt;code&gt;Put&lt;/code&gt; and &lt;code&gt;FetchFailed&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;I will talk about references and cache dumps in the next section.&lt;/p&gt;
&lt;p&gt;There was one more big problem with the existing code that prevented introducing these cache actors: that the mirrors were not really singletons but some mirrors created new instances of existing mirrors (without any difference to the ones created in the main function). These shared the singleton mutable lock-protected cache map in the original version, that’s why it worked. But in the new implementation each mirror spawned its own cache actor, so it was no longer allowed to create multiple instances of the same thing.&lt;/p&gt;
&lt;p&gt;So in this step I collected all the mirrors to a class called &lt;code&gt;Mirrors&lt;/code&gt;, which later became the collection of all the resources needed to perform the “dig”, so in the final version it is called &lt;code&gt;DigSite&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;With this change the caching could be replaced, and with the &lt;strong&gt;ask pattern&lt;/strong&gt; I was able to fit it to the chain of futures created in the previous step.&lt;/p&gt;
&lt;p&gt;Did it solve the deadlock? No, of course not&lt;/p&gt;
&lt;h3 id=&quot;circular-references&quot;&gt;Circular references&lt;/h3&gt;
&lt;p&gt;But now it was obvious that there are some circular references. And by simply drawing it, I could see that this is actually the core concept of the whole thing :)&lt;/p&gt;
&lt;p&gt;Let me show you &lt;em&gt;the drawing&lt;/em&gt;:&lt;/p&gt;
&lt;img src=&quot;/images/prezidig-img-3.webp&quot; alt=&quot;Hand-drawn diagram of the ECS resource dependencies from load balancers to Route 53 and CloudFront&quot; width=&quot;800&quot; height=&quot;776&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;So everything refers back to everything, not a surprise that this chained-together code cannot finish.&lt;/p&gt;
&lt;p&gt;To be honest, I was not sure how exactly did it work in the original version, whether the boundary of sync and async calls were carefully designed to make this work or just accidentally, whatever.&lt;/p&gt;
&lt;p&gt;I wanted to have a solution where you don’t have to think about it so nobody will fuck it up next time when it has to be modified.&lt;/p&gt;
&lt;p&gt;The chosen solution can be summarized in the following way:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;em&gt;models&lt;/em&gt; are only storing &lt;strong&gt;references to other models&lt;/strong&gt; encoded by the &lt;code&gt;ModelRef&lt;/code&gt; type. A reference is basically selecting a mirror (by its &lt;em&gt;cache&lt;/em&gt;) and an item in it by its &lt;em&gt;key&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;When fetching a model, you immediately get back a &lt;em&gt;model reference&lt;/em&gt; from the cache so it can be stored in the owner model, even with circular references. The real data is still fetched and cached as before.&lt;/li&gt;
&lt;li&gt;This works because nobody uses the actual child models until the &lt;strong&gt;rendering&lt;/strong&gt; of the output. So we have the asynchronous, parallel fetching of all the models, and then a completely separate, non-async step where we need the real connections to actually render the output based on the templates. I could change how the rendering works to query the model references from the cache, but I did not want to touch that part. So I introduced a middle step where all the &lt;em&gt;model cache actors&lt;/em&gt; &lt;strong&gt;dump&lt;/strong&gt; their state to simple immutable maps, and then the model gets &lt;em&gt;updated&lt;/em&gt; by selecting the referenced models from this map and changing a field. Yes, a mutable field. It is a non-threadsafe operation that has a single, well defined place to be called, and this way the whole third part (rendering the output) could remain untouched.&lt;/li&gt;
&lt;li&gt;Because of decoupling the actual fetching from the result future (it is completed earlier, as it only needs the references!), I had to have something that keeps track of the ongoing tasks ran by the cache actors, so there is also a &lt;em&gt;work monitor actor&lt;/em&gt; that notifies the main logic once everything is complete.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Considering all this, the main steps before starting to render the output looks like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    models &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; runRelevantMirrors&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;digSite.allMirrors, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Context&lt;/span&gt;&lt;span&gt;.initial&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;input&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fetchingDone &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; digSite.workMonitor &lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; WorkMonitor&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;WaitForReady&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    cacheDumps &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CacheDumps&lt;/span&gt;&lt;span&gt;.fromMirrors&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;digSite.allMirrors&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    _ &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; models.foreach&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_.resolveChildren&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;cacheDumps&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; side effect!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; yield&lt;/span&gt;&lt;span&gt; models&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;anyone-else-blocking&quot;&gt;Anyone else blocking?&lt;/h2&gt;
&lt;p&gt;At this point the tool started to work again and produce results. So I went back checking if any other blocking code remained that can be implemented in other ways. The progress tracker was like that, it had mutable state and locks, so I converted that to an actor too. It was quite simple, and on the usage side almost nothing changed compared to the original.&lt;/p&gt;
&lt;h2 id=&quot;and-what-about-the-throttling&quot;&gt;And what about the throttling?&lt;/h2&gt;
&lt;p&gt;Ok so at this point I refactored the whole stuff but still did not solve the throttling issue, right?&lt;/p&gt;
&lt;p&gt;Right.&lt;/p&gt;
&lt;p&gt;But now finally I knew how to do it!&lt;/p&gt;
&lt;p&gt;I already wrapped all AWS calls with that specific function (and at this point it was really &lt;em&gt;all&lt;/em&gt; calls, not just &lt;em&gt;almost)&lt;/em&gt;. So I just had to write it in a better way.&lt;/p&gt;
&lt;p&gt;I wanted to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Have control on how many AWS requests are we doing in parallel&lt;/li&gt;
&lt;li&gt;In case of throttling errors delay &lt;strong&gt;everything&lt;/strong&gt; as soon as possible&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This can be achieved easily by some standard patterns like treating AWS as an encapsulated resource and putting some circuit breaking logic in it, and explicitly distributing the work among multiple workers.&lt;/p&gt;
&lt;p&gt;Let’s see the designed solution on a drawing:&lt;/p&gt;
&lt;img src=&quot;/images/prezidig-img-4.webp&quot; alt=&quot;Diagram of the AWS throttling design: mirrors calling the AWS actor, which fans out to worker actors&quot; width=&quot;800&quot; height=&quot;505&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: the &lt;em&gt;classic Akka&lt;/em&gt; has built-in support for this routing and circuit breaking, but I prefer &lt;em&gt;Akka-typed&lt;/em&gt; because of its type safety, where there are no official reusable higher level components like this yet. The one I implemented here is quite specific, later could be refactored to be built from more reusable typed actor components.&lt;/p&gt;
&lt;p&gt;So how does this work?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;There is a single coordinator actor called &lt;strong&gt;AWS&lt;/strong&gt; and multiple (32 by default) worker actors called &lt;strong&gt;AWS Worker&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The number of worker actors control the maximum number of parallel AWS operations, because each worker actor is guaranteed to run maximum one such operation at the same time. All the other incoming requests are distributed among the workers and gets enqueued.&lt;/li&gt;
&lt;li&gt;The AWS calls are executed on a different thread pool, not blocking the actors. Their result is sent back by the already mentioned &lt;em&gt;pipe to&lt;/em&gt; pattern&lt;/li&gt;
&lt;li&gt;AWS throttling errors are detected on the worker nodes, and the worker node immediately switches to &lt;strong&gt;open circuit state&lt;/strong&gt; in which it does not start any new AWS command. The length of the open state increases with every throttling error, and gets reseted after a number of successful requests.&lt;/li&gt;
&lt;li&gt;Opening the circuit breaker on one worker node is immediately followed by opening it on &lt;strong&gt;all other&lt;/strong&gt; worker nodes too, to stop overloading AWS.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This could be further improved with more advanced logic but I believe it is good enough for our current purposes, and now we can use &lt;strong&gt;prezidig&lt;/strong&gt; again!&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Bari with Visual Studio Code</title>
        <published>2016-01-21T00:00:00+00:00</published>
        <updated>2016-01-21T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/bari-vscode/"/>
        <id>https://blog.vigoo.dev/posts/bari-vscode/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/bari-vscode/">&lt;h2 id=&quot;intro&quot;&gt;Intro&lt;/h2&gt;
&lt;p&gt;A few weeks ago I discovered &lt;a rel=&quot;external&quot; href=&quot;https://code.visualstudio.com/&quot;&gt;Visual Studio Code&lt;/a&gt; and started using it for some of my work. &lt;em&gt;(Note: I&#39;m using multiple editors/IDEs all the time, based on the task; Emacs, Sublime, Atom, IntelliJ, VS, etc.)&lt;/em&gt; So far &lt;em&gt;Code&lt;/em&gt; is my favourite among the set of similar editors, such as Atom. I was pleasently surprised how well it works with its integrated &lt;a rel=&quot;external&quot; href=&quot;http://www.omnisharp.net/&quot;&gt;OmniSharp&lt;/a&gt; plugin on &lt;a rel=&quot;external&quot; href=&quot;http://vigoo.github.io/bari/&quot;&gt;bari&#39;s&lt;/a&gt; codebase, so I decided to try to write a &lt;em&gt;bari plugin&lt;/em&gt; for it.&lt;/p&gt;
&lt;p&gt;Writing an extension for &lt;em&gt;Code&lt;/em&gt; was a nice experience. The outcome is the &lt;a rel=&quot;external&quot; href=&quot;https://marketplace.visualstudio.com/items/vigoo.bari&quot;&gt;bari build management extension&lt;/a&gt;, which I&#39;ll demonstrate in the next section.&lt;/p&gt;
&lt;h2 id=&quot;developing-net-applications-with-visual-studio-code-and-bari&quot;&gt;Developing .NET applications with Visual Studio Code and bari&lt;/h2&gt;
&lt;p&gt;As &lt;em&gt;Code&lt;/em&gt; is multiplatform, and &lt;em&gt;bari&lt;/em&gt; also works with &lt;a rel=&quot;external&quot; href=&quot;http://www.mono-project.com/&quot;&gt;Mono&lt;/a&gt;, I&#39;ll demonstrate how you can use these tools to develop a .NET application (actually &lt;em&gt;bari&lt;/em&gt; itself) on a Mac. The steps here (except installing Mono) would be the same on Windows or Linux as well.&lt;/p&gt;
&lt;h3 id=&quot;installing-the-tools&quot;&gt;Installing the tools&lt;/h3&gt;
&lt;p&gt;First, if you are not on Windows, you&#39;ll have to install the latest &lt;a rel=&quot;external&quot; href=&quot;http://www.mono-project.com/&quot;&gt;Mono&lt;/a&gt; framework. On OSX I recommed to use &lt;a rel=&quot;external&quot; href=&quot;http://brew.sh/&quot;&gt;&lt;code&gt;brew&lt;/code&gt;&lt;/a&gt; to do that:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;brew install mono&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;mono --version&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then get the latest &lt;a rel=&quot;external&quot; href=&quot;https://code.visualstudio.com/&quot;&gt;Visual Studio Code&lt;/a&gt; version, either by downloading it from its homepage or with &lt;a rel=&quot;external&quot; href=&quot;https://github.com/caskroom/homebrew-cask&quot;&gt;&lt;code&gt;brew cask&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;brew cask install visual-studio-code&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Get the latest &lt;em&gt;bari&lt;/em&gt;. On Windows I recommend downloading and extracting the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/bari/releases/latest&quot;&gt;latest official release&lt;/a&gt; and adding it to the &lt;code&gt;PATH&lt;/code&gt;.
On OSX, with &lt;code&gt;mono&lt;/code&gt; we already have &lt;code&gt;nuget&lt;/code&gt;, so let&#39;s use that:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cd /opt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nuget install bari-mono&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ln -s bari-mono.1.0.2.2 bari&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and create a script to execute it somewhere in your &lt;code&gt;PATH&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;#!&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;/bin/sh&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;mono&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; /opt/bari/tools/bari.exe&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; $&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;@&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That&#39;s it. Future versions of the &lt;em&gt;bari extension&lt;/em&gt; will probably be able to install &lt;em&gt;bari&lt;/em&gt; itself.&lt;/p&gt;
&lt;p&gt;Let&#39;s start &lt;em&gt;Code&lt;/em&gt; now!&lt;/p&gt;
&lt;h3 id=&quot;installing-the-extension&quot;&gt;Installing the extension&lt;/h3&gt;
&lt;p&gt;Open the &lt;em&gt;command palette&lt;/em&gt; (F1, or ⇧⌘P) and type &lt;code&gt;ext install bari&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/images/baricode1.webp&quot; class=&quot;zimg&quot;&gt;&lt;img src=&quot;/images/baricode1.webp&quot; alt=&quot;Installing the bari extension from the Visual Studio Code marketplace view&quot; width=&quot;600&quot; height=&quot;86&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&quot;loading-the-project&quot;&gt;Loading the project&lt;/h3&gt;
&lt;p&gt;After that restart the editor. Have your bari-built project available somewhere. As we are going to develop bari itself, let&#39;s clone its repository:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;git clone https://github.com/vigoo/bari.git&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then open the result &lt;code&gt;bari&lt;/code&gt; directory with &lt;em&gt;Code&lt;/em&gt;. This should look like the following:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/images/baricode2.webp&quot; class=&quot;zimg&quot;&gt;&lt;img src=&quot;/images/baricode2.webp&quot; alt=&quot;Visual Studio Code with the bari suite loaded in the explorer&quot; width=&quot;800&quot; height=&quot;564&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;bari plugin&lt;/em&gt; automatically detected that the opened folder has a &lt;code&gt;suite.yaml&lt;/code&gt; in its root, and loaded it. That&#39;s why we can see the two sections on the statusbar&#39;s right side: &lt;code&gt;full&lt;/code&gt; and &lt;code&gt;debug&lt;/code&gt;. The first one is the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/bari/wiki/Product&quot;&gt;selected target product&lt;/a&gt; and the second one is the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/bari/wiki/Goal&quot;&gt;selected goal&lt;/a&gt;. All the &lt;em&gt;bari commands&lt;/em&gt; provided by the extension will be executed with these settings.&lt;/p&gt;
&lt;h3 id=&quot;changing-the-target&quot;&gt;Changing the target&lt;/h3&gt;
&lt;p&gt;To change the active product or goal, you can click on the statusbar or use the &lt;em&gt;command palette&lt;/em&gt; (F1, or ⇧⌘P) and choose &lt;code&gt;bari: Change goal&lt;/code&gt; or &lt;code&gt;bari: Change target product&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Let&#39;s change the &lt;em&gt;goal&lt;/em&gt; to &lt;code&gt;debug-mono&lt;/code&gt;, as we are working on a non-Windows environment:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/images/baricode3.webp&quot; class=&quot;zimg&quot;&gt;&lt;img src=&quot;/images/baricode3.webp&quot; alt=&quot;Choosing a bari build configuration from the quick pick menu&quot; width=&quot;800&quot; height=&quot;560&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&quot;generating-the-solution&quot;&gt;Generating the solution&lt;/h3&gt;
&lt;p&gt;The next step before starting coding is to actually &lt;strong&gt;generate&lt;/strong&gt; the solution and projects files (and fetch the dependencies, etc.) so &lt;em&gt;OmniSharp&lt;/em&gt; can load it and provide code completion, analysis, etc. features.&lt;/p&gt;
&lt;p&gt;To do so, just use the &lt;em&gt;command palette&lt;/em&gt; and choose &lt;code&gt;bari: Regenerate solution&lt;/code&gt;, which &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/bari/wiki/VsCommand&quot;&gt;runs the &lt;code&gt;bari vs&lt;/code&gt; command&lt;/a&gt; with the correct parameters. The command&#39;s output is displayed in an &lt;em&gt;output panel&lt;/em&gt; called &lt;code&gt;bari&lt;/code&gt;. This looks like the following:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/images/baricode4.webp&quot; class=&quot;zimg&quot;&gt;&lt;img src=&quot;/images/baricode4.webp&quot; alt=&quot;The bari output panel resolving NuGet package references&quot; width=&quot;800&quot; height=&quot;562&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;There&#39;s nothing else left than pointing &lt;em&gt;OmniSharp&lt;/em&gt; to the generated solution, with the following command:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/images/baricode5.webp&quot; class=&quot;zimg&quot;&gt;&lt;img src=&quot;/images/baricode5.webp&quot; alt=&quot;The command palette searching for OmniSharp commands&quot; width=&quot;800&quot; height=&quot;126&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It will automatically find the generated &lt;code&gt;.sln&lt;/code&gt; file, just select the correct one:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/images/baricode6.webp&quot; class=&quot;zimg&quot;&gt;&lt;img src=&quot;/images/baricode6.webp&quot; alt=&quot;Selecting between the test-plugin and full-withtests solutions&quot; width=&quot;800&quot; height=&quot;130&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In a few seconds (and with a few warnings for this project), &lt;em&gt;OmniSharp&lt;/em&gt; works. To see what it can do, &lt;a rel=&quot;external&quot; href=&quot;https://code.visualstudio.com/Docs/languages/csharp&quot;&gt;check this page&lt;/a&gt;. A simple example is to jump to a given class or interface with ⌘P:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/images/baricode7.webp&quot; class=&quot;zimg&quot;&gt;&lt;img src=&quot;/images/baricode7.webp&quot; alt=&quot;Workspace symbol search finding IUserOutput&quot; width=&quot;600&quot; height=&quot;118&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&quot;working-on-the-project&quot;&gt;Working on the project&lt;/h3&gt;
&lt;p&gt;You can work on the project and build it from &lt;em&gt;Code&lt;/em&gt; or run its tests using the &lt;code&gt;bari: Build&lt;/code&gt; and &lt;code&gt;bari: Test&lt;/code&gt; commands. The build output will be shown just like in the &lt;em&gt;solution generation step&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/images/baricode8.webp&quot; class=&quot;zimg&quot;&gt;&lt;img src=&quot;/images/baricode8.webp&quot; alt=&quot;The bari command palette listing build, test and solution commands&quot; width=&quot;598&quot; height=&quot;211&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Whenever the suite definition itself must be modified, you can jump there with the &lt;code&gt;bari: Open suite.yaml&lt;/code&gt; command and then just regenerate the solution as it was shown above.&lt;/p&gt;
&lt;h2 id=&quot;implementation&quot;&gt;Implementation&lt;/h2&gt;
&lt;p&gt;The implementation was really straightforward. The source code &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/bari-code&quot;&gt;can be found here&lt;/a&gt;. It&#39;s basically a &lt;em&gt;JSON&lt;/em&gt; defining how the plugin is integrated and some implementation code in &lt;em&gt;TypeScript&lt;/em&gt;. It&#39;s easy to run and debug the plugin from &lt;em&gt;Code&lt;/em&gt; itself.&lt;/p&gt;
&lt;p&gt;For example the following section from the extension definition describes what events triggers the extension:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;json&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;activationEvents&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;onCommand:bari.build&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;onCommand:bari.test&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;onCommand:bari.vs&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;onCommand:bari.openSuiteYaml&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;onCommand:bari.selfUpdate&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;onCommand:bari.goal.changeCurrentGoal&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;onCommand:bari.goal.changeCurrentProduct&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;workspaceContains:suite.yaml&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It&#39;s either done by invoking one of the defined commands from the &lt;em&gt;command palette&lt;/em&gt;, or if the opened workspace contains a &lt;code&gt;suite.yaml&lt;/code&gt;. The latter enables the extension to parse the suite definition and initialize the statusbar immediately one the suite has been opened.&lt;/p&gt;
&lt;p&gt;The package definition also specifies the provided configuration values, such as:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;json&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;bari.commandLine&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;bari&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;description&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Command line to execute bari&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;bari.verboseOutput&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;boolean&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-8&quot;&gt; false&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;description&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Turns on verbose output for all the executed bari commands&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The implementation itself is really simple, all the user interface elements involved such as the console output window, the command palette, the statusbar panels can be easily managed.&lt;/p&gt;
&lt;p&gt;For example the panel showing &lt;code&gt;bari&lt;/code&gt;&#39;s output is created by the following code snippet:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; channel&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; vscode&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;window&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;createOutputChannel&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;bari&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;channel&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;show&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or to display the result of an operation:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;vscode&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;window&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;showErrorMessage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;No suite.yaml in the current workspace!&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or to create the statusbar panel:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;goals&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; vscode&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;window&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;createStatusBarItem&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;vscode&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;StatusBarAlignment&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Right&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;goals&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;bari.goal.changeCurrentGoal&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;this&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;goals&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;show&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This API is simple and well documented enough so basic integrations like this can be done in an hour.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Gradle-Haskell-plugin with experimental Stack support</title>
        <published>2015-12-22T00:00:00+00:00</published>
        <updated>2015-12-22T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/gradle-haskell-plugin-stack/"/>
        <id>https://blog.vigoo.dev/posts/gradle-haskell-plugin-stack/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/gradle-haskell-plugin-stack/">&lt;p&gt;I&#39;ve released a &lt;strong&gt;new version (0.4)&lt;/strong&gt; of &lt;a rel=&quot;external&quot; href=&quot;https://github.com/prezi/gradle-haskell-plugin&quot;&gt;gradle-haskell-plugin&lt;/a&gt; today, with &lt;strong&gt;experimental stack support&lt;/strong&gt;.
It is not enabled by default, but I used it exclusively for months and it seems to get quite stable. To use it you need &lt;a rel=&quot;external&quot; href=&quot;https://haskellstack.com&quot;&gt;stack&lt;/a&gt;,
have it enabled with &lt;code&gt;-Puse-stack&lt;/code&gt; and have to keep some rules in your &lt;code&gt;.cabal&lt;/code&gt; file, as explained &lt;a rel=&quot;external&quot; href=&quot;https://github.com/prezi/gradle-haskell-plugin#explanation-stack-mode&quot;&gt;in the README&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;how-does-it-work&quot;&gt;How does it work?&lt;/h2&gt;
&lt;p&gt;The core idea did not change &lt;a href=&quot;https://blog.vigoo.dev/posts/gradle-haskell-plugin/&quot;&gt;compared to the original, cabal based solution&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To support chaining the binary artifacts, I had to add a new option to &lt;em&gt;stack&lt;/em&gt; called &lt;a rel=&quot;external&quot; href=&quot;https://github.com/commercialhaskell/stack/pull/990&quot;&gt;extra package databases&lt;/a&gt;. The databases listed in this section are passed &lt;em&gt;after the global&lt;/em&gt; but &lt;strong&gt;before&lt;/strong&gt; the snapshot and the local databases, which means that the snapshot database cannot be used (the packages in the binary artifacts are not &quot;seeing&quot; them). This sounds bad, but &lt;em&gt;gradle-haskell-plugin&lt;/em&gt; does a workaround; it &lt;strong&gt;generates&lt;/strong&gt; the &lt;code&gt;stack.yaml&lt;/code&gt; automatically, and in a way that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;it disables snapshots on stack level (uses a resolver like &lt;code&gt;ghc-7.10.2&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;lists all the dependencies explicitly in &lt;code&gt;extra-deps&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;but it still figures out the &lt;em&gt;versions&lt;/em&gt; of the dependencies (to be listed in &lt;code&gt;extra-deps&lt;/code&gt;) based on a given &lt;em&gt;stackage snapshot&lt;/em&gt;!&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With this approach we get the same behavior that was already proven in cabal mode, but with the advantage that the generated &lt;code&gt;stack.yaml&lt;/code&gt; completely defines the project for any tool that knows stack. So after gradle extracted the dependencies and generated the &lt;code&gt;stack.yaml&lt;/code&gt;, it is no longer needed to succesfully compile/run/test the project, which means that tools like IDE integration will work much better than with the more hacky cabal mode of the plugin.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Case Study - Haskell at Prezi</title>
        <published>2015-09-21T00:00:00+00:00</published>
        <updated>2015-09-21T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/haskell-case-study/"/>
        <id>https://blog.vigoo.dev/posts/haskell-case-study/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/haskell-case-study/">&lt;p&gt;I wrote a &lt;em&gt;case study&lt;/em&gt; for &lt;a rel=&quot;external&quot; href=&quot;http://www.fpcomplete.com&quot;&gt;FPComplete&lt;/a&gt; on how we use Haskell at &lt;a rel=&quot;external&quot; href=&quot;https://prezi.com&quot;&gt;Prezi&lt;/a&gt;. It is published &lt;a rel=&quot;external&quot; href=&quot;https://www.fpcomplete.com/page/case-study-prezi&quot;&gt;here&lt;/a&gt;, but I&#39;m just posting it here as well:&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://prezi.com&quot;&gt;Prezi&lt;/a&gt; is a cloud-based presentation and storytelling tool, based on a zoomable canvas. The company was founded in 2009, and today we have more than 50 million users, with more than 160 million prezis created.&lt;/p&gt;
&lt;p&gt;The company is using several different platforms and technologies; one of these is &lt;em&gt;Haskell&lt;/em&gt;, which we are using server side, for code generation and for testing.&lt;/p&gt;
&lt;h2 id=&quot;pdom&quot;&gt;PDOM&lt;/h2&gt;
&lt;p&gt;Prezi&#39;s document format is continuously evolving as we add features to the application. It is very important for us that this format is handled correctly on all our supported platforms, and both on client and server side. To achieve this, we created an eDSL in Haskell that defines the schema of a Prezi. From this schema we are able to generate several artifacts.&lt;/p&gt;
&lt;p&gt;Most importantly we are generating a &lt;em&gt;Prezi Document Object Model (PDOM)&lt;/em&gt; library for multiple platforms - Haxe (compiled to JS) code for the web, C++ code for the native platforms, and Haskell code for our tests, tools and the server side. These libraries are responsible for loading, updating, maintaining consistency and saving Prezis.&lt;/p&gt;
&lt;p&gt;This API also implements &lt;em&gt;collaborative editing&lt;/em&gt; functionality by transparently synchronising document changes between multiple clients. This technique is called &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Operational_transformation&quot;&gt;operational transformation (OT)&lt;/a&gt;. We implemented the server side of this in Haskell; it supports clients from any of the supported platforms and it is connected to several other backend services.&lt;/p&gt;
&lt;h2 id=&quot;benefits&quot;&gt;Benefits&lt;/h2&gt;
&lt;p&gt;Using &lt;em&gt;Haskell&lt;/em&gt; for this project turned out to have huge benefits.&lt;/p&gt;
&lt;p&gt;We are taking advantage of Haskell&#39;s capabilities to create embedded domain specific languages, using it to define the document&#39;s schema in our own eDSL which is used not only by Haskell developers but many others too.&lt;/p&gt;
&lt;p&gt;Haskell&#39;s clean and terse code allows us to describe document invariants and rules in a very readable way and the type system guarantees that we handle all the necessary cases, providing a stable base Haskell implementation which we can compare the other language backends to.&lt;/p&gt;
&lt;p&gt;It was also possible to define a set of merge laws for OT, which are verified whenever we introduce a new element to the document schema, guaranteeing that the collaboration functionality works correctly.&lt;/p&gt;
&lt;p&gt;We use the &lt;em&gt;QuickCheck&lt;/em&gt; testing library on all levels. We can generate arbitrary Prezi documents and test serialization on all the backends. We are even generating arbitrary JavaScript code which uses our generated API to test random collaborative network sessions. These tests turned out to be critical for our success as they caught many interesting problems before we deployed anything to production&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Haskell plugin for Gradle</title>
        <published>2015-04-22T00:00:00+00:00</published>
        <updated>2015-04-22T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/gradle-haskell-plugin/"/>
        <id>https://blog.vigoo.dev/posts/gradle-haskell-plugin/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/gradle-haskell-plugin/">&lt;p&gt;My team at &lt;a rel=&quot;external&quot; href=&quot;https://prezi.com&quot;&gt;Prezi&lt;/a&gt; uses &lt;strong&gt;Haskell&lt;/strong&gt; for several projects, which usually depend on each other, often with build steps using other languages such as Scala, C++ or Haxe. As &lt;a rel=&quot;external&quot; href=&quot;https://gradle.org/&quot;&gt;Gradle&lt;/a&gt; is used heavily in the company, we decided to try to integrate our Haskell projects within Gradle.&lt;/p&gt;
&lt;p&gt;The result is &lt;a rel=&quot;external&quot; href=&quot;https://github.com/prezi/gradle-haskell-plugin&quot;&gt;Gradle Haskell Plugin&lt;/a&gt;, which we were using succesfully in the last 2 months in our daily work, and we have &lt;em&gt;open-sourced&lt;/em&gt; recently.&lt;/p&gt;
&lt;p&gt;What makes this solution interesting is that it not just simply wraps &lt;em&gt;cabal&lt;/em&gt; within Gradle tasks, but implements a way to define &lt;strong&gt;dependencies&lt;/strong&gt; between Haskell projects and to upload the binary Haskell artifacts to a &lt;em&gt;repository&lt;/em&gt; such as &lt;a rel=&quot;external&quot; href=&quot;http://www.jfrog.com/open-source/&quot;&gt;artifactory&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This makes it easy to modularize our projects, publish them, and also works perfectly with &lt;a rel=&quot;external&quot; href=&quot;https://github.com/prezi/pride&quot;&gt;pride&lt;/a&gt;, an other &lt;em&gt;open-source&lt;/em&gt; Prezi project. This means that we can work on a subset of our Haskell projects while the other dependencies are built on Jenkins, and it also integrates well with our non-Haskell projects.&lt;/p&gt;
&lt;h2 id=&quot;how-does-it-work&quot;&gt;How does it work?&lt;/h2&gt;
&lt;p&gt;The main idea is that we let &lt;em&gt;cabal&lt;/em&gt; manage the Haskell packages, and handle whole Haskell &lt;em&gt;sandboxes&lt;/em&gt; on Gradle level. So if you have a single Haskell project, it will be built using &lt;em&gt;cabal&lt;/em&gt; and the result sandbox (the built project together with all the dependent cabal packages which are not installed in the &lt;em&gt;global package database&lt;/em&gt;) will be packed/published as a Gradle &lt;em&gt;artifact&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;This is not very interesting so far, but when you introduce dependencies on Gradle level, the plugin does something which (as far as I know) is not really done by anyone else, which I call &lt;em&gt;sandbox chaining&lt;/em&gt;. This basically means that to compile the haskell project, the plugin will pass all the dependent sandboxes&#39; package database to cabal and GHC, so for the actual sandbox only the packages which are &lt;strong&gt;not&lt;/strong&gt; in any of the dependent sandboxes will be installed.&lt;/p&gt;
&lt;h2 id=&quot;example&quot;&gt;Example&lt;/h2&gt;
&lt;p&gt;Let&#39;s see an example scenario with &lt;em&gt;4 gradle-haskell projects&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://raw.githubusercontent.com/prezi/gradle-haskell-plugin/master/doc/gradle-haskell-plugin-drawing1.png&quot; class=&quot;zimg&quot;&gt;&lt;img src=&quot;https://raw.githubusercontent.com/prezi/gradle-haskell-plugin/master/doc/gradle-haskell-plugin-drawing1.png&quot; alt=&quot;Diagram of the gradle-haskell-plugin module structure&quot; width=&quot;600&quot; height=&quot;429&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The project called &lt;em&gt;Haskell project&lt;/em&gt; depends on two other projects, which taking into accound the transitive dependencies means it depends on &lt;em&gt;three other haskell projects&lt;/em&gt;. Each project has its own haskell source and &lt;em&gt;cabal file&lt;/em&gt;. Building this suite consists of the following steps:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;dependency 1&lt;/strong&gt; is built using only the &lt;em&gt;global package database&lt;/em&gt;, everything &lt;strong&gt;not&lt;/strong&gt; in that database, together with the compiled project goes into its &lt;code&gt;build/sandbox&lt;/code&gt; directory, which is a combination of a &lt;em&gt;GHC package database&lt;/em&gt; and the project&#39;s build output. This is packed as &lt;strong&gt;dependency 1&lt;/strong&gt;&#39;s build artifact.&lt;/li&gt;
&lt;li&gt;For &lt;strong&gt;dependency 2&lt;/strong&gt;, Gradle first downloads the build artifact of &lt;em&gt;dependency 1&lt;/em&gt; and extracts it to &lt;code&gt;build/deps/dependency1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Then it runs &lt;a rel=&quot;external&quot; href=&quot;https://github.com/exFalso/sandfix&quot;&gt;SandFix&lt;/a&gt; on it&lt;/li&gt;
&lt;li&gt;And compiles the second project, now passing &lt;strong&gt;both&lt;/strong&gt; the &lt;em&gt;global package database&lt;/em&gt; and &lt;strong&gt;dependency 1&lt;/strong&gt;&#39;s sandbox to cabal/ghc. The result is that only the packages which are &lt;strong&gt;not&lt;/strong&gt; in any of these two package databases will be installed in the project&#39;s own sandbox, which becomes the build artifact of &lt;strong&gt;dependency 2&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;strong&gt;dependency 3&lt;/strong&gt;, Gradle extracts both the direct dependency and the transitive dependency&#39;s sandbox, to &lt;code&gt;build/deps/dependency2&lt;/code&gt; and &lt;code&gt;build/deps/dependency3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Then it runs &lt;a rel=&quot;external&quot; href=&quot;https://github.com/exFalso/sandfix&quot;&gt;SandFix&lt;/a&gt; on both the dependencies&lt;/li&gt;
&lt;li&gt;And finally passes three package databases to cabal/ghc to compile the project. Only those cabal dependencies will be installed into this sandbox which are not in global, neither in any of the dependent sandboxes.&lt;/li&gt;
&lt;li&gt;Finally, for &lt;strong&gt;Haskell project&lt;/strong&gt; it goes the same way, but here we have three sandboxes, all chained together to make sure only the built sandbox only contains what is not in the dependent sandboxes yet.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For more information, check out &lt;a rel=&quot;external&quot; href=&quot;https://github.com/prezi/gradle-haskell-plugin&quot;&gt;the documentation&lt;/a&gt;.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>bari 1.0 released</title>
        <published>2014-12-08T00:00:00+00:00</published>
        <updated>2014-12-08T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/bari-1-0/"/>
        <id>https://blog.vigoo.dev/posts/bari-1-0/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/bari-1-0/">&lt;p&gt;I already wrote about &lt;a rel=&quot;external&quot; href=&quot;http://vigoo.github.io/bari&quot;&gt;bari&lt;/a&gt; in &lt;a href=&quot;https://blog.vigoo.dev/posts/introducing-bari/&quot;&gt;May&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;As a reminder, &lt;a rel=&quot;external&quot; href=&quot;http://vigoo.github.io/bari&quot;&gt;bari&lt;/a&gt; is a &lt;em&gt;build management system&lt;/em&gt; primarily for .NET, trying to fix Visual Studio&#39;s bad parts while keeping the good ones.&lt;/p&gt;
&lt;p&gt;After more than two years of development, and being in production at &lt;a rel=&quot;external&quot; href=&quot;http://www.kotem.com/&quot;&gt;KOTEM&lt;/a&gt; for almost half a year, bari reached a state when it can be considered as a &lt;em&gt;stable&lt;/em&gt; and &lt;em&gt;usable&lt;/em&gt; first version.&lt;/p&gt;
&lt;p&gt;To indicate this today I released &lt;strong&gt;bari 1.0&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Try it out and feel free to give any kind of feedback or ask any questions!&lt;/p&gt;
&lt;img src=&quot;/images/barilogo-small.webp&quot; alt=&quot;The bari logo&quot; width=&quot;45&quot; height=&quot;64&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>ScalaFXML 0.2.2 available</title>
        <published>2014-10-22T00:00:00+00:00</published>
        <updated>2014-10-22T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/scalafxml-0-2-2/"/>
        <id>https://blog.vigoo.dev/posts/scalafxml-0-2-2/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/scalafxml-0-2-2/">&lt;p&gt;I&#39;ve released a new version of &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/scalafxml&quot;&gt;ScalaFXML&lt;/a&gt;, which now supports &lt;em&gt;both&lt;/em&gt; &lt;a rel=&quot;external&quot; href=&quot;https://github.com/scalafx/scalafx&quot;&gt;ScalaFX 8&lt;/a&gt; with &lt;em&gt;JavaFX 8&lt;/em&gt; on Java 8, and &lt;a rel=&quot;external&quot; href=&quot;https://github.com/scalafx/scalafx&quot;&gt;ScalaFX 2.2&lt;/a&gt; with &lt;em&gt;JavaFX 2.x&lt;/em&gt; on Java 7.&lt;/p&gt;
&lt;p&gt;The two branches are separated by the &lt;code&gt;sfx2&lt;/code&gt; and &lt;code&gt;sfx8&lt;/code&gt; postfixes, and both are available for &lt;em&gt;Scala&lt;/em&gt; &lt;code&gt;2.10.x&lt;/code&gt; and &lt;code&gt;2.11.x&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To use it with &lt;a rel=&quot;external&quot; href=&quot;http://www.scala-sbt.org/&quot;&gt;sbt&lt;/a&gt; on Java 7:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;addCompilerPlugin&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;org.scalamacros&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;paradise&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;2.0.1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; cross &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;CrossVersion&lt;/span&gt;&lt;span&gt;.full&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;libraryDependencies &lt;/span&gt;&lt;span&gt;+=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;org.scalafx&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; %%&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;scalafx&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;2.2.67-R10&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;libraryDependencies &lt;/span&gt;&lt;span&gt;+=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;org.scalafx&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; %%&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;scalafxml-core-sfx2&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;0.2.2&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And on Java 8:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;addCompilerPlugin&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;org.scalamacros&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;paradise&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;2.0.1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; cross &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;CrossVersion&lt;/span&gt;&lt;span&gt;.full&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;libraryDependencies &lt;/span&gt;&lt;span&gt;+=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;org.scalafx&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; %%&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;scalafx&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;8.0.20-R6&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;libraryDependencies &lt;/span&gt;&lt;span&gt;+=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;org.scalafx&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; %%&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;scalafxml-core-sfx8&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;0.2.2&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>A python/thrift profiling story</title>
        <published>2014-09-15T00:00:00+00:00</published>
        <updated>2014-09-15T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/thrift-profiling/"/>
        <id>https://blog.vigoo.dev/posts/thrift-profiling/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/thrift-profiling/">&lt;p&gt;A few weeks ago I met a problem where a script, running once every night sending out some emails did not run correctly because a remote thrift call timed out in it. As I started investigating it, turned out that it&#39;s a &lt;em&gt;search&lt;/em&gt; call:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;staff_users&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-3&quot;&gt; RemoteUserFactory&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-3&quot;&gt;search&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9 z-d-i&quot;&gt;is_staff&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;True&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The details here are not really important, what this call does is that it asks a service to return a &lt;em&gt;set of users&lt;/em&gt;, and the communication is going on &lt;a rel=&quot;external&quot; href=&quot;https://thrift.apache.org/&quot;&gt;thrift&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Executing it manually on the server revealed that it should return &lt;em&gt;5649&lt;/em&gt; users. Checking out the logs I could see that the call took extremely long time, between 8 to 12 seconds. Even when the cron job was moved from 3:00 AM to a less busy time (several other jobs were executing at the same time), it took more than 6 seconds!&lt;/p&gt;
&lt;p&gt;This was suspicious so I also checked the log of a &lt;em&gt;proxy&lt;/em&gt; which runs on the same host as the script itself and provides client side load balancing, circuit breaking, retry logic etc. for thrift connections. This log showed that the service replied in &lt;em&gt;2.5 seconds&lt;/em&gt;, but it took almost 4 seconds to get this response from the proxy to the client on localhost! This seemed to be completely unacceptable, and also the 2.5 second response time from the service seemed to be too big (I ran the query on one of the server nodes and it returned the users from the database almost instantly). I also had similar experience (but without measurements) before.&lt;/p&gt;
&lt;p&gt;So I decided to find out what&#39;s going on. And I found the process interesting enough to write this post about it :)&lt;/p&gt;
&lt;h2 id=&quot;test-environment&quot;&gt;Test environment&lt;/h2&gt;
&lt;p&gt;I started by adding a test method to the service&#39;s thrift API called &lt;code&gt;test_get_users(count, sleep)&lt;/code&gt; which returns &lt;code&gt;count&lt;/code&gt; fake users after waiting &lt;code&gt;sleep&lt;/code&gt; seconds. Then in the following experiments I called it with &lt;code&gt;(5499, 1)&lt;/code&gt;. The 1 second sleep was intended to simulate the network latency and database query; there was no advantage from having it at the end, but as it is visible everywhere in the results, I had to mention.&lt;/p&gt;
&lt;p&gt;For finding out what&#39;s going on I used &lt;a rel=&quot;external&quot; href=&quot;https://docs.python.org/2/library/profile.html&quot;&gt;cProfile&lt;/a&gt; with &lt;a rel=&quot;external&quot; href=&quot;https://code.google.com/p/jrfonseca/&quot;&gt;gprof2dot&lt;/a&gt;, calling the remote test method from a django shell, while everything is running on localhost.&lt;/p&gt;
&lt;h3 id=&quot;first-measurement&quot;&gt;First measurement&lt;/h3&gt;
&lt;p&gt;Without touching anything, returning 5499 dummy users on localhost took &lt;strong&gt;5.272 seconds&lt;/strong&gt;!&lt;/p&gt;
&lt;p&gt;The client side of the call looked like this:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/images/profile1.webp&quot; class=&quot;zimg&quot;&gt;&lt;img src=&quot;/images/profile1.webp&quot; alt=&quot;Call graph of the initial Thrift get_users test run, dominated by Python object construction&quot; width=&quot;600&quot; height=&quot;644&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here we can see that the call has two major phases:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The thrift call itself (65%)&lt;/li&gt;
&lt;li&gt;Converting the raw results to model objects with &lt;code&gt;_row_to_model&lt;/code&gt; (35%)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&#39;s see first the thrift call (the green branch on the picture). Once again it has two, nearly equivalent branches:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;send_test_get_users&lt;/code&gt; which sends the request and waits for the response. This includes the 1 second sleep as well.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;recv_test_get_users&lt;/code&gt; processes the response&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;What&#39;s interesting here is that &lt;code&gt;recv_test_get_users&lt;/code&gt; took ~32% of the overall time which is around ~1.6 seconds for simple data deserialization.&lt;/p&gt;
&lt;h3 id=&quot;optimizing-thrift-deserialization&quot;&gt;Optimizing thrift deserialization&lt;/h3&gt;
&lt;p&gt;I did not want to believe that the python thrift deserialization is that slow, so I did a search and found that the &lt;code&gt;TBinaryProtocol&lt;/code&gt; which we are using is really that slow.&lt;/p&gt;
&lt;p&gt;But the thrift library contains a class called &lt;code&gt;TBinaryProtocolAccelerated&lt;/code&gt; which is about 10x faster (according to a stackoverflow post).&lt;/p&gt;
&lt;p&gt;First I simply changed the used protocol to this, but nothing happened. Digging deeper I found that this is not a real protocol implementation, but a lower level hack.&lt;/p&gt;
&lt;p&gt;The documentation of the protocol class says:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  C-Accelerated version of TBinaryProtocol.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  This class does not override any of TBinaryProtocol&amp;#39;s methods,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  but the generated code recognizes it directly and will call into&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  our C module to do the encoding, bypassing this object entirely.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  We inherit from TBinaryProtocol so that the normal TBinaryProtocol&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  encoding can happen if the fastbinary module doesn&amp;#39;t work for some&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  reason.  (TODO(dreiss): Make this happen sanely in more cases.)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  In order to take advantage of the C module, just use&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  TBinaryProtocolAccelerated instead of TBinaryProtocol.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So why didn&#39;t it work? The answer is in &lt;a rel=&quot;external&quot; href=&quot;https://github.com/apache/thrift/blob/master/lib/py/src/protocol/TBase.py#L52-L58&quot;&gt;TBase.py&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The following conditions have to met in order to use the fast deserializer:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Protocol must be &lt;code&gt;TBinaryProtocolAccelerated&lt;/code&gt; (I changed that)&lt;/li&gt;
&lt;li&gt;Protocol&#39;s transport implementation must implement the &lt;code&gt;TTransport.CReadableTransport&lt;/code&gt; interface&lt;/li&gt;
&lt;li&gt;&lt;code&gt;thrift_spec&lt;/code&gt; must be available (this was true in this case)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;fastbinary&lt;/code&gt; must be available (also true)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The problem was that we were replacing the &lt;code&gt;TTransport&lt;/code&gt; implementation with a custom class called &lt;code&gt;ThriftifyTransport&lt;/code&gt; in order to do thrift logging, HMAC authentication, etc.&lt;/p&gt;
&lt;p&gt;Fortunately all the default transport implementations implement the &lt;code&gt;CReadableTransport&lt;/code&gt; interface, and one of them, &lt;code&gt;TBufferedTransport&lt;/code&gt; can be used to wrap another transport to add buffering around it. That&#39;s what I did, and it immediately started using the fast deserialization code.&lt;/p&gt;
&lt;p&gt;The test call now ran in &lt;strong&gt;3.624 seconds&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;And the new profiling results with this change:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/images/profile2.webp&quot; class=&quot;zimg&quot;&gt;&lt;img src=&quot;/images/profile2.webp&quot; alt=&quot;Call graph with cached configuration, shifting time into the Thrift call path&quot; width=&quot;600&quot; height=&quot;678&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The left-hand side of the call graph remained the same, but &lt;code&gt;recv_test_get_users&lt;/code&gt; is now only 2.35% of the overall time which is ~0.08 seconds (to be compared with the 1.6 seconds with the original deserializer!)&lt;/p&gt;
&lt;h3 id=&quot;optimizing-thrift-serialization&quot;&gt;Optimizing thrift serialization&lt;/h3&gt;
&lt;p&gt;The obvious next step was to apply this change on the server side as well, so our service can use the fast binary protocol for serialization too. For this I simply copied the change and remeasured everything.&lt;/p&gt;
&lt;p&gt;The test call now ran in &lt;strong&gt;3.328 seconds&lt;/strong&gt;!&lt;/p&gt;
&lt;p&gt;Let&#39;s see the call graph of this stage:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/images/profile3.webp&quot; class=&quot;zimg&quot;&gt;&lt;img src=&quot;/images/profile3.webp&quot; alt=&quot;Call graph after caching the Thrift model, with serialization and socket reads remaining&quot; width=&quot;600&quot; height=&quot;695&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&quot;optimizing-result-processing&quot;&gt;Optimizing result processing&lt;/h3&gt;
&lt;p&gt;The client side of the test method was written similar to how the original API method is written:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; test_get_users_thrift&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8 z-d-i&quot;&gt; count&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8 z-d-i&quot;&gt; sleep&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    rpc&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-3&quot;&gt; ThriftRPC&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;UserDataService&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9 z-d-i&quot;&gt; service_name&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;service_name&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9 z-d-i&quot;&gt; client_config&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt;client_config&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    result&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    for&lt;/span&gt;&lt;span&gt; row&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; in&lt;/span&gt;&lt;span&gt; rpc&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-3&quot;&gt;test_get_users&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;count&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; sleep&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-3&quot;&gt;iteritems&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        user&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-3&quot;&gt;_row_to_model&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-10&quot;&gt;self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;user_factory&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; row&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        result&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-3&quot;&gt;append&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;user&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    return&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It is clearly visible on the call graph that the 5499 call to &lt;code&gt;_row_to_model&lt;/code&gt; takes 53% of the total time, which is ~1.7 seconds. There are two main branches of this call. The left hand side (&lt;code&gt;row_to_model&lt;/code&gt;) seemed to be simple data conversion, and its slowest part is date-time deserialization.&lt;/p&gt;
&lt;p&gt;The other branch however looked like a real problem; why should we resolve HMAC host, or parse configuration for each row?&lt;/p&gt;
&lt;p&gt;It turned out to be a bug, &lt;code&gt;_row_to_model&lt;/code&gt; created a new &lt;em&gt;model factory&lt;/em&gt; in each call, which involves a lot of initialization, config parsing, and similar things.&lt;/p&gt;
&lt;p&gt;So the simple fix was to create a &lt;code&gt;_rows_to_model&lt;/code&gt; helper function which does the same for multiple rows with a single factory.&lt;/p&gt;
&lt;p&gt;Running my test code once again showed that the optimization makes sense. Now it ran in &lt;strong&gt;2.448 seconds&lt;/strong&gt;, with the following call graph:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/images/profile4.webp&quot; class=&quot;zimg&quot;&gt;&lt;img src=&quot;/images/profile4.webp&quot; alt=&quot;Call graph of the optimized path, dominated by the HTTP request and transport flush&quot; width=&quot;600&quot; height=&quot;923&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&quot;further-optimizations&quot;&gt;Further optimizations&lt;/h3&gt;
&lt;p&gt;I saw two possible ways to further optimize this case:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Lazy conversion of raw thrift data to model data (per field). This would make sense because many times only a few fields (the id for example) are used, but it seemed to be a too complex change&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Checking the server side as well&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To profile the server side and only measure the thrift request processing I had to add profiling code to the django view class in the following way:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;import&lt;/span&gt;&lt;span&gt; cProfile&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cProfile&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-3&quot;&gt;runctx&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;self._call_processor(op_data)&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; globals&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; locals&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;callstats&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; self._call_processor(op_data)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The server-side call took &lt;strong&gt;1.691 seconds&lt;/strong&gt; and looked like this:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/images/profile5.webp&quot; class=&quot;zimg&quot;&gt;&lt;img src=&quot;/images/profile5.webp&quot; alt=&quot;Call graph of the final run, with the process mostly sleeping&quot; width=&quot;600&quot; height=&quot;773&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As expected, 60% of this was the 1 second sleep. The rest of the calls are data conversion with no obvious point to improve.&lt;/p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;
&lt;p&gt;These optimizations are decreasing the response time significantly, especially for calls returning multiple rows.&lt;/p&gt;
&lt;p&gt;The interesting was that the extremely slow performance was caused by both the slow perfomance of the python thrift serializer and a bug in our code.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Conditional blocks in Distributed Documentor</title>
        <published>2014-07-13T00:00:00+00:00</published>
        <updated>2014-07-13T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/conditional-blocks-in-ddoc/"/>
        <id>https://blog.vigoo.dev/posts/conditional-blocks-in-ddoc/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/conditional-blocks-in-ddoc/">&lt;p&gt;I&#39;ve added a new feature to &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/distributed-documentor&quot;&gt;Distributed Documentor&lt;/a&gt; today, &lt;em&gt;conditional blocks&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The idea is that parts of the documents can be enabled when a given &lt;em&gt;condition&lt;/em&gt; is present. This is very similar to &lt;a rel=&quot;external&quot; href=&quot;http://gcc.gnu.org/onlinedocs/cpp/Ifdef.html&quot;&gt;C&#39;s ifdef blocks&lt;/a&gt;. To use it with the &lt;em&gt;MediaWiki syntax&lt;/em&gt;, put &lt;code&gt;[When:X]&lt;/code&gt; and &lt;code&gt;[End]&lt;/code&gt; commands in separate lines:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Unconditional&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[When:FIRST]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;First conditional&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[When:SECOND]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;First and second conditional&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[End]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[End]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[When:SECOND]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Second conditional&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[End]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Snippets&lt;/em&gt; can also have conditional blocks.&lt;/p&gt;
&lt;p&gt;There are two possibilities to set which conditionals are enabled:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Specifying it with command line arguments, such as&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; java -jar DistributedDocumentor.jar -D FIRST -D SECOND&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is useful when exporting a documentation from command line, or to launch the documentation editor with a predefined set of enabled conditions.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;On the user interface, using &lt;em&gt;View&lt;/em&gt; menu&#39;s &lt;em&gt;Enabled conditions...&lt;/em&gt; menu item:&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;img src=&quot;/images/enabled-conditions-dialog.webp&quot; alt=&quot;The D editor&#39;s Enabled conditions dialog with the FIRST and SECOND conditions checked&quot; width=&quot;537&quot; height=&quot;430&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Introducing bari</title>
        <published>2014-05-16T00:00:00+00:00</published>
        <updated>2014-05-16T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/introducing-bari/"/>
        <id>https://blog.vigoo.dev/posts/introducing-bari/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/introducing-bari/">&lt;p&gt;In the past two years I worked on a project called &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/bari&quot;&gt;bari&lt;/a&gt; which now reached an usable state. &lt;strong&gt;bari&lt;/strong&gt; is a &lt;em&gt;build management system&lt;/em&gt;, trying to fix Visual Studio&#39;s bad parts while keeping the good ones.&lt;/p&gt;
&lt;p&gt;Basically it tries to make .NET development more convenient, when&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The application may consist of a &lt;em&gt;large number of projects&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;There may be several different &lt;em&gt;subsets&lt;/em&gt; of these projects defining
valuable target &lt;em&gt;products&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Custom build steps&lt;/em&gt; may be required&lt;/li&gt;
&lt;li&gt;It is important to be able to &lt;em&gt;reproduce&lt;/em&gt; the build environment as
easily as possible&lt;/li&gt;
&lt;li&gt;The developers want to use the full power of their &lt;em&gt;IDE&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The main idea is to generate Visual Studio solutions and projects &lt;em&gt;on the fly&lt;/em&gt; as needed, from a concise &lt;em&gt;declarative&lt;/em&gt;  build description. I tried to optimize this build description for human readability. Let&#39;s see an example, a short section from &lt;strong&gt;bari&lt;/strong&gt;&#39;s own build definition:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ame&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ari&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  t&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;ype&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; e&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;xecutable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  r&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;eferences&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; g&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ac://System&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;uget://log4net&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;uget://Ninject/3.0.1.10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;uget://QuickGraph&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    -&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;odule://Bari.Core&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;  c&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;sharp&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;    r&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;oot-namespace&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;ari.Console&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The main advantage of generating solutions and projects on the fly is that each developer can work on the subset he needs for his current task keeping the IDE fast, but can also open everything in one solution if it is useful for performing a refactoring.&lt;/p&gt;
&lt;p&gt;To keep build definitions short and readable, &lt;strong&gt;bari&lt;/strong&gt; prefers &lt;em&gt;convention&lt;/em&gt; over &lt;em&gt;configuration&lt;/em&gt;. For example the directory stucture in which the source code lays defines not only the name of the modules to build, but also the way it is built. For example, in a simple &lt;em&gt;hello world&lt;/em&gt; example the C# source code would be put in the &lt;code&gt;src/TestModule/HelloWorld/cs&lt;/code&gt; directory, and &lt;strong&gt;bari&lt;/strong&gt; would build &lt;code&gt;target/TestModule/HelloWorld.exe&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;bari&lt;/strong&gt; unifies the handling of &lt;em&gt;project references&lt;/em&gt; in a way that referencing projects within a suite, from the GAC, using &lt;a rel=&quot;external&quot; href=&quot;http://www.nuget.org&quot;&gt;Nuget&lt;/a&gt; or from a custom repository works exactly the same. It is also possible to write &lt;em&gt;custom builders&lt;/em&gt; in Python.&lt;/p&gt;
&lt;p&gt;For more information check out &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/bari/wiki/GettingStarted&quot;&gt;the getting started page&lt;/a&gt;.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>ScalaFX with FXML</title>
        <published>2014-01-12T00:00:00+00:00</published>
        <updated>2014-01-12T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/scalafx-with-fxml/"/>
        <id>https://blog.vigoo.dev/posts/scalafx-with-fxml/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/scalafx-with-fxml/">&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://code.google.com/p/scalafx/&quot;&gt;ScalaFX&lt;/a&gt; is a nice wrapper around JavaFX for Scala, but currently it lacks support for using &lt;a rel=&quot;external&quot; href=&quot;http://docs.oracle.com/javafx/2/api/javafx/fxml/doc-files/introduction_to_fxml.html&quot;&gt;FXML&lt;/a&gt; instead of Scala code for defining the user interfaces. This can be understood as &lt;em&gt;ScalaFX&lt;/em&gt; is in fact a DSL for defining the UI in Scala instead of an XML file. Still I believe that using FXML instead may have its advantages; first of all it has a visual designer (&lt;a rel=&quot;external&quot; href=&quot;http://www.oracle.com/technetwork/java/javafx/tools/index.html&quot;&gt;JavaFX Scene Builder&lt;/a&gt;). For me, designing an UI without immediate visual feedback is hard, and involves a lot of iterations of tweaking the code, running it and checking the results. I also expect that in the future there will be more tools available which work on FXML data.&lt;/p&gt;
&lt;p&gt;It is not impossible to use FXML user interfaces from Scala, but the ScalaFX wrappers does not help and the code for the controller classes is not clean enough. See &lt;a rel=&quot;external&quot; href=&quot;https://github.com/jpsacha/ProScalaFX/blob/master/src/proscalafx/ch10/fxml/AdoptionFormController.scala&quot;&gt;the following example&lt;/a&gt; to get a feeling how it looks like.&lt;/p&gt;
&lt;p&gt;To make it better I wrote a small library called &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/scalafxml&quot;&gt;ScalaFXML&lt;/a&gt;. In this post I&#39;ll go through a small example to explain how it works.&lt;/p&gt;
&lt;p&gt;The following image shows how our sample application will look like:&lt;/p&gt;
&lt;img src=&quot;/images/unit-conversion-shot.webp&quot; alt=&quot;The running unit converter converting ten inches to 254 millimeters&quot; width=&quot;437&quot; height=&quot;251&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;The &lt;em&gt;From&lt;/em&gt; fiels is editable, and the result in the &lt;em&gt;To&lt;/em&gt; field is filled as you type using &lt;em&gt;data binding&lt;/em&gt;. The &lt;em&gt;Close&lt;/em&gt; button&#39;s only purpose is to demonstrate event handlers.&lt;/p&gt;
&lt;p&gt;The conversion logic itself is implemented by &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/scalafxml/blob/master/demo/src/main/scala/scalafxml/demo/unitconverter/UnitConverter.scala&quot;&gt;small classes&lt;/a&gt; sharing the same trait:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UnitConverter&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; description&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; toString&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; description&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; MMtoInches&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UnitConverter&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; description&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Millimeters to inches&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      try&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          (&lt;/span&gt;&lt;span&gt;input.toDouble &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 25.4&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.toString &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; catch&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;          case&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; ex&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Throwable&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; ex.toString &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; InchesToMM&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UnitConverter&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; description&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Inches to millimeters&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      try&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          (&lt;/span&gt;&lt;span&gt;input.toDouble &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 25.4&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.toString &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; catch&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;          case&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt; ex&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Throwable&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; ex.toString &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To describe the set of available &lt;em&gt;unit converters&lt;/em&gt;, we define one more helper class:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UnitConverters&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;converters&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UnitConverter&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; available&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; List&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;converters&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span&gt; _*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let&#39;s start with a &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/scalafxml/blob/master/demo/src/main/scala/scalafxml/demo/unitconverter/PureScalaFX.scala&quot;&gt;pure ScalaFX solution&lt;/a&gt;, where the user interface is defined in Scala. I&#39;ve implemented the view itself in a class called &lt;code&gt;PureScalaFXView&lt;/code&gt;, which gets the set of available &lt;em&gt;unit converters&lt;/em&gt; as a dependency through its constructor. This makes the main application object very simple:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PureScalaFX&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; JFXApp&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  stage &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PureScalaFXView&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UnitConverters&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;InchesToMM&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;MMtoInches&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;PureScalaFXView&lt;/code&gt; class consists of two distinct parts. First we define the user interface using the &lt;em&gt;ScalaFX UI DSL&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PureScalaFXView&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;converters&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UnitConverters&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; JFXApp&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PrimaryStage&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; UI Definition&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  title &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Unit conversion&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; types&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ComboBox&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;UnitConverter&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    maxWidth &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Double&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;MaxValue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    margin &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Insets&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; from&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextField&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    margin &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Insets&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    prefWidth &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 200.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextField&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    prefWidth &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; 200.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    margin &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Insets&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    editable &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  scene &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Scene&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    content &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; GridPane&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      padding &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Insets&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Label&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Conversion type:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Label&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;From:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Label&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;To:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;types, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;from, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;to, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Button&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Close&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; inline event handler binding&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        onAction &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;e&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ActionEvent&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Platform&lt;/span&gt;&lt;span&gt;.exit&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      columnConstraints &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; List&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ColumnConstraints&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          halignment &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HPos&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;LEFT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          hgrow &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Priority&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;SOMETIMES&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          margin &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Insets&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ColumnConstraints&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          halignment &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HPos&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;RIGHT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          hgrow &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Priority&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ALWAYS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          margin &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Insets&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is not 100% pure UI definition, because it also contains an inline event handler definition for the &lt;em&gt;Close&lt;/em&gt; button.&lt;/p&gt;
&lt;p&gt;The next part fills the &lt;em&gt;combo box&lt;/em&gt; and defines the data binding. Filling the combo box is a simple procedural loop:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;converter &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; converters.available&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    types &lt;/span&gt;&lt;span&gt;+=&lt;/span&gt;&lt;span&gt; converter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  types.getSelectionModel.selectFirst&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For the data binding we define a &lt;a rel=&quot;external&quot; href=&quot;http://docs.oracle.com/javafx/2/binding/jfxpub-binding.htm&quot;&gt;low level data binding&lt;/a&gt; which depends on the combo box&#39;s selected value and the &lt;em&gt;From&lt;/em&gt; field&#39;s text, and produces the output for the &lt;em&gt;To&lt;/em&gt; field:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  to.text &lt;/span&gt;&lt;span&gt;&amp;lt;==&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; StringBinding&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    bind&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;from.text.delegate, types.getSelectionModel.selectedItemProperty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; computeValue&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; types.getSelectionModel.getSelectedItem.run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;from.text.value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That&#39;s all, the application is fully functional. The next thing is to split this class so the UI definition and the UI logic got separated. This &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/scalafxml/blob/master/demo/src/main/scala/scalafxml/demo/unitconverter/RefactoredPureScalaFX.scala&quot;&gt;refactored ScalaFX solution&lt;/a&gt; is very similar to the previous one, but the initialization of the combo box, the data binding and the event handler are all encapsulated by a new, separate class:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; RawUnitConverterPresenter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                              private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; from&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextField&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                              private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; to&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextField&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                              private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; types&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ComboBox&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;UnitConverter&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                              private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; converters&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UnitConverters&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Filling the combo box&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;converter &lt;/span&gt;&lt;span&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; converters.available&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    types &lt;/span&gt;&lt;span&gt;+=&lt;/span&gt;&lt;span&gt; converter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  types.getSelectionModel.selectFirst&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Data binding&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  to.text &lt;/span&gt;&lt;span&gt;&amp;lt;==&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; StringBinding&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    bind&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;from.text.delegate, types.getSelectionModel.selectedItemProperty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; computeValue&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; types.getSelectionModel.getSelectedItem.run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;from.text.value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Close button event handler&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; onClose&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;event&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ActionEvent&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;    Platform&lt;/span&gt;&lt;span&gt;.exit&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What I wanted is to be able to define the controller class exactly like this while building the user interface from FXML. Without &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/scalafxml&quot;&gt;ScalaFXML&lt;/a&gt; the controller class have some serious limitations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It must implement the &lt;a rel=&quot;external&quot; href=&quot;http://docs.oracle.com/javafx/2/api/javafx/fxml/Initializable.html&quot;&gt;Initializable&lt;/a&gt; interface&lt;/li&gt;
&lt;li&gt;It cannot have any constructor arguments&lt;/li&gt;
&lt;li&gt;The user interface objects must be variable fields of the class&lt;/li&gt;
&lt;li&gt;And they have to have the type of the JavaFX controls, so to be able to use the ScalaFX wrappers, they have to be explicitly wrapped in the &lt;code&gt;initialize&lt;/code&gt; method.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/scalafxml&quot;&gt;ScalaFXML&lt;/a&gt; the process is really simple. First we create the FXML, for example with the &lt;a rel=&quot;external&quot; href=&quot;http://www.oracle.com/technetwork/java/javafx/tools/index.html&quot;&gt;JavaFX Scene Builder&lt;/a&gt;:&lt;/p&gt;
&lt;img src=&quot;/images/unit-conversion-scenebuilder.webp&quot; alt=&quot;JavaFX Scene Builder editing the unit converter&#39;s GridPane layout&quot; width=&quot;1031&quot; height=&quot;594&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;
&lt;p&gt;In the FXML we give the &lt;code&gt;from&lt;/code&gt;, &lt;code&gt;to&lt;/code&gt;, and &lt;code&gt;types&lt;/code&gt; identifiers to our controls using the &lt;code&gt;fx:id&lt;/code&gt; attribute, for example:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;TextField&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; fx&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;id&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; prefWidth&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;200.0&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;               GridPane.columnIndex&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;               GridPane.margin&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;$x1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;               GridPane.rowIndex&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The event handlers can be specified simply by their name:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;Button&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; onAction&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;#onClose&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; text&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Close&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;        mnemonicParsing&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;        GridPane.columnIndex&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;        GridPane.halignment&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;RIGHT&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;        GridPane.rowIndex&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and the controller class must be referenced on the root node&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fx:controller=&amp;quot;scalafxml.demo.unitconverter.UnitConverterPresenter&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The controller class &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/scalafxml/blob/master/demo/src/main/scala/scalafxml/demo/unitconverter/ScalaFXML.scala&quot;&gt;can be exactly the same as the &lt;code&gt;RawUnitConverterPresenter&lt;/code&gt;&lt;/a&gt;, adding an additional &lt;code&gt;@sfxml&lt;/code&gt; annotation for it. Everything else is handled by the library, as we will see.&lt;/p&gt;
&lt;p&gt;The application object itself looks like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ScalaFXML&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; extends&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; JFXApp&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; root&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; FXMLView&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;getClass.getResource&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;unitconverter.fxml&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DependenciesByType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      typeOf&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;UnitConverters&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UnitConverters&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;InchesToMM&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;MMtoInches&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  stage &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; JFXApp&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;PrimaryStage&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    title &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Unit conversion&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    scene &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Scene&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;root&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Beside giving the URI for the FXML file we also has to provide the &lt;em&gt;additional dependencies&lt;/em&gt; of the controller class. This is an easily extensible part of the library, and it already has support for &lt;a rel=&quot;external&quot; href=&quot;https://github.com/dickwall/subcut&quot;&gt;SubCut&lt;/a&gt; and &lt;a rel=&quot;external&quot; href=&quot;https://code.google.com/p/google-guice/&quot;&gt;Guice&lt;/a&gt; as well. Here we are using a simple &lt;em&gt;type-&amp;gt;value&lt;/em&gt; mapping instead.&lt;/p&gt;
&lt;p&gt;How does this work? What happens behind the scenes?&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;@sfxml&lt;/code&gt; is a &lt;a rel=&quot;external&quot; href=&quot;http://docs.scala-lang.org/overviews/macros/annotations.html&quot;&gt;macro annotation&lt;/a&gt;. In &lt;em&gt;compile-time&lt;/em&gt;, the class definition itself is transformed by the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/vigoo/scalafxml/blob/master/core-macros/src/main/scala/scalafxml/core/macros/sfxmlMacro.scala&quot;&gt;&lt;code&gt;sfxmlMacro.impl&lt;/code&gt; function&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The transformation&#39;s result is a class definition with the source class&#39; name, but with a completely different content. The original class is added as an inner class, always called &lt;code&gt;Controller&lt;/code&gt;. In our example, the generated class definition would look like something similar:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UnitConverterPresenter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; dependencyResolver&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ControllerDependencyResolver&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    extends&lt;/span&gt;&lt;span&gt; javafx.fxml.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Initializable&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    with&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; FxmlProxyGenerator&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ProxyDependencyInjection&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Controller&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; from&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextField&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; to&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; TextField&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; types&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ComboBox&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;UnitConverter&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; converters&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; UnitConverters&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; …&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; impl&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Controller&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; …&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;   &lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The class have four distinct parts:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Getting the additional dependencies from the &lt;em&gt;dependency resolver&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;Variable fields for binding the JavaFX controls defined in the FXML&lt;/li&gt;
&lt;li&gt;Event handler methods&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;initializable&lt;/code&gt; method&#39;s implementation&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The first one is simple - for each constructor argument of the controller class which is &lt;em&gt;not&lt;/em&gt; a ScalaFX control, we query the &lt;em&gt;dependency resolver&lt;/em&gt; to get a value for it. These are performed when the outer, generated class is instantiated and stored through the &lt;code&gt;ProxyDependencyInjection&lt;/code&gt; trait.&lt;/p&gt;
&lt;p&gt;The variable fields are simple fields for all the ScalaFX constructor arguments of the controller class, but converted to their JavaFX counterpart. For example the generated field for the controller&#39;s &lt;code&gt;from&lt;/code&gt; argument will look like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;javafx.fxml.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;FXML&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; var&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt; from&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; javafx.scene.control.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TextField&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;em&gt;event handler&lt;/em&gt;&#39;s are proxies for all the public methods of the controller, but the ScalaFX event argument types are replaced with JavaFX event argument types and they are wrapped automatically when forwarding the call to the real implementation. For the &lt;code&gt;onClose&lt;/code&gt; event handler it would look like the following:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;/span&gt;&lt;span&gt;javafx.fxml.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;FXML&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; onClose&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;e&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; javafx.event.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ActionEvent&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    impl.onClose&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;new&lt;/span&gt;&lt;span&gt; scalafx.event.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ActionEvent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;e&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When JavaFX calls the generated controller&#39;s &lt;code&gt;initialize&lt;/code&gt; method, the control fields are already set up, and the additional dependencies were already gathered from the dependency resolver so we have all the values required to instantiate the real controller class. For ScalaFX arguments we wrap the JavaFX controls, for the additional dependencies we use the &lt;code&gt;ProxyDependencyInjection&lt;/code&gt; trait&#39;s &lt;code&gt;getDependency&lt;/code&gt; method:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; initialize&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;url&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; java.net.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;URL&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;rb&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; java.util.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ResourceBundle&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    impl &lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Controller&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        new&lt;/span&gt;&lt;span&gt; scalafx.scene.control.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TextField&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;from&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        new&lt;/span&gt;&lt;span&gt; scalafx.scene.control.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;TextField&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;to&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        new&lt;/span&gt;&lt;span&gt; scalafx.scene.control.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ComboBox&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;UnitConverter&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;types&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        getDependencies&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;UnitConverters&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;converters&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That&#39;s all. The final interesting bit is the &lt;code&gt;FXMLView&lt;/code&gt; object, which overrides JavaFX&#39;s default controller factory. This is only necessary to be able to pass the given &lt;code&gt;ControllerDependencyResolver&lt;/code&gt; to the generated controller&#39;s constructor:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; apply&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;fxml&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; URL&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;dependencies&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ControllerDependencyResolver&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; jfxs.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Parent&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    jfxf.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;FXMLLoader&lt;/span&gt;&lt;span&gt;.load&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      fxml, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt;      null&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      new&lt;/span&gt;&lt;span&gt; jfxf.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;JavaFXBuilderFactory&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;      new&lt;/span&gt;&lt;span&gt; jfxu.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Callback&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Class&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Object&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        override&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; call&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9 z-d-i&quot;&gt;cls&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Class&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Object&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;            FxmlProxyGenerator&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;cls, dependencies&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;FxmlProxyGenerator&lt;/code&gt; uses reflection to create a new instance of the generated controller, and pass the dependency resolver as its only constructor argument.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Trying out Ceylon - Part 1</title>
        <published>2013-11-17T00:00:00+00:00</published>
        <updated>2013-11-17T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/trying-out-ceylon-part-1/"/>
        <id>https://blog.vigoo.dev/posts/trying-out-ceylon-part-1/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/trying-out-ceylon-part-1/">&lt;p&gt;Ceylon&#39;s first production release was announced on 12th of November. I decided to try it out after going through the quick introduction, as it looked quite promising. In a series of posts I&#39;d like to share my first attempts to use this interesting language.&lt;/p&gt;
&lt;p&gt;This first release came with an eclipse plugin as well - after installing it I was immediately able to start working on my test project. In this few hours the plugin seemed to be stable enough, I did not experience any problems.&lt;/p&gt;
&lt;p&gt;I have a &lt;code&gt;JVLT&lt;/code&gt; file which I created while attending a foreign language course about a year ago. I was using only a limited subset of this application, so basically what I have is a .jvlt file, which is in fact a ZIP archive, in which a &lt;code&gt;dict.xml&lt;/code&gt; stores a set of words and for each word one or more translation and the lesson we have learnt it.&lt;/p&gt;
&lt;p&gt;See the following example:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;dictionary&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; language&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;french&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; version&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;1.4&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;entry&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; id&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e275&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;orth&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;à côté de&lt;/span&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;orth&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;sense&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; id&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e275-s1&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;trans&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;mellett&lt;/span&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;trans&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;lt;/&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;sense&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;sense&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; id&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;e275-s2&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;trans&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;mellé&lt;/span&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;trans&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;lt;/&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;sense&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;lesson&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;8&lt;/span&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;lesson&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &amp;lt;/&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;entry&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;z-l-11 z-d-9&quot;&gt;dictionary&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;My idea was to write an application that helps me learning and practicing these words.&lt;/p&gt;
&lt;p&gt;In this first post I&#39;m going to load the dictionary from the JVLT file.&lt;/p&gt;
&lt;p&gt;To get started, I created a new Ceylon module with the help of the IDE called jvlt. This immediately created three program units: &lt;code&gt;module.ceylon&lt;/code&gt;, &lt;code&gt;package.ceylon&lt;/code&gt; and &lt;code&gt;run.ceylon&lt;/code&gt;. The &lt;code&gt;module.ceylon&lt;/code&gt; contains the module definition, which also describes the module&#39;s dependencies. As I was trying to implement the dictionary reader, I ended up with the following module definition:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;module jvlt &amp;quot;1.0.0&amp;quot; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    shared import ceylon.file &amp;quot;1.0.0&amp;quot;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	import ceylon.collection &amp;quot;1.0.0&amp;quot;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    import ceylon.interop.java &amp;quot;1.0.0&amp;quot;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    import javax.xml &amp;quot;7&amp;quot;; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    import ceylon.test &amp;quot;1.0.0&amp;quot;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&#39;s start with the data model we want to build up! The dictionary consists of words:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;quot;Represents a foreign word with one or more senses&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;shared class Word(shared String word, shared Set&amp;lt;string&amp;gt; senses, shared Integer lesson){ &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The word, senses and lessons are all shared attributes of this class, accessible from the outside. To make it easy to access the word objects by their foreign word, I&#39;m currently storing them in a map:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;quot;Represents a dictionary of words in a given language&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;shared class Dictionary(shared String language, shared Map&amp;lt;string word=&amp;quot;&amp;quot;&amp;gt; words) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Basically that&#39;s the data model, but I wrapped the whole thing in an abstract JVLT class which looks like this:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;quot;Represents a JVLT file&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;abstract shared class JVLT() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	&amp;quot;The dictionary stored in this JVLT&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	formal shared Dictionary dictionary;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The idea is that you get a JVLT instance from one of the helper functions and then use it as a root of the data model.&lt;/p&gt;
&lt;p&gt;The next thing is to create this data model from the JVLT files. For this, I needed two things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Reading a ZIP archive&lt;/li&gt;
&lt;li&gt;Parsing XML&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It turned out that Ceylon&#39;s file module has ZIP support, with the &lt;code&gt;createZipFileSystem&lt;/code&gt; function as an entry point. I made two module-level functions beside the JVLT class for creating instances deriving from the abstract JVLT class:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;loadJVLT&lt;/code&gt; which loads a JVLT ZIP archive from the file system&lt;/li&gt;
&lt;li&gt;&lt;code&gt;loadJVLTFromDictionaryString&lt;/code&gt; oads directly a dict.xml-like XML passed as a simple string. I&#39;m using this for unit testing the XML parser.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&#39;s see the ZIP handling first:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;quot;Loads a JVLT file from a `.jvlt` ZIP archive, if possible.&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;shared JVLT? loadJVLT(File file) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	value zip = createZipFileSystem(file);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	value dictPath = zip.parsePath(&amp;quot;/dict.xml&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	if (is File dictFile = dictPath.resource) {  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		try (reader = dictFile.Reader()) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   			return loadJVLTFromDictionaryString(readAll(reader));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	} else {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		return null;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Well, the error handling is not too sophisticated in this case, it either returns a JVLT or returns &lt;code&gt;null&lt;/code&gt; if the given file did not have a &lt;code&gt;dict.xml&lt;/code&gt; in it. Other error conditions such as a &lt;code&gt;dict.xml&lt;/code&gt; with a wrong format, etc., are not handled currently. As you can see, I&#39;m reusing my other load function here, once the &lt;code&gt;dict.xml&lt;/code&gt; is read.&lt;/p&gt;
&lt;p&gt;There are two interesting things here. First, the if statement where we check if the resource is an instance of &lt;code&gt;File&lt;/code&gt; and immediately store it in the value called &lt;code&gt;dictFile&lt;/code&gt;. The &lt;code&gt;dictPath.resource&lt;/code&gt; attribute has the type &lt;code&gt;Resource&lt;/code&gt; which is a Ceylon interface. It is either an &lt;code&gt;ExistingResource&lt;/code&gt;: &lt;code&gt;Directory&lt;/code&gt;, &lt;code&gt;File&lt;/code&gt; or &lt;code&gt;Link&lt;/code&gt;, or &lt;code&gt;Nil&lt;/code&gt;. In any case if it is not a &lt;code&gt;File&lt;/code&gt; instance, we just return &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For simplicity, I&#39;m reading the full &lt;code&gt;dict.xml&lt;/code&gt; into a string before parsing it. For this purpose I wrote a small helper function &lt;code&gt;readAll&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;quot;Reads all lines from a file reader and returns the concatenated string&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;String readAll(File.Reader reader) { &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	variable String result = &amp;quot;&amp;quot;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	while (exists line = reader.readLine()) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		result += line; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	return result;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Probably it&#39;s not an optimal solution, but works :)&lt;/p&gt;
&lt;p&gt;Now that we have our data model and have a way to build it up from XML, we can write some unit tests to see how it works. The Ceylon SDK has a test module and the Ceylon IDE supports running the tests. There is a &lt;a rel=&quot;external&quot; href=&quot;http://ceylon-lang.org/documentation/1.0/ide/test-plugin/&quot;&gt;separate page in the documentation&lt;/a&gt; describing how. It is really simple, I had to add the test module as a dependency, and I created a separate file to hold my test definitions. The class groups the tests together and optionally supports running extra code before/after each test case, as in other test frameworks:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;class DictionaryParserTests() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	shared test void emptyDictionary() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	  	value dic = loadJVLTFromDictionaryString(&amp;quot;&amp;lt;dictionary&amp;gt;&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	  	assert (dic.dictionary.words.empty);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	  	assert (dic.dictionary.language == &amp;quot;unknown&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	shared test void languageAttributeRead() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		value dic = loadJVLTFromDictionaryString(&amp;quot;&amp;lt;dictionary language=&amp;quot;testlang&amp;quot;&amp;gt;&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		assert (dic.dictionary.language == &amp;quot;testlang&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	// ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I won&#39;t paste here all the test code, only a few samples to get the feeling how the Ceylon code looks like. To test whether a given word&#39;s translations are loaded correctly, I wrote a helper function:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;void assertSenses(JVLT jvlt, String w, [String+] expectedSenses) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Word? word = jvlt.dictionary.words[w];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    if (exists word) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    	assert (word.senses.equals(HashSet(expectedSenses)));  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    } else {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     	fail(&amp;quot;Word does not exists&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This helper function can be used to assert that a word has been loaded correctly:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;shared test void wordWithMultipleSenses() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	value dic = loadJVLTFromDictionaryString(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		&amp;quot;&amp;lt;dictionary&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   			&amp;lt;entry id=&amp;quot;e1&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    			&amp;lt;orth&amp;gt;src1&amp;lt;/orth&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    			&amp;lt;sense id=&amp;quot;e1-s1&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     				&amp;lt;trans&amp;gt;dst1&amp;lt;/trans&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    			&amp;lt;/sense&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    			&amp;lt;sense id=&amp;quot;e1-s2&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     				&amp;lt;trans&amp;gt;dst2&amp;lt;/trans&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    			&amp;lt;/sense&amp;gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   			&amp;lt;/entry&amp;gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   		&amp;lt;/dictionary&amp;gt;&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	assertSenses(dic, &amp;quot;src1&amp;quot;, [&amp;quot;dst1&amp;quot;, &amp;quot;dst2&amp;quot;]);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now the only problem is that there is no XML parsing support in the Ceylon SDK currently, so it has to be done using Java interop. As I wrote the code to build up the data model from the XML, I wrote several helper functions to make it easier to fit into the language. So let&#39;s see first how the dictionary loading is defined, and then I&#39;ll show the helper functions.&lt;/p&gt;
&lt;p&gt;The XML parsing is done by two module level functions which are not shared - only used by the JVLT constructor functions I shown before. The first one creates a map entry for a single word:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;quot;Creates a word entry for the dictionary&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;String-&amp;gt;Word loadEntry(Element elem) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	value w = Word {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		word = selectNodeText(elem, &amp;quot;orth&amp;quot;) else &amp;quot;???&amp;quot;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		lesson = selectNodeInteger(elem, &amp;quot;lesson&amp;quot;) else 0;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		senses = HashSet(selectNodes(elem, &amp;quot;sense/trans&amp;quot;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  					.map((Node n) =&amp;gt; n.textContent));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	return w.word-&amp;gt;w;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and the second one loads all the words from the XML document:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;quot;Loads a dictionary from JVLT&amp;#39;s `dict.xml` format.&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Dictionary loadDictionaryFromXML(Document doc) { &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	doc.documentElement.normalize();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	return Dictionary { &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		language = getAttribute(doc.documentElement, &amp;quot;language&amp;quot;) else &amp;quot;unknown&amp;quot;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		words = HashMap({ &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     		for (node in selectNodes(doc, &amp;quot;dictionary/entry&amp;quot;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       			if (is Element elem = node)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;             		loadEntry(elem) });   &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The function which returns the JVLT instance uses this function and Java interop to read the dictionary:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;quot;Loads a JVLT file by the parsing the dictionary XML directly from a string&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;shared JVLT loadJVLTFromDictionaryString(String dictXML) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	value docBuilderFactory = DocumentBuilderFactory.newInstance();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	value builder = docBuilderFactory.newDocumentBuilder();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	value doc = builder.parse(ByteArrayInputStream(javaString(dictXML).bytes));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	object result extends JVLT() { &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		dictionary = loadDictionaryFromXML(doc);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	return result;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are two things to notice here: we had to convert from Ceylon&#39;s string to Java string. This is not done automatically and we need the &lt;code&gt;ceylon.interop.java&lt;/code&gt; module to do it. In the last lines we define an anonymous class extending from JVLT and overwriting it&#39;s abstract dictionary attribute. Then this anonymous class instance is returned as the loaded JVLT.&lt;/p&gt;
&lt;p&gt;To make the XML parsing less painful, I defined a few helper functions in a separate compilation unit (&lt;code&gt;XmlHelper.ceylon&lt;/code&gt;). I won&#39;t show here the full file but there are some interesting parts. First, from Ceylon you cannot call static methods, but you can import them. I&#39;m using the following two import statements:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;import org.w3c.dom { Node, NodeList, Element }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;import javax.xml.xpath { XPathFactory { newXPathFactory = newInstance },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       XPathConstants { nodeSet = \iNODESET }}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first one is straightforward. It imports three DOM interfaces. The second one first imports the &lt;code&gt;XPathFactory.newInstance&lt;/code&gt; static method and also renames it, as newInstance is a too generic name without its class name as a prefix. The third line imports a constant value and gives it a Ceylon-compatible name. Because in Ceylon only the types can start with an uppercase character, we have to use a special and ugly syntax which helps the interoperability - prefixing it with &lt;code&gt;\i&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;ceylon.interop.java&lt;/code&gt; module has helper classes to make Java Iterable objects iterable in Ceylon, but unfortunately the &lt;code&gt;NodeList&lt;/code&gt; interface is not iterable in Java either. So I wrote a simple wrapper that iterates through a node list:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;class NodeListIterator(NodeList nodes) satisfies Iterable&amp;lt;Node&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	shared actual default Iterator&amp;lt;Node&amp;gt; iterator() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		object it satisfies Iterator&amp;lt;Node&amp;gt; {   &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   			variable Integer i = 0;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   			shared actual Node|Finished next() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    			if (i &amp;lt; nodes.length) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     				return nodes.item(i++);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    			} else {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     				return finished;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    			}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   			}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		return it;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using this iterator and the imports I wrote a &lt;code&gt;selectNodes&lt;/code&gt; function to run XPath expressions and return the result as a Ceylon iterable:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{Node*} selectNodes(Node root, String xpath) { &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	value factory = newXPathFactory();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	value xpathCompiler = factory.newXPath();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	value expr = xpathCompiler.compile(xpath);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	value nodeList = expr.evaluate(root, nodeSet); &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	if (is NodeList nodeList) {  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		return NodeListIterator(nodeList);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	else {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		return [];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using this function it is very easy to write a variant that selects a single node:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Node? selectNode(Node root, String xpath) { &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	return selectNodes(root, xpath).first;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are some other helper functions returning the node&#39;s text, converting it to integer, etc. but I think they are not that interesting. Now that I have my data model which is built from my JVLT file, the next thing is to make a user interface somehow where the vocabulary can be shown an the user&#39;s knowledge can be tested/improved. This will be the topic of some future posts, as soon as I have time to experiment more with this new language.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Cloning WPF flow document fragments</title>
        <published>2013-10-25T00:00:00+00:00</published>
        <updated>2013-10-25T00:00:00+00:00</updated>
        
        <author>
          <name>Daniel Vigovszky</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vigoo.dev/posts/cloning-wpf-flow-document-fragments/"/>
        <id>https://blog.vigoo.dev/posts/cloning-wpf-flow-document-fragments/</id>
        
        <content type="html" xml:base="https://blog.vigoo.dev/posts/cloning-wpf-flow-document-fragments/">&lt;p&gt;Today I had to write such an ugly hack to fix a bug that I decided to start writing a blog where I can show it to the world :)&lt;/p&gt;
&lt;p&gt;The software I&#39;m working on has some sort of context sensitive help panel, which is implemented using dynamically generated &lt;a rel=&quot;external&quot; href=&quot;http://msdn.microsoft.com/en-us/library/aa970909.aspx&quot;&gt;flow documents&lt;/a&gt;. The software loads a large set of flow document sections from a XAML file runtime, and later builds documents from a subset of them.&lt;/p&gt;
&lt;p&gt;For some reason (which belong to a separate post), it is not possible to reuse these flow document elements in multiple flow documents, not even if there is only one at a time. To work around this, I was cloning these sections before adding them to the document.&lt;/p&gt;
&lt;p&gt;As WPF elements are not &lt;em&gt;cloneable&lt;/em&gt;, I was using the method recommended many places, for example &lt;a rel=&quot;external&quot; href=&quot;http://stackoverflow.com/questions/32541/how-can-you-clone-a-wpf-object&quot;&gt;in this StackOverflow post&lt;/a&gt;: saving the object tree to an in-memory XAML stream, and loading it back.&lt;/p&gt;
&lt;p&gt;This worked quite well.. until we discovered a bug, which I still cannot explain. In some cases which were easily reproducible for any developer, but the code running in those cases being exactly the same as in other, working cases, the clone method simply stopped working.&lt;/p&gt;
&lt;p&gt;Stopped working here means that the following code:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; xaml&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; XamlWriter&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Save&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;block&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;would write out the correct object hierarchy, but without any properties (no attributes, no content properties, nothing but the element names)! In the same time the objects in the memory were untouched and still had all the relevant properties set.&lt;/p&gt;
&lt;p&gt;I also tried to write my own XAML serializer based on the code found &lt;a rel=&quot;external&quot; href=&quot;http://go4answers.webhost4life.com/Example/xaml-serialization-replacement-75133.aspx&quot;&gt;at this site&lt;/a&gt;, but this was only good to find out that the problem lies deep within the &lt;code&gt;MarkupWriter&lt;/code&gt; class, which is the same what the &lt;code&gt;XamlWriter&lt;/code&gt; uses internally. When the &lt;code&gt;XamlWriter&lt;/code&gt; failed, my own code could not find any properties using the returned &lt;a rel=&quot;external&quot; href=&quot;http://msdn.microsoft.com/en-us/library/system.windows.markup.primitives.markupobject.aspx&quot;&gt;MarkupObject&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;MarkupObject&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; markupObj&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; MarkupWriter&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;GetMarkupObjectFor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;obj&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For the same object, in the working scenarios it returned a markup object with a working &lt;code&gt;Properties&lt;/code&gt; collection.&lt;/p&gt;
&lt;p&gt;So here is the final &lt;em&gt;&quot;solution&quot;&lt;/em&gt; which I&#39;m not really proud of, but solved the problem. Maybe with some modifications it is useful for someone struggling with the framework:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-l-i z-d-9 z-d-i&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Horrible ugly clone hack to issues where XamlWriter/XamlReader based&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; clone method did not work.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt; &amp;lt;/&lt;/span&gt;&lt;span class=&quot;z-l-11 z-l-i z-d-9 z-d-i&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; static&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; CloneHelper&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    public&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; static&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Block&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; Clone&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;t&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; block&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        where&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; T&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; Block&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        var&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;T&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;DeepClone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;block&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; result&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; static&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; object&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; DeepClone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; obj&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; !=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;            //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Replacing ResourceDictionary and Style values with null. &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;            //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; In this particular use case it is correct to do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;obj&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;GetType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; typeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;ResourceDictionary&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ||&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                obj&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;GetType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; typeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Style&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                return&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            else&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;                //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Value types and some special cases where we don&amp;#39;t want to clone&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;obj&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;GetType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;IsValueType&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ||&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                    obj&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;GetType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; typeof&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;Cursor&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ||&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                    obj&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;GetType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; typeof&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;XmlLanguage&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                    return&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; obj&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                else&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;                    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; If it is cloneable, use it&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                    var&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; cloneable&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; obj&lt;/span&gt;&lt;span&gt; as&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; ICloneable&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;cloneable&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; !=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                        return&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; cloneable&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                    else&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;                        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Creating the clone with reflection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                        var&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; typ&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; obj&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;GetType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                        var&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; clone&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; Activator&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;CreateInstance&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;typ&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt;                     &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;                        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Property names which are known locally set &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;                        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; dependency properties&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                        var&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; usedNames&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; HashSet&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt;                        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;                        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Copying locally set dependency properties from the &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;                        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; source to the target&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                        var&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; dobjSource&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; obj&lt;/span&gt;&lt;span&gt; as&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DependencyObject&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                        var&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; dobjTarget&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; clone&lt;/span&gt;&lt;span&gt; as&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; DependencyObject&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;dobjSource&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; !=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; dobjTarget&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; !=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                            var&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; locallySetProperties&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                                dobjSource&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;GetLocalValueEnumerator&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                            while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;locallySetProperties&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;MoveNext&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                            {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;                                DependencyProperty&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; dp&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                                    locallySetProperties&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Current&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Property&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                                if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;dp&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;ReadOnly&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                                    dobjTarget&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;SetValue&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;dp&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; dobjSource&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;GetValue&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;dp&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                                    usedNames&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;dp&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        }&lt;/span&gt;&lt;span&gt;                        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;                        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Getting all the public, non-static properties of the source&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                        foreach&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; pi&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; typ&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;GetProperties&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                                            BindingFlags&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Instance&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; |&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                                            BindingFlags&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Public&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; |&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                                            BindingFlags&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;FlattenHierarchy&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;                            //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; If it is not a dependency property &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;                            //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; and not the default property...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                            if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;pi&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;CanRead&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-1&quot;&gt;                                !&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;usedNames&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Contains&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;pi&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-1&quot;&gt;                                !&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;IsDependencyProperty&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;dobjSource&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; pi&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                                pi&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; !=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Item&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                            {&lt;/span&gt;&lt;span&gt;                                    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                                var&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; val&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; pi&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;GetValue&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;obj&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;                                //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ..and it is writeable, then we recursively clone &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;                                //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; the value and set the property:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                                if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;pi&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;CanWrite&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                {&lt;/span&gt;&lt;span&gt;                                        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                                    pi&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;SetValue&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;clone&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; DeepClone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;val&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                                else&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;                                    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; ..otherwise if it is a readonly list property, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;                                    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; go through each item,  clone it and add to &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;                                    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; the clone&amp;#39;s list property&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                                    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;pi&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;PropertyType&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                          .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;GetInterfaces&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                          .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Contains&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt;typeof&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;IList&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                                        var&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; source&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; val&lt;/span&gt;&lt;span&gt; as&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IList&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                                        var&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; target&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; pi&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;GetValue&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;clone&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; as&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; IList&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                                        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; !=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; target&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; !=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                        {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                                            foreach&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; item&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; source&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                                                target&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;Add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;DeepClone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;item&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        }&lt;/span&gt;&lt;span&gt;                        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                        return&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; clone&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        else&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            return&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-l-i z-d-9 z-d-i&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; Tries to determine if a property is a dependency property, by reflection and &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; naming convention&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt; &amp;lt;/&lt;/span&gt;&lt;span class=&quot;z-l-11 z-l-i z-d-9 z-d-i&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-l-i z-d-9 z-d-i&quot;&gt;param&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-8 z-d-i&quot;&gt; name&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-5 z-d-i&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-5 z-d-i&quot;&gt;dobj&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-5 z-d-i&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;Dependency object&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-l-i z-d-9 z-d-i&quot;&gt;param&lt;/span&gt;&lt;span class=&quot;z-l-6 z-l-i z-d-8 z-d-i&quot;&gt; name&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-5 z-d-i&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-5 z-d-i&quot;&gt;pi&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-5 z-d-i&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;Property info&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-l-i z-d-9 z-d-i&quot;&gt;returns&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;Returns &lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-11 z-l-i z-d-9 z-d-i&quot;&gt;c&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;true&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;z-l-11 z-l-i z-d-9 z-d-i&quot;&gt;c&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt; if the given property seems to be a &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-7 z-l-i z-d-4 z-d-i&quot;&gt;          CLR access property for a dependency property.&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;z-l-11 z-l-i z-d-9 z-d-i&quot;&gt;returns&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    private&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; static&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; bool&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; IsDependencyProperty&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DependencyObject&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; dobj&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt; PropertyInfo&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; pi&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;dobj&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; !=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            var&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; dpProp&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; dobj&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;GetType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;GetProperty&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;pi&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Property&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                                                    BindingFlags&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Static&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                                                    BindingFlags&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Public&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                                                    BindingFlags&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;FlattenHierarchy&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;dpProp&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; !=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; dpProp&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;PropertyType&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; typeof&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DependencyProperty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                return&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            else&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                var&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt; dpField&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; dobj&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;GetType&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;GetField&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;pi&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;Property&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-5&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                                                      BindingFlags&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Static&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; |&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                                                      BindingFlags&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;Public&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; |&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                                                      BindingFlags&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;FlattenHierarchy&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-d-9&quot;&gt;dpField&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; !=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; null&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                    dpField&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;FieldType&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-d-7&quot;&gt; typeof&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-10&quot;&gt;DependencyProperty&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-d-10&quot;&gt;                    dpField&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;IsInitOnly&lt;/span&gt;&lt;span class=&quot;z-d-1&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt; dpField&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-d-10&quot;&gt;IsStatic&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;                    return&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; true&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-8&quot;&gt; false&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
        
    </entry>
</feed>
