@mcarvin/smart-diff 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +9 -0
- package/README.md +91 -0
- package/dist/index.cjs +652 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.min.cjs +2 -0
- package/dist/index.min.cjs.map +1 -0
- package/dist/index.min.mjs +2 -0
- package/dist/index.min.mjs.map +1 -0
- package/dist/index.mjs +631 -0
- package/dist/index.mjs.map +1 -0
- package/dist/typings/ai/aiSummary.d.ts +18 -0
- package/dist/typings/ai/openAIConfig.d.ts +21 -0
- package/dist/typings/git/gitDiff.d.ts +33 -0
- package/dist/typings/index.d.ts +24 -0
- package/dist/typings/src/ai/aiSummary.d.ts +19 -0
- package/dist/typings/src/ai/openAIConfig.d.ts +21 -0
- package/dist/typings/src/git/gitDiff.d.ts +33 -0
- package/dist/typings/src/index.d.ts +24 -0
- package/dist/typings/test/aiSummary.spec.d.ts +1 -0
- package/dist/typings/test/gitDiff.async.spec.d.ts +1 -0
- package/dist/typings/test/gitDiff.spec.d.ts +1 -0
- package/dist/typings/test/index.spec.d.ts +1 -0
- package/dist/typings/test/openAIConfig.spec.d.ts +1 -0
- package/dist/typings/test/openAiSdk.spec.d.ts +1 -0
- package/dist/typings/test/summarizeGitDiff.spec.d.ts +1 -0
- package/package.json +72 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Matthew Carvin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# smart-diff
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/smart-diff)
|
|
4
|
+
[](https://raw.githubusercontent.com/mcarvin8/smart-diff/main/LICENSE.md)
|
|
5
|
+
[](https://npmjs.org/package/smart-diff)
|
|
6
|
+
|
|
7
|
+
TypeScript library that turns a **git revision range** into a **Markdown summary** using an OpenAI-compatible Chat Completions API. It uses [`simple-git`](https://github.com/steveukx/git-js) to read the repo, respects **path includes/excludes** and **commit message include/exclude regexes**, and sends commits, paths, structured diff stats, and unified diff text to the model.
|
|
8
|
+
|
|
9
|
+
There is **no local fallback**: you must either configure an LLM gateway via environment variables or pass `openAiClientProvider` (for tests or custom clients).
|
|
10
|
+
|
|
11
|
+
## Requirements
|
|
12
|
+
|
|
13
|
+
- **Node.js** 20+
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install smart-diff
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Build the package from source with `npm run build` (Rollup outputs CommonJS and ESM under `dist/`).
|
|
22
|
+
|
|
23
|
+
## LLM configuration
|
|
24
|
+
|
|
25
|
+
The library is considered “configured” when `shouldUseLlmGateway()` is true: API key, base URL, and/or JSON default headers are set. Otherwise `summarizeGitDiff` / `generateSummary` throw with `LLM_GATEWAY_REQUIRED_MESSAGE` unless you pass **`openAiClientProvider`**.
|
|
26
|
+
|
|
27
|
+
| Variable | Purpose |
|
|
28
|
+
|----------|---------|
|
|
29
|
+
| `OPENAI_API_KEY` or `LLM_API_KEY` | API key (`LLM_*` wins over `OPENAI_*` where both exist). |
|
|
30
|
+
| `OPENAI_BASE_URL` or `LLM_BASE_URL` | Base URL for an OpenAI-compatible gateway (`LLM_*` overrides). |
|
|
31
|
+
| `OPENAI_DEFAULT_HEADERS` / `LLM_DEFAULT_HEADERS` | JSON object of extra headers; `LLM_*` merges on top of `OPENAI_*`. Can supply `Authorization` (e.g. raw `sk-…`) when no env key is set. |
|
|
32
|
+
| `LLM_MAX_DIFF_CHARS` | Max size of unified diff text sent to the model (default ~120k characters). |
|
|
33
|
+
| `LLM_MAX_TOKENS` or `OPENAI_MAX_TOKENS` | Max completion tokens (default 4000). |
|
|
34
|
+
|
|
35
|
+
The client is created with the official [`openai`](https://www.npmjs.com/package/openai) SDK via `createOpenAiLikeClient()`; use a compatible endpoint and model ID for your provider.
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
### `summarizeGitDiff`
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { summarizeGitDiff } from 'smart-diff';
|
|
43
|
+
|
|
44
|
+
const markdown = await summarizeGitDiff({
|
|
45
|
+
from: 'origin/main',
|
|
46
|
+
to: 'HEAD',
|
|
47
|
+
cwd: '/path/to/repo', // optional; default process.cwd()
|
|
48
|
+
includeFolders: ['src'],
|
|
49
|
+
excludeFolders: ['node_modules', 'dist'],
|
|
50
|
+
commitMessageExcludeRegexes: ['^\\[bot\\]'],
|
|
51
|
+
commitMessageIncludeRegexes: ['^feat:'], // optional; OR across patterns
|
|
52
|
+
teamName: 'Platform',
|
|
53
|
+
systemPrompt: undefined, // optional; overrides DEFAULT_GIT_DIFF_SYSTEM_PROMPT
|
|
54
|
+
model: 'gpt-4o-mini', // optional
|
|
55
|
+
maxDiffChars: 120_000, // optional; also see LLM_MAX_DIFF_CHARS
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
| Option | Description |
|
|
60
|
+
|--------|-------------|
|
|
61
|
+
| `from` / `to` | Git refs for the range; `to` defaults to `HEAD`. |
|
|
62
|
+
| `cwd` / `git` | Working tree for `simple-git`, or inject your own `SimpleGit` instance. |
|
|
63
|
+
| `includeFolders` | Limit diff to these paths relative to repo root (omit for full repo minus excludes). |
|
|
64
|
+
| `excludeFolders` | Excluded paths (git `:(exclude)` pathspecs), e.g. `node_modules`. |
|
|
65
|
+
| `commitMessageIncludeRegexes` | If any pattern is non-empty, only commits whose **full message** matches at least one pattern are kept (after excludes). Case-insensitive. |
|
|
66
|
+
| `commitMessageExcludeRegexes` | Drop commits whose message matches **any** of these patterns. |
|
|
67
|
+
| `teamName` | Adds a `Team:` line to the user payload for the model. |
|
|
68
|
+
| `systemPrompt` | Replaces the default system prompt. |
|
|
69
|
+
| `model` | Chat model id (default `gpt-4o-mini`). |
|
|
70
|
+
| `maxDiffChars` | Caps unified diff size for the request. |
|
|
71
|
+
| `openAiClientProvider` | `() => Promise<OpenAiLikeClient>` — bypasses env-based client creation (required in tests or when you wire the SDK yourself). |
|
|
72
|
+
|
|
73
|
+
### Diff shape: single range vs per-commit
|
|
74
|
+
|
|
75
|
+
- **Single unified diff** for `from..to` when no commit-message filters apply and the filtered commit list matches the full log for that range.
|
|
76
|
+
- **Concatenated per-commit patches** (`<hash>^!`) when you use include/exclude regexes or when the filtered commit list differs in length from the full range (so the diff reflects only the commits that remain).
|
|
77
|
+
|
|
78
|
+
### Lower-level API
|
|
79
|
+
|
|
80
|
+
The package also exports helpers such as `createGitClient`, `getCommits`, `getDiff`, `getDiffSummary`, `getChangedFiles`, `filterCommitsByMessageRegexes`, `buildDiffPathspecs`, `generateSummary`, and OpenAI config utilities (`resolveLlmBaseUrl`, `shouldUseLlmGateway`, `createOpenAiLikeClient`, …). Use these if you build a custom pipeline but still want the same git and LLM behavior.
|
|
81
|
+
|
|
82
|
+
## Development
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
npm test # Jest + coverage
|
|
86
|
+
npm run build # Rollup → dist/
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT — see [LICENSE.md](LICENSE.md).
|