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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# Table of Contents

* [Structure of Coder](#structure-of-coder)
* [CtrlExMem\_CoderFlow](#CtrlExMem_CoderFlow)
  * [CtrlExMem\_CoderFlow](#CtrlExMem_CoderFlow.CtrlExMem_CoderFlow)
    * [detect\_finish\_or\_continue](#CtrlExMem_CoderFlow.CtrlExMem_CoderFlow.detect_finish_or_continue)
* [run\_coder](#run_coder)
* [Planner\_CoderFlow](#Planner_CoderFlow)
  * [Planner\_CoderFlow](#Planner_CoderFlow.Planner_CoderFlow)
* [Controller\_CoderFlow](#Controller_CoderFlow)
  * [Controller\_CoderFlow](#Controller_CoderFlow.Controller_CoderFlow)
    * [\_\_init\_\_](#Controller_CoderFlow.Controller_CoderFlow.__init__)
    * [instantiate\_from\_config](#Controller_CoderFlow.Controller_CoderFlow.instantiate_from_config)
    * [run](#Controller_CoderFlow.Controller_CoderFlow.run)
* [UpdatePlanAtomicFlow](#UpdatePlanAtomicFlow)
  * [UpdatePlanAtomicFlow](#UpdatePlanAtomicFlow.UpdatePlanAtomicFlow)
    * [run](#UpdatePlanAtomicFlow.UpdatePlanAtomicFlow.run)
* [CoderFlow](#CoderFlow)
  * [CoderFlow](#CoderFlow.CoderFlow)
    * [run](#CoderFlow.CoderFlow.run)
* [\_\_init\_\_](#__init__)



# Structure of Coder

```
     goal, memory_files (dict)
             |
             v
    +-------------------+
    |  MemoryReading    |  Reads in the content of the memory files
    |    Flow           |
    +-------------------+
             |
             | (memory_content)
             |
             v
    +-------------------+
    |    PlanWriter     |  Writes a step-by-step plan to achieve the goal
    +-------------------+
             |
             | (plan)
             |
             v
    +-------------------+
    |   CtrlExMemFlow   |  Illustrated below. Carries out the plan in an controller-executor fashion,
    |                   |  with memory management mechanisms.
    +-------------------+
             |
    (summary, result)

```

Here is the structure of the `CtrlExMemFlow`:
```
    plan, memory_files, memory_content, goal
                |
                v
        +---------------+
        |  Controller   | --------<<<<-----------+
        +---------------+                        |
                |                                |
                | (command, command args)        |
                |                                |
                v                                |
        +------------------+                     |
        |   Executor       |  Each branch is an  |
        | (Tree Structure) |  executor           |
        +------------------+                     |
                |                                ^
                | (execution results)            ^
                |                                ^
                v                                ^
        +---------------+                        ^
        | MemWriteFlow  |  Updates memory files  ^
        +---------------+                        ^
                |                                ^
                | (summary)                      |
                |                                |
                v                                |
        +---------------+                        |
        | MemReadFlow   |  Reads updated memory  |
        +---------------+                        |
                |                                |
                | (updated memory content)       |
                |                                |
                +-> goes back to the Controller>-+

```
Structure of the Executors:
```
                 +-------------------+
                 |   Branching       |
                 |    Executor       |
                 +-------------------+
        /          |       |       |         \
       /           |       |       |          \
      /            |       |       |           \
     /             |       |       |            \
Extend_library  ask_user re_plan update_plan   run_code

```

Memory files of Coder:
- plan_coder.txt
- logs_coder.txt
- library.py

About the branches:
- [ExtendLibrary](https://huggingface.co/aiflows/ExtendLibraryFlowModule): Writes and tests code functions in an interactive fashion, finally saves the written function to the code library.
- [ask_user](https://huggingface.co/aiflows/ExtendLibraryFlowModule/blob/main/ExtLibAskUserFlow.py): Ask user for info / confirmation, etc.
- [re_plan](https://huggingface.co/aiflows/ReplanningFlowModule): One branch of the executors, when something goes wrong, re-draft the plan.
- [update_plan](https://huggingface.co/aiflows/JarvisFlowModule/blob/main/UpdatePlanAtomicFlow.py): One branch of the executors, when the controller realizes that one (or some, depending on the LLM's response) step of the plan is (are) done, it generates a new plan that marks the step(s) as done.
- [run_code](https://huggingface.co/aiflows/RunCodeFlowModule): Runs the code written by the Controller, will first open up a temp file with the code for user confirmation and editing, then the code is passed to the [InterpreterFlow](https://huggingface.co/aiflows/InterpreterFlowModule).
  





<a id="CtrlExMem_CoderFlow"></a>

# CtrlExMem\_CoderFlow

<a id="CtrlExMem_CoderFlow.CtrlExMem_CoderFlow"></a>

## CtrlExMem\_CoderFlow Objects

```python
class CtrlExMem_CoderFlow(CtrlExMemFlow)
```

This class inherits from the CtrlExMemFlow class from AbstractBossFlowModule.
See: https://huggingface.co/aiflows/AbstractBossFlowModule/blob/main/CtrlExMemFlow.py

*Input Interface*:
- `plan`
- `logs`
- `code_library`: the signatures and docstring of the functions in the code library.
- `memory_files`
- `goal`

*Output Interface*
- `result` (str): The result of the flow, the result will be returned to the caller.
- `summary` (str): The summary of the flow, the summary will be logged into the logs of the caller flow.

*Configuration Parameters*:
- See the configuration parameters of the CtrlExMemFlow class from AbstractBoss (https://huggingface.co/aiflows/AbstractBossFlowModule/blob/main/CtrlExMemFlow.py).
- `input_interface`: the input interface of the flow
- `output_interface`: the output interface of the flow
- `subflows_config`: the configuration of the subflows

<a id="CtrlExMem_CoderFlow.CtrlExMem_CoderFlow.detect_finish_or_continue"></a>

#### detect\_finish\_or\_continue

```python
@CircularFlow.output_msg_payload_processor
def detect_finish_or_continue(output_payload: Dict[str, Any],
                              src_flow) -> Dict[str, Any]
```

This method is called when the flow receives a message from a subflow.

This method will check the message and decide whether the flow should continue or finish.

**Arguments**:

- `output_payload`: the output payload of the subflow
- `src_flow`: the source flow of the message

**Returns**:

`Dict[str, Any]`: the output payload of the flow

<a id="run_coder"></a>

# run\_coder

<a id="Planner_CoderFlow"></a>

# Planner\_CoderFlow

<a id="Planner_CoderFlow.Planner_CoderFlow"></a>

## Planner\_CoderFlow Objects

```python
class Planner_CoderFlow(PlanWriterFlow)
```

Planner of the coder flow, it inherits from PlanWriterFlow, see: https://huggingface.co/aiflows/PlanWriterFlowModule
This flow is responsible for generating the plan for the coder flow.

*Input Interface*
- `goal`
- `memory_files`
- `code_library`

*Output Interface*
- `plan`
- `summary`
- `status`

*Configuration Parameters*:
- Look at the configuration parameters of PlanWriterFlowModule for detailed info. (https://huggingface.co/aiflows/PlanWriterFlowModule)
- `input_interface`: the input interface of the flow, default: `["goal", "memory_files", "code_library"]`
- `output_interface`: the output interface of the flow, default: `["plan", "summary", "status"]`
- `subflows_config`: configuration of the subflows.
- `topology`: topology of the subflows.

<a id="Controller_CoderFlow"></a>

# Controller\_CoderFlow

<a id="Controller_CoderFlow.Controller_CoderFlow"></a>

## Controller\_CoderFlow Objects

```python
class Controller_CoderFlow(ChatAtomicFlow)
```

Refer to: https://huggingface.co/aiflows/JarvisFlowModule/blob/main/Controller_JarvisFlow.py
This flow is used to control the coder flow.

*Input Interface Non Initialized*:
- `goal`
- `plan`
- `code_library`
- `logs`
- `memory_files`

*Input Interface Initialized*:
- `goal`
- `plan`
- `code_library`
- `logs`
- `memory_files`
- `result`

*Output Interface*:
- `command`
- `command_args`

*Configuration Parameters*:
- `Input Interface Non Initialized`: Input interface before the conversation is initialized.
- `Input Interface Initialized`: Input interface after the conversation is initialized.
- `Output Interface`: Output interface.
- `backend`: The backend of the LLM.
- `command`: A list of available commands for the controller to call.
- `system_message_prompt_template`: The template of the system message prompt.
- `init_human_message_prompt_template`: The template of the initial human message prompt.
- `human_message_prompt_template`: The template of the human message prompt.
- `previous_messages`: The sliding window of previous messages.

<a id="Controller_CoderFlow.Controller_CoderFlow.__init__"></a>

#### \_\_init\_\_

```python
def __init__(commands: List[Command], **kwargs)
```

Initialize the flow.

**Arguments**:

- `commands` (`List[Command]`): A list of available commands for the controller to call.
- `kwargs` (`Dict[str, Any]`): Refer to the configuration parameters.

<a id="Controller_CoderFlow.Controller_CoderFlow.instantiate_from_config"></a>

#### instantiate\_from\_config

```python
@classmethod
def instantiate_from_config(cls, config)
```

Instantiate the flow from the configuration.

**Arguments**:

- `config` (`Dict[str, Any]`): The configuration.

**Returns**:

`Controller_CoderFlow`: The instantiated flow.

<a id="Controller_CoderFlow.Controller_CoderFlow.run"></a>

#### run

```python
def run(input_data: Dict[str, Any]) -> Dict[str, Any]
```

Run the flow.

**Arguments**:

- `input_data` (`Dict[str, Any]`): The input data.

**Returns**:

`Dict[str, Any]`: The output data.

<a id="UpdatePlanAtomicFlow"></a>

# UpdatePlanAtomicFlow

<a id="UpdatePlanAtomicFlow.UpdatePlanAtomicFlow"></a>

## UpdatePlanAtomicFlow Objects

```python
class UpdatePlanAtomicFlow(AtomicFlow)
```

This flow updates the plan file with the updated plan. The controller should pass the updated plan to this flow.
This design (controller reflect on the existing plan--update plan) is intended to let the controller more aware of the
plan it has. However one setback is that this process in then not deterministic.

*Input Interface*
- `updated_plan`: the updated plan in string format

*Output Interface*
- `result`: the result of the update plan operation

*Configuration Parameters*:
- `input_interface`: the input interface of the flow, default: `["updated_plan"]`
- `output_interface`: the output interface of the flow, default: `["result"]`

<a id="UpdatePlanAtomicFlow.UpdatePlanAtomicFlow.run"></a>

#### run

```python
def run(input_data: Dict[str, Any])
```

The run function of the flow.

**Arguments**:

- `input_data` (`Dict[str, Any]`): the input data to the flow

**Returns**:

`Dict[str, Any]`: the output data of the flow

<a id="CoderFlow"></a>

# CoderFlow

<a id="CoderFlow.CoderFlow"></a>

## CoderFlow Objects

```python
class CoderFlow(AbstractBossFlow)
```

Coder flow is one executor branch of the Jarvis flow. At a higher level, it is a flow that
writes and runs code given a goal. In the Jarvis flow, the Coder flow in invoked by the controller,
The Coder flow receives the goal generated by the controller, writes and runs code in an interactive fashion.

The Coder flow has the similar structure as the Jarvis flow (inherited from AbstractBossFlow).


*Input Interface (expected input)*
- `goal` (str): The goal from the caller (source flow, i.e. JarvisFlow)


*Output Interface (expected output)*
- `result` (str): The result of the flow, the result will be returned to the caller (i.e. JarvisFlow).
- `summary` (str): The summary of the flow, the summary will be logged into the logs of the caller flow (i.e. JarvisFlow).

*Configuration Parameters*: (Also see super class: AbstractBossFlow)
- `memory_files` (dict): The memory files of the flow. The memory files are the files that the flow reads and writes. Typically it should contain plan, logs, and code_library.


Typical workflow of Coder:
0. JarvisFlow calls Coder with a goal.
1. MemoryReading reads plans, logs and code library.
2. Planner makes plan based on goal.
3. Extend library with the goal given by the controller.
4. Run code with code (possibly calls the newly written function) given by the controller.
5. Finish and give an answer.

<a id="CoderFlow.CoderFlow.run"></a>

#### run

```python
def run(input_data: Dict[str, Any]) -> Dict[str, Any]
```

The run function of the Coder flow.

**Arguments**:

- `input_data` (`Dict[str, Any]`): The input data of the flow.

**Returns**:

`Dict[str, Any]`: The output data of the flow.

<a id="__init__"></a>

# \_\_init\_\_