|
1 | 1 | import { IEvaluationInput, IEvaluationResult } from './types' |
2 | 2 |
|
3 | | -// TODO: Replace with the actual AI evaluation algorithm once the external repo is integrated. |
4 | | -// The algorithm is described in the technical spec and currently takes ~30-40s per project |
5 | | -// at ~$0.15/project. Reference: https://github.com/... (link TBD). |
| 3 | +interface IApiResponseContent { |
| 4 | + onboard: boolean |
| 5 | + non_onboard_reason?: string |
| 6 | +} |
| 7 | + |
6 | 8 | export async function evaluateProject(input: IEvaluationInput): Promise<IEvaluationResult> { |
7 | | - console.error(`evaluateProject is not implemented yet for repo: ${input.repoUrl}`) |
8 | | - return { outcome: 'unsure', reason: 'evaluator not implemented' } |
| 9 | + const endpoint = process.env.CROWD_PROJECT_EVALUATION_API_ENDPOINT |
| 10 | + const userId = process.env.CROWD_PROJECT_EVALUATION_API_USER_ID |
| 11 | + const secret = process.env.CROWD_PROJECT_EVALUATION_API_SECRET |
| 12 | + |
| 13 | + if (!endpoint || !userId || !secret) { |
| 14 | + return { |
| 15 | + outcome: 'unsure', |
| 16 | + evaluationResult: 'error', |
| 17 | + evaluationReason: |
| 18 | + 'Missing API configuration: CROWD_PROJECT_EVALUATION_API_ENDPOINT, CROWD_PROJECT_EVALUATION_API_USER_ID, or CROWD_PROJECT_EVALUATION_API_SECRET', |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + const body = new URLSearchParams() |
| 23 | + body.append('message', JSON.stringify({ repo_url: input.repoUrl })) |
| 24 | + body.append('stream', 'false') |
| 25 | + body.append('user_id', userId) |
| 26 | + |
| 27 | + let response: Response |
| 28 | + try { |
| 29 | + response = await fetch(endpoint, { |
| 30 | + method: 'POST', |
| 31 | + headers: { |
| 32 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 33 | + Authorization: `Bearer ${secret}`, |
| 34 | + }, |
| 35 | + body, |
| 36 | + }) |
| 37 | + } catch (err) { |
| 38 | + const message = err instanceof Error ? err.message : String(err) |
| 39 | + return { |
| 40 | + outcome: 'unsure', |
| 41 | + evaluationResult: 'error', |
| 42 | + evaluationReason: `API request failed: ${message}`, |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if (!response.ok) { |
| 47 | + return { |
| 48 | + outcome: 'unsure', |
| 49 | + evaluationResult: 'error', |
| 50 | + evaluationReason: `API returned HTTP ${response.status}: ${response.statusText}`, |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + let responseBody: unknown |
| 55 | + try { |
| 56 | + responseBody = await response.json() |
| 57 | + } catch (err) { |
| 58 | + const message = err instanceof Error ? err.message : String(err) |
| 59 | + return { |
| 60 | + outcome: 'unsure', |
| 61 | + evaluationResult: 'error', |
| 62 | + evaluationReason: `Failed to parse API response: ${message}`, |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + const content = (responseBody as { content?: unknown } | null)?.content |
| 67 | + if ( |
| 68 | + !content || |
| 69 | + typeof content !== 'object' || |
| 70 | + typeof (content as IApiResponseContent).onboard !== 'boolean' |
| 71 | + ) { |
| 72 | + return { |
| 73 | + outcome: 'unsure', |
| 74 | + evaluationResult: 'error', |
| 75 | + evaluationReason: `Unexpected API response shape: ${JSON.stringify(responseBody)}`, |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + const { onboard, non_onboard_reason } = content as IApiResponseContent |
| 80 | + |
| 81 | + return { |
| 82 | + outcome: onboard ? 'onboard' : 'skip', |
| 83 | + evaluationResult: String(onboard), |
| 84 | + evaluationReason: non_onboard_reason ?? null, |
| 85 | + } |
9 | 86 | } |
0 commit comments