Skip to content

Commit 7b95d8d

Browse files
author
Nick Sweeting
committed
centralize v3 stagehand-api schema and generate openapi spec from zod shapes
1 parent 8ff3c21 commit 7b95d8d

File tree

4 files changed

+1107
-93
lines changed

4 files changed

+1107
-93
lines changed

packages/core/lib/v3/server/index.ts

Lines changed: 7 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -14,106 +14,20 @@ import type {
1414
import type { StagehandZodSchema } from "../zodCompat";
1515
import { SessionManager } from "./sessions";
1616
import { createStreamingResponse } from "./stream";
17+
import {
18+
actSchemaV3,
19+
extractSchemaV3,
20+
observeSchemaV3,
21+
agentExecuteSchemaV3,
22+
navigateSchemaV3,
23+
} from "./schemas";
1724

1825
export interface StagehandServerOptions {
1926
port?: number;
2027
host?: string;
2128
sessionTTL?: number;
2229
}
2330

24-
// Zod schemas for V3 API (we only support V3 in the library server)
25-
const actSchemaV3 = z.object({
26-
input: z.string().or(
27-
z.object({
28-
selector: z.string(),
29-
description: z.string(),
30-
backendNodeId: z.number().optional(),
31-
method: z.string().optional(),
32-
arguments: z.array(z.string()).optional(),
33-
}),
34-
),
35-
options: z
36-
.object({
37-
model: z
38-
.object({
39-
provider: z.string().optional(),
40-
model: z.string().optional(),
41-
apiKey: z.string().optional(),
42-
baseURL: z.string().url().optional(),
43-
})
44-
.optional(),
45-
variables: z.record(z.string(), z.string()).optional(),
46-
timeout: z.number().optional(),
47-
})
48-
.optional(),
49-
frameId: z.string().optional(),
50-
});
51-
52-
const extractSchemaV3 = z.object({
53-
instruction: z.string().optional(),
54-
schema: z.record(z.string(), z.unknown()).optional(),
55-
options: z
56-
.object({
57-
model: z
58-
.object({
59-
provider: z.string().optional(),
60-
model: z.string().optional(),
61-
apiKey: z.string().optional(),
62-
baseURL: z.string().url().optional(),
63-
})
64-
.optional(),
65-
timeout: z.number().optional(),
66-
selector: z.string().optional(),
67-
})
68-
.optional(),
69-
frameId: z.string().optional(),
70-
});
71-
72-
const observeSchemaV3 = z.object({
73-
instruction: z.string().optional(),
74-
options: z
75-
.object({
76-
model: z
77-
.object({
78-
provider: z.string().optional(),
79-
model: z.string().optional(),
80-
apiKey: z.string().optional(),
81-
baseURL: z.string().url().optional(),
82-
})
83-
.optional(),
84-
timeout: z.number().optional(),
85-
selector: z.string().optional(),
86-
})
87-
.optional(),
88-
frameId: z.string().optional(),
89-
});
90-
91-
const agentExecuteSchemaV3 = z.object({
92-
agentConfig: z.object({
93-
provider: z.enum(["openai", "anthropic", "google"]).optional(),
94-
model: z
95-
.string()
96-
.optional()
97-
.or(
98-
z.object({
99-
provider: z.enum(["openai", "anthropic", "google"]).optional(),
100-
modelName: z.string(),
101-
apiKey: z.string().optional(),
102-
baseURL: z.string().url().optional(),
103-
}),
104-
)
105-
.optional(),
106-
systemPrompt: z.string().optional(),
107-
cua: z.boolean().optional(),
108-
}),
109-
executeOptions: z.object({
110-
instruction: z.string(),
111-
maxSteps: z.number().optional(),
112-
highlightCursor: z.boolean().optional(),
113-
}),
114-
frameId: z.string().optional(),
115-
});
116-
11731
/**
11832
* StagehandServer - Embedded API server for peer-to-peer Stagehand communication
11933
*
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { z } from "zod";
2+
3+
/**
4+
* Shared Zod schemas for Stagehand P2P Server API
5+
* These schemas are used for both runtime validation and OpenAPI generation
6+
*/
7+
8+
// Zod schemas for V3 API (we only support V3 in the library server)
9+
export const actSchemaV3 = z.object({
10+
input: z.string().or(
11+
z.object({
12+
selector: z.string(),
13+
description: z.string(),
14+
backendNodeId: z.number().optional(),
15+
method: z.string().optional(),
16+
arguments: z.array(z.string()).optional(),
17+
}),
18+
),
19+
options: z
20+
.object({
21+
model: z
22+
.object({
23+
provider: z.string().optional(),
24+
model: z.string().optional(),
25+
apiKey: z.string().optional(),
26+
baseURL: z.string().url().optional(),
27+
})
28+
.optional(),
29+
variables: z.record(z.string(), z.string()).optional(),
30+
timeout: z.number().optional(),
31+
})
32+
.optional(),
33+
frameId: z.string().optional(),
34+
});
35+
36+
export const extractSchemaV3 = z.object({
37+
instruction: z.string().optional(),
38+
schema: z.record(z.string(), z.unknown()).optional(),
39+
options: z
40+
.object({
41+
model: z
42+
.object({
43+
provider: z.string().optional(),
44+
model: z.string().optional(),
45+
apiKey: z.string().optional(),
46+
baseURL: z.string().url().optional(),
47+
})
48+
.optional(),
49+
timeout: z.number().optional(),
50+
selector: z.string().optional(),
51+
})
52+
.optional(),
53+
frameId: z.string().optional(),
54+
});
55+
56+
export const observeSchemaV3 = z.object({
57+
instruction: z.string().optional(),
58+
options: z
59+
.object({
60+
model: z
61+
.object({
62+
provider: z.string().optional(),
63+
model: z.string().optional(),
64+
apiKey: z.string().optional(),
65+
baseURL: z.string().url().optional(),
66+
})
67+
.optional(),
68+
timeout: z.number().optional(),
69+
selector: z.string().optional(),
70+
})
71+
.optional(),
72+
frameId: z.string().optional(),
73+
});
74+
75+
export const agentExecuteSchemaV3 = z.object({
76+
agentConfig: z.object({
77+
provider: z.enum(["openai", "anthropic", "google"]).optional(),
78+
model: z
79+
.string()
80+
.optional()
81+
.or(
82+
z.object({
83+
provider: z.enum(["openai", "anthropic", "google"]).optional(),
84+
modelName: z.string(),
85+
apiKey: z.string().optional(),
86+
baseURL: z.string().url().optional(),
87+
}),
88+
)
89+
.optional(),
90+
systemPrompt: z.string().optional(),
91+
cua: z.boolean().optional(),
92+
}),
93+
executeOptions: z.object({
94+
instruction: z.string(),
95+
maxSteps: z.number().optional(),
96+
highlightCursor: z.boolean().optional(),
97+
}),
98+
frameId: z.string().optional(),
99+
});
100+
101+
export const navigateSchemaV3 = z.object({
102+
url: z.string(),
103+
options: z
104+
.object({
105+
waitUntil: z.enum(["load", "domcontentloaded", "networkidle"]).optional(),
106+
})
107+
.optional(),
108+
frameId: z.string().optional(),
109+
});

0 commit comments

Comments
 (0)