oira666_pi-browser-agent 0.0.12 → 0.0.13
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 +201 -201
- package/README.md +101 -101
- package/package.json +28 -28
- package/src/broker/protocol.ts +128 -128
- package/src/broker/remote.ts +340 -321
- package/src/broker/server.ts +454 -436
- package/src/broker/task-store.ts +192 -192
- package/src/index.ts +1 -1
- package/src/tools/_register.ts +90 -90
- package/src/tools/browser_agent_tools.ts +83 -72
- package/src/tools/browser_js_navigation_tools.ts +491 -489
- package/src/tools/browser_observability_tools.ts +163 -163
- package/src/tools/browser_task_tools.ts +399 -383
- package/src/tools/read_only/common.ts +235 -232
- package/src/tools/read_only/index.ts +344 -344
- package/src/util/instances.ts +102 -102
- package/src/util/paths.ts +19 -19
- package/src/util/truncate.ts +34 -34
- package/tests/broker.test.ts +35 -0
- package/tests/index.test.ts +215 -215
- package/tests/instances.test.ts +80 -80
- package/tests/protocol.test.ts +48 -48
- package/tests/task-store.test.ts +35 -35
- package/tests/tools/browser-js-navigation-tools.test.ts +251 -251
- package/tests/tools/browser-observability-tools.test.ts +100 -100
- package/tests/tools/browser-read-only-tools.test.ts +229 -229
- package/tests/tools/browser-task-tools.test.ts +387 -348
- package/tests/tools/meta-tool.test.ts +110 -110
- package/tsconfig.json +15 -15
package/package.json
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "oira666_pi-browser-agent",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"private": false,
|
|
5
|
-
"type": "module",
|
|
6
|
-
"description": "Pi browser agent broker skeleton with lazy browser tool registration",
|
|
7
|
-
"repository": {
|
|
8
|
-
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/gee666/pi-browser-agent.git"
|
|
10
|
-
},
|
|
11
|
-
"pi": {
|
|
12
|
-
"extensions": [
|
|
13
|
-
"./src/index.ts"
|
|
14
|
-
]
|
|
15
|
-
},
|
|
16
|
-
"scripts": {
|
|
17
|
-
"test": "node --test ./tests/*.test.ts ./tests/tools/*.test.ts"
|
|
18
|
-
},
|
|
19
|
-
"dependencies": {
|
|
20
|
-
"@sinclair/typebox": "^0.34.41",
|
|
21
|
-
"ws": "^8.18.3"
|
|
22
|
-
},
|
|
23
|
-
"devDependencies": {
|
|
24
|
-
"@types/node": "^25.6.0",
|
|
25
|
-
"@types/ws": "^8.18.1",
|
|
26
|
-
"typescript": "^5.9.3"
|
|
27
|
-
}
|
|
28
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "oira666_pi-browser-agent",
|
|
3
|
+
"version": "0.0.13",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"description": "Pi browser agent broker skeleton with lazy browser tool registration",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/gee666/pi-browser-agent.git"
|
|
10
|
+
},
|
|
11
|
+
"pi": {
|
|
12
|
+
"extensions": [
|
|
13
|
+
"./src/index.ts"
|
|
14
|
+
]
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "node --test ./tests/*.test.ts ./tests/tools/*.test.ts"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@sinclair/typebox": "^0.34.41",
|
|
21
|
+
"ws": "^8.18.3"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/node": "^25.6.0",
|
|
25
|
+
"@types/ws": "^8.18.1",
|
|
26
|
+
"typescript": "^5.9.3"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/broker/protocol.ts
CHANGED
|
@@ -1,128 +1,128 @@
|
|
|
1
|
-
import { Type } from '@sinclair/typebox';
|
|
2
|
-
import type { Static } from '@sinclair/typebox';
|
|
3
|
-
import { Value } from '@sinclair/typebox/value';
|
|
4
|
-
|
|
5
|
-
export const PROTOCOL_VERSION = 1;
|
|
6
|
-
|
|
7
|
-
const ErrorFrameSchema = Type.Object({
|
|
8
|
-
code: Type.String(),
|
|
9
|
-
message: Type.String(),
|
|
10
|
-
details: Type.Optional(Type.Unknown()),
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
export const HelloFrameSchema = Type.Object({
|
|
14
|
-
v: Type.Literal(PROTOCOL_VERSION),
|
|
15
|
-
kind: Type.Literal('hello'),
|
|
16
|
-
extensionId: Type.String(),
|
|
17
|
-
version: Type.String(),
|
|
18
|
-
capabilities: Type.Array(Type.String()),
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
export const WelcomeFrameSchema = Type.Object({
|
|
22
|
-
v: Type.Literal(PROTOCOL_VERSION),
|
|
23
|
-
kind: Type.Literal('welcome'),
|
|
24
|
-
brokerVersion: Type.String(),
|
|
25
|
-
serverTime: Type.Number(),
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
export const ProbeFrameSchema = Type.Object({
|
|
29
|
-
v: Type.Literal(PROTOCOL_VERSION),
|
|
30
|
-
kind: Type.Literal('probe'),
|
|
31
|
-
id: Type.Optional(Type.String()),
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
export const RequestFrameSchema = Type.Object({
|
|
35
|
-
v: Type.Literal(PROTOCOL_VERSION),
|
|
36
|
-
kind: Type.Literal('request'),
|
|
37
|
-
id: Type.String(),
|
|
38
|
-
type: Type.String(),
|
|
39
|
-
params: Type.Optional(Type.Unknown()),
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
export const ResponseFrameSchema = Type.Object({
|
|
43
|
-
v: Type.Literal(PROTOCOL_VERSION),
|
|
44
|
-
kind: Type.Literal('response'),
|
|
45
|
-
id: Type.String(),
|
|
46
|
-
ok: Type.Boolean(),
|
|
47
|
-
data: Type.Optional(Type.Unknown()),
|
|
48
|
-
error: Type.Optional(ErrorFrameSchema),
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
export const AnyIncomingFrameSchema = Type.Union([
|
|
52
|
-
HelloFrameSchema,
|
|
53
|
-
ProbeFrameSchema,
|
|
54
|
-
RequestFrameSchema,
|
|
55
|
-
ResponseFrameSchema,
|
|
56
|
-
]);
|
|
57
|
-
|
|
58
|
-
export type HelloFrame = Static<typeof HelloFrameSchema>;
|
|
59
|
-
export type WelcomeFrame = Static<typeof WelcomeFrameSchema>;
|
|
60
|
-
export type ProbeFrame = Static<typeof ProbeFrameSchema>;
|
|
61
|
-
export type RequestFrame = Static<typeof RequestFrameSchema>;
|
|
62
|
-
export type ResponseFrame = Static<typeof ResponseFrameSchema>;
|
|
63
|
-
export type IncomingFrame = Static<typeof AnyIncomingFrameSchema>;
|
|
64
|
-
|
|
65
|
-
export interface ProbeResult {
|
|
66
|
-
brokerReachable: boolean;
|
|
67
|
-
brokerListening: boolean;
|
|
68
|
-
bridgeConnected: boolean;
|
|
69
|
-
bridgeVersion?: string;
|
|
70
|
-
capabilities?: string[];
|
|
71
|
-
startupError?: string;
|
|
72
|
-
url?: string;
|
|
73
|
-
bridgeSessionSerial?: number;
|
|
74
|
-
supportsBrokerProxy?: boolean;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export function parseIncomingFrame(raw: string): IncomingFrame {
|
|
78
|
-
let parsed: unknown;
|
|
79
|
-
try {
|
|
80
|
-
parsed = JSON.parse(raw);
|
|
81
|
-
} catch (error) {
|
|
82
|
-
throw new Error(`Invalid JSON frame: ${error instanceof Error ? error.message : String(error)}`);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (!Value.Check(AnyIncomingFrameSchema, parsed)) {
|
|
86
|
-
throw new Error('Invalid protocol frame');
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (parsed.kind === 'response') {
|
|
90
|
-
if (parsed.ok && parsed.error) {
|
|
91
|
-
throw new Error('Response frame cannot include error when ok=true');
|
|
92
|
-
}
|
|
93
|
-
if (!parsed.ok && !parsed.error) {
|
|
94
|
-
throw new Error('Response frame must include error when ok=false');
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return parsed;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export function createWelcomeFrame(brokerVersion: string): WelcomeFrame {
|
|
102
|
-
return {
|
|
103
|
-
v: PROTOCOL_VERSION,
|
|
104
|
-
kind: 'welcome',
|
|
105
|
-
brokerVersion,
|
|
106
|
-
serverTime: Date.now(),
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
export function createResponseFrame(id: string, data: unknown): ResponseFrame {
|
|
111
|
-
return {
|
|
112
|
-
v: PROTOCOL_VERSION,
|
|
113
|
-
kind: 'response',
|
|
114
|
-
id,
|
|
115
|
-
ok: true,
|
|
116
|
-
data,
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export function createErrorResponseFrame(id: string, code: string, message: string, details?: unknown): ResponseFrame {
|
|
121
|
-
return {
|
|
122
|
-
v: PROTOCOL_VERSION,
|
|
123
|
-
kind: 'response',
|
|
124
|
-
id,
|
|
125
|
-
ok: false,
|
|
126
|
-
error: { code, message, details },
|
|
127
|
-
};
|
|
128
|
-
}
|
|
1
|
+
import { Type } from '@sinclair/typebox';
|
|
2
|
+
import type { Static } from '@sinclair/typebox';
|
|
3
|
+
import { Value } from '@sinclair/typebox/value';
|
|
4
|
+
|
|
5
|
+
export const PROTOCOL_VERSION = 1;
|
|
6
|
+
|
|
7
|
+
const ErrorFrameSchema = Type.Object({
|
|
8
|
+
code: Type.String(),
|
|
9
|
+
message: Type.String(),
|
|
10
|
+
details: Type.Optional(Type.Unknown()),
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export const HelloFrameSchema = Type.Object({
|
|
14
|
+
v: Type.Literal(PROTOCOL_VERSION),
|
|
15
|
+
kind: Type.Literal('hello'),
|
|
16
|
+
extensionId: Type.String(),
|
|
17
|
+
version: Type.String(),
|
|
18
|
+
capabilities: Type.Array(Type.String()),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export const WelcomeFrameSchema = Type.Object({
|
|
22
|
+
v: Type.Literal(PROTOCOL_VERSION),
|
|
23
|
+
kind: Type.Literal('welcome'),
|
|
24
|
+
brokerVersion: Type.String(),
|
|
25
|
+
serverTime: Type.Number(),
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
export const ProbeFrameSchema = Type.Object({
|
|
29
|
+
v: Type.Literal(PROTOCOL_VERSION),
|
|
30
|
+
kind: Type.Literal('probe'),
|
|
31
|
+
id: Type.Optional(Type.String()),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
export const RequestFrameSchema = Type.Object({
|
|
35
|
+
v: Type.Literal(PROTOCOL_VERSION),
|
|
36
|
+
kind: Type.Literal('request'),
|
|
37
|
+
id: Type.String(),
|
|
38
|
+
type: Type.String(),
|
|
39
|
+
params: Type.Optional(Type.Unknown()),
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
export const ResponseFrameSchema = Type.Object({
|
|
43
|
+
v: Type.Literal(PROTOCOL_VERSION),
|
|
44
|
+
kind: Type.Literal('response'),
|
|
45
|
+
id: Type.String(),
|
|
46
|
+
ok: Type.Boolean(),
|
|
47
|
+
data: Type.Optional(Type.Unknown()),
|
|
48
|
+
error: Type.Optional(ErrorFrameSchema),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
export const AnyIncomingFrameSchema = Type.Union([
|
|
52
|
+
HelloFrameSchema,
|
|
53
|
+
ProbeFrameSchema,
|
|
54
|
+
RequestFrameSchema,
|
|
55
|
+
ResponseFrameSchema,
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
export type HelloFrame = Static<typeof HelloFrameSchema>;
|
|
59
|
+
export type WelcomeFrame = Static<typeof WelcomeFrameSchema>;
|
|
60
|
+
export type ProbeFrame = Static<typeof ProbeFrameSchema>;
|
|
61
|
+
export type RequestFrame = Static<typeof RequestFrameSchema>;
|
|
62
|
+
export type ResponseFrame = Static<typeof ResponseFrameSchema>;
|
|
63
|
+
export type IncomingFrame = Static<typeof AnyIncomingFrameSchema>;
|
|
64
|
+
|
|
65
|
+
export interface ProbeResult {
|
|
66
|
+
brokerReachable: boolean;
|
|
67
|
+
brokerListening: boolean;
|
|
68
|
+
bridgeConnected: boolean;
|
|
69
|
+
bridgeVersion?: string;
|
|
70
|
+
capabilities?: string[];
|
|
71
|
+
startupError?: string;
|
|
72
|
+
url?: string;
|
|
73
|
+
bridgeSessionSerial?: number;
|
|
74
|
+
supportsBrokerProxy?: boolean;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function parseIncomingFrame(raw: string): IncomingFrame {
|
|
78
|
+
let parsed: unknown;
|
|
79
|
+
try {
|
|
80
|
+
parsed = JSON.parse(raw);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
throw new Error(`Invalid JSON frame: ${error instanceof Error ? error.message : String(error)}`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (!Value.Check(AnyIncomingFrameSchema, parsed)) {
|
|
86
|
+
throw new Error('Invalid protocol frame');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (parsed.kind === 'response') {
|
|
90
|
+
if (parsed.ok && parsed.error) {
|
|
91
|
+
throw new Error('Response frame cannot include error when ok=true');
|
|
92
|
+
}
|
|
93
|
+
if (!parsed.ok && !parsed.error) {
|
|
94
|
+
throw new Error('Response frame must include error when ok=false');
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return parsed;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function createWelcomeFrame(brokerVersion: string): WelcomeFrame {
|
|
102
|
+
return {
|
|
103
|
+
v: PROTOCOL_VERSION,
|
|
104
|
+
kind: 'welcome',
|
|
105
|
+
brokerVersion,
|
|
106
|
+
serverTime: Date.now(),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function createResponseFrame(id: string, data: unknown): ResponseFrame {
|
|
111
|
+
return {
|
|
112
|
+
v: PROTOCOL_VERSION,
|
|
113
|
+
kind: 'response',
|
|
114
|
+
id,
|
|
115
|
+
ok: true,
|
|
116
|
+
data,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function createErrorResponseFrame(id: string, code: string, message: string, details?: unknown): ResponseFrame {
|
|
121
|
+
return {
|
|
122
|
+
v: PROTOCOL_VERSION,
|
|
123
|
+
kind: 'response',
|
|
124
|
+
id,
|
|
125
|
+
ok: false,
|
|
126
|
+
error: { code, message, details },
|
|
127
|
+
};
|
|
128
|
+
}
|