DEV Community

Ramu Narasinga
Ramu Narasinga

Posted on • Edited on • Originally published at thinkthroo.com

3

Generating a JSDoc comment for your function using OpenAI.

This article is inspired by Remotion’s internal package, ai-improvements. This internal package is used to improve/generate JSDoc comment and log in the console for a function in the Remotion’s source code.

This use case is quite different because you would normally use the source code and let OpenAI generate API documentation but instead in Remotion, this internal package uses the OpenAI API to generate JSDoc comment for a function using API documentation and the source code.

Image description

We will look at a high level how this package is configured:

  1. improve-docs.ts
  2. generate-jsdoc

Improve docs

import path from "path";
import { OpenAI } from "openai";
import { getApis } from "./map-over-api";
import { findMistakes } from "./tasks/find-mistakes";
import { generateJSDocTask } from "./tasks/generate-jsdoc";
Enter fullscreen mode Exit fullscreen mode

improve-docs.ts has the above imports. generateJSDocTask is imported from ‘tasks’ folder.

Image description

depending on the task, you could either generate a JSDoc or find mistakes.

Generate JSDoc

Image description

generate-jsdoc.ts uses the openai API and has prompts as shown in the above image.

Image description

Please note that this will only write the generated JSDoc to the console. Would it be possible to write these JSDoc comments programatically in the file? Not sure, but it is worth reading the AST documentation to see if there’s any API available to add JSDoc comments just above the function or class name. ts-morph is known to be used in shadcn-ui/ui to manipulate the source code at AST level.

Image description

About me:

Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.

I am open to work on an interesting project. Send me an email at ramu.narasinga@gmail.com

My Github - https://github.com/ramu-narasinga
My website - https://ramunarasinga.com
My Youtube channel - https://www.youtube.com/@thinkthroo
Learning platform - https://thinkthroo.com
Codebase Architecture - https://app.thinkthroo.com/architecture
Best practices - https://app.thinkthroo.com/best-practices
Production-grade projects - https://app.thinkthroo.com/production-grade-projects

References:

https://github.com/remotion-dev/remotion/tree/main/packages/ai-improvements

https://github.com/remotion-dev/remotion/blob/main/packages/ai-improvements/improve-docs.ts

https://github.com/remotion-dev/remotion/blob/main/packages/ai-improvements/tasks/generate-jsdoc.ts

https://github.com/remotion-dev/remotion/blob/main/packages/ai-improvements/tasks/find-mistakes.ts

https://github.com/remotion-dev/remotion/blob/main/packages/ai-improvements/map-over-api.ts

https://github.com/remotion-dev/remotion/blob/main/packages/docs/src/data/articles.ts

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay