Skip to content

Commit adbbdee

Browse files
committed
Add documentation about reusing processor modular functions
1 parent 2915ff4 commit adbbdee

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,88 @@ Components that transform frames:
327327

328328
Read core.async.flow docs for more information about flow precesses.
329329

330+
## Modular Processor Functions
331+
332+
Simulflow processors are designed for modularity and reuse. Each processor can expose its core functionality as multi-arity functions that can be used independently or composed into custom processors.
333+
334+
### Multi-Arity Function Pattern
335+
336+
Processors follow a standard multi-arity pattern that maps directly to `core.async.flow` lifecycle:
337+
338+
```clojure
339+
(defn processor-fn
340+
([] {:ins {:in "Description"} :outs {:out "Description"} :params {...}}) ; 0-arity: describe
341+
([config] {...}) ; 1-arity: init
342+
([state transition] {...}) ; 2-arity: transition
343+
([state input-port data] [state {...}])) ; 3-arity: transform
344+
```
345+
346+
### Example: Reusing Transport Functions
347+
348+
Here's how you can reuse transport processor functions in your own custom processors:
349+
350+
```clojure
351+
(ns my-cool-processor
352+
(:require [simulflow.transport :as transport]
353+
[simulflow.frame :as frame]
354+
[simulflow.utils.audio :as audio]))
355+
356+
(defn mic-transport-fn
357+
"Custom microphone transport with audio processing"
358+
([] (transport/mic-transport-in-describe))
359+
([params] (transport/mic-transport-in-init! params))
360+
([state transition]
361+
(transport/mic-transport-in-transition state transition))
362+
363+
;; Custom transform with audio processing
364+
([state _ {:keys [audio-data timestamp]}]
365+
(let [processed-audio (audio/apply-noise-reduction audio-data)
366+
float-audio (PCMConverter/convertToFloat32Buffer processed-audio)]
367+
[state {:out [(frame/audio-input-raw float-audio {:timestamp timestamp})]}])))
368+
369+
;; Use in a flow
370+
(def my-flow
371+
(flow/create-flow
372+
{:procs {:custom-mic {:proc (flow/process mic-transport-fn)
373+
:args {:audio-in/sample-rate 16000}}}
374+
:conns [[:custom-mic :out] [:next-processor :in]]}))
375+
```
376+
377+
### Composing Processor Logic
378+
379+
You can also compose transform logic from multiple processors:
380+
381+
```clojure
382+
(defn hybrid-processor-fn
383+
([] {:ins {:in "Mixed input"} :outs {:out "Processed output"}})
384+
([params] {:config params})
385+
([state transition] (when (= transition :stop) (cleanup state)))
386+
387+
([state input-port data]
388+
(cond
389+
;; Handle audio using transport transform
390+
(frame/audio-input-raw? data)
391+
(transport/mic-transport-transform state input-port data)
392+
393+
;; Handle text using LLM transform
394+
(frame/llm-context? data)
395+
(openai/transform state input-port data)
396+
397+
;; Custom handling for other frames
398+
:else
399+
[state {:out [(custom-transform data)]}])))
400+
```
401+
402+
### Benefits of Modular Functions
403+
404+
- **Reusability**: Use processor logic across different flows
405+
- **Testability**: Test individual transform functions in isolation
406+
- **Composability**: Mix and match functionality from different processors
407+
- **Customization**: Override specific behaviors while reusing core logic
408+
- **Debugging**: Easier to debug individual components
409+
410+
This pattern enables building complex AI pipelines by composing smaller, well-tested components while maintaining the data-driven architecture that makes simulflow powerful.
411+
330412

331413
## Built With
332414

0 commit comments

Comments
 (0)