Skip to content

fix(api): resolve TypeError in chat/completions route - #41

Merged
diegosouzapw merged 1 commit into
mainfrom
fix/chat-completions-then-error
Feb 15, 2026
Merged

diegosouzapw merged 1 commit into
mainfrom
fix/chat-completions-then-error

Conversation

@diegosouzapw

Copy link
Copy Markdown
Owner

Bug: Cannot read properties of undefined (reading 'then') in every chat completion request.

Root cause: initTranslators() in open-sse/translator/index.js is a no-op stub returning undefined (translators self-register via static imports). The route called .then() on it.

Fix: Wrapped with Promise.resolve() so .then() works regardless of return type.

initTranslators() is a no-op stub (translators self-register via static
imports). Wrapping the call with Promise.resolve() ensures .then() works
even when the function returns undefined instead of a Promise.
Copilot AI review requested due to automatic review settings February 15, 2026 16:25
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@github-actions

Copy link
Copy Markdown
Contributor

@codex review

@diegosouzapw
diegosouzapw merged commit 404befd into main Feb 15, 2026
3 checks passed
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @diegosouzapw, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical TypeError that prevented chat completion requests from functioning correctly. The issue stemmed from an asynchronous initialization function returning an undefined value, which subsequently caused a runtime error when attempting to chain a .then() call. The fix ensures robust handling of the initialization process by explicitly converting the return value into a resolved Promise, thereby stabilizing the chat completion API.

Highlights

  • Bug Fix: Resolved a TypeError: Cannot read properties of undefined (reading 'then') that occurred in every chat completion request.
  • Root Cause Identified: The initTranslators() function in open-sse/translator/index.js was a no-op stub returning undefined, and the chat completion route attempted to call .then() on this undefined value.
  • Solution Implemented: Wrapped the call to initTranslators() with Promise.resolve() to ensure that the result is always a Promise-like object, allowing .then() to be called safely regardless of the original return type.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/app/api/v1/chat/completions/route.js
    • Wrapped initTranslators() with Promise.resolve() to prevent TypeError.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@diegosouzapw
diegosouzapw deleted the fix/chat-completions-then-error branch February 15, 2026 16:25
@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly resolves a TypeError that occurred when initializing translators. The fix wraps the call to initTranslators() in Promise.resolve() to ensure a promise is always returned. My review includes a suggestion to use an async IIFE, which would make the initialization logic even more robust by caching any potential initialization failures, improving the overall reliability of the singleton initialization pattern.

Comment on lines +12 to 14
initPromise = Promise.resolve(initTranslators()).then(() => {
console.log("[SSE] Translators initialized");
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While Promise.resolve() correctly fixes the TypeError, using an async IIFE (Immediately Invoked Function Expression) would make the logic slightly more robust, especially regarding error handling. If initTranslators() were to throw an error, this approach would cache the resulting rejected promise, preventing repeated failing attempts on subsequent calls. This improves predictability and aligns well with the singleton pattern you're using for initialization.

    initPromise = (async () => {
      await initTranslators();
      console.log("[SSE] Translators initialized");
    })();

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a TypeError that occurred in every chat completion request to /v1/chat/completions. The bug was caused by calling .then() on the return value of initTranslators(), which is a no-op stub function returning undefined (translators self-register via static imports at module load time). The fix wraps the call with Promise.resolve() to ensure it always returns a Promise that can be chained with .then().

Changes:

  • Wrapped initTranslators() call with Promise.resolve() in the ensureInitialized function to handle the undefined return value

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

diegosouzapw added a commit that referenced this pull request Mar 10, 2026
TLS / Certificate Validation (#50)
- src/mitm/server.ts: rejectUnauthorized now defaults to true
  (opt-out via MITM_DISABLE_TLS_VERIFY=1 env var only)

Path Injection (#44, #41-#49)
- src/mitm/server.ts: safeLogPath() guards log filenames inside LOG_DIR
- src/lib/db/backup.ts: path.resolve() anchor + sep/slash guard on backupId
- src/shared/services/backupService.ts: safePath() helper anchors toolId
  and backupId within BACKUP_DIR (prevents path traversal)
- src/app/api/cli-tools/codex-profiles/route.ts: safeProfilePath() helper
  anchors profileId within PROFILES_DIR

Prototype Pollution (#18, #19, #20)
- src/lib/usage/usageHistory.ts: byModel/byAccount initialized with
  Object.create(null); assignments guarded with hasOwnProperty checks

Missing Workflow Permissions (#1-#8, #53)
- .github/workflows/ci.yml: added global permissions: contents: read

Dependabot CVE-2026-0540 (#16)
- dompurify updated to ^3.3.2 (fixes XSS vulnerability)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants