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

hicommonwealth / commonwealth / 13203531373

07 Feb 2025 04:05PM UTC coverage: 46.356% (-0.08%) from 46.438%
13203531373

push

github

web-flow
Merge pull request #10777 from hicommonwealth/kaleemNeslit_onboarding

Kaleem neslit onboarding

1365 of 3289 branches covered (41.5%)

Branch coverage included in aggregate %.

2617 of 5301 relevant lines covered (49.37%)

36.63 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 DEFAULT_CONTEST_BOT_PARAMS = {
9
  payoutStructure: [50, 30, 20],
29✔
10
  voterShare: 10,
11
  image_url:
29✔
12
    'https://assets.commonwealth.im/42b9d2d9-79b8-473d-b404-b4e819328ded.png',
13
};
29✔
14

15
export type ContestMetadataResponse = {
16
  contestName: string;
17
  payoutStructure: number[];
18
  voterShare: number;
19
  image_url: string;
20
  tokenAddress: string;
21
};
22

23
const system_prompt: ChatCompletionMessage = {
24
  role: 'assistant',
25
  content: `
26
    You are a data extraction system that extracts information from the user.
27

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

29✔
30
    Extract the name of the contest as well as the token address from the user prompt.
31
    contestName is the name of the contest.
32
    tokenAddress is the ethereum address of the funding token.
33

34
    Example: "Hey @contestbot, create a Big Donut with 0xc204af95b0307162118f7bc36a91c9717490ab69"
35
    Expected Output:
36
    {
37
      contestName: "Big Donut",
38
      tokenAddress: "0xc204af95b0307162118f7bc36a91c9717490ab69"
39
    }
40

41
    Example: "@commonbot, ignite the engine! Happy Monday funded via 0x833589fcd6edb6e08f4c7c32d4f71b54bda02913"
42
    Expected Output:
43
    {
44
      contestName: "Happy Monday",
45
      tokenAddress: "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913"
46
    }
47

48
    Example: "@commonbot, create a Good Dogs Only dog battle. Use 0x0555E30da8f98308EdB960aa94C0Db47230d2B9c."
49
    Expected Output:
50
    {
51
      contestName: "Good Dogs Only",
52
      tokenAddress: "0x0555E30da8f98308EdB960aa94C0Db47230d2B9c"
53
    }
54

55
    Example: "@commonbot, rev up the meme machine! Launch 0xcbb7c0000ab88b473b1f5afd9ef808440eed33bf."
56
    Expected Output:
57
    {
58
      contestName: "meme machine",
59
      tokenAddress: "0xcbb7c0000ab88b473b1f5afd9ef808440eed33bf"
60
    }
61

62
    Example: "@contestbot, let's go! Diamond Handz funded by 0x820c137fa70c8691f0e44dc420a5e53c168921dc"
63
    Expected Output:
64
    {
65
      contestName: "Diamond Handz",
66
      tokenAddress: "0x820c137fa70c8691f0e44dc420a5e53c168921dc"
67
    }
68
    `,
69
};
70

71
const tools: ChatCompletionTool[] = [
72
  {
73
    type: 'function',
74
    function: {
75
      name: 'parse_data',
76
      parameters: {
77
        type: 'object',
78
        properties: {
79
          contestName: { type: 'string' },
80
          tokenAddress: { type: 'string' },
81
        },
82
      },
83
    },
84
  },
85
];
29✔
86

87
// Custom error type that returns a human-readable error intended for end users
88
export class ParseBotCommandError extends Error {
89
  static ERRORS = {
90
    InvalidParams:
91
      'Failed to create contest. Specify all contest name and token address.',
92
  } as const;
93

94
  constructor(message: keyof typeof ParseBotCommandError.ERRORS) {
95
    super(message);
96
    this.name = this.constructor.name;
97

98
    if (Error.captureStackTrace) {
99
      Error.captureStackTrace(this, this.constructor);
100
    }
101
  }
102

103
  getPrettyError(): string {
104
    return (
29✔
105
      ParseBotCommandError.ERRORS[
106
        this.message as keyof typeof ParseBotCommandError.ERRORS
107
      ] || 'An unknown error occurred.'
108
    );
109
  }
110
}
×
111

×
112
export const parseBotCommand = async (
113
  command: string,
×
114
): Promise<ContestMetadataResponse> => {
×
115
  const openai = new OpenAI({
116
    organization: config.OPENAI.ORGANIZATION,
117
    apiKey: config.OPENAI.API_KEY,
118
  });
119

×
120
  const response = await openai.chat.completions.create({
×
121
    model: 'o3-mini',
122
    messages: [
123
      system_prompt,
124
      {
125
        role: 'user',
126
        content: command,
127
      },
29✔
128
    ],
2!
129
    tools,
×
130
  });
131

2✔
132
  let data = null;
133
  try {
134
    data = JSON.parse(
29✔
135
      response.choices[0].message.tool_calls![0].function.arguments,
136
    );
137
  } catch (err) {
×
138
    throw new ParseBotCommandError('InvalidParams');
139
  }
140

141
  if (!data.contestName || !data.tokenAddress) {
142
    throw new ParseBotCommandError('InvalidParams');
×
143
  }
144

145
  return {
146
    contestName: data.contestName,
147
    tokenAddress: data.tokenAddress,
148
    ...DEFAULT_CONTEST_BOT_PARAMS,
149
  };
150
};
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