File size: 1,511 Bytes
b5e4648
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { tool } from "langchain";
import { z } from "zod";
import solc from "solc";

/**
 * Tool que compila c贸digo Solidity usando solc e retorna erros/warnings.
 */
export const compileSolidityTool = tool(
  async (input) => {
    const compilerInput = {
      language: "Solidity",
      sources: {
        [input.filename]: { content: input.sourceCode },
      },
      settings: {
        outputSelection: {
          "*": { "*": ["abi", "evm.bytecode.object"] },
        },
      },
    };

    const output = JSON.parse(solc.compile(JSON.stringify(compilerInput)));

    const errors = (output.errors || [])
      .filter((e: { severity: string }) => e.severity === "error")
      .map((e: { formattedMessage: string }) => e.formattedMessage);

    const warnings = (output.errors || [])
      .filter((e: { severity: string }) => e.severity === "warning")
      .map((e: { formattedMessage: string }) => e.formattedMessage);

    const contracts = output.contracts?.[input.filename] || {};
    const contractNames = Object.keys(contracts);

    return {
      success: errors.length === 0,
      errors,
      warnings,
      contracts: contractNames,
    };
  },
  {
    name: "compilar_solidity",
    description: "Compila c贸digo Solidity com solc e retorna erros, warnings e contratos encontrados.",
    schema: z.object({
      sourceCode: z.string().describe("C贸digo-fonte Solidity completo"),
      filename: z.string().default("Contract.sol").describe("Nome do arquivo .sol"),
    }),
  },
);