Skip to content

Commit e6453cb

Browse files
refactor: improve gemini client and thought signature extraction
- Remove redundant comments from gemini-client.ts. - Replace `any` with `ExtendedGenerateContentRequest` intersection type in `generateContentWithFallback`. - Simplify thought signature extraction with optional chaining. - Secure `NEW_THOUGHT_SIG` extraction in `reusable-gemini-review.yml` by checking against `"null"`. - Refactor regex for `THOUGHT_SIG` in `decide-review-strategy.sh` to use `\K` for more robust matching. - Update `identify-tech-debt.ts` to destructure the updated return type of `generateContentWithFallback`. Co-authored-by: arii <342438+arii@users.noreply.github.com>
1 parent a588e5c commit e6453cb

4 files changed

Lines changed: 9 additions & 19 deletions

File tree

.github/workflows/reusable-gemini-review.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,9 @@ jobs:
433433
FOOTER="\n\n---\n#### 🤖 [Gemini Manual Trigger Guide](${{ github.server_url }}/${{ github.repository }}/blob/leader/docs/workflows/MANUAL_TRIGGERS.md)"
434434
435435
# Extract the new thought signature if present
436-
NEW_THOUGHT_SIG=$(jq -r '.thoughtSignature // empty' review_result.json)
436+
NEW_THOUGHT_SIG=$(jq -r '.thoughtSignature // ""' review_result.json)
437437
SIG_MARKER=""
438-
if [ -n "$NEW_THOUGHT_SIG" ]; then
438+
if [[ -n "$NEW_THOUGHT_SIG" && "$NEW_THOUGHT_SIG" != "null" ]]; then
439439
SIG_MARKER="\n\n<!-- thought_signature: ${NEW_THOUGHT_SIG} -->"
440440
fi
441441

scripts/decide-review-strategy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ else
132132
SKIP_REASON=""
133133
else
134134
# Extract thought signature if present
135-
THOUGHT_SIG=$(echo "$LAST_COMMENT_BODY" | grep -oP '(?<=<!-- thought_signature: )[a-zA-Z0-9+/=]+(?= -->)' | head -n 1 || echo "")
135+
THOUGHT_SIG=$(echo "$LAST_COMMENT_BODY" | grep -oP '<!-- thought_signature: \K[a-zA-Z0-9+/=]+(?= -->)' | head -n 1)
136136
if [ -n "$THOUGHT_SIG" ]; then
137137
echo "::info::Extracted thought signature from last review."
138138
echo "thought-signature=$THOUGHT_SIG" >> "$GITHUB_OUTPUT"

scripts/gemini-client.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ const outputFile = getArg('--output')
2525
const preset = getArg('--preset')
2626
const instructions = getArg('--instructions')
2727

28-
// List of models to try in order.
29-
// The first model in the list is the primary model, and the rest are fallbacks.
30-
31-
// UPDATED: Aligned with latest model recommendations (Q1 2026+)
3228
const defaultFallbacks = [
3329
'gemini-3.1-flash-lite-preview',
3430
'gemini-2.5-flash',
@@ -314,6 +310,8 @@ async function main() {
314310
}
315311
}
316312

313+
type ExtendedGenerateContentRequest = GenerateContentRequest & { thought_signature?: string };
314+
317315
export async function generateContentWithFallback({
318316
genAI,
319317
prompt,
@@ -330,14 +328,10 @@ export async function generateContentWithFallback({
330328
for (const modelName of MODEL_FALLBACKS) {
331329
try {
332330
const model = genAI.getGenerativeModel({ model: modelName })
333-
const request: any = {
331+
const request: ExtendedGenerateContentRequest = {
334332
contents: [{ role: 'user', parts: [{ text: prompt }] }],
335333
...config,
336-
}
337-
338-
// Implement thought signature circulation if provided
339-
if (thoughtSignature) {
340-
request.thought_signature = thoughtSignature
334+
...(thoughtSignature && { thought_signature: thoughtSignature })
341335
}
342336

343337
const result = await model.generateContent(request)
@@ -346,11 +340,7 @@ export async function generateContentWithFallback({
346340
const text = result.response.text()
347341

348342
// Capture thought signature from the response if present
349-
const candidate = result.response.candidates?.[0]
350-
const capturedSignature =
351-
candidate && 'thought_signature' in candidate
352-
? (candidate as any).thought_signature
353-
: undefined
343+
const capturedSignature = (result.response.candidates?.[0] as any)?.thought_signature
354344

355345
return { text, thoughtSignature: capturedSignature }
356346
} catch (error: unknown) {

scripts/identify-tech-debt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export async function main() {
9393

9494
const prompt = promptTemplate.replace('{{diff}}', processedDiff)
9595

96-
const rawResponse = await generateContentWithFallback({
96+
const { text: rawResponse } = await generateContentWithFallback({
9797
genAI,
9898
prompt,
9999
config: {

0 commit comments

Comments
 (0)