Skip to content

Commit 00b2ed6

Browse files
committed
feat: init webtools for demos
1 parent e647b37 commit 00b2ed6

File tree

5 files changed

+85
-1
lines changed

5 files changed

+85
-1
lines changed

cocoa-core/src/main/kotlin/cc/unitmesh/agent/Tool.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package cc.unitmesh.agent
22

33

44
@Retention(AnnotationRetention.RUNTIME)
5-
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
5+
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER,
6+
AnnotationTarget.CLASS
7+
)
68
annotation class Tool(
79
val name: String = "",
810
vararg val value: String = [""],

settings.gradle.kts

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ include(":llm-modules:connection-manager")
2323
include(":code:interpreter")
2424
include(":code:code-splitter")
2525

26+
include(":tools:web-tools")
27+
2628
dependencyResolutionManagement {
2729
repositories {
2830
mavenCentral()

tools/web-tools/build.gradle.kts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@Suppress("DSL_SCOPE_VIOLATION")
2+
plugins {
3+
java
4+
alias(libs.plugins.jvm)
5+
alias(libs.plugins.serialization)
6+
}
7+
8+
dependencies {
9+
implementation(projects.cocoaCore)
10+
implementation(libs.kotlin.stdlib)
11+
implementation(libs.serialization.json)
12+
13+
implementation("org.jsoup:jsoup:1.16.1")
14+
15+
testImplementation(libs.bundles.test)
16+
testRuntimeOnly(libs.test.junit.engine)
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cc.unitmesh.tools.web
2+
3+
import cc.unitmesh.agent.Tool
4+
import org.jsoup.Jsoup
5+
import java.net.URLEncoder
6+
import java.nio.charset.StandardCharsets
7+
8+
9+
@Tool(name = "wikimedia", value = ["wikimedia"])
10+
class Wikimedia {
11+
val headers = mapOf(
12+
"User-Agent" to "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " +
13+
"Chrome/117.0.0.0 Safari/537.36 Edg/113.0.1774.35"
14+
)
15+
16+
private fun removeNestedParentheses(string: String): String {
17+
val pattern = "\\([^()]+\\)".toRegex()
18+
var result = string
19+
while (pattern.containsMatchIn(result)) {
20+
result = pattern.replace(result, "")
21+
}
22+
return result
23+
}
24+
25+
val urlList = mutableListOf<String>()
26+
fun run(title: String): MutableList<String> {
27+
val url = url(title)
28+
val doc = Jsoup.connect(url).headers(headers).get()
29+
val mwDivs = doc.select("div.mw-search-result-heading")
30+
if (mwDivs.isNotEmpty()) {
31+
val resultTitles = mwDivs.map { it.text().trim() }
32+
.map { removeNestedParentheses(it) }
33+
34+
urlList.addAll(resultTitles.map { url(it) })
35+
} else {
36+
val pageContent = (doc.select("p") + doc.select("ul")).map { it.text().trim() }
37+
if (pageContent.any { "may refer to:" in it }) {
38+
urlList.addAll(run("[$title]"))
39+
} else {
40+
urlList.add(url)
41+
}
42+
}
43+
44+
return urlList
45+
}
46+
47+
private fun url(it: String) =
48+
"https://en.wikipedia.org/w/index.php?search=${URLEncoder.encode(it, StandardCharsets.UTF_8)}"
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cc.unitmesh.tools.web;
2+
3+
import org.junit.jupiter.api.Test
4+
import kotlin.test.Ignore
5+
6+
class MyServiceTest {
7+
8+
9+
@Test
10+
@Ignore
11+
fun should_return_url_list_when_run_with_valid_title() {
12+
Wikimedia().run("Intellij").forEach { println(it) }
13+
}
14+
}

0 commit comments

Comments
 (0)