-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtypes.spec.ts
134 lines (120 loc) · 5.22 KB
/
types.spec.ts
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { describe, it, expect } from "vitest";
import { isXMbSpec, validateXMbSpec } from "../../src/config/types";
type ArbitraryObject = { [key: string]: unknown };
describe("src/config", () => {
it("validateXMbSpec complete flow", () => {
expect(() => validateXMbSpec(null)).toThrow("x-mb spec must be an object");
// Start with empty spec
let spec: ArbitraryObject = {};
expect(() => validateXMbSpec(spec)).toThrow(
"x-mb spec must contain assistant object",
);
let assistant: ArbitraryObject = {};
spec.assistant = assistant;
expect(() => validateXMbSpec(spec)).toThrow(
"assistant must contain name as string",
);
assistant.name = "assistantName";
expect(() => validateXMbSpec(spec)).toThrow(
"assistant must contain description as string",
);
assistant.description = "assistantDescription";
expect(() => validateXMbSpec(spec)).toThrow(
"assistant must contain instructions as string",
);
assistant.instructions = "assistantInstructions";
// Checkpoint Valid XMbSpec
expect(spec).toStrictEqual({
assistant: {
name: "assistantName",
description: "assistantDescription",
instructions: "assistantInstructions",
},
});
expect(validateXMbSpec(spec)).toBeUndefined();
expect(isXMbSpec(spec)).toBe(true);
// Optional fields
assistant.tools = null;
expect(() => validateXMbSpec(spec)).toThrow("tools must be an array");
assistant.tools = [];
expect(validateXMbSpec(spec)).toBeUndefined();
assistant.tools = [1];
expect(() => validateXMbSpec(spec)).toThrow("each tool must be an object");
assistant.tools = [{}];
expect(() => validateXMbSpec(spec)).toThrow(
"each tool must have a type string",
);
assistant.tools = [{ type: 1 }];
expect(() => validateXMbSpec(spec)).toThrow(
"each tool must have a type string",
);
assistant.tools = [{ type: "function" }];
// TODO(bh2smith): Should the tool type be an enum?
expect(validateXMbSpec(spec)).toBeUndefined();
assistant.chainIds = null;
expect(() => validateXMbSpec(spec)).toThrow("chainIds must be an array");
assistant.chainIds = ["ethereum"];
expect(() => validateXMbSpec(spec)).toThrow(
"chainIds must be an array of number",
);
assistant.chainIds = [1, 2];
expect(validateXMbSpec(spec)).toBeUndefined();
assistant.categories = null;
expect(() => validateXMbSpec(spec)).toThrow("categories must be an array");
assistant.categories = [null];
expect(() => validateXMbSpec(spec)).toThrow(
"categories must be an array of strings",
);
assistant.categories = ["ethereum", "solana"];
expect(validateXMbSpec(spec)).toBeUndefined();
assistant.version = null;
expect(() => validateXMbSpec(spec)).toThrow("version must be a string");
assistant.version = "1.0.0";
expect(validateXMbSpec(spec)).toBeUndefined();
assistant.repo = null;
expect(() => validateXMbSpec(spec)).toThrow("repo must be a string");
assistant.repo = "https://github.com/example/repo";
expect(validateXMbSpec(spec)).toBeUndefined();
spec.email = 123;
expect(() => validateXMbSpec(spec)).toThrow("email must be a string");
spec.email = "test@example.com";
expect(validateXMbSpec(spec)).toBeUndefined();
// Full Example Spec:
expect(spec).toStrictEqual({
assistant: {
name: "assistantName",
description: "assistantDescription",
instructions: "assistantInstructions",
tools: [{ type: "function" }],
chainIds: [1, 2],
categories: ["ethereum", "solana"],
version: "1.0.0",
repo: "https://github.com/example/repo",
},
email: "test@example.com",
});
});
it("validateXMbSpec real example (CoW Agent)", () => {
const cowSpec = {
"account-id": "max-normal.near",
assistant: {
name: "CoWSwap Assistant",
description:
"An assistant that generates EVM transaction data for CoW Protocol Interactions",
instructions:
"Encodes transactions as signature requests on EVM networks. This assistant is only for EVM networks. Passes the the transaction fields of the response to generate-evm-tx tool for signing and displays the meta content of the response to the user after signing. For selling native assets, such as ETH, xDAI, POL, BNB it uses 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE as the sellToken. It does not infer the chainId. Do not infer the token decimals. Use Token Units for sellAmountBeforeFee. Uses token symbols for sellToken and buyToken unless addresses are provided. Always passes evmAddress as the safeAddress on any request requiring safeAddress. The only supported chains for the cowswap endpoint are Ethereum, Gnosis, Arbitrum and Base. All network support for balance, weth and erc20 endpoints.",
tools: [
{
type: "generate-evm-tx",
},
],
image: "https://near-cow-agent.vercel.app/cowswap.svg",
categories: ["defi"],
chainIds: [1, 100, 8453, 42161, 11155111],
},
image: "https://near-cow-agent.vercel.app/cowswap.svg",
};
expect(validateXMbSpec(cowSpec)).toBeUndefined();
expect(isXMbSpec(cowSpec)).toBe(true);
});
});