@tinyweb_dev/agent-framework-tool 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +114 -0
- package/package.json +50 -0
- package/src/builtins.ts +78 -0
- package/src/display_response.ts +79 -0
- package/src/file_read.ts +31 -0
- package/src/file_write.ts +33 -0
- package/src/image_describe.ts +58 -0
- package/src/image_edit.ts +74 -0
- package/src/image_generate.ts +102 -0
- package/src/index.ts +47 -0
- package/src/info/crypto.ts +79 -0
- package/src/info/time.ts +83 -0
- package/src/info/weather.ts +134 -0
- package/src/search/perplexity.ts +129 -0
- package/src/shared/html.ts +87 -0
- package/src/shared/http.ts +40 -0
- package/src/shared/s3.ts +157 -0
- package/src/shared/storage.ts +83 -0
- package/src/vn/gold.ts +192 -0
- package/src/vn/lottery.ts +129 -0
- package/src/web_fetch.ts +80 -0
- package/src/web_search.ts +92 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { defineTool } from "@tinyweb-agent-framework/agent"
|
|
2
|
+
import { Schema } from "effect"
|
|
3
|
+
import { fetchJson, postJson } from "./shared/http"
|
|
4
|
+
|
|
5
|
+
export type WebSearchProvider = "tavily" | "brave" | "exa" | "serper"
|
|
6
|
+
|
|
7
|
+
export type WebSearchConfig = {
|
|
8
|
+
provider: WebSearchProvider
|
|
9
|
+
apiKey: string
|
|
10
|
+
name?: string
|
|
11
|
+
description?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const ResultItem = Schema.Struct({
|
|
15
|
+
title: Schema.String,
|
|
16
|
+
url: Schema.String,
|
|
17
|
+
snippet: Schema.String,
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
const Parameters = Schema.Struct({
|
|
21
|
+
query: Schema.String,
|
|
22
|
+
limit: Schema.optional(Schema.Number),
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
const Success = Schema.Struct({
|
|
26
|
+
results: Schema.Array(ResultItem),
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
type SearchInput = Schema.Schema.Type<typeof Parameters>
|
|
30
|
+
type SearchResult = Schema.Schema.Type<typeof ResultItem>
|
|
31
|
+
|
|
32
|
+
type TavilyResult = { title: string; url: string; content?: string }
|
|
33
|
+
type TavilyResponse = { results: TavilyResult[] }
|
|
34
|
+
type BraveResult = { title: string; url: string; description?: string }
|
|
35
|
+
type BraveResponse = { web?: { results?: BraveResult[] } }
|
|
36
|
+
type ExaResult = { title?: string; url: string; text?: string }
|
|
37
|
+
type ExaResponse = { results: ExaResult[] }
|
|
38
|
+
type SerperResult = { title: string; link: string; snippet?: string }
|
|
39
|
+
type SerperResponse = { organic?: SerperResult[] }
|
|
40
|
+
|
|
41
|
+
const searchTavily = async (apiKey: string, input: SearchInput): Promise<SearchResult[]> => {
|
|
42
|
+
const data = await postJson<TavilyResponse>("https://api.tavily.com/search", {
|
|
43
|
+
api_key: apiKey,
|
|
44
|
+
query: input.query,
|
|
45
|
+
max_results: input.limit ?? 5,
|
|
46
|
+
})
|
|
47
|
+
return data.results.map((r) => ({ title: r.title, url: r.url, snippet: r.content ?? "" }))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const searchBrave = async (apiKey: string, input: SearchInput): Promise<SearchResult[]> => {
|
|
51
|
+
const params = new URLSearchParams({ q: input.query, count: String(input.limit ?? 5) })
|
|
52
|
+
const data = await fetchJson<BraveResponse>(`https://api.search.brave.com/res/v1/web/search?${params}`, {
|
|
53
|
+
headers: { "X-Subscription-Token": apiKey, accept: "application/json" },
|
|
54
|
+
})
|
|
55
|
+
const items = data.web?.results ?? []
|
|
56
|
+
return items.map((r) => ({ title: r.title, url: r.url, snippet: r.description ?? "" }))
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const searchExa = async (apiKey: string, input: SearchInput): Promise<SearchResult[]> => {
|
|
60
|
+
const data = await postJson<ExaResponse>(
|
|
61
|
+
"https://api.exa.ai/search",
|
|
62
|
+
{ query: input.query, numResults: input.limit ?? 5, contents: { text: true } },
|
|
63
|
+
{ headers: { "x-api-key": apiKey } },
|
|
64
|
+
)
|
|
65
|
+
return data.results.map((r) => ({ title: r.title ?? r.url, url: r.url, snippet: (r.text ?? "").slice(0, 500) }))
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const searchSerper = async (apiKey: string, input: SearchInput): Promise<SearchResult[]> => {
|
|
69
|
+
const data = await postJson<SerperResponse>(
|
|
70
|
+
"https://google.serper.dev/search",
|
|
71
|
+
{ q: input.query, num: input.limit ?? 5 },
|
|
72
|
+
{ headers: { "X-API-KEY": apiKey } },
|
|
73
|
+
)
|
|
74
|
+
return (data.organic ?? []).map((r) => ({ title: r.title, url: r.link, snippet: r.snippet ?? "" }))
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const dispatch = (provider: WebSearchProvider, apiKey: string, input: SearchInput): Promise<SearchResult[]> => {
|
|
78
|
+
if (provider === "tavily") return searchTavily(apiKey, input)
|
|
79
|
+
if (provider === "brave") return searchBrave(apiKey, input)
|
|
80
|
+
if (provider === "exa") return searchExa(apiKey, input)
|
|
81
|
+
if (provider === "serper") return searchSerper(apiKey, input)
|
|
82
|
+
throw new Error(`Unknown web_search provider: ${provider as string}`)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export const webSearch = (config: WebSearchConfig) =>
|
|
86
|
+
defineTool({
|
|
87
|
+
name: config.name ?? "web_search",
|
|
88
|
+
description: config.description ?? "Search the web and return a list of {title, url, snippet} results.",
|
|
89
|
+
parameters: Parameters,
|
|
90
|
+
success: Success,
|
|
91
|
+
execute: async (input) => ({ results: await dispatch(config.provider, config.apiKey, input) }),
|
|
92
|
+
})
|