{
  "contract": "algal.organism.v1",
  "key": "organism:reply",
  "name": "reply",
  "budgets": {
    "maxAgentCalls": 2,
    "maxContextBytes": 65536,
    "maxDepth": 4,
    "maxOutputBytes": 65536,
    "maxSteps": 256,
    "maxWork": 1000000
  },
  "cells": [
    {
      "id": "input",
      "kind": "input",
      "outputs": {
        "email": {
          "type": "text"
        }
      }
    },
    {
      "id": "b1-intent-decide",
      "kind": "decide",
      "questions": {
        "answer": {
          "type": "choice",
          "instructions": "What does this email need?",
          "criteria": {
            "help": "Help with a problem",
            "sales": "Information before buying",
            "other": "Anything else"
          }
        }
      },
      "view": {
        "inputs": [
          "context"
        ]
      },
      "inputs": {
        "context": {
          "type": "text"
        }
      }
    },
    {
      "id": "b1-intent",
      "kind": "expr",
      "expr": {
        "contract": "algal.expr.v1",
        "program": [
          "let",
          "_source_decision",
          [
            "get",
            "raw",
            "answers",
            "answer"
          ],
          [
            "nth",
            [
              "if",
              [
                "and",
                [
                  "contains",
                  [
                    "quote",
                    [
                      "help",
                      "sales",
                      "other"
                    ]
                  ],
                  [
                    "get",
                    "_source_decision",
                    "choice"
                  ]
                ],
                [
                  "isNum",
                  [
                    "get",
                    "_source_decision",
                    "confidence"
                  ]
                ],
                [
                  "gte",
                  [
                    "get",
                    "_source_decision",
                    "confidence"
                  ],
                  0
                ],
                [
                  "lte",
                  [
                    "get",
                    "_source_decision",
                    "confidence"
                  ],
                  1
                ],
                [
                  "fold",
                  [
                    "quote",
                    [
                      "help",
                      "sales",
                      "other"
                    ]
                  ],
                  true,
                  "_source_valid",
                  "_source_label",
                  [
                    "and",
                    [
                      "get",
                      "_source_valid"
                    ],
                    [
                      "let",
                      "_source_probability",
                      [
                        "get",
                        "_source_decision",
                        "probabilities",
                        [
                          "get",
                          "_source_label"
                        ]
                      ],
                      [
                        "and",
                        [
                          "isNum",
                          [
                            "get",
                            "_source_probability"
                          ]
                        ],
                        [
                          "gte",
                          [
                            "get",
                            "_source_probability"
                          ],
                          0
                        ],
                        [
                          "lte",
                          [
                            "get",
                            "_source_probability"
                          ],
                          1
                        ]
                      ]
                    ]
                  ]
                ]
              ],
              [
                "list",
                {
                  "value": [
                    "get",
                    "_source_decision",
                    "choice"
                  ],
                  "confidence": [
                    "get",
                    "_source_decision",
                    "confidence"
                  ],
                  "probabilities": {
                    "help": [
                      "get",
                      "_source_decision",
                      "probabilities",
                      "help"
                    ],
                    "sales": [
                      "get",
                      "_source_decision",
                      "probabilities",
                      "sales"
                    ],
                    "other": [
                      "get",
                      "_source_decision",
                      "probabilities",
                      "other"
                    ]
                  }
                }
              ],
              [
                "list"
              ]
            ],
            0
          ]
        ]
      },
      "output": {
        "kind": "json",
        "schema": {
          "type": "object",
          "required": [
            "value",
            "confidence",
            "probabilities"
          ],
          "properties": {
            "value": {
              "type": "string"
            },
            "confidence": {
              "type": "number"
            },
            "probabilities": {
              "type": "object"
            }
          }
        }
      },
      "inputs": {
        "raw": {
          "type": "json"
        }
      }
    },
    {
      "id": "b2-task",
      "kind": "expr",
      "expr": {
        "contract": "algal.expr.v1",
        "program": [
          "let",
          "_source_match",
          [
            "get",
            "value-1",
            "value"
          ],
          [
            "if",
            [
              "contains",
              [
                "quote",
                [
                  "help",
                  "sales"
                ]
              ],
              [
                "get",
                "_source_match"
              ]
            ],
            [
              "if",
              [
                "contains",
                [
                  "quote",
                  [
                    "help"
                  ]
                ],
                [
                  "get",
                  "_source_match"
                ]
              ],
              "Draft a helpful support reply.",
              "Draft a concise sales reply."
            ],
            "Draft a clarifying question."
          ]
        ]
      },
      "output": {
        "kind": "text"
      },
      "inputs": {
        "value-1": {
          "type": "json"
        }
      }
    },
    {
      "id": "result",
      "kind": "agent",
      "prompt": "Algal source generation envelope v1. Follow the instruction in inputs.instruction. Use only inputs.context as task context. Return the requested text.",
      "view": {
        "inputs": [
          "instruction",
          "context"
        ]
      },
      "output": {
        "kind": "text"
      },
      "inputs": {
        "instruction": {
          "type": "text"
        },
        "context": {
          "type": "text"
        }
      }
    }
  ],
  "edges": [
    {
      "from": {
        "cell": "input",
        "port": "email"
      },
      "to": {
        "cell": "b1-intent-decide",
        "port": "context"
      }
    },
    {
      "from": {
        "cell": "b1-intent-decide",
        "port": "out"
      },
      "to": {
        "cell": "b1-intent",
        "port": "raw"
      }
    },
    {
      "from": {
        "cell": "b1-intent",
        "port": "out"
      },
      "to": {
        "cell": "b2-task",
        "port": "value-1"
      }
    },
    {
      "from": {
        "cell": "b2-task",
        "port": "out"
      },
      "to": {
        "cell": "result",
        "port": "instruction"
      }
    },
    {
      "from": {
        "cell": "input",
        "port": "email"
      },
      "to": {
        "cell": "result",
        "port": "context"
      }
    }
  ],
  "interface": {
    "inputs": {
      "email": {
        "cell": "input",
        "port": "email"
      }
    },
    "outputs": {
      "result": {
        "cell": "result",
        "port": "out"
      }
    }
  }
}
