|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import json |
| 17 | +import urllib.request |
| 18 | +import sys |
| 19 | + |
| 20 | +def get_gemini_response(api_key, prompt): |
| 21 | + url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key={api_key}" |
| 22 | + headers = {'Content-Type': 'application/json'} |
| 23 | + data = { |
| 24 | + "contents": [{ |
| 25 | + "parts": [{"text": prompt}] |
| 26 | + }] |
| 27 | + } |
| 28 | + |
| 29 | + req = urllib.request.Request(url, data=json.dumps(data).encode('utf-8'), headers=headers) |
| 30 | + try: |
| 31 | + with urllib.request.urlopen(req) as response: |
| 32 | + res_data = json.loads(response.read().decode('utf-8')) |
| 33 | + return res_data['candidates'][0]['content']['parts'][0]['text'] |
| 34 | + except urllib.error.HTTPError as e: |
| 35 | + print(f"Gemini API Error ({e.code}): {e.reason}", file=sys.stderr) |
| 36 | + try: |
| 37 | + error_body = e.read().decode('utf-8') |
| 38 | + print(f"Error details: {error_body}", file=sys.stderr) |
| 39 | + except: |
| 40 | + pass |
| 41 | + return None |
| 42 | + except Exception as e: |
| 43 | + print(f"Error calling Gemini API: {e}", file=sys.stderr) |
| 44 | + return None |
| 45 | + |
| 46 | +def main(): |
| 47 | + api_key = os.getenv("GEMINI_API_KEY") |
| 48 | + diff_file = os.getenv("DIFF_FILE", "release_diff.patch") |
| 49 | + skill_file = os.getenv("SKILL_FILE", ".gemini/skills/android-maps-compose/SKILL.md") |
| 50 | + |
| 51 | + if not api_key: |
| 52 | + print("GEMINI_API_KEY not found", file=sys.stderr) |
| 53 | + sys.exit(1) |
| 54 | + |
| 55 | + if not os.path.exists(diff_file): |
| 56 | + print(f"Diff file {diff_file} not found.", file=sys.stderr) |
| 57 | + sys.exit(1) |
| 58 | + |
| 59 | + if not os.path.exists(skill_file): |
| 60 | + print(f"Skill file {skill_file} not found.", file=sys.stderr) |
| 61 | + sys.exit(1) |
| 62 | + |
| 63 | + with open(diff_file, "r") as f: |
| 64 | + diff_content = f.read() |
| 65 | + |
| 66 | + with open(skill_file, "r") as f: |
| 67 | + skill_content = f.read() |
| 68 | + |
| 69 | + prompt = f""" |
| 70 | +You are an expert technical writer and Android developer. |
| 71 | +Your task is to update the Gemini CLI skill instructions for an SDK based on the latest release changes. |
| 72 | +
|
| 73 | +Here is the current `SKILL.md` file: |
| 74 | +```markdown |
| 75 | +{skill_content} |
| 76 | +``` |
| 77 | +
|
| 78 | +Here is the git diff representing the changes introduced in this release: |
| 79 | +```diff |
| 80 | +{diff_content} |
| 81 | +``` |
| 82 | +
|
| 83 | +Please analyze the diff to identify any new APIs, deprecated functions, structural changes, or changes in implementation best practices. |
| 84 | +Update the `SKILL.md` content to incorporate these new concepts or deprecations. |
| 85 | +
|
| 86 | +CRITICAL REQUIREMENTS: |
| 87 | +- Preserve the overall markdown structure and formatting of the existing `SKILL.md`. |
| 88 | +- Ensure you keep the YAML frontmatter intact at the top of the file (between `---`). |
| 89 | +- Do NOT remove the `// x-release-please-version` comments in the Gradle dependencies, as they are required by our release process. |
| 90 | +- Return ONLY the raw updated markdown content. Do NOT wrap it in ```markdown...``` code blocks, just return the exact file content so it can be directly saved. |
| 91 | +""" |
| 92 | + |
| 93 | + print("Requesting update from Gemini...", file=sys.stderr) |
| 94 | + response_text = get_gemini_response(api_key, prompt) |
| 95 | + if response_text: |
| 96 | + # Clean up response text if the model wrapped it in markdown code blocks despite instructions |
| 97 | + if response_text.startswith("```markdown"): |
| 98 | + response_text = response_text.replace("```markdown\n", "", 1) |
| 99 | + if response_text.endswith("```"): |
| 100 | + response_text = response_text[:-3] |
| 101 | + elif response_text.startswith("```"): |
| 102 | + response_text = response_text.replace("```\n", "", 1) |
| 103 | + if response_text.endswith("```"): |
| 104 | + response_text = response_text[:-3] |
| 105 | + |
| 106 | + # Trim whitespace at the very beginning or end |
| 107 | + response_text = response_text.strip() + "\n" |
| 108 | + |
| 109 | + with open(skill_file, "w") as f: |
| 110 | + f.write(response_text) |
| 111 | + print(f"Successfully updated {skill_file}", file=sys.stderr) |
| 112 | + else: |
| 113 | + sys.exit(1) |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + main() |
0 commit comments