Skip to content

feat: expose generateCommitMessage as a library API - #599

Open
Rutvij97 wants to merge 1 commit into
di-sukharev:masterfrom
Rutvij97:feat/library-api
Open

Rutvij97 wants to merge 1 commit into
di-sukharev:masterfrom
Rutvij97:feat/library-api

Conversation

@Rutvij97

Copy link
Copy Markdown

Closes #540

Problem

OpenCommit can only be used as a CLI. package.json points main at a cli.js file that does not exist, and the prompt modules read ~/.opencommit and .env when they are imported, so the core cannot be called from other code with its own settings.

Changes

  • New library entry src/index.ts, built to out/index.cjs with a generated out/index.d.ts. main, types and files in package.json point at them.

    import { generateCommitMessage } from 'opencommit';
    
    const message = await generateCommitMessage(diff, {
      config: { OCO_AI_PROVIDER: 'openai', OCO_API_KEY: key, OCO_EMOJI: true },
      context: 'optional, like oco --context',
      fullGitMojiSpec: false
    });

    The config keys are the existing OCO_* keys. Omitted keys get the CLI defaults (the model defaults per provider). The library does not read ~/.opencommit, .env or OCO_* env vars, does not prompt, and throws provider errors to the caller. Unknown providers, unsupported languages and empty diffs throw. It always uses the conventional-commit prompt module, because @commitlint needs the interactive setup.

  • generateCommitMessageFromGitDiff.ts: the generation logic moves into generateCommitMessageWithConfig(diff, config, ...), which passes the config to the prompt builders and to getEngine(config). generateCommitMessageByDiff keeps its signature. It resolves getConfig() and keeps the interactive model-not-found recovery, so the CLI and GitHub Action behave as before.

  • prompts.ts and modules/commitlint/*: these modules no longer call getConfig() at import time. They take the config as a parameter. getMainCommitPrompt still defaults to getConfig().

  • esbuild.config.js builds the library bundle and emits its declarations with ts.transpileDeclaration. The public types live in src/index.ts so the emit does not need to type-check the whole project.

  • README: new "Use OpenCommit as a library" section.

I did not regenerate the committed out/cli.cjs, because the release commits do that.

Testing

New test/unit/library.test.ts runs the real OpenAI engine against a local HTTP server on 127.0.0.1. It checks that the API key, model, emoji, language and context in the passed config reach the request, that OCO_* env vars are ignored, that @commitlint is not used, that a model-not-found error is rejected without prompting, and that invalid input is rejected.

Commands run locally on Node 22 / npm 10, all passing:

  • npm run lint (biome + tsc)
  • npm run format:check
  • npm run build
  • npm run test:unit: 23 suites, 219 tests
  • npm run test:e2e: smoke, core (6 suites, 39 tests) and prompt-module (4 tests). These run against the rebuilt out/cli.cjs.

Also checked by hand:

  • I required out/index.cjs from a temp directory with an empty HOME and a .env file, using OCO_AI_PROVIDER: 'test'. It generated messages for a small diff and for a 400-file diff (the split path, which uses the bundled tiktoken wasm). It did not load .env and did not write any files to HOME. ESM import worked too.
  • I type-checked a consumer .mts and .cts file against the generated out/index.d.ts with moduleResolution: nodenext.
  • npm pack --dry-run lists out/index.cjs, out/index.d.ts, out/cli.cjs and out/tiktoken_bg.wasm.

Add a library entry (out/index.cjs with out/index.d.ts) that generates
a commit message from a diff with an explicit config. The library does
not read ~/.opencommit, .env or OCO_* environment variables, and it does
not prompt.

Pass the resolved config through the prompt builders and engines instead
of reading it when the modules load. The CLI still resolves its config
with getConfig() and keeps its interactive model recovery.
@di-sukharev

Copy link
Copy Markdown
Owner

proxy settings are ignored, split diffs can return empty output, and commonjs types fail. please fix these before merging.

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.

[Feature]: expose programmatic API to consume as Library/ Module in my code

2 participants