|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | + |
| 3 | +import { isXMbSpec, validateXMbSpec } from "../../src/config/types"; |
| 4 | + |
| 5 | +type ArbitraryObject = { [key: string]: unknown }; |
| 6 | +describe("src/config", () => { |
| 7 | + it("validateXMbSpec complete flow", () => { |
| 8 | + expect(() => validateXMbSpec(null)).toThrow("x-mb spec must be an object"); |
| 9 | + // Start with empty spec |
| 10 | + let spec: ArbitraryObject = {}; |
| 11 | + expect(() => validateXMbSpec(spec)).toThrow( |
| 12 | + "x-mb spec must contain assistant object", |
| 13 | + ); |
| 14 | + |
| 15 | + let assistant: ArbitraryObject = {}; |
| 16 | + spec.assistant = assistant; |
| 17 | + expect(() => validateXMbSpec(spec)).toThrow( |
| 18 | + "assistant must contain name as string", |
| 19 | + ); |
| 20 | + assistant.name = "assistantName"; |
| 21 | + expect(() => validateXMbSpec(spec)).toThrow( |
| 22 | + "assistant must contain description as string", |
| 23 | + ); |
| 24 | + assistant.description = "assistantDescription"; |
| 25 | + expect(() => validateXMbSpec(spec)).toThrow( |
| 26 | + "assistant must contain instructions as string", |
| 27 | + ); |
| 28 | + assistant.instructions = "assistantInstructions"; |
| 29 | + |
| 30 | + // Checkpoint Valid XMbSpec |
| 31 | + expect(spec).toStrictEqual({ |
| 32 | + assistant: { |
| 33 | + name: "assistantName", |
| 34 | + description: "assistantDescription", |
| 35 | + instructions: "assistantInstructions", |
| 36 | + }, |
| 37 | + }); |
| 38 | + expect(validateXMbSpec(spec)).toBeUndefined(); |
| 39 | + expect(isXMbSpec(spec)).toBe(true); |
| 40 | + |
| 41 | + // Optional fields |
| 42 | + assistant.tools = null; |
| 43 | + expect(() => validateXMbSpec(spec)).toThrow("tools must be an array"); |
| 44 | + assistant.tools = []; |
| 45 | + expect(validateXMbSpec(spec)).toBeUndefined(); |
| 46 | + assistant.tools = [1]; |
| 47 | + expect(() => validateXMbSpec(spec)).toThrow("each tool must be an object"); |
| 48 | + assistant.tools = [{}]; |
| 49 | + expect(() => validateXMbSpec(spec)).toThrow( |
| 50 | + "each tool must have a type string", |
| 51 | + ); |
| 52 | + assistant.tools = [{ type: 1 }]; |
| 53 | + expect(() => validateXMbSpec(spec)).toThrow( |
| 54 | + "each tool must have a type string", |
| 55 | + ); |
| 56 | + assistant.tools = [{ type: "function" }]; |
| 57 | + // TODO(bh2smith): Should the tool type be an enum? |
| 58 | + expect(validateXMbSpec(spec)).toBeUndefined(); |
| 59 | + |
| 60 | + assistant.chainIds = null; |
| 61 | + expect(() => validateXMbSpec(spec)).toThrow("chainIds must be an array"); |
| 62 | + assistant.chainIds = ["ethereum"]; |
| 63 | + expect(() => validateXMbSpec(spec)).toThrow( |
| 64 | + "chainIds must be an array of number", |
| 65 | + ); |
| 66 | + assistant.chainIds = [1, 2]; |
| 67 | + expect(validateXMbSpec(spec)).toBeUndefined(); |
| 68 | + |
| 69 | + assistant.categories = null; |
| 70 | + expect(() => validateXMbSpec(spec)).toThrow("categories must be an array"); |
| 71 | + assistant.categories = [null]; |
| 72 | + expect(() => validateXMbSpec(spec)).toThrow( |
| 73 | + "categories must be an array of strings", |
| 74 | + ); |
| 75 | + assistant.categories = ["ethereum", "solana"]; |
| 76 | + expect(validateXMbSpec(spec)).toBeUndefined(); |
| 77 | + |
| 78 | + assistant.version = null; |
| 79 | + expect(() => validateXMbSpec(spec)).toThrow("version must be a string"); |
| 80 | + assistant.version = "1.0.0"; |
| 81 | + expect(validateXMbSpec(spec)).toBeUndefined(); |
| 82 | + |
| 83 | + assistant.repo = null; |
| 84 | + expect(() => validateXMbSpec(spec)).toThrow("repo must be a string"); |
| 85 | + |
| 86 | + assistant.repo = "https://github.com/example/repo"; |
| 87 | + expect(validateXMbSpec(spec)).toBeUndefined(); |
| 88 | + |
| 89 | + spec.email = 123; |
| 90 | + expect(() => validateXMbSpec(spec)).toThrow("email must be a string"); |
| 91 | + |
| 92 | + |
| 93 | + expect(validateXMbSpec(spec)).toBeUndefined(); |
| 94 | + |
| 95 | + // Full Example Spec: |
| 96 | + expect(spec).toStrictEqual({ |
| 97 | + assistant: { |
| 98 | + name: "assistantName", |
| 99 | + description: "assistantDescription", |
| 100 | + instructions: "assistantInstructions", |
| 101 | + tools: [{ type: "function" }], |
| 102 | + chainIds: [1, 2], |
| 103 | + categories: ["ethereum", "solana"], |
| 104 | + version: "1.0.0", |
| 105 | + repo: "https://github.com/example/repo", |
| 106 | + }, |
| 107 | + |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + it("validateXMbSpec real example (CoW Agent)", () => { |
| 112 | + const cowSpec = { |
| 113 | + "account-id": "max-normal.near", |
| 114 | + assistant: { |
| 115 | + name: "CoWSwap Assistant", |
| 116 | + description: |
| 117 | + "An assistant that generates EVM transaction data for CoW Protocol Interactions", |
| 118 | + instructions: |
| 119 | + "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.", |
| 120 | + tools: [ |
| 121 | + { |
| 122 | + type: "generate-evm-tx", |
| 123 | + }, |
| 124 | + ], |
| 125 | + image: "https://near-cow-agent.vercel.app/cowswap.svg", |
| 126 | + categories: ["defi"], |
| 127 | + chainIds: [1, 100, 8453, 42161, 11155111], |
| 128 | + }, |
| 129 | + image: "https://near-cow-agent.vercel.app/cowswap.svg", |
| 130 | + }; |
| 131 | + expect(validateXMbSpec(cowSpec)).toBeUndefined(); |
| 132 | + expect(isXMbSpec(cowSpec)).toBe(true); |
| 133 | + }); |
| 134 | +}); |
0 commit comments