|
1 | 1 | import * as core from '@actions/core'
|
2 | 2 | import * as github from '@actions/github'
|
| 3 | +import {get_encoding} from '@dqbd/tiktoken' |
3 | 4 | import {Octokit} from '@octokit/action'
|
4 | 5 | import {Bot} from './bot.js'
|
5 | 6 | import {Commenter} from './commenter.js'
|
6 | 7 | import {Inputs, Options, Prompts} from './options.js'
|
7 | 8 |
|
| 9 | +// TODO: make this configurable |
| 10 | +const tokenizer = get_encoding('cl100k_base') |
| 11 | + |
8 | 12 | const token = core.getInput('token')
|
9 | 13 | ? core.getInput('token')
|
10 | 14 | : process.env.GITHUB_TOKEN
|
11 | 15 | const octokit = new Octokit({auth: `token ${token}`})
|
12 | 16 | const context = github.context
|
13 | 17 | const repo = context.repo
|
14 | 18 |
|
| 19 | +const MAX_TOKENS_FOR_EXTRA_CONTENT = 2500 |
| 20 | + |
15 | 21 | export const codeReview = async (
|
16 | 22 | bot: Bot,
|
17 | 23 | options: Options,
|
@@ -127,40 +133,54 @@ export const codeReview = async (
|
127 | 133 | // reset chat session for each file while reviewing
|
128 | 134 | next_review_ids = review_begin_ids
|
129 | 135 |
|
130 |
| - if (file_content.length > 0 && file_content.length < 3000) { |
131 |
| - // review file |
132 |
| - const [resp, review_file_ids] = await bot.chat( |
133 |
| - prompts.render_review_file(inputs), |
134 |
| - next_review_ids |
135 |
| - ) |
136 |
| - if (!resp) { |
137 |
| - core.info('review: nothing obtained from chatgpt') |
| 136 | + if (file_content.length > 0) { |
| 137 | + const file_content_tokens = await get_token_count(file_content) |
| 138 | + if (file_content_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) { |
| 139 | + // review file |
| 140 | + const [resp, review_file_ids] = await bot.chat( |
| 141 | + prompts.render_review_file(inputs), |
| 142 | + next_review_ids |
| 143 | + ) |
| 144 | + if (!resp) { |
| 145 | + core.info('review: nothing obtained from chatgpt') |
| 146 | + } else { |
| 147 | + next_review_ids = review_file_ids |
| 148 | + } |
138 | 149 | } else {
|
139 |
| - next_review_ids = review_file_ids |
| 150 | + core.info( |
| 151 | + `skip sending content of file: ${inputs.filename} due to token count: ${file_content_tokens}` |
| 152 | + ) |
140 | 153 | }
|
141 | 154 | }
|
142 | 155 |
|
143 |
| - if (file_diff.length > 0 && file_diff.length < 3000) { |
144 |
| - // review diff |
145 |
| - const [resp, review_diff_ids] = await bot.chat( |
146 |
| - prompts.render_review_file_diff(inputs), |
147 |
| - next_review_ids |
148 |
| - ) |
149 |
| - if (!resp) { |
150 |
| - core.info('review: nothing obtained from chatgpt') |
151 |
| - } else { |
152 |
| - next_review_ids = review_diff_ids |
153 |
| - } |
| 156 | + if (file_diff.length > 0) { |
| 157 | + const file_diff_tokens = await get_token_count(file_diff) |
| 158 | + if (file_diff_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) { |
| 159 | + // review diff |
| 160 | + const [resp, review_diff_ids] = await bot.chat( |
| 161 | + prompts.render_review_file_diff(inputs), |
| 162 | + next_review_ids |
| 163 | + ) |
| 164 | + if (!resp) { |
| 165 | + core.info('review: nothing obtained from chatgpt') |
| 166 | + } else { |
| 167 | + next_review_ids = review_diff_ids |
| 168 | + } |
154 | 169 |
|
155 |
| - // summarize diff |
156 |
| - const [summarize_resp, summarize_diff_ids] = await bot.chat( |
157 |
| - prompts.render_summarize_file_diff(inputs), |
158 |
| - next_summarize_ids |
159 |
| - ) |
160 |
| - if (!summarize_resp) { |
161 |
| - core.info('summarize: nothing obtained from chatgpt') |
| 170 | + // summarize diff |
| 171 | + const [summarize_resp, summarize_diff_ids] = await bot.chat( |
| 172 | + prompts.render_summarize_file_diff(inputs), |
| 173 | + next_summarize_ids |
| 174 | + ) |
| 175 | + if (!summarize_resp) { |
| 176 | + core.info('summarize: nothing obtained from chatgpt') |
| 177 | + } else { |
| 178 | + next_summarize_ids = summarize_diff_ids |
| 179 | + } |
162 | 180 | } else {
|
163 |
| - next_summarize_ids = summarize_diff_ids |
| 181 | + core.info( |
| 182 | + `skip sending diff of file: ${inputs.filename} due to token count: ${file_diff_tokens}` |
| 183 | + ) |
164 | 184 | }
|
165 | 185 | }
|
166 | 186 |
|
@@ -308,3 +328,8 @@ const patch_comment_line = (patch: string): number => {
|
308 | 328 | return -1
|
309 | 329 | }
|
310 | 330 | }
|
| 331 | + |
| 332 | +const get_token_count = async (text: string): Promise<number> => { |
| 333 | + text = text.replace(/<\|endoftext\|>/g, '') |
| 334 | + return tokenizer.encode(text).length |
| 335 | +} |
0 commit comments