test-wuying-agentbay-sdk 0.13.0-beta.20251212114304 → 0.13.0-beta.20251212140302
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/dist/index.cjs +5418 -150
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +20 -17
- package/dist/index.d.ts +20 -17
- package/dist/index.mjs +5328 -60
- package/dist/index.mjs.map +1 -1
- package/docs/api/browser-use/browser-agent.md +77 -22
- package/docs/examples/browser-use/browser/basic-usage.ts +31 -24
- package/docs/examples/browser-use/browser/run-2048.ts +47 -37
- package/docs/examples/browser-use/browser/run-sudoku.ts +55 -36
- package/package.json +4 -2
|
@@ -7,16 +7,19 @@
|
|
|
7
7
|
|
|
8
8
|
- [act](#act)
|
|
9
9
|
- [actAsync](#actasync)
|
|
10
|
+
- [close](#close)
|
|
10
11
|
- [extract](#extract)
|
|
11
12
|
- [extractAsync](#extractasync)
|
|
13
|
+
- [navigate](#navigate)
|
|
12
14
|
- [observe](#observe)
|
|
13
15
|
- [observeAsync](#observeasync)
|
|
16
|
+
- [screenshot](#screenshot)
|
|
14
17
|
|
|
15
18
|
## Methods
|
|
16
19
|
|
|
17
20
|
### act
|
|
18
21
|
|
|
19
|
-
▸ **act**(`options`, `page
|
|
22
|
+
▸ **act**(`options`, `page?`): `Promise`\<``ActResult``\>
|
|
20
23
|
|
|
21
24
|
------------------ ACT ------------------ *
|
|
22
25
|
|
|
@@ -25,7 +28,7 @@
|
|
|
25
28
|
| Name | Type |
|
|
26
29
|
| :------ | :------ |
|
|
27
30
|
| `options` | ``ActOptions`` |
|
|
28
|
-
| `page
|
|
31
|
+
| `page?` | `any` |
|
|
29
32
|
|
|
30
33
|
#### Returns
|
|
31
34
|
|
|
@@ -35,14 +38,14 @@ ___
|
|
|
35
38
|
|
|
36
39
|
### actAsync
|
|
37
40
|
|
|
38
|
-
▸ **actAsync**(`options`, `page
|
|
41
|
+
▸ **actAsync**(`options`, `page?`): `Promise`\<``ActResult``\>
|
|
39
42
|
|
|
40
43
|
#### Parameters
|
|
41
44
|
|
|
42
45
|
| Name | Type |
|
|
43
46
|
| :------ | :------ |
|
|
44
47
|
| `options` | ``ActOptions`` |
|
|
45
|
-
| `page
|
|
48
|
+
| `page?` | `any` |
|
|
46
49
|
|
|
47
50
|
#### Returns
|
|
48
51
|
|
|
@@ -50,57 +53,87 @@ ___
|
|
|
50
53
|
|
|
51
54
|
___
|
|
52
55
|
|
|
56
|
+
### close
|
|
57
|
+
|
|
58
|
+
▸ **close**(): `Promise`\<`boolean`\>
|
|
59
|
+
|
|
60
|
+
------------------ CLOSE ------------------ *
|
|
61
|
+
|
|
62
|
+
#### Returns
|
|
63
|
+
|
|
64
|
+
`Promise`\<`boolean`\>
|
|
65
|
+
|
|
66
|
+
___
|
|
67
|
+
|
|
53
68
|
### extract
|
|
54
69
|
|
|
55
|
-
▸ **extract**\<`
|
|
70
|
+
▸ **extract**\<`TSchema`\>(`options`, `page?`): `Promise`\<[`boolean`, ``null`` \| `TypeOf`\<`TSchema`\>]\>
|
|
56
71
|
|
|
57
72
|
------------------ EXTRACT ------------------ *
|
|
58
73
|
|
|
59
74
|
#### Type parameters
|
|
60
75
|
|
|
61
|
-
| Name |
|
|
62
|
-
| :------ |
|
|
63
|
-
| `
|
|
76
|
+
| Name | Type |
|
|
77
|
+
| :------ | :------ |
|
|
78
|
+
| `TSchema` | extends `ZodType`\<`any`, `any`, `any`, `TSchema`\> |
|
|
64
79
|
|
|
65
80
|
#### Parameters
|
|
66
81
|
|
|
67
82
|
| Name | Type |
|
|
68
83
|
| :------ | :------ |
|
|
69
|
-
| `options` | ``ExtractOptions``\<`
|
|
70
|
-
| `page
|
|
84
|
+
| `options` | ``ExtractOptions``\<`TSchema`\> |
|
|
85
|
+
| `page?` | `any` |
|
|
71
86
|
|
|
72
87
|
#### Returns
|
|
73
88
|
|
|
74
|
-
`Promise`\<[`boolean`, ``null`` \| `
|
|
89
|
+
`Promise`\<[`boolean`, ``null`` \| `TypeOf`\<`TSchema`\>]\>
|
|
75
90
|
|
|
76
91
|
___
|
|
77
92
|
|
|
78
93
|
### extractAsync
|
|
79
94
|
|
|
80
|
-
▸ **extractAsync**\<`
|
|
95
|
+
▸ **extractAsync**\<`TSchema`\>(`options`, `page?`): `Promise`\<[`boolean`, ``null`` \| `TypeOf`\<`TSchema`\>]\>
|
|
81
96
|
|
|
82
97
|
#### Type parameters
|
|
83
98
|
|
|
84
|
-
| Name |
|
|
85
|
-
| :------ |
|
|
86
|
-
| `
|
|
99
|
+
| Name | Type |
|
|
100
|
+
| :------ | :------ |
|
|
101
|
+
| `TSchema` | extends `ZodType`\<`any`, `any`, `any`, `TSchema`\> |
|
|
102
|
+
|
|
103
|
+
#### Parameters
|
|
104
|
+
|
|
105
|
+
| Name | Type |
|
|
106
|
+
| :------ | :------ |
|
|
107
|
+
| `options` | ``ExtractOptions``\<`TSchema`\> |
|
|
108
|
+
| `page?` | `any` |
|
|
109
|
+
|
|
110
|
+
#### Returns
|
|
111
|
+
|
|
112
|
+
`Promise`\<[`boolean`, ``null`` \| `TypeOf`\<`TSchema`\>]\>
|
|
113
|
+
|
|
114
|
+
___
|
|
115
|
+
|
|
116
|
+
### navigate
|
|
117
|
+
|
|
118
|
+
▸ **navigate**(`url`): `Promise`\<`string`\>
|
|
119
|
+
|
|
120
|
+
------------------ NAVIGATE ------------------ *
|
|
87
121
|
|
|
88
122
|
#### Parameters
|
|
89
123
|
|
|
90
124
|
| Name | Type |
|
|
91
125
|
| :------ | :------ |
|
|
92
|
-
| `
|
|
93
|
-
| `page` | `any` |
|
|
126
|
+
| `url` | `string` |
|
|
94
127
|
|
|
95
128
|
#### Returns
|
|
96
129
|
|
|
97
|
-
`Promise
|
|
130
|
+
`Promise`\<`string`\>
|
|
98
131
|
|
|
99
132
|
___
|
|
100
133
|
|
|
101
134
|
### observe
|
|
102
135
|
|
|
103
|
-
▸ **observe**(`options`, `page
|
|
136
|
+
▸ **observe**(`options`, `page?`): `Promise`\<``boolean`, [`ObserveResult``[]]\>
|
|
104
137
|
|
|
105
138
|
------------------ OBSERVE ------------------ *
|
|
106
139
|
|
|
@@ -109,7 +142,7 @@ ___
|
|
|
109
142
|
| Name | Type |
|
|
110
143
|
| :------ | :------ |
|
|
111
144
|
| `options` | ``ObserveOptions`` |
|
|
112
|
-
| `page
|
|
145
|
+
| `page?` | `any` |
|
|
113
146
|
|
|
114
147
|
#### Returns
|
|
115
148
|
|
|
@@ -119,15 +152,37 @@ ___
|
|
|
119
152
|
|
|
120
153
|
### observeAsync
|
|
121
154
|
|
|
122
|
-
▸ **observeAsync**(`options`, `page
|
|
155
|
+
▸ **observeAsync**(`options`, `page?`): `Promise`\<``boolean`, [`ObserveResult``[]]\>
|
|
123
156
|
|
|
124
157
|
#### Parameters
|
|
125
158
|
|
|
126
159
|
| Name | Type |
|
|
127
160
|
| :------ | :------ |
|
|
128
161
|
| `options` | ``ObserveOptions`` |
|
|
129
|
-
| `page
|
|
162
|
+
| `page?` | `any` |
|
|
130
163
|
|
|
131
164
|
#### Returns
|
|
132
165
|
|
|
133
166
|
`Promise`\<``boolean`, [`ObserveResult``[]]\>
|
|
167
|
+
|
|
168
|
+
___
|
|
169
|
+
|
|
170
|
+
### screenshot
|
|
171
|
+
|
|
172
|
+
▸ **screenshot**(`page?`, `full_page?`, `quality?`, `clip?`, `timeout?`): `Promise`\<`string`\>
|
|
173
|
+
|
|
174
|
+
------------------ SCREENSHOT ------------------ *
|
|
175
|
+
|
|
176
|
+
#### Parameters
|
|
177
|
+
|
|
178
|
+
| Name | Type | Default value |
|
|
179
|
+
| :------ | :------ | :------ |
|
|
180
|
+
| `page` | `any` | `null` |
|
|
181
|
+
| `full_page` | `boolean` | `true` |
|
|
182
|
+
| `quality` | `number` | `80` |
|
|
183
|
+
| `clip?` | `Record`\<`string`, `number`\> | `undefined` |
|
|
184
|
+
| `timeout?` | `number` | `undefined` |
|
|
185
|
+
|
|
186
|
+
#### Returns
|
|
187
|
+
|
|
188
|
+
`Promise`\<`string`\>
|
|
@@ -3,14 +3,19 @@
|
|
|
3
3
|
* This example demonstrates the core browser operations without external dependencies.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
6
|
+
import {
|
|
7
|
+
AgentBay,
|
|
8
|
+
CreateSessionParams,
|
|
9
|
+
ActOptions,
|
|
10
|
+
ObserveOptions,
|
|
11
|
+
ExtractOptions,
|
|
12
|
+
} from "wuying-agentbay-sdk";
|
|
13
|
+
import { z } from "zod";
|
|
14
|
+
|
|
15
|
+
const PageInfoSchema = z.object({
|
|
16
|
+
title: z.string().describe("page title"),
|
|
17
|
+
url: z.string().describe("page url"),
|
|
18
|
+
});
|
|
14
19
|
async function main() {
|
|
15
20
|
// Get API key from environment variable
|
|
16
21
|
const apiKey = process.env.AGENTBAY_API_KEY;
|
|
@@ -57,7 +62,7 @@ async function main() {
|
|
|
57
62
|
// Example: Mock page object (in real usage, you'd get this from Playwright)
|
|
58
63
|
const mockPage = {
|
|
59
64
|
url: () => "https://example.com",
|
|
60
|
-
title: () => "Example Domain"
|
|
65
|
+
title: () => "Example Domain",
|
|
61
66
|
};
|
|
62
67
|
|
|
63
68
|
// Example 1: Perform an action
|
|
@@ -65,7 +70,7 @@ async function main() {
|
|
|
65
70
|
try {
|
|
66
71
|
const actOptions: ActOptions = {
|
|
67
72
|
action: "Click the 'More information...' link",
|
|
68
|
-
|
|
73
|
+
timeout: 5,
|
|
69
74
|
};
|
|
70
75
|
|
|
71
76
|
const actResult = await session.browser.agent.act(actOptions, mockPage);
|
|
@@ -81,10 +86,11 @@ async function main() {
|
|
|
81
86
|
console.log("\n--- Example 2: Observing page elements ---");
|
|
82
87
|
try {
|
|
83
88
|
const observeOptions: ObserveOptions = {
|
|
84
|
-
instruction: "Find all links and buttons on the page"
|
|
89
|
+
instruction: "Find all links and buttons on the page",
|
|
85
90
|
};
|
|
86
91
|
|
|
87
|
-
const [observeSuccess, observations] =
|
|
92
|
+
const [observeSuccess, observations] =
|
|
93
|
+
await session.browser.agent.observe(observeOptions, mockPage);
|
|
88
94
|
console.log("Observe success:", observeSuccess);
|
|
89
95
|
console.log("Number of observations:", observations.length);
|
|
90
96
|
|
|
@@ -92,7 +98,7 @@ async function main() {
|
|
|
92
98
|
console.log(`Observation ${index + 1}:`, {
|
|
93
99
|
selector: obs.selector,
|
|
94
100
|
description: obs.description,
|
|
95
|
-
method: obs.method
|
|
101
|
+
method: obs.method,
|
|
96
102
|
});
|
|
97
103
|
});
|
|
98
104
|
} catch (error) {
|
|
@@ -102,22 +108,24 @@ async function main() {
|
|
|
102
108
|
// Example 3: Extract structured data from the page
|
|
103
109
|
console.log("\n--- Example 3: Extracting structured data ---");
|
|
104
110
|
try {
|
|
105
|
-
const extractOptions: ExtractOptions<
|
|
111
|
+
const extractOptions: ExtractOptions<typeof PageInfoSchema> = {
|
|
106
112
|
instruction: "Extract the page title and URL",
|
|
107
|
-
schema:
|
|
108
|
-
use_text_extract: false
|
|
113
|
+
schema: PageInfoSchema,
|
|
114
|
+
use_text_extract: false,
|
|
109
115
|
};
|
|
110
116
|
|
|
111
|
-
const [extractSuccess,
|
|
117
|
+
const [extractSuccess, extracted] = await session.browser.agent.extract(
|
|
118
|
+
extractOptions,
|
|
119
|
+
mockPage
|
|
120
|
+
);
|
|
112
121
|
console.log("Extract success:", extractSuccess);
|
|
113
|
-
console.log("Extracted data count:", extractedData.length);
|
|
114
122
|
|
|
115
|
-
|
|
116
|
-
console.log(
|
|
117
|
-
title:
|
|
118
|
-
url:
|
|
123
|
+
if (extractSuccess && extracted) {
|
|
124
|
+
console.log("Extracted data:", {
|
|
125
|
+
title: extracted.title,
|
|
126
|
+
url: extracted.url,
|
|
119
127
|
});
|
|
120
|
-
}
|
|
128
|
+
}
|
|
121
129
|
} catch (error) {
|
|
122
130
|
console.log("Extraction failed:", error);
|
|
123
131
|
}
|
|
@@ -125,7 +133,6 @@ async function main() {
|
|
|
125
133
|
// Clean up
|
|
126
134
|
console.log("\n--- Cleanup ---");
|
|
127
135
|
console.log("Browser session completed successfully");
|
|
128
|
-
|
|
129
136
|
} catch (error) {
|
|
130
137
|
console.error("Error in main function:", error);
|
|
131
138
|
}
|
|
@@ -6,35 +6,29 @@
|
|
|
6
6
|
* - Utilize PageUseAgent to run 2048 game
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import { AgentBay, CreateSessionParams } from
|
|
10
|
-
import { BrowserOption, ExtractOptions
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class GameState {
|
|
14
|
-
score?: number;
|
|
15
|
-
highestTile?: number;
|
|
16
|
-
grid: number[][] = [];
|
|
17
|
-
}
|
|
9
|
+
import { AgentBay, CreateSessionParams } from "wuying-agentbay-sdk";
|
|
10
|
+
import { BrowserOption, ExtractOptions } from "wuying-agentbay-sdk";
|
|
11
|
+
import { z } from "zod";
|
|
18
12
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
13
|
+
const GameStateSchema = z.object({
|
|
14
|
+
score: z.number().optional(),
|
|
15
|
+
highestTile: z.number().optional(),
|
|
16
|
+
grid: z.array(z.array(z.number())),
|
|
17
|
+
});
|
|
24
18
|
|
|
25
19
|
function transposeGrid(grid: number[][]): number[][] {
|
|
26
20
|
if (!grid || grid.length === 0) {
|
|
27
21
|
return [];
|
|
28
22
|
}
|
|
29
|
-
return grid[0].map((_, colIndex) => grid.map(row => row[colIndex]));
|
|
23
|
+
return grid[0].map((_, colIndex) => grid.map((row) => row[colIndex]));
|
|
30
24
|
}
|
|
31
25
|
|
|
32
26
|
function formatGridForLlmInstruction(gridData: number[][]): string {
|
|
33
27
|
const formattedRows: string[] = [];
|
|
34
28
|
for (let i = 0; i < gridData.length; i++) {
|
|
35
|
-
formattedRows.push(`row${i + 1}: [${gridData[i].join(
|
|
29
|
+
formattedRows.push(`row${i + 1}: [${gridData[i].join(", ")}]`);
|
|
36
30
|
}
|
|
37
|
-
return formattedRows.join(
|
|
31
|
+
return formattedRows.join("\n");
|
|
38
32
|
}
|
|
39
33
|
|
|
40
34
|
async function main() {
|
|
@@ -66,17 +60,17 @@ async function main() {
|
|
|
66
60
|
console.log("endpoint_url =", endpointUrl);
|
|
67
61
|
|
|
68
62
|
// Note: Install playwright with: npm install playwright
|
|
69
|
-
const { chromium } = require(
|
|
63
|
+
const { chromium } = require("playwright");
|
|
70
64
|
const browser = await chromium.connectOverCDP(endpointUrl);
|
|
71
65
|
let page = null;
|
|
72
66
|
|
|
73
67
|
try {
|
|
74
|
-
const context = browser.contexts()[0]
|
|
68
|
+
const context = browser.contexts()[0];
|
|
75
69
|
page = await context.newPage();
|
|
76
70
|
console.log("🌐 Navigating to 2048...");
|
|
77
71
|
await page.goto("https://ovolve.github.io/2048-AI/", {
|
|
78
72
|
waitUntil: "domcontentloaded",
|
|
79
|
-
timeout: 180000
|
|
73
|
+
timeout: 180000,
|
|
80
74
|
});
|
|
81
75
|
console.log("🌐 Navigated to 2048 done");
|
|
82
76
|
await page.waitForSelector(".grid-container", { timeout: 10000 });
|
|
@@ -87,11 +81,11 @@ async function main() {
|
|
|
87
81
|
|
|
88
82
|
while (true) {
|
|
89
83
|
console.log("🔄 Game loop iteration...");
|
|
90
|
-
await new Promise(resolve => setTimeout(resolve, 300));
|
|
84
|
+
await new Promise((resolve) => setTimeout(resolve, 300));
|
|
91
85
|
|
|
92
86
|
// Get current game state
|
|
93
87
|
console.log("📊 Extracting game state...");
|
|
94
|
-
const gameStateOptions: ExtractOptions<
|
|
88
|
+
const gameStateOptions: ExtractOptions<typeof GameStateSchema> = {
|
|
95
89
|
instruction: `
|
|
96
90
|
Extract the current game state:
|
|
97
91
|
1. Score from the score counter
|
|
@@ -105,20 +99,25 @@ async function main() {
|
|
|
105
99
|
For instance, if the only tiles present are the two above, the grid should be:[[0, 0, 0, 2], [0, 0, 0, 0], [0, 0, 0, 0], [2, 0, 0, 0]]
|
|
106
100
|
3. Highest tile value present
|
|
107
101
|
`,
|
|
108
|
-
schema:
|
|
109
|
-
use_text_extract: false
|
|
110
|
-
|
|
102
|
+
schema: GameStateSchema,
|
|
103
|
+
use_text_extract: false,
|
|
111
104
|
};
|
|
112
105
|
|
|
113
|
-
const [success,
|
|
114
|
-
|
|
115
|
-
|
|
106
|
+
const [success, gameState] = await session.browser.agent.extract(
|
|
107
|
+
gameStateOptions,
|
|
108
|
+
page
|
|
109
|
+
);
|
|
110
|
+
if (success && gameState) {
|
|
116
111
|
const transposedGrid = transposeGrid(gameState.grid);
|
|
117
112
|
console.log(`transposed grid: ${JSON.stringify(transposedGrid)}`);
|
|
118
113
|
console.log(`gameState: ${JSON.stringify(gameState)}`);
|
|
119
114
|
const gridInstruction = formatGridForLlmInstruction(transposedGrid);
|
|
120
115
|
|
|
121
|
-
if (
|
|
116
|
+
if (
|
|
117
|
+
lastTransposedGrid !== null &&
|
|
118
|
+
JSON.stringify(transposedGrid) ===
|
|
119
|
+
JSON.stringify(lastTransposedGrid)
|
|
120
|
+
) {
|
|
122
121
|
transposedGridNotChangedTimes += 1;
|
|
123
122
|
} else {
|
|
124
123
|
transposedGridNotChangedTimes = 0;
|
|
@@ -144,22 +143,33 @@ async function main() {
|
|
|
144
143
|
|
|
145
144
|
if (transposedGridNotChangedTimes >= 1) {
|
|
146
145
|
instructionStr += `
|
|
147
|
-
9. Do not generate move value in ${JSON.stringify(
|
|
148
|
-
|
|
146
|
+
9. Do not generate move value in ${JSON.stringify(
|
|
147
|
+
lastMoveHistory
|
|
148
|
+
)}
|
|
149
|
+
10. If last move value ${
|
|
150
|
+
lastMoveHistory[lastMoveHistory.length - 1]
|
|
151
|
+
} moves up or down, then generate move value with left or right direction, otherwise generate move value with up or down direction
|
|
149
152
|
`;
|
|
150
153
|
}
|
|
151
154
|
|
|
152
|
-
const nextMoveOptions: ExtractOptions<
|
|
155
|
+
const nextMoveOptions: ExtractOptions<any> = {
|
|
153
156
|
instruction: instructionStr,
|
|
154
|
-
schema:
|
|
155
|
-
|
|
157
|
+
schema: z.object({
|
|
158
|
+
move: z.number().optional(),
|
|
159
|
+
confidence: z.number().optional(),
|
|
160
|
+
reasoning: z.string().optional(),
|
|
161
|
+
}),
|
|
162
|
+
use_text_extract: false,
|
|
156
163
|
};
|
|
157
164
|
|
|
158
|
-
const [moveSuccess, nextMove] = await session.browser.agent.extract(
|
|
165
|
+
const [moveSuccess, nextMove] = await session.browser.agent.extract(
|
|
166
|
+
nextMoveOptions,
|
|
167
|
+
page
|
|
168
|
+
);
|
|
159
169
|
let selectedMove = 4; // Default to no move
|
|
160
170
|
|
|
161
|
-
if (moveSuccess && nextMove
|
|
162
|
-
selectedMove = nextMove
|
|
171
|
+
if (moveSuccess && nextMove && typeof nextMove === "object") {
|
|
172
|
+
selectedMove = (nextMove as any).move ?? 4;
|
|
163
173
|
} else {
|
|
164
174
|
console.log("❌ Failed to extract next move, retry observing");
|
|
165
175
|
continue;
|
|
@@ -6,20 +6,28 @@
|
|
|
6
6
|
* - Utilize PageUseAgent to run sudoku game
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
9
|
+
import {
|
|
10
|
+
AgentBay,
|
|
11
|
+
CreateSessionParams,
|
|
12
|
+
BrowserOption,
|
|
13
|
+
ExtractOptions,
|
|
14
|
+
ActOptions,
|
|
15
|
+
} from "wuying-agentbay-sdk";
|
|
16
|
+
|
|
17
|
+
import { chromium } from "playwright";
|
|
18
|
+
import { z } from "zod";
|
|
19
|
+
|
|
20
|
+
const SudokuBoardSchema = z.object({
|
|
21
|
+
board: z.array(z.array(z.number())),
|
|
22
|
+
});
|
|
23
|
+
type SudokuBoardType = z.infer<typeof SudokuBoardSchema>;
|
|
24
|
+
|
|
25
|
+
const SudokuSolutionSchema = z.object({
|
|
26
|
+
solution: z.array(z.array(z.number())),
|
|
27
|
+
});
|
|
28
|
+
type SudokuSolutionType = z.infer<typeof SudokuSolutionSchema>;
|
|
21
29
|
function formatBoardForLlm(board: number[][]): string {
|
|
22
|
-
return board.map(row => ` [${row.join(
|
|
30
|
+
return board.map((row) => ` [${row.join(", ")}]`).join("\n");
|
|
23
31
|
}
|
|
24
32
|
|
|
25
33
|
async function main() {
|
|
@@ -52,7 +60,7 @@ async function main() {
|
|
|
52
60
|
|
|
53
61
|
const browser = await chromium.connectOverCDP(endpointUrl);
|
|
54
62
|
try {
|
|
55
|
-
const context = browser.contexts()[0]
|
|
63
|
+
const context = browser.contexts()[0];
|
|
56
64
|
const page = await context.newPage();
|
|
57
65
|
console.log("🌐 Navigating to Sudoku site...");
|
|
58
66
|
const url = "https://widget.websudoku.com/";
|
|
@@ -65,27 +73,31 @@ async function main() {
|
|
|
65
73
|
|
|
66
74
|
while (!success) {
|
|
67
75
|
console.log("📊 Extracting sudoku board...");
|
|
68
|
-
const options: ExtractOptions<
|
|
69
|
-
instruction:
|
|
70
|
-
|
|
71
|
-
|
|
76
|
+
const options: ExtractOptions<typeof SudokuBoardSchema> = {
|
|
77
|
+
instruction:
|
|
78
|
+
"Extract the current sudoku board as a 9x9 array. Each cell should be a number (1-9) if filled, or 0 if empty.",
|
|
79
|
+
schema: SudokuBoardSchema,
|
|
80
|
+
use_text_extract: false,
|
|
72
81
|
};
|
|
73
82
|
|
|
74
|
-
const [extractSuccess,
|
|
75
|
-
|
|
83
|
+
const [extractSuccess, boardObj] =
|
|
84
|
+
await session.browser.agent.extract(options, page);
|
|
85
|
+
if (extractSuccess && boardObj) {
|
|
76
86
|
success = true;
|
|
77
|
-
board =
|
|
87
|
+
board = boardObj.board;
|
|
78
88
|
} else {
|
|
79
89
|
console.log("❌ Failed to extract sudoku board, retry extracting");
|
|
80
|
-
await new Promise(resolve => setTimeout(resolve, 3000));
|
|
90
|
+
await new Promise((resolve) => setTimeout(resolve, 3000));
|
|
81
91
|
}
|
|
82
92
|
}
|
|
83
93
|
|
|
84
|
-
console.log(
|
|
85
|
-
|
|
94
|
+
console.log(
|
|
95
|
+
"Current Board:\n" + board.map((row) => row.join(" ")).join("\n")
|
|
96
|
+
);
|
|
97
|
+
const originalBoard = board.map((row) => [...row]);
|
|
86
98
|
|
|
87
99
|
// 2. Solve the sudoku
|
|
88
|
-
const solutionOptions: ExtractOptions<
|
|
100
|
+
const solutionOptions: ExtractOptions<typeof SudokuSolutionSchema> = {
|
|
89
101
|
instruction: `You are an expert sudoku solver. Given the following sudoku board as a 9x9 array (0 means empty), solve the sudoku and return the completed 9x9 array as 'solution'.
|
|
90
102
|
|
|
91
103
|
Sudoku rules:
|
|
@@ -103,39 +115,46 @@ Return:
|
|
|
103
115
|
{
|
|
104
116
|
solution: number[][] // 9x9, all filled, valid sudoku
|
|
105
117
|
}`,
|
|
106
|
-
schema:
|
|
107
|
-
use_text_extract: false
|
|
118
|
+
schema: SudokuSolutionSchema,
|
|
119
|
+
use_text_extract: false,
|
|
108
120
|
};
|
|
109
121
|
|
|
110
|
-
const [solutionSuccess,
|
|
111
|
-
|
|
122
|
+
const [solutionSuccess, solutionObj] =
|
|
123
|
+
await session.browser.agent.extract(solutionOptions, page);
|
|
124
|
+
if (!solutionSuccess || !solutionObj) {
|
|
112
125
|
console.log("❌ Failed to solve sudoku");
|
|
113
126
|
return;
|
|
114
127
|
}
|
|
115
128
|
|
|
116
|
-
const solution =
|
|
117
|
-
console.log(
|
|
129
|
+
const solution = solutionObj.solution;
|
|
130
|
+
console.log(
|
|
131
|
+
"Solved Board:\n" +
|
|
132
|
+
solution.map((row: any) => row.join(" ")).join("\n")
|
|
133
|
+
);
|
|
118
134
|
|
|
119
135
|
// 3. Fill the solution
|
|
120
136
|
for (let row = 0; row < 9; row++) {
|
|
121
137
|
for (let col = 0; col < 9; col++) {
|
|
122
138
|
if (originalBoard[row][col] === 0) {
|
|
123
139
|
const inputId = `f${col}${row}`;
|
|
124
|
-
console.log(
|
|
140
|
+
console.log(
|
|
141
|
+
`Type '${solution[row][col]}' into the cell with id '${inputId}'`
|
|
142
|
+
);
|
|
125
143
|
|
|
126
144
|
// Use the act method for natural language action
|
|
127
145
|
const actOptions: ActOptions = {
|
|
128
|
-
action: `Enter '${solution[row][col]}' into the input element where the attribute id is exactly '${inputId}' (for example, if id='f53', you must match the full string 'f53', not just the number 53; do not split or extract numbers from the id)
|
|
146
|
+
action: `Enter '${solution[row][col]}' into the input element where the attribute id is exactly '${inputId}' (for example, if id='f53', you must match the full string 'f53', not just the number 53; do not split or extract numbers from the id)`,
|
|
129
147
|
};
|
|
130
148
|
|
|
131
149
|
await session.browser.agent.act(actOptions, page);
|
|
132
|
-
await new Promise(resolve => setTimeout(resolve, 500));
|
|
150
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
133
151
|
}
|
|
134
152
|
}
|
|
135
153
|
}
|
|
136
154
|
|
|
137
|
-
console.log(
|
|
138
|
-
|
|
155
|
+
console.log(
|
|
156
|
+
"✅ Finished! The board has been solved and filled in the browser."
|
|
157
|
+
);
|
|
139
158
|
} catch (error) {
|
|
140
159
|
console.log(`❌ Error in game loop: ${error}`);
|
|
141
160
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "test-wuying-agentbay-sdk",
|
|
3
|
-
"version": "0.13.0-beta.
|
|
3
|
+
"version": "0.13.0-beta.20251212140302",
|
|
4
4
|
"description": "TypeScript SDK for interacting with the Wuying AgentBay cloud runtime environment",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -66,7 +66,9 @@
|
|
|
66
66
|
"ts-jest": "^26.5.6",
|
|
67
67
|
"tsup": "^8.5.0",
|
|
68
68
|
"typedoc-plugin-markdown": "^3.16.0",
|
|
69
|
-
"typescript": "^4.9.5"
|
|
69
|
+
"typescript": "^4.9.5",
|
|
70
|
+
"zod": "^3.25.76",
|
|
71
|
+
"zod-to-json-schema": "^3.22.4"
|
|
70
72
|
},
|
|
71
73
|
"dependencies": {
|
|
72
74
|
"@alicloud/openapi-core": "^1.0.4",
|