@graphai/brave_search_agent 0.0.1

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/README.md ADDED
@@ -0,0 +1,79 @@
1
+
2
+ # @graphai/brave_search_agent for GraphAI
3
+
4
+ An agent that uses the Brave Search API
5
+
6
+ ### Install
7
+
8
+ ```sh
9
+ yarn add @graphai/brave_search_agent
10
+ ```
11
+
12
+
13
+ ### Usage
14
+
15
+ ```typescript
16
+ import { GraphAI } from "graphai";
17
+ import { braveSearchAgent } from "@graphai/brave_search_agent";
18
+
19
+ const agents = { braveSearchAgent };
20
+
21
+ const graph = new GraphAI(graph_data, agents);
22
+ const result = await graph.run();
23
+ ```
24
+
25
+ ### Agents description
26
+ - braveSearchAgent - An agent that uses the Brave Search API. https://api-dashboard.search.brave.com/app/documentation/web-search/get-started
27
+
28
+ ### Input/Output/Params Schema & samples
29
+ - [braveSearchAgent](https://github.com/receptron/graphai-agents/blob/main/docs/agentDocs/net/braveSearchAgent.md)
30
+
31
+ ### Input/Params example
32
+ - braveSearchAgent
33
+
34
+ ```typescript
35
+ {
36
+ "inputs": {
37
+ "query": "GraphAI framework"
38
+ },
39
+ "params": {}
40
+ }
41
+ ```
42
+
43
+
44
+ ```typescript
45
+ {
46
+ "inputs": {
47
+ "query": "GraphAI vs TensorFlow",
48
+ "search_args": {
49
+ "country": "JP",
50
+ "language": "ja"
51
+ }
52
+ },
53
+ "params": {}
54
+ }
55
+ ```
56
+
57
+
58
+ ```typescript
59
+ {
60
+ "inputs": {
61
+ "query": "GraphAI tutorials"
62
+ },
63
+ "params": {
64
+ "debug": true
65
+ }
66
+ }
67
+ ```
68
+
69
+
70
+ ### Environment Variables
71
+ - braveSearchAgent
72
+ - BRAVE_SEARCH_API_TOKEN
73
+
74
+
75
+
76
+
77
+
78
+
79
+
@@ -0,0 +1,28 @@
1
+ import { GraphAIOnError } from "@graphai/agent_utils";
2
+ import { AgentFunction, AgentFunctionInfo, DefaultConfigData } from "graphai";
3
+ interface BraveSearchInputs {
4
+ query: string;
5
+ search_args?: Record<string, any>;
6
+ }
7
+ interface BraveSearchParams {
8
+ apiKey?: string;
9
+ debug?: boolean;
10
+ throwError?: boolean;
11
+ search_args?: Record<string, any>;
12
+ }
13
+ interface BraveSearchResult {
14
+ title: string;
15
+ link: string;
16
+ snippet: string;
17
+ }
18
+ type BraveSearchResponse = {
19
+ items: BraveSearchResult[];
20
+ } | GraphAIOnError<string> | {
21
+ url: string;
22
+ method: string;
23
+ headers: Record<string, string>;
24
+ params: Record<string, any>;
25
+ };
26
+ export declare const braveSearchAgent: AgentFunction<BraveSearchParams, BraveSearchResponse, BraveSearchInputs, DefaultConfigData>;
27
+ declare const braveSearchAgentInfo: AgentFunctionInfo;
28
+ export default braveSearchAgentInfo;
@@ -0,0 +1,221 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.braveSearchAgent = void 0;
4
+ const graphai_1 = require("graphai");
5
+ const getBraveSearchToken = (params, config) => {
6
+ if (params?.apiKey) {
7
+ return params.apiKey;
8
+ }
9
+ if (config?.apiKey) {
10
+ return config.apiKey;
11
+ }
12
+ return typeof process !== "undefined" ? process?.env?.BRAVE_SEARCH_API_TOKEN : null;
13
+ };
14
+ const braveSearchAgent = async ({ namedInputs, params, config, }) => {
15
+ const { query, search_args } = namedInputs;
16
+ (0, graphai_1.assert)(!!query, "braveSearchAgent: query is required! set inputs: { query: 'your search query' }");
17
+ const throwError = params?.throwError ?? false;
18
+ const _search_args = search_args ?? params?.search_args ?? {};
19
+ const braveSearchToken = getBraveSearchToken(params, config);
20
+ // Check if API token is provided
21
+ if (!braveSearchToken) {
22
+ const errorMessage = "Brave Search API token is required. Please set the BRAVE_SEARCH_API_TOKEN environment variable.";
23
+ throw new Error(errorMessage);
24
+ }
25
+ const baseUrl = "https://api.search.brave.com/res/v1/web/search";
26
+ const searchParams = {
27
+ ..._search_args,
28
+ q: query,
29
+ };
30
+ // Return request information in debug mode
31
+ if (params?.debug) {
32
+ return {
33
+ url: baseUrl,
34
+ method: "GET",
35
+ headers: {
36
+ "X-Subscription-Token": braveSearchToken,
37
+ Accept: "application/json",
38
+ },
39
+ params: searchParams,
40
+ };
41
+ }
42
+ // Build URL with query parameters
43
+ const url = new URL(baseUrl);
44
+ Object.entries(searchParams).forEach(([key, value]) => {
45
+ url.searchParams.append(key, String(value));
46
+ });
47
+ const fetchOptions = {
48
+ method: "GET",
49
+ headers: new Headers({
50
+ "X-Subscription-Token": braveSearchToken,
51
+ Accept: "application/json",
52
+ }),
53
+ };
54
+ try {
55
+ const response = await fetch(url.toString(), fetchOptions);
56
+ if (!response.ok) {
57
+ const status = response.status;
58
+ const error = await response.text();
59
+ if (throwError) {
60
+ throw new Error(`Brave Search HTTP error: ${status}`);
61
+ }
62
+ return {
63
+ items: [],
64
+ onError: {
65
+ message: `Brave Search HTTP error: ${status}`,
66
+ status,
67
+ error,
68
+ },
69
+ };
70
+ }
71
+ const jsonResponse = await response.json();
72
+ const webResults = jsonResponse.web?.results || [];
73
+ const formattedResults = webResults.map((item) => ({
74
+ title: item.title,
75
+ link: item.url,
76
+ snippet: item.description,
77
+ }));
78
+ return {
79
+ items: formattedResults,
80
+ };
81
+ }
82
+ catch (error) {
83
+ if (throwError) {
84
+ throw error;
85
+ }
86
+ return {
87
+ onError: {
88
+ message: error instanceof Error ? error.message : "Unknown error occurred",
89
+ error: error instanceof Error ? error.toString() : String(error),
90
+ },
91
+ };
92
+ }
93
+ };
94
+ exports.braveSearchAgent = braveSearchAgent;
95
+ const braveSearchAgentInfo = {
96
+ name: "braveSearchAgent",
97
+ agent: exports.braveSearchAgent,
98
+ mock: exports.braveSearchAgent,
99
+ params: {
100
+ type: "object",
101
+ properties: {
102
+ apiKey: {
103
+ type: "string",
104
+ description: "Brave Search API key",
105
+ },
106
+ debug: {
107
+ type: "boolean",
108
+ description: "Enable debug mode",
109
+ },
110
+ throwError: {
111
+ type: "boolean",
112
+ description: "Throw error if the request fails",
113
+ },
114
+ search_args: {
115
+ type: "object",
116
+ description: "Additional search parameters to pass to the Brave Search API. See https://api-dashboard.search.brave.com/app/documentation/web-search/query",
117
+ },
118
+ },
119
+ },
120
+ inputs: {
121
+ type: "object",
122
+ properties: {
123
+ query: {
124
+ type: "string",
125
+ description: "The search query to send to Brave Search",
126
+ },
127
+ search_args: {
128
+ type: "object",
129
+ description: "Additional search parameters to pass to the Brave Search API. See https://api-dashboard.search.brave.com/app/documentation/web-search/query",
130
+ },
131
+ },
132
+ required: ["query"],
133
+ },
134
+ output: {
135
+ type: "object",
136
+ properties: {
137
+ items: {
138
+ type: "array",
139
+ items: {
140
+ type: "object",
141
+ properties: {
142
+ title: {
143
+ type: "string",
144
+ description: "The title of the search result",
145
+ },
146
+ link: {
147
+ type: "string",
148
+ description: "The URL of the search result",
149
+ },
150
+ snippet: {
151
+ type: "string",
152
+ description: "A snippet of text from the search result",
153
+ },
154
+ },
155
+ },
156
+ },
157
+ },
158
+ },
159
+ samples: [
160
+ {
161
+ inputs: {
162
+ query: "GraphAI framework",
163
+ },
164
+ params: {},
165
+ result: {
166
+ items: [
167
+ {
168
+ title: "GraphAI: A Modern AI Framework",
169
+ link: "https://example.com/graphai",
170
+ snippet: "GraphAI is a modern framework for building AI applications with a focus on graph-based architectures.",
171
+ },
172
+ {
173
+ title: "Getting Started with GraphAI",
174
+ link: "https://example.com/graphai/docs",
175
+ snippet: "Learn how to get started with GraphAI, the powerful framework for AI development.",
176
+ },
177
+ ],
178
+ },
179
+ },
180
+ {
181
+ inputs: {
182
+ query: "GraphAI vs TensorFlow",
183
+ search_args: {
184
+ country: "JP",
185
+ language: "ja",
186
+ },
187
+ },
188
+ params: {},
189
+ result: {
190
+ items: [
191
+ {
192
+ title: "GraphAIとは何か?",
193
+ link: "https://example.com/ja/graphai",
194
+ snippet: "GraphAIの概要を説明します。",
195
+ },
196
+ ],
197
+ },
198
+ },
199
+ {
200
+ inputs: {
201
+ query: "GraphAI tutorials",
202
+ },
203
+ params: {
204
+ debug: true,
205
+ },
206
+ result: {
207
+ url: "https://api.search.brave.com/res/v1/web/search",
208
+ method: "GET",
209
+ headers: { "X-Subscription-Token": "your-api-key", Accept: "application/json" },
210
+ params: { q: "GraphAI tutorials", extra_snippets: true },
211
+ },
212
+ },
213
+ ],
214
+ description: "An agent that uses the Brave Search API. https://api-dashboard.search.brave.com/app/documentation/web-search/get-started",
215
+ category: ["net"],
216
+ author: "kawamataryo",
217
+ repository: "https://github.com/receptron/graphai-agents",
218
+ license: "MIT",
219
+ environmentVariables: ["BRAVE_SEARCH_API_TOKEN"],
220
+ };
221
+ exports.default = braveSearchAgentInfo;
package/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import braveSearchAgent from "./brave_search_agent";
2
+ export { braveSearchAgent };
package/lib/index.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.braveSearchAgent = void 0;
7
+ const brave_search_agent_1 = __importDefault(require("./brave_search_agent"));
8
+ exports.braveSearchAgent = brave_search_agent_1.default;
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@graphai/brave_search_agent",
3
+ "version": "0.0.1",
4
+ "description": "An agent that uses the Brave Search API",
5
+ "main": "lib/index.js",
6
+ "files": [
7
+ "./lib"
8
+ ],
9
+ "scripts": {
10
+ "build": "tsc",
11
+ "eslint": "eslint src --fix",
12
+ "format": "prettier --write '{src,tests}/**/*.ts'",
13
+ "doc": "npx agentdoc",
14
+ "test_run": "node --test --require ts-node/register ./tests/run_*.ts",
15
+ "test": "node --test -r tsconfig-paths/register --require ts-node/register ./tests/test_*.ts",
16
+ "ci": "yarn run format && yarn run eslint && yarn run test && yarn run build",
17
+ "sample": "npx ts-node ./samples/search.ts"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/receptron/graphai-agents.git"
22
+ },
23
+ "author": "kawamataryo",
24
+ "license": "MIT",
25
+ "bugs": {
26
+ "url": "https://github.com/receptron/graphai-agents/issues"
27
+ },
28
+ "homepage": "https://github.com/receptron/graphai-agents/blob/main/net/brave_search_agent/README.md",
29
+ "devDependencies": {
30
+ "undici": "^6.21.1"
31
+ },
32
+ "types": "./lib/index.d.ts",
33
+ "directories": {
34
+ "lib": "lib"
35
+ }
36
+ }