Skip to content

Commit 0128d71

Browse files
committed
Rename parallel/reduce -> fork/merge
1 parent 0303aa8 commit 0128d71

File tree

7 files changed

+36
-36
lines changed

7 files changed

+36
-36
lines changed

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/agent/entity/AIAgentNode.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ internal class AIAgentNode<Input, Output> internal constructor(
134134
*
135135
* @param Input The type of input data this node processes and produces as output.
136136
*/
137-
public open class StartAIAgentNodeBase<Input>() : AIAgentNodeBase<Input, Input>() {
137+
public open class AIAgentStartNodeBase<Input>() : AIAgentNodeBase<Input, Input>() {
138138
/**
139139
* The name of the subgraph associated with the AI agent's starting node.
140140
*
@@ -160,7 +160,7 @@ public open class StartAIAgentNodeBase<Input>() : AIAgentNodeBase<Input, Input>(
160160
*
161161
* @param Output The type of data this node processes and produces.
162162
*/
163-
public open class FinishAIAgentNodeBase<Output>() : AIAgentNodeBase<Output, Output>() {
163+
public open class AIAgentFinishNodeBase<Output>() : AIAgentNodeBase<Output, Output>() {
164164
/**
165165
* Stores the name of the subgraph associated with the node.
166166
*
@@ -196,7 +196,7 @@ public open class FinishAIAgentNodeBase<Output>() : AIAgentNodeBase<Output, Outp
196196
* This node effectively passes its input as-is to the next node in the execution
197197
* pipeline, allowing downstream nodes to transform or handle the data further.
198198
*/
199-
internal class StartNode internal constructor() : StartAIAgentNodeBase<String>()
199+
internal class StartNode internal constructor() : AIAgentStartNodeBase<String>()
200200

201201
/**
202202
* A specialized implementation of [FinishNode] that finalizes the execution of an AI agent subgraph.
@@ -210,5 +210,5 @@ internal class StartNode internal constructor() : StartAIAgentNodeBase<String>()
210210
*
211211
* This node is critical to denote the completion of localized processing within a subgraph context.
212212
*/
213-
internal class FinishNode internal constructor() : FinishAIAgentNodeBase<String>()
213+
internal class FinishNode internal constructor() : AIAgentFinishNodeBase<String>()
214214

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/agent/entity/AIAgentStrategy.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import ai.koog.agents.core.utils.runCatchingCancellable
1515
@OptIn(InternalAgentsApi::class)
1616
public class AIAgentStrategy(
1717
override val name: String,
18-
public val nodeStart: StartAIAgentNodeBase<String>,
19-
public val nodeFinish: FinishAIAgentNodeBase<String>,
18+
public val nodeStart: AIAgentStartNodeBase<String>,
19+
public val nodeFinish: AIAgentFinishNodeBase<String>,
2020
toolSelectionStrategy: ToolSelectionStrategy
2121
) : AIAgentSubgraph<String, String>(
2222
name, nodeStart, nodeFinish, toolSelectionStrategy

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/agent/entity/AIAgentSubgraph.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import kotlin.uuid.ExperimentalUuidApi
2828
*/
2929
public open class AIAgentSubgraph<Input, Output>(
3030
override val name: String,
31-
public val start: StartAIAgentNodeBase<Input>,
32-
public val finish: FinishAIAgentNodeBase<Output>,
31+
public val start: AIAgentStartNodeBase<Input>,
32+
public val finish: AIAgentFinishNodeBase<Output>,
3333
private val toolSelectionStrategy: ToolSelectionStrategy,
3434
) : AIAgentNodeBase<Input, Output>() {
3535
private companion object {
@@ -99,7 +99,7 @@ public open class AIAgentSubgraph<Input, Output>(
9999
"Invalid finish node output type: ${currentInput?.let { it::class.simpleName }}"
100100
)
101101
}
102-
throw IllegalStateException("${FinishAIAgentNodeBase::class.simpleName} should always return String")
102+
throw IllegalStateException("${AIAgentFinishNodeBase::class.simpleName} should always return String")
103103
}
104104
}
105105

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/dsl/builder/AIAgentStrategyBuilder.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class AIAgentStrategyBuilder(
1414
private val name: String,
1515
private val toolSelectionStrategy: ToolSelectionStrategy,
1616
) : AIAgentSubgraphBuilderBase<String, String>(), BaseBuilder<AIAgentStrategy> {
17-
public override val nodeStart: StartAIAgentNodeBase<String> = StartNode()
18-
public override val nodeFinish: FinishAIAgentNodeBase<String> = FinishNode()
17+
public override val nodeStart: AIAgentStartNodeBase<String> = StartNode()
18+
public override val nodeFinish: AIAgentFinishNodeBase<String> = FinishNode()
1919

2020
override fun build(): AIAgentStrategy {
2121
return AIAgentStrategy(

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/dsl/builder/AIAgentSubgraphBuilder.kt

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public abstract class AIAgentSubgraphBuilderBase<Input, Output> {
2727
*
2828
* @param Input The type of input data that this starting node processes.
2929
*/
30-
public abstract val nodeStart: StartAIAgentNodeBase<Input>
30+
public abstract val nodeStart: AIAgentStartNodeBase<Input>
3131

3232
/**
3333
* Represents the "finish" node in the AI agent's subgraph structure. This node indicates
@@ -43,7 +43,7 @@ public abstract class AIAgentSubgraphBuilderBase<Input, Output> {
4343
*
4444
* @param Output The type of data processed and produced by this node.
4545
*/
46-
public abstract val nodeFinish: FinishAIAgentNodeBase<Output>
46+
public abstract val nodeFinish: AIAgentFinishNodeBase<Output>
4747

4848
/**
4949
* Defines a new node in the agent's stage, representing a unit of execution that takes an input and produces an output.
@@ -108,20 +108,20 @@ public abstract class AIAgentSubgraphBuilderBase<Input, Output> {
108108
vararg nodes: AIAgentNodeBase<Input, Output>,
109109
dispatcher: CoroutineDispatcher = Dispatchers.Default,
110110
name: String? = null,
111-
): AIAgentNodeDelegateBase<Input, List<ForkedNodeResult<Input, Output>>> {
112-
return AIAgentNodeDelegate(name, ForkNodeBuilder(nodes.asList(), dispatcher))
111+
): AIAgentNodeDelegateBase<Input, List<ForkResult<Input, Output>>> {
112+
return AIAgentNodeDelegate(name, AIAgentForkNodeBuilder(nodes.asList(), dispatcher))
113113
}
114114

115115
/**
116116
* Creates a node that merges the results of the forked nodes.
117-
* @param execute Function to merge the contexts after parallel execution
117+
* @param execute Function to merge the contexts and outputs after parallel execution
118118
* @param name Optional node name
119119
*/
120120
public fun <Input, Output> merge(
121121
name: String? = null,
122-
execute: suspend AIAgentContextBase.(List<ForkedNodeResult<Input, Output>>) -> Pair<AIAgentContextBase, Output>,
123-
): AIAgentNodeDelegateBase<List<ForkedNodeResult<Input, Output>>, Output> {
124-
return AIAgentNodeDelegate(name, MergeNodeBuilder(execute))
122+
execute: suspend AIAgentContextBase.(List<ForkResult<Input, Output>>) -> Pair<AIAgentContextBase, Output>,
123+
): AIAgentNodeDelegateBase<List<ForkResult<Input, Output>>, Output> {
124+
return AIAgentNodeDelegate(name, AIAgentMergeNodeBuilder(execute))
125125
}
126126

127127
/**
@@ -136,11 +136,11 @@ public abstract class AIAgentSubgraphBuilderBase<Input, Output> {
136136
}
137137

138138
/**
139-
* Checks if finish node is reachable from start node.
139+
* Checks if the finish node is reachable from start node.
140140
* @param start Starting node
141-
* @return True if finish node is reachable
141+
* @return True if the finish node is reachable
142142
*/
143-
protected fun isFinishReachable(start: StartAIAgentNodeBase<Input>): Boolean {
143+
protected fun isFinishReachable(start: AIAgentStartNodeBase<Input>): Boolean {
144144
val visited = mutableSetOf<AIAgentNodeBase<*, *>>()
145145

146146
fun visit(node: AIAgentNodeBase<*, *>): Boolean {
@@ -173,8 +173,8 @@ public class AIAgentSubgraphBuilder<Input, Output>(
173173
private val toolSelectionStrategy: ToolSelectionStrategy
174174
) : AIAgentSubgraphBuilderBase<Input, Output>(),
175175
BaseBuilder<AIAgentSubgraphDelegate<Input, Output>> {
176-
override val nodeStart: StartAIAgentNodeBase<Input> = StartAIAgentNodeBase()
177-
override val nodeFinish: FinishAIAgentNodeBase<Output> = FinishAIAgentNodeBase()
176+
override val nodeStart: AIAgentStartNodeBase<Input> = AIAgentStartNodeBase()
177+
override val nodeFinish: AIAgentFinishNodeBase<Output> = AIAgentFinishNodeBase()
178178

179179
override fun build(): AIAgentSubgraphDelegate<Input, Output> {
180180
require(isFinishReachable(nodeStart)) {
@@ -227,8 +227,8 @@ public interface AIAgentSubgraphDelegateBase<Input, Output> {
227227
*/
228228
public open class AIAgentSubgraphDelegate<Input, Output> internal constructor(
229229
private val name: String?,
230-
public val nodeStart: StartAIAgentNodeBase<Input>,
231-
public val nodeFinish: FinishAIAgentNodeBase<Output>,
230+
public val nodeStart: AIAgentStartNodeBase<Input>,
231+
public val nodeFinish: AIAgentFinishNodeBase<Output>,
232232
private val toolSelectionStrategy: ToolSelectionStrategy
233233
) : AIAgentSubgraphDelegateBase<Input, Output> {
234234
private var subgraph: AIAgentSubgraph<Input, Output>? = null
@@ -258,7 +258,7 @@ public open class AIAgentSubgraphDelegate<Input, Output> internal constructor(
258258
* @property context Context of the node on the node termination state
259259
* @property output Output of the node
260260
*/
261-
public data class ForkedNodeResult<Input, Output>(
261+
public data class ForkResult<Input, Output>(
262262
val nodeName: String,
263263
val input: Input,
264264
val context: AIAgentContextBase,
@@ -272,10 +272,10 @@ public data class ForkedNodeResult<Input, Output>(
272272
* @param dispatcher Coroutine dispatcher to use for parallel execution
273273
*/
274274
@OptIn(ExperimentalUuidApi::class)
275-
public class ForkNodeBuilder<Input, Output> internal constructor(
275+
public class AIAgentForkNodeBuilder<Input, Output> internal constructor(
276276
private val nodes: List<AIAgentNodeBase<Input, Output>>,
277277
private val dispatcher: CoroutineDispatcher
278-
) : AIAgentNodeBuilder<Input, List<ForkedNodeResult<Input, Output>>>(
278+
) : AIAgentNodeBuilder<Input, List<ForkResult<Input, Output>>>(
279279
execute = { input ->
280280
val initialContext: AIAgentContextBase = this
281281
val mapResults = supervisorScope {
@@ -284,7 +284,7 @@ public class ForkNodeBuilder<Input, Output> internal constructor(
284284
val nodeContext = (initialContext as? ai.koog.agents.core.agent.context.AIAgentContext)?.fork()
285285
?: initialContext.fork()
286286
val result = node.execute(nodeContext, input)
287-
ForkedNodeResult(node.name, input, nodeContext, result)
287+
ForkResult(node.name, input, nodeContext, result)
288288
}
289289
}
290290
}
@@ -299,9 +299,9 @@ public class ForkNodeBuilder<Input, Output> internal constructor(
299299
* @param execute Function to merge the contexts after parallel execution
300300
*/
301301
@OptIn(ExperimentalUuidApi::class)
302-
public class MergeNodeBuilder<Input, Output> internal constructor(
303-
private val execute: suspend AIAgentContextBase.(List<ForkedNodeResult<Input, Output>>) -> Pair<AIAgentContextBase, Output>,
304-
) : AIAgentNodeBuilder<List<ForkedNodeResult<Input, Output>>, Output>(
302+
public class AIAgentMergeNodeBuilder<Input, Output> internal constructor(
303+
private val execute: suspend AIAgentContextBase.(List<ForkResult<Input, Output>>) -> Pair<AIAgentContextBase, Output>,
304+
) : AIAgentNodeBuilder<List<ForkResult<Input, Output>>, Output>(
305305
execute = { input ->
306306
val (context, output) = execute(input)
307307
this.replace(context)

agents/agents-core/src/commonTest/kotlin/ai/koog/agents/core/dsl/extension/ParallelNodesTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package ai.koog.agents.core.dsl.extension
33
import ai.koog.agents.core.agent.AIAgent
44
import ai.koog.agents.core.agent.config.AIAgentConfig
55
import ai.koog.agents.core.agent.entity.AIAgentStorageKey
6-
import ai.koog.agents.core.dsl.builder.ForkedNodeResult
6+
import ai.koog.agents.core.dsl.builder.ForkResult
77
import ai.koog.agents.core.dsl.builder.forwardTo
88
import ai.koog.agents.core.dsl.builder.strategy
99
import ai.koog.agents.core.tools.ToolRegistry
@@ -148,7 +148,7 @@ class ParallelNodesTest {
148148
)
149149

150150
// Create nodes to verify the context isolation during parallel execution
151-
val verifyNode by node<List<ForkedNodeResult<Unit, String>>, String>("verifyNode") { results ->
151+
val verifyNode by node<List<ForkResult<Unit, String>>, String>("verifyNode") { results ->
152152
results.map {
153153
// This node should only see the changes from node1
154154
val value1 = it.context.storage.get(testKey1)

agents/agents-test/src/commonMain/kotlin/ai/koog/agents/testing/feature/TestingFeature.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public sealed class NodeReference<Input, Output> {
108108
override fun resolve(subgraph: AIAgentSubgraph<*, *>): AIAgentNodeBase<Input, Output> {
109109
val visited = mutableSetOf<String>()
110110
fun visit(node: AIAgentNodeBase<*, *>): AIAgentNodeBase<Input, Output>? {
111-
if (node is FinishAIAgentNodeBase) return null
111+
if (node is AIAgentFinishNodeBase) return null
112112
if (visited.contains(node.name)) return null
113113
visited.add(node.name)
114114
if (node.name == name) return node as? AIAgentNodeBase<Input, Output>

0 commit comments

Comments
 (0)