-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayground.ts
More file actions
109 lines (101 loc) · 2.76 KB
/
Copy pathplayground.ts
File metadata and controls
109 lines (101 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { createHostNodeRuntime } from "./src/host-node/index.ts";
import {
createClient,
createProgramContract,
defineTool,
} from "./src/index.ts";
import { testSchema } from "./src/testing/schema.ts";
async function main() {
const FlightRequest = testSchema({
type: "object",
properties: {
source: { type: "string", description: "The source airport code." },
dest: {
type: "string",
description: "The destination airport code.",
},
departureDate: {
type: "string",
description: "The date of the departure.",
format: "date",
},
returnDate: {
type: "string",
description: "The date of the return.",
format: "date",
},
},
required: ["source", "dest", "departureDate", "returnDate"],
additionalProperties: false,
} as const);
const listFlights = defineTool(
"listFlights",
{
description: "List air flights matching a set of criteria.",
inputSchema: FlightRequest,
outputSchema: testSchema({
type: "object",
properties: {
flights: {
type: "array",
items: {
type: "object",
properties: { code: { type: "string" } },
required: ["code"],
additionalProperties: false,
},
},
},
required: ["flights"],
additionalProperties: false,
} as const),
},
async (_ctx, query) => {
console.log(query);
return {
flights: [],
};
},
);
const contract = createProgramContract({
inputSchema: FlightRequest,
tools: [listFlights],
});
const runtime = await createHostNodeRuntime({
nodePath: process.execPath,
cwd: process.cwd(),
}, AbortSignal.timeout(5_000));
try {
const client = createClient({
runtime,
contract,
});
console.log(contract.typeDefinitions);
const source = `export default async function ({ input, codemode, result }: AgentProgramScope) {
const response = await codemode.listFlights(input);
result.appendText(JSON.stringify(response));
}`;
console.dir(await client.validate(source, AbortSignal.timeout(5_000)));
const outcome = await client.run(source, {
input: {
source: "YYZ",
dest: "LHR",
departureDate: "2026-07-01",
returnDate: "2026-07-08",
},
signal: AbortSignal.timeout(5_000),
});
if (outcome.kind === "program-failed" && outcome.error.details !== null) {
console.dir(outcome.error.details.report);
}
if (outcome.kind === "success") {
console.dir(outcome.content);
}
} finally {
await runtime[Symbol.asyncDispose]();
}
}
main().catch((e) => {
console.error(e);
process.exit(1);
});