Skip to content

Commit 1e313ba

Browse files
authored
refactor(agents)!: Agent clock migration (#1925)
Introduced KoogClock and migrated APIs usages to that clock BREAKING: All apis that use kotlin.time.Clock
1 parent e40dc81 commit 1e313ba

179 files changed

Lines changed: 716 additions & 755 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import ai.koog.prompt.executor.model.PromptExecutor
1414
import ai.koog.prompt.llm.LLModel
1515
import ai.koog.prompt.processor.ResponseProcessor
1616
import ai.koog.utils.io.Closeable
17+
import ai.koog.utils.time.KoogClock
1718
import kotlin.jvm.JvmStatic
18-
import kotlin.time.Clock
1919
import kotlin.uuid.ExperimentalUuidApi
2020

2121
/**
@@ -87,7 +87,7 @@ public expect abstract class AIAgent<Input, Output> constructor() : Closeable {
8787
strategy: AIAgentGraphStrategy<Input, Output>,
8888
toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
8989
id: String? = null,
90-
clock: Clock = Clock.System,
90+
clock: KoogClock = KoogClock.System,
9191
noinline installFeatures: FeatureContext.() -> Unit = {},
9292
): AIAgent<Input, Output>
9393

@@ -133,7 +133,7 @@ public expect abstract class AIAgent<Input, Output> constructor() : Closeable {
133133
strategy: AIAgentFunctionalStrategy<Input, Output>,
134134
toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
135135
id: String? = null,
136-
clock: Clock = Clock.System,
136+
clock: KoogClock = KoogClock.System,
137137
installFeatures: FunctionalAIAgent.FeatureContext.() -> Unit = {},
138138
): FunctionalAIAgent<Input, Output>
139139

@@ -179,7 +179,7 @@ public expect abstract class AIAgent<Input, Output> constructor() : Closeable {
179179
* @param responseProcessor The processor responsible for processing the model's responses.
180180
* @param toolRegistry An optional [ToolRegistry] specifying the tools available to the agent for execution. Defaults to `[ToolRegistry.EMPTY]`.
181181
* @param id Unique identifier for the agent. Random UUID will be generated if set to null.
182-
* @param clock A `Clock` instance used for time-related operations. Defaults to `Clock.System`.
182+
* @param clock A `Clock` instance used for time-related operations. Defaults to `KoogClock.System`.
183183
* @param systemPrompt Optional system prompt for the agent.
184184
* @param temperature Optional model temperature, with valid values ranging typically from 0.0 to 1.0.
185185
* @param numberOfChoices The number of choices the model should generate per invocation. Defaults to `1`.
@@ -195,7 +195,7 @@ public expect abstract class AIAgent<Input, Output> constructor() : Closeable {
195195
responseProcessor: ResponseProcessor? = null,
196196
toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
197197
id: String? = null,
198-
clock: Clock = Clock.System,
198+
clock: KoogClock = KoogClock.System,
199199
systemPrompt: String? = null,
200200
temperature: Double? = null,
201201
numberOfChoices: Int = 1,
@@ -284,7 +284,7 @@ public expect abstract class AIAgent<Input, Output> constructor() : Closeable {
284284
strategy: AIAgentPlannerStrategy<Input, Output, *>,
285285
toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
286286
id: String? = null,
287-
clock: Clock = Clock.System,
287+
clock: KoogClock = KoogClock.System,
288288
installFeatures: PlannerAIAgent.FeatureContext.() -> Unit = {},
289289
): AIAgent<Input, Output>
290290
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import ai.koog.agents.planner.AIAgentPlannerStrategy
1515
import ai.koog.agents.planner.PlannerAIAgent
1616
import ai.koog.prompt.executor.model.PromptExecutor
1717
import ai.koog.serialization.TypeToken
18-
import kotlin.time.Clock
18+
import ai.koog.utils.time.KoogClock
1919

2020
/**
2121
* Represents a configurational builder for setting up and customizing the execution parameters and
@@ -49,7 +49,7 @@ public class GraphAgentBuilder<Input, Output>(
4949
toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
5050
id: String? = null,
5151
config: AIAgentConfig,
52-
clock: Clock = Clock.System,
52+
clock: KoogClock = KoogClock.System,
5353
private var featureInstallers: MutableList<FeatureContext.() -> Unit> = mutableListOf(),
5454
) : AIAgentBuilderBase<GraphAgentBuilder<Input, Output>>(
5555
promptExecutor = promptExecutor,
@@ -131,7 +131,7 @@ public class GraphAgentBuilder<Input, Output>(
131131
* @property toolRegistry A registry of tools available to the agent, by default set to `ToolRegistry.EMPTY`.
132132
* @property id An optional unique identifier for the agent.
133133
* @property config [AIAgentConfig] containing initial agent configuration for the builder
134-
* @property clock The clock instance used for time-related functionality, default is `Clock.System`.
134+
* @property clock The clock instance used for time-related functionality, default is `KoogClock.System`.
135135
* @property featureInstallers A list of feature installation lambdas defining additional functionalities the agent should have.
136136
*/
137137
public class FunctionalAgentBuilder<Input, Output>(
@@ -140,7 +140,7 @@ public class FunctionalAgentBuilder<Input, Output>(
140140
toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
141141
id: String? = null,
142142
config: AIAgentConfig,
143-
clock: Clock = Clock.System,
143+
clock: KoogClock = KoogClock.System,
144144
private var featureInstallers: MutableList<FunctionalAIAgent.FeatureContext.() -> Unit> = mutableListOf(),
145145
) : AIAgentBuilderBase<FunctionalAgentBuilder<Input, Output>>(
146146
promptExecutor = promptExecutor,
@@ -213,7 +213,7 @@ public class PlannerAgentBuilder<Input, Output>(
213213
toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
214214
id: String? = null,
215215
config: AIAgentConfig,
216-
clock: Clock = Clock.System,
216+
clock: KoogClock = KoogClock.System,
217217
private var featureInstallers: MutableList<PlannerAIAgent.FeatureContext.() -> Unit> = mutableListOf(),
218218
) : AIAgentBuilderBase<PlannerAgentBuilder<Input, Output>>(
219219
promptExecutor = promptExecutor,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import ai.koog.prompt.dsl.Prompt
66
import ai.koog.prompt.executor.model.PromptExecutor
77
import ai.koog.serialization.JSONSerializer
88
import ai.koog.serialization.kotlinx.KotlinxSerializer
9-
import kotlin.time.Clock
9+
import ai.koog.utils.time.KoogClock
1010

1111
/**
1212
* Shared fluent configuration for agent builders.
@@ -16,7 +16,7 @@ public abstract class AIAgentBuilderBase<Self : AIAgentBuilderBase<Self>> intern
1616
toolRegistry: ToolRegistry,
1717
protected var id: String?,
1818
config: AIAgentConfig,
19-
protected var clock: Clock,
19+
protected var clock: KoogClock,
2020
) : AIAgentServiceBuilderBase<Self>(
2121
promptExecutor = promptExecutor,
2222
toolRegistry = toolRegistry,
@@ -34,7 +34,7 @@ public abstract class AIAgentBuilderBase<Self : AIAgentBuilderBase<Self>> intern
3434
maxAgentIterations = 50,
3535
serializer = serializer
3636
),
37-
Clock.System,
37+
KoogClock.System,
3838
)
3939

4040
/**
@@ -50,7 +50,7 @@ public abstract class AIAgentBuilderBase<Self : AIAgentBuilderBase<Self>> intern
5050
/**
5151
* Sets the clock for the agent.
5252
*/
53-
public fun clock(clock: Clock): Self = self().apply {
53+
public fun clock(clock: KoogClock): Self = self().apply {
5454
this.clock = clock
5555
}
5656
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import ai.koog.prompt.llm.LLModel
1212
import ai.koog.prompt.params.LLMParams
1313
import ai.koog.prompt.processor.ResponseProcessor
1414
import ai.koog.serialization.typeToken
15+
import ai.koog.utils.time.KoogClock
1516
import kotlin.jvm.JvmStatic
16-
import kotlin.time.Clock
1717
import kotlin.uuid.ExperimentalUuidApi
1818

1919
@PublishedApi
@@ -50,7 +50,7 @@ internal object AIAgentHelper {
5050
strategy: AIAgentGraphStrategy<Input, Output>,
5151
toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
5252
id: String? = null,
53-
clock: Clock = Clock.System,
53+
clock: KoogClock = KoogClock.System,
5454
noinline installFeatures: FeatureContext.() -> Unit = {},
5555
): AIAgent<Input, Output> {
5656
return GraphAIAgent(
@@ -94,7 +94,7 @@ internal object AIAgentHelper {
9494
toolRegistry = toolRegistry,
9595
strategy = strategy,
9696
id = id,
97-
clock = Clock.System,
97+
clock = KoogClock.System,
9898
installFeatures = installFeatures
9999
)
100100

@@ -117,7 +117,7 @@ internal object AIAgentHelper {
117117
strategy: AIAgentFunctionalStrategy<Input, Output>,
118118
toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
119119
id: String? = null,
120-
clock: Clock = Clock.System,
120+
clock: KoogClock = KoogClock.System,
121121
installFeatures: FunctionalAIAgent.FeatureContext.() -> Unit = {},
122122
): FunctionalAIAgent<Input, Output> {
123123
return FunctionalAIAgent(
@@ -191,7 +191,7 @@ internal object AIAgentHelper {
191191
* @param strategy The agent strategy [AIAgentGraphStrategy] defining how the agent processes inputs and outputs.
192192
* @param toolRegistry An optional [ToolRegistry] specifying the tools available to the agent for execution. Defaults to `[ToolRegistry.EMPTY]`.
193193
* @param id Unique identifier for the agent. Random UUID will be generated if set to null.
194-
* @param clock A `Clock` instance used for time-related operations. Defaults to `Clock.System`.
194+
* @param clock A `Clock` instance used for time-related operations. Defaults to `KoogClock.System`.
195195
* @param systemPrompt A string representing the system-level prompt for the agent. Defaults to an empty string.
196196
* @param temperature A double value controlling the randomness of the model's output. Defaults to `1.0`.
197197
* @param numberOfChoices The number of choices the model should generate per invocation. Defaults to `1`.
@@ -208,7 +208,7 @@ internal object AIAgentHelper {
208208
responseProcessor: ResponseProcessor? = null,
209209
toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
210210
id: String? = null,
211-
clock: Clock = Clock.System,
211+
clock: KoogClock = KoogClock.System,
212212
systemPrompt: String? = null,
213213
temperature: Double? = null,
214214
numberOfChoices: Int = 1,
@@ -326,7 +326,7 @@ internal object AIAgentHelper {
326326
strategy: AIAgentPlannerStrategy<Input, Output, *>,
327327
toolRegistry: ToolRegistry,
328328
id: String?,
329-
clock: Clock,
329+
clock: KoogClock,
330330
installFeatures: PlannerAIAgent.FeatureContext.() -> Unit
331331
): AIAgent<Input, Output> = PlannerAIAgent(
332332
promptExecutor = promptExecutor,

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import ai.koog.serialization.KSerializerTypeToken
1818
import ai.koog.serialization.TypeToken
1919
import ai.koog.serialization.annotations.InternalKoogSerializationApi
2020
import ai.koog.serialization.typeToken
21+
import ai.koog.utils.time.KoogClock
2122
import kotlinx.coroutines.sync.Mutex
2223
import kotlinx.coroutines.sync.withLock
2324
import kotlinx.serialization.KSerializer
2425
import kotlin.jvm.JvmStatic
25-
import kotlin.time.Clock
2626

2727
/**
2828
* [AIAgentService] is a core interface for managing AI agents. The service allows creation, removal, and
@@ -201,7 +201,7 @@ public expect abstract class AIAgentService<Input, Output, TAgent : AIAgent<Inpu
201201
id: String? = null,
202202
additionalToolRegistry: ToolRegistry = ToolRegistry.EMPTY,
203203
agentConfig: AIAgentConfig = this.agentConfig,
204-
clock: Clock = Clock.System,
204+
clock: KoogClock = KoogClock.System,
205205
): TAgent
206206

207207
/**
@@ -219,7 +219,7 @@ public expect abstract class AIAgentService<Input, Output, TAgent : AIAgent<Inpu
219219
id: String? = null,
220220
additionalToolRegistry: ToolRegistry = this.toolRegistry,
221221
agentConfig: AIAgentConfig = this.agentConfig,
222-
clock: Clock = Clock.System,
222+
clock: KoogClock = KoogClock.System,
223223
): Output
224224
}
225225

@@ -250,7 +250,7 @@ public abstract class AIAgentServiceBase<Input, Output, TAgent : AIAgent<Input,
250250
id: String?,
251251
additionalToolRegistry: ToolRegistry,
252252
agentConfig: AIAgentConfig,
253-
clock: Clock
253+
clock: KoogClock
254254
): Output = createAgent(id, additionalToolRegistry, agentConfig, clock).run(agentInput, null)
255255

256256
/**
@@ -267,7 +267,7 @@ public abstract class AIAgentServiceBase<Input, Output, TAgent : AIAgent<Input,
267267
id: String? = null,
268268
additionalToolRegistry: ToolRegistry,
269269
agentConfig: AIAgentConfig,
270-
clock: Clock = Clock.System,
270+
clock: KoogClock = KoogClock.System,
271271
): TAgent
272272

273273
/**
@@ -284,7 +284,7 @@ public abstract class AIAgentServiceBase<Input, Output, TAgent : AIAgent<Input,
284284
id: String?,
285285
additionalToolRegistry: ToolRegistry,
286286
agentConfig: AIAgentConfig,
287-
clock: Clock
287+
clock: KoogClock
288288
): TAgent = managedAgentsMutex.withLock {
289289
val agent = createManagedAgent(id, additionalToolRegistry, agentConfig, clock)
290290
managedAgents[agent.id] = agent
@@ -353,7 +353,7 @@ public constructor(
353353
id: String?,
354354
additionalToolRegistry: ToolRegistry,
355355
agentConfig: AIAgentConfig,
356-
clock: Clock,
356+
clock: KoogClock,
357357
): GraphAIAgent<Input, Output> = GraphAIAgent(
358358
inputType = inputType,
359359
outputType = outputType,
@@ -403,7 +403,7 @@ public constructor(
403403
id: String?,
404404
additionalToolRegistry: ToolRegistry,
405405
agentConfig: AIAgentConfig,
406-
clock: Clock,
406+
clock: KoogClock,
407407
): FunctionalAIAgent<Input, Output> = FunctionalAIAgent(
408408
promptExecutor = promptExecutor,
409409
agentConfig = agentConfig,
@@ -448,7 +448,7 @@ public operator fun AIAgentService.Companion.invoke(
448448
* @param outputType Type token representing agent output.
449449
* @return A special tool that wraps the agent functionality.
450450
* @param parentAgentId Optional ID of the parent AI agent. Tool agent IDs will be generated as "parentAgentId.<number of tool call>"
451-
* @param clock The clock instance used to manage time-related operations. Defaults to `Clock.System`.
451+
* @param clock The clock instance used to manage time-related operations. Defaults to `KoogClock.System`.
452452
* @return A tool instance configured with the provided parameters, representing the AI agent.
453453
*/
454454
@OptIn(InternalAgentToolsApi::class)
@@ -459,7 +459,7 @@ public inline fun <reified Input, reified Output> AIAgentService<Input, Output,
459459
inputType: TypeToken = typeToken<Input>(),
460460
outputType: TypeToken = typeToken<Output>(),
461461
parentAgentId: String? = null,
462-
clock: Clock = Clock.System
462+
clock: KoogClock = KoogClock.System
463463
): Tool<AgentToolInput<Input>, AgentToolResult<Output>> = AIAgentTool(
464464
agentService = this,
465465
agentName = agentName,
@@ -482,7 +482,7 @@ public inline fun <reified Input, reified Output> AIAgentService<Input, Output,
482482
* @param outputSerializer Serializer to serialize agent output to a tool result.
483483
* @return A special tool that wraps the agent functionality.
484484
* @param parentAgentId Optional ID of the parent AI agent. Tool agent IDs will be generated as "parentAgentId.<number of tool call>"
485-
* @param clock The clock instance used to manage time-related operations. Defaults to `Clock.System`.
485+
* @param clock The clock instance used to manage time-related operations. Defaults to `KoogClock.System`.
486486
* @return A tool instance configured with the provided parameters, representing the AI agent.
487487
*/
488488
@Deprecated("Use createAgentTool with TypeToken instead of KSerializer")
@@ -494,7 +494,7 @@ public inline fun <reified Input, reified Output> AIAgentService<Input, Output,
494494
inputSerializer: KSerializer<Input>,
495495
outputSerializer: KSerializer<Output>,
496496
parentAgentId: String? = null,
497-
clock: Clock = Clock.System
497+
clock: KoogClock = KoogClock.System
498498
): Tool<AgentToolInput<Input>, AgentToolResult<Output>> = createAgentTool(
499499
agentName = agentName,
500500
agentDescription = agentDescription,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import ai.koog.agents.core.tools.ToolRegistry
1313
import ai.koog.agents.core.utils.ConfigureAction
1414
import ai.koog.prompt.executor.model.PromptExecutor
1515
import ai.koog.serialization.TypeToken
16-
import kotlin.time.Clock
16+
import ai.koog.utils.time.KoogClock
1717

1818
/**
1919
* Builder for creating AIAgentService instances.
@@ -55,7 +55,7 @@ public class GraphAgentServiceBuilder<Input, Output> internal constructor(
5555
toolRegistry = toolRegistry,
5656
id = null,
5757
config = config,
58-
clock = Clock.System,
58+
clock = KoogClock.System,
5959
) {
6060
override fun self(): GraphAgentServiceBuilder<Input, Output> = this
6161

@@ -127,7 +127,7 @@ public class FunctionalAgentServiceBuilder<Input, Output> internal constructor(
127127
toolRegistry = toolRegistry,
128128
id = null,
129129
config = config,
130-
clock = Clock.System,
130+
clock = KoogClock.System,
131131
) {
132132
override fun self(): FunctionalAgentServiceBuilder<Input, Output> = this
133133

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import ai.koog.agents.core.feature.config.FeatureConfig
1717
import ai.koog.agents.core.feature.pipeline.AIAgentFunctionalPipeline
1818
import ai.koog.agents.core.tools.ToolRegistry
1919
import ai.koog.prompt.executor.model.PromptExecutor
20+
import ai.koog.utils.time.KoogClock
2021
import io.github.oshai.kotlinlogging.KotlinLogging
21-
import kotlin.time.Clock
2222

2323
/**
2424
* Represents the core AI agent for processing input and generating output using
@@ -42,7 +42,7 @@ public class FunctionalAIAgent<Input, Output>(
4242
override val strategy: AIAgentFunctionalStrategy<Input, Output>,
4343
public val toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
4444
id: String? = null,
45-
public val clock: Clock = Clock.System,
45+
public val clock: KoogClock = KoogClock.System,
4646
@property:InternalAgentsApi
4747
public val installFeatures: FeatureContext.() -> Unit = {}
4848
) : AIAgentBase<Input, Output, AIAgentFunctionalContext>(

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import ai.koog.agents.core.tools.ToolRegistry
2121
import ai.koog.prompt.executor.model.PromptExecutor
2222
import ai.koog.serialization.TypeToken
2323
import ai.koog.serialization.typeToken
24+
import ai.koog.utils.time.KoogClock
2425
import io.github.oshai.kotlinlogging.KotlinLogging
2526
import kotlin.reflect.KType
26-
import kotlin.time.Clock
2727

2828
/**
2929
* Represents an implementation of an AI agent that provides functionalities to execute prompts,
@@ -57,7 +57,7 @@ public open class GraphAIAgent<Input, Output>(
5757
override val strategy: AIAgentGraphStrategy<Input, Output>,
5858
public val toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
5959
id: String? = null,
60-
public val clock: Clock = Clock.System,
60+
public val clock: KoogClock = KoogClock.System,
6161
@property:InternalAgentsApi
6262
public val installFeatures: FeatureContext.() -> Unit = {}
6363
) : AIAgentBase<Input, Output, AIAgentGraphContextBase>(
@@ -86,7 +86,7 @@ public open class GraphAIAgent<Input, Output>(
8686
strategy: AIAgentGraphStrategy<Input, Output>,
8787
toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
8888
id: String? = null,
89-
clock: Clock = Clock.System,
89+
clock: KoogClock = KoogClock.System,
9090
installFeatures: FeatureContext.() -> Unit = {}
9191
) : this(
9292
typeToken(inputType),

0 commit comments

Comments
 (0)