com.simiacryptus.skyenet.apps.plan.ForeachTask.kt Maven / Gradle / Ivy
package com.simiacryptus.skyenet.apps.plan
import com.simiacryptus.jopenai.API
import com.simiacryptus.skyenet.TabbedDisplay
import com.simiacryptus.skyenet.apps.plan.PlanningTask.TaskBreakdownInterface
import com.simiacryptus.skyenet.webui.session.SessionTask
import com.simiacryptus.skyenet.apps.plan.PlanUtil.diagram
import com.simiacryptus.skyenet.apps.plan.PlanUtil.executionOrder
import org.slf4j.LoggerFactory
class ForeachTask(
planSettings: PlanSettings,
planTask: PlanningTask.PlanTask
) : AbstractTask(planSettings, planTask) {
override fun promptSegment(): String {
return """
ForeachTask - Execute a task for each item in a list
** Specify the list of items to iterate over
** Define the task to be executed for each item
""".trimIndent()
}
override fun run(
agent: PlanCoordinator,
taskId: String,
userMessage: String,
plan: TaskBreakdownInterface,
planProcessingState: PlanProcessingState,
task: SessionTask,
taskTabs: TabbedDisplay,
api: API
) {
val items = planTask.foreachItems ?: throw RuntimeException("No items specified for ForeachTask")
val subTasks = planTask.subTasksByID ?: throw RuntimeException("No subTasks specified for ForeachTask")
val subPlanTask = agent.ui.newTask(false)
task.add(subPlanTask.placeholder)
items.forEachIndexed { index, item ->
val itemSubTasks = subTasks.mapValues { (_, subTaskPlan) ->
subTaskPlan.copy(description = "${subTaskPlan.description} - Item $index: $item")
}
val itemPlanProcessingState = PlanProcessingState(itemSubTasks.toMutableMap())
agent.executePlan(
task = subPlanTask,
diagramBuffer = subPlanTask.add(diagram(agent.ui, itemPlanProcessingState.subTasks)),
subTasks = itemSubTasks,
diagramTask = subPlanTask,
planProcessingState = itemPlanProcessingState,
taskIdProcessingQueue = executionOrder(itemSubTasks).toMutableList(),
pool = agent.pool,
userMessage = "$userMessage\nProcessing item $index: $item",
plan = object : TaskBreakdownInterface {
override val tasksByID = itemSubTasks
override val finalTaskID = null
},
api = api
)
}
subPlanTask.complete("Completed ForeachTask for ${items.size} items")
}
companion object {
private val log = LoggerFactory.getLogger(ForeachTask::class.java)
}
}