refactor(openai): simplify OpenAI API client code #587
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #531
Applied changes based on Issue #531.
I’ve refactored src/api/openai.ts to improve readability, modularity, and maintain the exact same functionality:
What changed
Merged the two separate
openai
imports into one line using TS’stype
import.MAX_COMPLETION_TOKENS
andMAX_SUBJECT_LENGTH
replace magic numbers.CommitContext
interface for{ prNumber?, issueNumber? }
.buildUserMessage
: assembles the AI prompt (files, user request, PR/issue context).getFallbackMessage
: centralizes the fallback (“chore:” …) commit message logic.generateCommitMessage
ParseError
on invalid AI output to fall through to fallback handling.catch
returninggetFallbackMessage
.Removed the old verbose doc-block and updated comments to reflect the new structure.
These changes remove inline concatenations, collapse duplicated logic, and improve overall maintainability without altering any behavior. Feel free to let me know if you’d like further tweaks!
Diff highlights:
【F:src/api/openai.ts†L7-L14】【F:src/api/openai.ts†L36-L147】
Let me know if you’d like any further adjustments!