• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hicommonwealth / commonwealth / 14932508016

09 May 2025 03:37PM UTC coverage: 42.784% (-0.2%) from 42.961%
14932508016

Pull #12084

github

web-flow
Merge 2ac89a0f9 into 04d9e075c
Pull Request #12084: Converted demo to follow master

1684 of 4345 branches covered (38.76%)

Branch coverage included in aggregate %.

3053 of 6727 relevant lines covered (45.38%)

37.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

27.5
/libs/model/src/services/openai/parseBotCommand.ts
1
import { OpenAI } from 'openai';
2
import {
3
  ChatCompletionMessage,
4
  ChatCompletionTool,
5
} from 'openai/resources/index.mjs';
6
import { config } from '../../config';
7

8
export const USDC_BASE_MAINNET_ADDRESS =
9
  '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913';
34✔
10
export const USDC_BASE_SEPOLIA_ADDRESS =
11
  '0x5dEaC602762362FE5f135FA5904351916053cF70';
34✔
12

13
export const DEFAULT_CONTEST_BOT_PARAMS = {
34✔
14
  payoutStructure: [50, 30, 20],
15
  voterShare: 10,
16
  image_url:
17
    'https://assets.commonwealth.im/42b9d2d9-79b8-473d-b404-b4e819328ded.png',
18
};
19

20
export type ContestMetadataResponse = {
21
  contestName: string;
22
  payoutStructure: number[];
23
  voterShare: number;
24
  image_url: string;
25
  tokenAddress: string;
26
  isUSDC: boolean;
27
};
28

29
const system_prompt: ChatCompletionMessage = {
34✔
30
  role: 'assistant',
31
  content: `
32
    You are a data extraction system that extracts information from the user.
33

34
    The user will ask to "launch", "start", "create", etc. That means they want to launch a contest.
35

36
    Extract the name of the contest as well as the token address from the user prompt.
37
    contestName is the name of the contest.
38
    tokenAddress is the ethereum address of the funding token.
39
    isUSDC should be set to true if the user shows intent to launch with the USDC token.
40

41
    Example: "Hey @contestbot, create a Big Donut with 0xc204af95b0307162118f7bc36a91c9717490ab69"
42
    Expected Output:
43
    {
44
      contestName: "Big Donut",
45
      tokenAddress: "0xc204af95b0307162118f7bc36a91c9717490ab69"
46
    }
47

48
    Example: "@commonbot, ignite the engine! Happy Monday funded via 0x833589fcd6edb6e08f4c7c32d4f71b54bda02913"
49
    Expected Output:
50
    {
51
      contestName: "Happy Monday",
52
      tokenAddress: "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913"
53
    }
54

55
    Example: "@commonbot, create a Good Dogs Only dog battle. Use 0x0555E30da8f98308EdB960aa94C0Db47230d2B9c."
56
    Expected Output:
57
    {
58
      contestName: "Good Dogs Only",
59
      tokenAddress: "0x0555E30da8f98308EdB960aa94C0Db47230d2B9c"
60
    }
61

62
    Example: "@commonbot, rev up the meme machine! Launch 0xcbb7c0000ab88b473b1f5afd9ef808440eed33bf."
63
    Expected Output:
64
    {
65
      contestName: "meme machine",
66
      tokenAddress: "0xcbb7c0000ab88b473b1f5afd9ef808440eed33bf"
67
    }
68

69
    Example: "@contestbot, let's go! Diamond Handz funded by 0x820c137fa70c8691f0e44dc420a5e53c168921dc"
70
    Expected Output:
71
    {
72
      contestName: "Diamond Handz",
73
      tokenAddress: "0x820c137fa70c8691f0e44dc420a5e53c168921dc"
74
    }
75

76
    Example: "@contestbot, launch Base Bangers on USDC"
77
    Expected Output:
78
    {
79
      contestName: "Diamond Handz",
80
      isUSDC: true
81
    }
82
    `,
83
  refusal: null,
84
};
85

86
const tools: ChatCompletionTool[] = [
34✔
87
  {
88
    type: 'function',
89
    function: {
90
      name: 'parse_data',
91
      parameters: {
92
        type: 'object',
93
        properties: {
94
          contestName: { type: 'string' },
95
          tokenAddress: { type: 'string' },
96
          isUSDC: { type: 'boolean' },
97
        },
98
      },
99
    },
100
  },
101
];
102

103
// Custom error type that returns a human-readable error intended for end users
104
export class ParseBotCommandError extends Error {
105
  static ERRORS = {
34✔
106
    InvalidParams:
107
      'Failed to create contest. Specify all contest name and token address.',
108
  } as const;
109

110
  constructor(message: keyof typeof ParseBotCommandError.ERRORS) {
111
    super(message);
×
112
    this.name = this.constructor.name;
×
113

114
    if (Error.captureStackTrace) {
×
115
      Error.captureStackTrace(this, this.constructor);
×
116
    }
117
  }
118

119
  getPrettyError(): string {
120
    return (
×
121
      ParseBotCommandError.ERRORS[
×
122
        this.message as keyof typeof ParseBotCommandError.ERRORS
123
      ] || 'An unknown error occurred.'
124
    );
125
  }
126
}
127

128
export const getContestUSDCAddress = () => {
34✔
129
  if (config.APP_ENV === 'production') {
2!
130
    return USDC_BASE_MAINNET_ADDRESS;
×
131
  }
132
  return USDC_BASE_SEPOLIA_ADDRESS;
2✔
133
};
134

135
export const parseBotCommand = async (
34✔
136
  command: string,
137
): Promise<ContestMetadataResponse> => {
138
  const openai = new OpenAI({
×
139
    organization: config.OPENAI.ORGANIZATION,
140
    apiKey: config.OPENAI.API_KEY,
141
  });
142

143
  const response = await openai.chat.completions.create({
×
144
    model: 'o3-mini',
145
    messages: [
146
      system_prompt,
147
      {
148
        role: 'user',
149
        content: command,
150
      },
151
    ],
152
    tools,
153
  });
154

155
  let data = null;
×
156
  try {
×
157
    data = JSON.parse(
×
158
      response.choices[0].message.tool_calls![0].function.arguments,
159
    );
160
  } catch (err) {
161
    throw new ParseBotCommandError('InvalidParams');
×
162
  }
163

164
  if (!data.contestName || !(data.tokenAddress || data.isUSDC)) {
×
165
    throw new ParseBotCommandError('InvalidParams');
×
166
  }
167

168
  const tokenAddress = data.isUSDC
×
169
    ? getContestUSDCAddress()
170
    : data.tokenAddress;
171

172
  return {
×
173
    contestName: data.contestName,
174
    tokenAddress,
175
    ...DEFAULT_CONTEST_BOT_PARAMS,
176
    isUSDC: data.isUSDC,
177
  };
178
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc