@xyz17711/websearch 0.0.0-stage → 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/README.md +88 -2
- package/dist/index.d.ts +48 -0
- package/dist/index.js +19 -0
- package/dist/langchain.d.ts +31 -0
- package/dist/langchain.js +29 -0
- package/package.json +40 -4
- package/src/index.ts +67 -0
- package/src/langchain.ts +44 -0
- package/test/index.test.mjs +58 -0
- package/test/langchain.test.mjs +32 -0
- package/tsconfig.json +16 -0
package/README.md
CHANGED
|
@@ -1,3 +1,89 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @xyz17711/websearch
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/17711-services/clients/actions/workflows/publish-node.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/@xyz17711/websearch)
|
|
5
|
+
[](https://github.com/17711-services/clients/blob/main/LICENSE)
|
|
6
|
+
|
|
7
|
+
Typed TypeScript client for **websearch**, part of the
|
|
8
|
+
[17711.xyz](https://api.17711.xyz) microservice fleet -- pay-per-call APIs settled in USDC via
|
|
9
|
+
[x402](https://x402.org), listed on [x402scan](https://www.x402scan.com/server/f1c7b972-3405-4b75-8b4f-d1bfbd6305ff).
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @xyz17711/websearch
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Methods
|
|
18
|
+
|
|
19
|
+
| Method | Description |
|
|
20
|
+
| --- | --- |
|
|
21
|
+
| `getTaskStatus` | Polls for the result of a task enqueued by a *_async tool |
|
|
22
|
+
| `searchWeb` | Searches the web, always through a free public proxy sourced from proxy-pool ($0.003, x402) |
|
|
23
|
+
| `searchWebAsync` | Enqueues a websearch_search_web job and returns a task id immediately ($0.003, x402) |
|
|
24
|
+
|
|
25
|
+
## Quickstart
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { Client } from "@xyz17711/websearch";
|
|
29
|
+
|
|
30
|
+
const client = new Client("https://api.17711.xyz/registry/websearch");
|
|
31
|
+
// see the Methods table above for every available call and its request/response type
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Paying past the free quota
|
|
35
|
+
|
|
36
|
+
Every gated tool allows a small number of free calls per day; beyond that it returns a 402
|
|
37
|
+
challenge. Without a wallet configured, this client throws `PaymentRequiredError` with the price,
|
|
38
|
+
network, and payee address attached:
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { Client, PaymentRequiredError } from "@xyz17711/websearch";
|
|
42
|
+
|
|
43
|
+
const client = new Client("https://api.17711.xyz/registry/websearch");
|
|
44
|
+
try {
|
|
45
|
+
await client.getTaskStatus(/* ... */);
|
|
46
|
+
} catch (e) {
|
|
47
|
+
if (e instanceof PaymentRequiredError) {
|
|
48
|
+
console.log(`needs ${e.priceUsdc} USDC on ${e.network}, payable to ${e.payTo}`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
To pay automatically, configure a wallet on the client -- the private key should come from your
|
|
54
|
+
own environment/secrets manager, never a literal string in source code:
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import { Client } from "@xyz17711/websearch";
|
|
58
|
+
|
|
59
|
+
const client = new Client("https://api.17711.xyz/registry/websearch", {
|
|
60
|
+
x402PrivateKey: process.env.X402_PRIVATE_KEY,
|
|
61
|
+
x402ExpectedNetwork: "eip155:8453", // Base mainnet; use eip155:84532 for Base Sepolia testnet
|
|
62
|
+
x402MaxPaymentUsdc: 0.01, // refuse any single call above this
|
|
63
|
+
x402MaxTotalSpendUsdc: 1.0, // refuse once this WALLET's lifetime spend would exceed it
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
**Never commit a private key.** Both spend caps are required together with the key -- a wallet
|
|
68
|
+
with real funds and no explicit caps refuses to construct at all (`WalletConfigError`), on
|
|
69
|
+
purpose. The cumulative cap is tracked per wallet address, shared by every `Client` you construct
|
|
70
|
+
with that same key in this process.
|
|
71
|
+
|
|
72
|
+
## LangChain.js
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { Client } from "@xyz17711/websearch";
|
|
76
|
+
import { WebsearchToolkit } from "@xyz17711/websearch/langchain";
|
|
77
|
+
|
|
78
|
+
const client = new Client("https://api.17711.xyz/registry/websearch", { /* ...wallet options... */ });
|
|
79
|
+
const tools = new WebsearchToolkit(client).getTools();
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Requires the optional `@langchain/core`+`zod` peer dependencies (`npm install @langchain/core
|
|
83
|
+
zod`) -- importing the base package above never requires them; only `@xyz17711/websearch/langchain`
|
|
84
|
+
does.
|
|
85
|
+
|
|
86
|
+
## License
|
|
87
|
+
|
|
88
|
+
MIT -- see [`LICENSE`](../../LICENSE), shared by every language's client library in the
|
|
89
|
+
`clients` repo this package lives in.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed HTTP client and request/response interfaces for websearch.
|
|
3
|
+
*/
|
|
4
|
+
import { Client as BaseClient, type ClientOptions } from "@xyz17711/core";
|
|
5
|
+
/** `"pending"` (enqueued, no result yet), `"finished"` (a worker wrote a result -- see `ok`/ `result`), or `"unknown"` (this id was never valid, or both it and its result have expired -- see [`nimpoleon_tasks::TaskState`]'s own doc comment for why this is 3 states, not 2). */
|
|
6
|
+
export interface TaskStatusKind {
|
|
7
|
+
}
|
|
8
|
+
export interface GetTaskStatusResponse {
|
|
9
|
+
ok?: boolean | null;
|
|
10
|
+
result?: unknown;
|
|
11
|
+
status: TaskStatusKind;
|
|
12
|
+
}
|
|
13
|
+
export interface GetTaskStatusRequest {
|
|
14
|
+
/** The `task_id` a `*_async` tool returned. */
|
|
15
|
+
task_id: string;
|
|
16
|
+
}
|
|
17
|
+
export interface SearchResultItem {
|
|
18
|
+
snippet: string;
|
|
19
|
+
title: string;
|
|
20
|
+
url: string;
|
|
21
|
+
}
|
|
22
|
+
export interface SearchWebResponse {
|
|
23
|
+
results: SearchResultItem[];
|
|
24
|
+
}
|
|
25
|
+
export interface SearchWebRequest {
|
|
26
|
+
/** The search query text, e.g. `"rust async runtime comparison"`. */
|
|
27
|
+
query: string;
|
|
28
|
+
}
|
|
29
|
+
/** The fixed response shape of every `*_async` tool -- deliberately NOT generic over the underlying sync tool's own response type, since no work has actually happened yet at this point. */
|
|
30
|
+
export interface AsyncTaskResponse {
|
|
31
|
+
task_id: string;
|
|
32
|
+
}
|
|
33
|
+
export interface SearchWebAsyncRequest {
|
|
34
|
+
/** When set, the worker that processes this task POSTs the eventual result (a `TaskResult` JSON body) here, best-effort -- polling `websearch_get_task_status` always works too, regardless of whether this is set. */
|
|
35
|
+
callback_url?: string | null;
|
|
36
|
+
/** The search query text, e.g. `"rust async runtime comparison"`. */
|
|
37
|
+
query: string;
|
|
38
|
+
}
|
|
39
|
+
/** Typed client for websearch, e.g. `client.getTaskStatus(...)`. */
|
|
40
|
+
export declare class Client extends BaseClient {
|
|
41
|
+
/** Polls for the result of a task enqueued by a *_async tool */
|
|
42
|
+
getTaskStatus(request: GetTaskStatusRequest): Promise<GetTaskStatusResponse>;
|
|
43
|
+
/** Searches the web, always through a free public proxy sourced from proxy-pool ($0.003, x402) */
|
|
44
|
+
searchWeb(request: SearchWebRequest): Promise<SearchWebResponse>;
|
|
45
|
+
/** Enqueues a websearch_search_web job and returns a task id immediately ($0.003, x402) */
|
|
46
|
+
searchWebAsync(request: SearchWebAsyncRequest): Promise<AsyncTaskResponse>;
|
|
47
|
+
}
|
|
48
|
+
export { ClientOptions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed HTTP client and request/response interfaces for websearch.
|
|
3
|
+
*/
|
|
4
|
+
import { Client as BaseClient } from "@xyz17711/core";
|
|
5
|
+
/** Typed client for websearch, e.g. `client.getTaskStatus(...)`. */
|
|
6
|
+
export class Client extends BaseClient {
|
|
7
|
+
/** Polls for the result of a task enqueued by a *_async tool */
|
|
8
|
+
async getTaskStatus(request) {
|
|
9
|
+
return this.post("/v1/websearch/get_task_status", request);
|
|
10
|
+
}
|
|
11
|
+
/** Searches the web, always through a free public proxy sourced from proxy-pool ($0.003, x402) */
|
|
12
|
+
async searchWeb(request) {
|
|
13
|
+
return this.post("/v1/websearch/search_web", request);
|
|
14
|
+
}
|
|
15
|
+
/** Enqueues a websearch_search_web job and returns a task id immediately ($0.003, x402) */
|
|
16
|
+
async searchWebAsync(request) {
|
|
17
|
+
return this.post("/v1/websearch/search_web_async", request);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LangChain.js tools for websearch, grouped in one Toolkit -- one tool per operation, each just an async function (JS has no separate sync form to generate). Requires the `@langchain/core`+`zod` peer dependencies declared in this package's own package.json -- imported from `websearch/langchain`, never from the package root, so importing the base client never requires them installed.
|
|
3
|
+
*/
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { Client } from "./index.js";
|
|
6
|
+
export declare class WebsearchToolkit {
|
|
7
|
+
private client;
|
|
8
|
+
constructor(client: Client);
|
|
9
|
+
getTools(): (import("@langchain/core/tools").DynamicStructuredTool<z.ZodObject<{
|
|
10
|
+
task_id: z.ZodString;
|
|
11
|
+
}, z.core.$strip>, {
|
|
12
|
+
task_id: string;
|
|
13
|
+
}, {
|
|
14
|
+
task_id: string;
|
|
15
|
+
}, import("./index.js").GetTaskStatusResponse, unknown, "getTaskStatus"> | import("@langchain/core/tools").DynamicStructuredTool<z.ZodObject<{
|
|
16
|
+
query: z.ZodString;
|
|
17
|
+
}, z.core.$strip>, {
|
|
18
|
+
query: string;
|
|
19
|
+
}, {
|
|
20
|
+
query: string;
|
|
21
|
+
}, import("./index.js").SearchWebResponse, unknown, "searchWeb"> | import("@langchain/core/tools").DynamicStructuredTool<z.ZodObject<{
|
|
22
|
+
callback_url: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
23
|
+
query: z.ZodString;
|
|
24
|
+
}, z.core.$strip>, {
|
|
25
|
+
query: string;
|
|
26
|
+
callback_url?: string | null | undefined;
|
|
27
|
+
}, {
|
|
28
|
+
query: string;
|
|
29
|
+
callback_url?: string | null | undefined;
|
|
30
|
+
}, import("./index.js").AsyncTaskResponse, unknown, "searchWebAsync">)[];
|
|
31
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LangChain.js tools for websearch, grouped in one Toolkit -- one tool per operation, each just an async function (JS has no separate sync form to generate). Requires the `@langchain/core`+`zod` peer dependencies declared in this package's own package.json -- imported from `websearch/langchain`, never from the package root, so importing the base client never requires them installed.
|
|
3
|
+
*/
|
|
4
|
+
import { tool } from "@langchain/core/tools";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
export class WebsearchToolkit {
|
|
7
|
+
constructor(client) {
|
|
8
|
+
this.client = client;
|
|
9
|
+
}
|
|
10
|
+
getTools() {
|
|
11
|
+
return [
|
|
12
|
+
tool(async (input) => this.client.getTaskStatus(input), {
|
|
13
|
+
name: "getTaskStatus",
|
|
14
|
+
description: "Polls for the result of a task enqueued by a *_async tool",
|
|
15
|
+
schema: z.object({ task_id: z.string() }),
|
|
16
|
+
}),
|
|
17
|
+
tool(async (input) => this.client.searchWeb(input), {
|
|
18
|
+
name: "searchWeb",
|
|
19
|
+
description: "Searches the web, always through a free public proxy sourced from proxy-pool ($0.003, x402)",
|
|
20
|
+
schema: z.object({ query: z.string() }),
|
|
21
|
+
}),
|
|
22
|
+
tool(async (input) => this.client.searchWebAsync(input), {
|
|
23
|
+
name: "searchWebAsync",
|
|
24
|
+
description: "Enqueues a websearch_search_web job and returns a task id immediately ($0.003, x402)",
|
|
25
|
+
schema: z.object({ callback_url: z.string().nullable().optional(), query: z.string() }),
|
|
26
|
+
}),
|
|
27
|
+
];
|
|
28
|
+
}
|
|
29
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xyz17711/websearch",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Generated typed client for the websearch 17711.xyz microservice.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./langchain": {
|
|
14
|
+
"types": "./dist/langchain.d.ts",
|
|
15
|
+
"default": "./dist/langchain.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc -p .",
|
|
20
|
+
"test": "npm run build && node --test test/*.test.mjs"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@xyz17711/core": "^0.1.0"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@langchain/core": ">=0.3",
|
|
27
|
+
"zod": "^3 || ^4"
|
|
28
|
+
},
|
|
29
|
+
"peerDependenciesMeta": {
|
|
30
|
+
"@langchain/core": {
|
|
31
|
+
"optional": true
|
|
32
|
+
},
|
|
33
|
+
"zod": {
|
|
34
|
+
"optional": true
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"typescript": "^5",
|
|
39
|
+
"@langchain/core": "^1.2",
|
|
40
|
+
"zod": "^4"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed HTTP client and request/response interfaces for websearch.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Client as BaseClient, type ClientOptions } from "@xyz17711/core";
|
|
6
|
+
|
|
7
|
+
/** `"pending"` (enqueued, no result yet), `"finished"` (a worker wrote a result -- see `ok`/ `result`), or `"unknown"` (this id was never valid, or both it and its result have expired -- see [`nimpoleon_tasks::TaskState`]'s own doc comment for why this is 3 states, not 2). */
|
|
8
|
+
export interface TaskStatusKind {
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface GetTaskStatusResponse {
|
|
12
|
+
ok?: boolean | null;
|
|
13
|
+
result?: unknown;
|
|
14
|
+
status: TaskStatusKind;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface GetTaskStatusRequest {
|
|
18
|
+
/** The `task_id` a `*_async` tool returned. */
|
|
19
|
+
task_id: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface SearchResultItem {
|
|
23
|
+
snippet: string;
|
|
24
|
+
title: string;
|
|
25
|
+
url: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface SearchWebResponse {
|
|
29
|
+
results: SearchResultItem[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface SearchWebRequest {
|
|
33
|
+
/** The search query text, e.g. `"rust async runtime comparison"`. */
|
|
34
|
+
query: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The fixed response shape of every `*_async` tool -- deliberately NOT generic over the underlying sync tool's own response type, since no work has actually happened yet at this point. */
|
|
38
|
+
export interface AsyncTaskResponse {
|
|
39
|
+
task_id: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface SearchWebAsyncRequest {
|
|
43
|
+
/** When set, the worker that processes this task POSTs the eventual result (a `TaskResult` JSON body) here, best-effort -- polling `websearch_get_task_status` always works too, regardless of whether this is set. */
|
|
44
|
+
callback_url?: string | null;
|
|
45
|
+
/** The search query text, e.g. `"rust async runtime comparison"`. */
|
|
46
|
+
query: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Typed client for websearch, e.g. `client.getTaskStatus(...)`. */
|
|
50
|
+
export class Client extends BaseClient {
|
|
51
|
+
/** Polls for the result of a task enqueued by a *_async tool */
|
|
52
|
+
async getTaskStatus(request: GetTaskStatusRequest): Promise<GetTaskStatusResponse> {
|
|
53
|
+
return this.post<GetTaskStatusResponse>("/v1/websearch/get_task_status", request);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Searches the web, always through a free public proxy sourced from proxy-pool ($0.003, x402) */
|
|
57
|
+
async searchWeb(request: SearchWebRequest): Promise<SearchWebResponse> {
|
|
58
|
+
return this.post<SearchWebResponse>("/v1/websearch/search_web", request);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Enqueues a websearch_search_web job and returns a task id immediately ($0.003, x402) */
|
|
62
|
+
async searchWebAsync(request: SearchWebAsyncRequest): Promise<AsyncTaskResponse> {
|
|
63
|
+
return this.post<AsyncTaskResponse>("/v1/websearch/search_web_async", request);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { ClientOptions };
|
package/src/langchain.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LangChain.js tools for websearch, grouped in one Toolkit -- one tool per operation, each just an async function (JS has no separate sync form to generate). Requires the `@langchain/core`+`zod` peer dependencies declared in this package's own package.json -- imported from `websearch/langchain`, never from the package root, so importing the base client never requires them installed.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { tool } from "@langchain/core/tools";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { Client } from "./index.js";
|
|
8
|
+
|
|
9
|
+
export class WebsearchToolkit {
|
|
10
|
+
private client: Client;
|
|
11
|
+
|
|
12
|
+
constructor(client: Client) {
|
|
13
|
+
this.client = client;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
getTools() {
|
|
17
|
+
return [
|
|
18
|
+
tool(
|
|
19
|
+
async (input) => this.client.getTaskStatus(input),
|
|
20
|
+
{
|
|
21
|
+
name: "getTaskStatus",
|
|
22
|
+
description: "Polls for the result of a task enqueued by a *_async tool",
|
|
23
|
+
schema: z.object({ task_id: z.string() }),
|
|
24
|
+
},
|
|
25
|
+
),
|
|
26
|
+
tool(
|
|
27
|
+
async (input) => this.client.searchWeb(input),
|
|
28
|
+
{
|
|
29
|
+
name: "searchWeb",
|
|
30
|
+
description: "Searches the web, always through a free public proxy sourced from proxy-pool ($0.003, x402)",
|
|
31
|
+
schema: z.object({ query: z.string() }),
|
|
32
|
+
},
|
|
33
|
+
),
|
|
34
|
+
tool(
|
|
35
|
+
async (input) => this.client.searchWebAsync(input),
|
|
36
|
+
{
|
|
37
|
+
name: "searchWebAsync",
|
|
38
|
+
description: "Enqueues a websearch_search_web job and returns a task id immediately ($0.003, x402)",
|
|
39
|
+
schema: z.object({ callback_url: z.string().nullable().optional(), query: z.string() }),
|
|
40
|
+
},
|
|
41
|
+
),
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Verifies every websearch client method sends a request to the expected path/HTTP verb and parses a well-formed response -- one test per operation, real fetch mocked via a plain reassignment of `globalThis.fetch` (no extra test/mocking dependency). Runs against the COMPILED package (`../dist/index.js`) -- build before testing.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { test } from "node:test";
|
|
6
|
+
import assert from "node:assert/strict";
|
|
7
|
+
import { Client } from "../dist/index.js";
|
|
8
|
+
|
|
9
|
+
test("getTaskStatus calls the expected path", async () => {
|
|
10
|
+
const originalFetch = globalThis.fetch;
|
|
11
|
+
globalThis.fetch = async (url, init = {}) => {
|
|
12
|
+
const parsed = new URL(url);
|
|
13
|
+
assert.equal((init.method ?? "GET").toUpperCase(), "POST");
|
|
14
|
+
assert.equal(parsed.pathname, "/v1/websearch/get_task_status");
|
|
15
|
+
return new Response(JSON.stringify({ ok: null, result: null, status: { } }), { status: 200 });
|
|
16
|
+
};
|
|
17
|
+
try {
|
|
18
|
+
const client = new Client("https://example.test");
|
|
19
|
+
const result = await client.getTaskStatus({ task_id: "test" });
|
|
20
|
+
assert.ok(result !== undefined);
|
|
21
|
+
} finally {
|
|
22
|
+
globalThis.fetch = originalFetch;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("searchWeb calls the expected path", async () => {
|
|
27
|
+
const originalFetch = globalThis.fetch;
|
|
28
|
+
globalThis.fetch = async (url, init = {}) => {
|
|
29
|
+
const parsed = new URL(url);
|
|
30
|
+
assert.equal((init.method ?? "GET").toUpperCase(), "POST");
|
|
31
|
+
assert.equal(parsed.pathname, "/v1/websearch/search_web");
|
|
32
|
+
return new Response(JSON.stringify({ results: [] }), { status: 200 });
|
|
33
|
+
};
|
|
34
|
+
try {
|
|
35
|
+
const client = new Client("https://example.test");
|
|
36
|
+
const result = await client.searchWeb({ query: "test" });
|
|
37
|
+
assert.ok(result !== undefined);
|
|
38
|
+
} finally {
|
|
39
|
+
globalThis.fetch = originalFetch;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("searchWebAsync calls the expected path", async () => {
|
|
44
|
+
const originalFetch = globalThis.fetch;
|
|
45
|
+
globalThis.fetch = async (url, init = {}) => {
|
|
46
|
+
const parsed = new URL(url);
|
|
47
|
+
assert.equal((init.method ?? "GET").toUpperCase(), "POST");
|
|
48
|
+
assert.equal(parsed.pathname, "/v1/websearch/search_web_async");
|
|
49
|
+
return new Response(JSON.stringify({ task_id: "test" }), { status: 200 });
|
|
50
|
+
};
|
|
51
|
+
try {
|
|
52
|
+
const client = new Client("https://example.test");
|
|
53
|
+
const result = await client.searchWebAsync({ callback_url: null, query: "test" });
|
|
54
|
+
assert.ok(result !== undefined);
|
|
55
|
+
} finally {
|
|
56
|
+
globalThis.fetch = originalFetch;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Verifies every websearch LangChain tool through the real StructuredTool (`.invoke()`), not just the underlying client method -- catches schema-shape bugs a plain client-method test would miss. Requires the `langchain`/`zod` dev dependencies.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { test } from "node:test";
|
|
6
|
+
import assert from "node:assert/strict";
|
|
7
|
+
import { Client } from "../dist/index.js";
|
|
8
|
+
import { WebsearchToolkit } from "../dist/langchain.js";
|
|
9
|
+
|
|
10
|
+
test("getTaskStatus tool invoke", async () => {
|
|
11
|
+
globalThis.fetch = async () => new Response(JSON.stringify({ ok: null, result: null, status: { } }), { status: 200 });
|
|
12
|
+
const toolkit = new WebsearchToolkit(new Client("https://example.test"));
|
|
13
|
+
const t = toolkit.getTools().find((x) => x.name === "getTaskStatus");
|
|
14
|
+
const result = await t.invoke({ task_id: "test" });
|
|
15
|
+
assert.ok(result !== undefined);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("searchWeb tool invoke", async () => {
|
|
19
|
+
globalThis.fetch = async () => new Response(JSON.stringify({ results: [] }), { status: 200 });
|
|
20
|
+
const toolkit = new WebsearchToolkit(new Client("https://example.test"));
|
|
21
|
+
const t = toolkit.getTools().find((x) => x.name === "searchWeb");
|
|
22
|
+
const result = await t.invoke({ query: "test" });
|
|
23
|
+
assert.ok(result !== undefined);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("searchWebAsync tool invoke", async () => {
|
|
27
|
+
globalThis.fetch = async () => new Response(JSON.stringify({ task_id: "test" }), { status: 200 });
|
|
28
|
+
const toolkit = new WebsearchToolkit(new Client("https://example.test"));
|
|
29
|
+
const t = toolkit.getTools().find((x) => x.name === "searchWebAsync");
|
|
30
|
+
const result = await t.invoke({ callback_url: "test", query: "test" });
|
|
31
|
+
assert.ok(result !== undefined);
|
|
32
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "ES2020",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"outDir": "dist",
|
|
8
|
+
"rootDir": "src",
|
|
9
|
+
"strict": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"skipLibCheck": true
|
|
12
|
+
},
|
|
13
|
+
"include": [
|
|
14
|
+
"src"
|
|
15
|
+
]
|
|
16
|
+
}
|