Skip to content

Commit 590fe8d

Browse files
authored
feat: add Kotlin coroutine extension layer (Mono -> suspend, Flux -> Flow) (#376)
1 parent 69b2fc9 commit 590fe8d

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

agentscope-dependencies-bom/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@
102102
<json-schema-validator.version>3.0.0</json-schema-validator.version>
103103
<jsonschema-generator.version>4.38.0</jsonschema-generator.version>
104104
<snakeyaml.version>2.5</snakeyaml.version>
105+
<kotlin.version>1.9.24</kotlin.version>
106+
<kotlin.coroutines.version>1.8.1</kotlin.coroutines.version>
105107

106108

107109
<spotless.version>3.1.0</spotless.version>
@@ -232,6 +234,20 @@
232234
<version>${qdrant.version}</version>
233235
</dependency>
234236

237+
<!-- Kotlin Coroutines -->
238+
<dependency>
239+
<groupId>org.jetbrains.kotlinx</groupId>
240+
<artifactId>kotlinx-coroutines-core</artifactId>
241+
<version>${kotlin.coroutines.version}</version>
242+
</dependency>
243+
244+
<dependency>
245+
<groupId>org.jetbrains.kotlinx</groupId>
246+
<artifactId>kotlinx-coroutines-reactor</artifactId>
247+
<version>${kotlin.coroutines.version}</version>
248+
</dependency>
249+
250+
235251
<!-- Milvus Java SDK -->
236252
<dependency>
237253
<groupId>io.milvus</groupId>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<!--
2+
~ Copyright 2024-2026 the original author or authors.
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
17+
18+
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
22+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
23+
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<parent>
27+
<groupId>io.agentscope</groupId>
28+
<artifactId>agentscope-extensions</artifactId>
29+
<version>${revision}</version>
30+
<relativePath>../pom.xml</relativePath>
31+
</parent>
32+
33+
<artifactId>agentscope-extensions-kotlin</artifactId>
34+
<name>AgentScope Kotlin Coroutine Extensions</name>
35+
<packaging>jar</packaging>
36+
37+
<dependencies>
38+
<!-- AgentScope core -->
39+
<dependency>
40+
<groupId>io.agentscope</groupId>
41+
<artifactId>agentscope-core</artifactId>
42+
</dependency>
43+
44+
<!-- Kotlin coroutines -->
45+
<dependency>
46+
<groupId>org.jetbrains.kotlinx</groupId>
47+
<artifactId>kotlinx-coroutines-core</artifactId>
48+
</dependency>
49+
50+
<!-- Reactor <-> Coroutine bridge -->
51+
<dependency>
52+
<groupId>org.jetbrains.kotlinx</groupId>
53+
<artifactId>kotlinx-coroutines-reactor</artifactId>
54+
</dependency>
55+
</dependencies>
56+
57+
<build>
58+
<plugins>
59+
<plugin>
60+
<groupId>org.jetbrains.kotlin</groupId>
61+
<artifactId>kotlin-maven-plugin</artifactId>
62+
<version>${kotlin.version}</version>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
67+
</project>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<!--
2+
~ Copyright 2024-2026 the original author or authors.
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
17+
18+
19+
20+
21+
package io.agentscope.kotlin
22+
23+
import io.agentscope.core.agent.Agent
24+
import io.agentscope.core.agent.Event
25+
import io.agentscope.core.agent.StreamOptions
26+
import io.agentscope.core.message.Msg
27+
import kotlinx.coroutines.flow.Flow
28+
import kotlinx.coroutines.reactor.awaitFirstOrNull
29+
import kotlinx.coroutines.reactor.awaitSingle
30+
import kotlinx.coroutines.reactive.asFlow
31+
32+
/* ---------- call(...) -> suspend ---------- */
33+
34+
suspend fun Agent.callSuspend(msg: Msg): Msg =
35+
this.call(msg).awaitSingle()
36+
37+
suspend fun Agent.callSuspend(msgs: List<Msg>): Msg =
38+
this.call(msgs).awaitSingle()
39+
40+
suspend fun Agent.callSuspend(): Msg =
41+
this.call().awaitSingle()
42+
43+
suspend fun Agent.callSuspend(
44+
msg: Msg,
45+
structuredModel: Class<*>
46+
): Msg =
47+
this.call(msg, structuredModel).awaitSingle()
48+
49+
suspend fun Agent.callSuspend(
50+
msgs: List<Msg>,
51+
structuredModel: Class<*>
52+
): Msg =
53+
this.call(msgs, structuredModel).awaitSingle()
54+
55+
suspend fun Agent.callSuspend(
56+
structuredModel: Class<*>
57+
): Msg =
58+
this.call(structuredModel).awaitSingle()
59+
60+
/* ---------- observe(...) -> suspend ---------- */
61+
62+
suspend fun Agent.observeSuspend(msg: Msg) {
63+
this.observe(msg).awaitFirstOrNull()
64+
}
65+
66+
suspend fun Agent.observeSuspend(msgs: List<Msg>) {
67+
this.observe(msgs).awaitFirstOrNull()
68+
}
69+
70+
/* ---------- stream(...) -> Flow ---------- */
71+
72+
fun Agent.streamFlow(
73+
msg: Msg,
74+
options: StreamOptions = StreamOptions.defaults()
75+
): Flow<Event> =
76+
this.stream(msg, options).asFlow()
77+
78+
fun Agent.streamFlow(
79+
msgs: List<Msg>,
80+
options: StreamOptions = StreamOptions.defaults()
81+
): Flow<Event> =
82+
this.stream(msgs, options).asFlow()
83+
84+
fun Agent.streamFlow(
85+
msg: Msg,
86+
options: StreamOptions,
87+
structuredModel: Class<*>
88+
): Flow<Event> =
89+
this.stream(msg, options, structuredModel).asFlow()
90+
91+
fun Agent.streamFlow(
92+
msgs: List<Msg>,
93+
options: StreamOptions,
94+
structuredModel: Class<*>
95+
): Flow<Event> =
96+
this.stream(msgs, options, structuredModel).asFlow()

0 commit comments

Comments
 (0)