Repository navigation
Conversation
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.
Owner
|
proxy settings are ignored, split diffs can return empty output, and commonjs types fail. please fix these before merging. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 #540
Problem
OpenCommit can only be used as a CLI.
package.jsonpointsmainat acli.jsfile that does not exist, and the prompt modules read~/.opencommitand.envwhen they are imported, so the core cannot be called from other code with its own settings.Changes
New library entry
src/index.ts, built toout/index.cjswith a generatedout/index.d.ts.main,typesandfilesinpackage.jsonpoint at them.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,.envorOCO_*env vars, does not prompt, and throws provider errors to the caller. Unknown providers, unsupported languages and empty diffs throw. It always uses theconventional-commitprompt module, because@commitlintneeds the interactive setup.generateCommitMessageFromGitDiff.ts: the generation logic moves intogenerateCommitMessageWithConfig(diff, config, ...), which passes the config to the prompt builders and togetEngine(config).generateCommitMessageByDiffkeeps its signature. It resolvesgetConfig()and keeps the interactive model-not-found recovery, so the CLI and GitHub Action behave as before.prompts.tsandmodules/commitlint/*: these modules no longer callgetConfig()at import time. They take the config as a parameter.getMainCommitPromptstill defaults togetConfig().esbuild.config.jsbuilds the library bundle and emits its declarations withts.transpileDeclaration. The public types live insrc/index.tsso 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.tsruns the real OpenAI engine against a local HTTP server on127.0.0.1. It checks that the API key, model, emoji, language and context in the passed config reach the request, thatOCO_*env vars are ignored, that@commitlintis 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:checknpm run buildnpm run test:unit: 23 suites, 219 testsnpm run test:e2e: smoke, core (6 suites, 39 tests) and prompt-module (4 tests). These run against the rebuiltout/cli.cjs.Also checked by hand:
out/index.cjsfrom a temp directory with an emptyHOMEand a.envfile, usingOCO_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.envand did not write any files toHOME. ESMimportworked too..mtsand.ctsfile against the generatedout/index.d.tswithmoduleResolution: nodenext.npm pack --dry-runlistsout/index.cjs,out/index.d.ts,out/cli.cjsandout/tiktoken_bg.wasm.