Skip to content

Commit

Permalink
remove permission checker
Browse files Browse the repository at this point in the history
  • Loading branch information
sjorsdev committed May 1, 2024
1 parent d0dbe4f commit e9fefbd
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import community.flock.aigentic.core.agent.tool.FinishReason
import community.flock.aigentic.core.agent.tool.FinishedOrStuck
import community.flock.aigentic.core.agent.tool.finishOrStuckTool
import community.flock.aigentic.core.message.*
import community.flock.aigentic.core.tool.DefaultToolPermissionHandler
import community.flock.aigentic.core.tool.ToolName
import community.flock.aigentic.core.tool.ToolPermissionHandler
import community.flock.aigentic.core.model.ModelResponse
import community.flock.aigentic.core.tool.Tool
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.datetime.Clock

class AgentExecutor {
var permissionHandler: ToolPermissionHandler = DefaultToolPermissionHandler()
interface ToolInterceptor { suspend fun intercept(agent: Agent, tool: Tool, toolCall: ToolCall) }

class AgentExecutor(val toolInterceptors: List<ToolInterceptor> = emptyList()) {
val agents: MutableList<Agent> = mutableListOf()
val startedAgents = MutableSharedFlow<String>(replay = 100)

Expand Down Expand Up @@ -100,11 +100,7 @@ class AgentExecutor {
private suspend fun Agent.execute(toolCall: ToolCall): Message.ToolResult {
val functionArgs = toolCall.argumentsAsJson()
val tool = tools[ToolName(toolCall.name)] ?: error("Tool not registered: $toolCall")
while (!permissionHandler.hasPermission(tool.toolConfiguration, toolCall)) {
setRunningState(AgentRunningState.WAITING_ON_APPROVAL)
println("Waiting for permission for ${toolCall.name}")
delay(300)
}
toolInterceptors.forEach { it.intercept(this, tool, toolCall) }
setRunningState(AgentRunningState.EXECUTING_TOOL)
val result = tool.handler(functionArgs)
setRunningState(AgentRunningState.RUNNING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package community.flock.aigentic.core.dsl

import community.flock.aigentic.core.agent.Agent
import community.flock.aigentic.core.agent.AgentExecutor
import community.flock.aigentic.core.agent.ToolInterceptor
import community.flock.aigentic.core.tool.DefaultToolPermissionHandler
import community.flock.aigentic.core.tool.ToolPermissionHandler

Expand All @@ -13,18 +14,17 @@ fun agentExecutor(agentExecutorBuilder: AgentExecutorConfig.() -> Unit): AgentEx
class AgentExecutorConfig : Config<AgentExecutor> {

private val agents: MutableList<Agent> = mutableListOf()
private var permissionHandler: ToolPermissionHandler = DefaultToolPermissionHandler()
private var interceptors: MutableList<ToolInterceptor> = mutableListOf()

fun AgentExecutorConfig.addAgent(agent: Agent) {
agents.add(agent)
}

fun AgentExecutorConfig.toolPermissionHandler(toolPermissionHandler: ToolPermissionHandler) {
this.permissionHandler = toolPermissionHandler
fun AgentExecutorConfig.addToolInterceptor(interceptor: ToolInterceptor) {
this.interceptors.add(interceptor)
}

override fun build(): AgentExecutor = AgentExecutor().also {
it.permissionHandler = this.permissionHandler
override fun build(): AgentExecutor = AgentExecutor(this.interceptors).also {
it.loadAgents(agents)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,3 @@ internal interface InternalTool<T> : ToolDescription, Handler<T>
interface ToolPermissionHandler {
suspend fun hasPermission(toolConfiguration: ToolConfiguration, toolCall: ToolCall): Boolean
}

class DefaultToolPermissionHandler: ToolPermissionHandler {
override suspend fun hasPermission(toolConfiguration: ToolConfiguration, toolCall: ToolCall): Boolean = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ suspend fun runAdministrativeAgentExample(openAIAPIKey: String) {
}

val executor = agentExecutor {
addAgent(agent)
addAgent(agent)
}.also { logEvents(it) }

executor.start()
Expand Down

0 comments on commit e9fefbd

Please sign in to comment.