TypeChat icon indicating copy to clipboard operation
TypeChat copied to clipboard

added to typechat.js

Open Saunakghosh10 opened this issue 2 years ago • 4 comments
trafficstars

Added type declarations for the callback functions createRequestPrompt and createRepairPrompt:

createRequestPrompt: (request: string) => string; createRepairPrompt: (validationError: string) => string; Use more descriptive names for some variables:

request -> naturalLanguageRequest responseText -> aiResponseText jsonText -> parsedJsonText Add some validation around the JSON parsing:

const startIndex = aiResponseText.indexOf("{"); const endIndex = aiResponseText.lastIndexOf("}"); if (startIndex == -1 || endIndex == -1 || startIndex >= endIndex) { // invalid JSON, return error } Changed translate() to be async instead of using promises directly.

Saunakghosh10 avatar Jul 22 '23 13:07 Saunakghosh10

Thanks for the PR, but I'm having a hard time reading the diff and understanding the intent of the change. Could you elaborate a little bit on what you were trying to improve here?

DanielRosenwasser avatar Jul 24 '23 18:07 DanielRosenwasser

  1. Type Declarations for Callback Functions: The type declarations for the callback functions createRequestPrompt and createRepairPrompt have been added to provide better type safety and clarity. This ensures that the callback functions accept the correct types of parameters and return the expected types of values.

  2. More Descriptive Variable Names: Some variable names have been changed to be more descriptive, making the code easier to understand. For example:

    • request has been renamed to naturalLanguageRequest to indicate that it represents a natural language user request.
    • responseText has been renamed to aiResponseText to clarify that it is the text received from the AI language model response.
    • jsonText has been renamed to parsedJsonText to highlight that it represents the JSON text extracted from the AI response and successfully parsed.
  3. Additional Validation for JSON Parsing: To ensure that the JSON parsing process is more robust, the code now includes validation checks before extracting the JSON text from the AI response. Specifically, it checks if the opening and closing curly braces are present in the aiResponseText, and if they are not, the code returns an error to handle the situation of invalid JSON.

  4. translate() Function Now Uses Async/Await: Instead of using Promises directly, the translate() function has been updated to use async/await syntax. This change improves the readability of asynchronous code and allows for better error handling within the function.

Overall, these changes aim to enhance the code's readability, maintainability, and error handling when working with JSON parsing and AI responses. The functionality of translating natural language requests into JSON objects remains unchanged.

Saunakghosh10 avatar Jul 27 '23 09:07 Saunakghosh10

@Saunakghosh10 - perhaps if you restore the export keyword it may improve the diff?

danmarshall avatar Jul 27 '23 17:07 danmarshall

The type declarations for the callback functions createRequestPrompt and createRepairPrompt have been added to provide better type safety and clarity.

I'm not certain I see how that was done. These were always well-typed, it just looks like the original types lost their JSDoc.

Some variable names have been changed to be more descriptive, making the code easier to understand. For example

I can kind of see this, though it's a bit subjective.

If you are looking to make some changes, I would suggest making them at a much smaller level so that we can more easily evaluate the changes. If you'd be open to it, we'd consider a PR starting with the last 2 changes - additional JSON validation and the async version of the translate function. I can't guarantee that we'll accept the changes, but it will be easier for us to judge the diff and decide if we like it better.

DanielRosenwasser avatar Jul 27 '23 18:07 DanielRosenwasser