@review-agent/sdk 0.0.0-stage → 0.2.1
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 +202 -0
- package/README.md +25 -2
- package/dist/index.d.ts +294 -0
- package/dist/index.js +606 -0
- package/package.json +37 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Cristian Moldovan
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
package/README.md
CHANGED
|
@@ -1,3 +1,26 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @review-agent/sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Embed [review-agent](https://github.com/crissmoldovan/review-agent), an AI pull-request reviewer you run inside your
|
|
4
|
+
own environment, in your own Node.js service or script: one call reviews a pull request and returns what it found —
|
|
5
|
+
the same review the GitHub App and the CLI run.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { reviewPullRequest } from '@review-agent/sdk'
|
|
9
|
+
|
|
10
|
+
const result = await reviewPullRequest({
|
|
11
|
+
repo: 'acme/widgets',
|
|
12
|
+
pr: 128,
|
|
13
|
+
github: { token: process.env.GITHUB_TOKEN! },
|
|
14
|
+
credential: { apiKey: process.env.ANTHROPIC_API_KEY! },
|
|
15
|
+
})
|
|
16
|
+
if (result.kind === 'reviewed') console.log(result.findings)
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Posts nothing unless asked (`post: true`). Needs Node.js 22+ and
|
|
20
|
+
[Claude Code](https://docs.anthropic.com/en/docs/claude-code) installed. It starts no server and stores nothing.
|
|
21
|
+
|
|
22
|
+
- [The SDK guide](https://github.com/crissmoldovan/review-agent/blob/main/docs/sdk.md) — every option, every result,
|
|
23
|
+
credentials, and examples
|
|
24
|
+
- [The library reference](https://github.com/crissmoldovan/review-agent/blob/main/docs/library.md) — every export
|
|
25
|
+
|
|
26
|
+
Apache-2.0.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { Octokit } from '@octokit/core';
|
|
2
|
+
import { ReviewRecord, Finding, ReportMode, Lens, SecretValue, Tier, Effort, TierSettings, SeverityBands, DeploymentConfig, SkillSource } from '@review-agent/core';
|
|
3
|
+
export { DeploymentConfig, Effort, Finding, Lens, ReportMode, ReviewRecord, SecretValue, Tier, Usage, defaultDeploymentConfig } from '@review-agent/core';
|
|
4
|
+
import { SessionStage, StoppedBy, ReportKind, CheckConclusion, PostedComment, PublishCounts, ReportWant } from '@review-agent/github';
|
|
5
|
+
export { CheckConclusion, DEFAULT_SESSION_DEADLINE_MS, PostedComment, PublishCounts, ReportKind, ReportWant, SessionStage, StoppedBy, pushAccessOf } from '@review-agent/github';
|
|
6
|
+
|
|
7
|
+
/** How the outcome was reported on the reviewed commit (spec 0.2.0 §2.2, §4.3). */
|
|
8
|
+
type ReportResult = {
|
|
9
|
+
/** What actually reported: `report: 'auto'` can start as a check run and fall back to a status. */
|
|
10
|
+
kind: ReportKind;
|
|
11
|
+
/** What a check run concluded — for a commit status too, whose own state says it differently: a
|
|
12
|
+
* review that did not run is `neutral`, which a status says as `success` with a description saying
|
|
13
|
+
* it is not a verdict (decision §14.2). `null` when the report was opened and could not be
|
|
14
|
+
* completed: it may still say pending on the commit. */
|
|
15
|
+
conclusion: CheckConclusion | null;
|
|
16
|
+
/** The check run's page; for a commit status, the summary comment it links to. */
|
|
17
|
+
url: string | null;
|
|
18
|
+
/** The check run's id; null for a commit status. */
|
|
19
|
+
id: number | null;
|
|
20
|
+
};
|
|
21
|
+
/** What was posted on the pull request (spec 0.2.0 §4.3). */
|
|
22
|
+
type PostedSummary = {
|
|
23
|
+
/** The mode it was published in, after the repository's own file and `check-only`-without-a-check-run
|
|
24
|
+
* (decision §14.3) had their say. */
|
|
25
|
+
mode: ReportMode;
|
|
26
|
+
/** The summary comment's page, as GitHub gave it; null when none was written (`check-only`,
|
|
27
|
+
* `silent`) or GitHub's answer carried none. */
|
|
28
|
+
summaryUrl: string | null;
|
|
29
|
+
summaryCommentId: number | null;
|
|
30
|
+
/** The review that carries the inline comments; null when none was posted. */
|
|
31
|
+
reviewId: number | null;
|
|
32
|
+
/** The inline comment each posted finding became — what a reply hangs off. */
|
|
33
|
+
comments: PostedComment[];
|
|
34
|
+
/** How many findings went where, and what the re-review did to earlier comments. */
|
|
35
|
+
counts: PublishCounts;
|
|
36
|
+
};
|
|
37
|
+
/** The review ran and said something — possibly that it found nothing. */
|
|
38
|
+
type ReviewedResult = {
|
|
39
|
+
kind: 'reviewed';
|
|
40
|
+
/** Everything about the review: request, settings, the model's whole output, every check verdict,
|
|
41
|
+
* usage and `costUsd` (model spend at per-token API prices, as the Claude Code CLI reports it — on
|
|
42
|
+
* a subscription token the same figure is drawn from its allowance instead of billed). */
|
|
43
|
+
record: ReviewRecord;
|
|
44
|
+
/** What the review SAYS: after the disprove-it pass — a refuted finding is not here, the rest are at
|
|
45
|
+
* the check's severity — with duplicates said once and paths checked against the code. Posted, it
|
|
46
|
+
* is what was posted; not posted, what would have been, said as a first review is (no earlier
|
|
47
|
+
* review is read). An empty list is a review that found nothing, which is a success. The model's
|
|
48
|
+
* unfiltered list is `record.output.findings`. */
|
|
49
|
+
findings: Finding[];
|
|
50
|
+
/** What was posted; null when not posting. */
|
|
51
|
+
posted: PostedSummary | null;
|
|
52
|
+
/** How it was reported on the commit; null when not posting, with `report: 'none'`, or in `silent`. */
|
|
53
|
+
report: ReportResult | null;
|
|
54
|
+
};
|
|
55
|
+
/** The model ran and produced nothing usable, or was stopped before it could (spec 0.2.0 §4.3). */
|
|
56
|
+
type UnfinishedResult = {
|
|
57
|
+
kind: 'unfinished';
|
|
58
|
+
record: ReviewRecord;
|
|
59
|
+
/** One sentence, already scanned for secrets: safe to show anyone who can see the pull request. */
|
|
60
|
+
reason: string;
|
|
61
|
+
/** `record.costUsd`: what was spent for nothing usable. */
|
|
62
|
+
spentUsd: number;
|
|
63
|
+
/** Whether running it again could not pay for the model twice: nothing was spent, and it was not
|
|
64
|
+
* stopped on purpose. A retry policy reads this. */
|
|
65
|
+
retryable: boolean;
|
|
66
|
+
/** Who stopped it: its deadline, or the `signal`. Null when it stopped on its own. */
|
|
67
|
+
stopped: StoppedBy | null;
|
|
68
|
+
/** The "This review did not complete" summary, when posting; null otherwise, and for a stopped
|
|
69
|
+
* review, which says so on its report alone. */
|
|
70
|
+
posted: PostedSummary | null;
|
|
71
|
+
report: ReportResult | null;
|
|
72
|
+
};
|
|
73
|
+
/** `authorize` said no. Nothing was downloaded, nothing was written, nothing was spent. */
|
|
74
|
+
type RefusedResult = {
|
|
75
|
+
kind: 'refused';
|
|
76
|
+
reason: string;
|
|
77
|
+
};
|
|
78
|
+
/** The review did not produce one, and `stage` says how far it got (spec 0.2.0 §4.3):
|
|
79
|
+
* - `before-model`: nothing was spent — GitHub would not answer, the report could not be opened
|
|
80
|
+
* (`report: 'check-run'` with a token that cannot create one), `authorize` threw, the archive would
|
|
81
|
+
* not download, or it was cancelled before the model started;
|
|
82
|
+
* - `model`: the model ran, and the review failed or was stopped after that;
|
|
83
|
+
* - `withheld`: the output matched a secret pattern, so nothing of it is said anywhere;
|
|
84
|
+
* - `publish`: the review ran and was paid for, and GitHub would not take it. `record` and `findings`
|
|
85
|
+
* carry it, so it is not lost. */
|
|
86
|
+
type FailedResult = {
|
|
87
|
+
kind: 'failed';
|
|
88
|
+
stage: SessionStage;
|
|
89
|
+
/** One sentence, already scanned for secrets: safe to show anyone who can see the pull request. */
|
|
90
|
+
reason: string;
|
|
91
|
+
/** Model spend known to be gone, at per-token API prices: the record's figure where there is one,
|
|
92
|
+
* what was watched going out where there is not — a lower bound when a pass was cut off. */
|
|
93
|
+
spentUsd: number;
|
|
94
|
+
retryable: boolean;
|
|
95
|
+
stopped: StoppedBy | null;
|
|
96
|
+
/** The finished review that could not be posted (`stage: 'publish'`); null otherwise. */
|
|
97
|
+
record: ReviewRecord | null;
|
|
98
|
+
/** What that review would have said, as `ReviewedResult.findings`; null with no `record`. */
|
|
99
|
+
findings: Finding[] | null;
|
|
100
|
+
report: ReportResult | null;
|
|
101
|
+
/** The error as it was thrown, for the host's own logs. Never scanned and never shown to anyone by
|
|
102
|
+
* this package: `reason` is the sentence that is safe to repeat. */
|
|
103
|
+
cause: unknown;
|
|
104
|
+
};
|
|
105
|
+
type ReviewResult = ReviewedResult | UnfinishedResult | RefusedResult | FailedResult;
|
|
106
|
+
|
|
107
|
+
/** Where a review has got to (spec 0.2.0 §4.2, `onProgress`). In the order a posted review passes
|
|
108
|
+
* them: `preparing` (reading the pull request and its settings), `downloading`, `reviewing`,
|
|
109
|
+
* `checking`, `rechecking` (a re-review of a pull request reviewed before), `publishing`,
|
|
110
|
+
* `reporting` (finishing the check run or commit status), `done`. A review skips what it does not
|
|
111
|
+
* do: a printed one never publishes or reports, a first review never re-checks. */
|
|
112
|
+
type ProgressStage = 'preparing' | 'downloading' | 'reviewing' | 'checking' | 'rechecking' | 'publishing' | 'reporting' | 'done';
|
|
113
|
+
type About = {
|
|
114
|
+
/** `'owner/name'`, so one handler can follow several reviews. */
|
|
115
|
+
repo: string;
|
|
116
|
+
pr: number;
|
|
117
|
+
};
|
|
118
|
+
/** The review has reached `stage`. `note` is absent, which is how these are told from a `NoteEvent`. */
|
|
119
|
+
type StageEvent = About & {
|
|
120
|
+
note?: undefined;
|
|
121
|
+
} & ({
|
|
122
|
+
stage: 'preparing';
|
|
123
|
+
} | {
|
|
124
|
+
stage: 'downloading';
|
|
125
|
+
}
|
|
126
|
+
/** Once per reviewer: on a multi-reviewer review, once per focus as it starts, with its `focus`;
|
|
127
|
+
* otherwise once, with `focus: null`. */
|
|
128
|
+
| {
|
|
129
|
+
stage: 'reviewing';
|
|
130
|
+
focus: Lens | null;
|
|
131
|
+
}
|
|
132
|
+
/** `findings`: how many the disprove-it pass is checking. */
|
|
133
|
+
| {
|
|
134
|
+
stage: 'checking';
|
|
135
|
+
findings: number;
|
|
136
|
+
}
|
|
137
|
+
/** `findings`: how many earlier findings the re-review pass is asked about. */
|
|
138
|
+
| {
|
|
139
|
+
stage: 'rechecking';
|
|
140
|
+
findings: number;
|
|
141
|
+
} | {
|
|
142
|
+
stage: 'publishing';
|
|
143
|
+
} | {
|
|
144
|
+
stage: 'reporting';
|
|
145
|
+
report: ReportKind;
|
|
146
|
+
}
|
|
147
|
+
/** The last event of every call that returns: the result's kind. Sent once the checkout is gone. */
|
|
148
|
+
| {
|
|
149
|
+
stage: 'done';
|
|
150
|
+
result: ReviewResult['kind'];
|
|
151
|
+
});
|
|
152
|
+
/** A line of progress inside the stage the review last reached — the lines the CLI prints on stderr
|
|
153
|
+
* (credential files removed from the checkout, a focused reviewer that failed, a report that could
|
|
154
|
+
* not be completed). Not a new stage. `details` are the fields the line was logged with, `{}` when
|
|
155
|
+
* none; meant for the host's own logs. */
|
|
156
|
+
type NoteEvent = About & {
|
|
157
|
+
stage: ProgressStage;
|
|
158
|
+
note: string;
|
|
159
|
+
details: Record<string, unknown>;
|
|
160
|
+
};
|
|
161
|
+
type ProgressEvent = StageEvent | NoteEvent;
|
|
162
|
+
|
|
163
|
+
/** How to reach GitHub (spec 0.2.0 §4.2): a token, from which a client is built with the API version
|
|
164
|
+
* every way in was proven against (`octokitFor`), or a client the host already has. `baseUrl` is a
|
|
165
|
+
* GitHub Enterprise Server's API root, `https://<host>/api/v3`; left out, github.com. */
|
|
166
|
+
type GitHubAccess = {
|
|
167
|
+
token: string;
|
|
168
|
+
baseUrl?: string;
|
|
169
|
+
} | {
|
|
170
|
+
octokit: Octokit;
|
|
171
|
+
};
|
|
172
|
+
/** The model credential (spec 0.2.0 §2.3): an Anthropic API key — the normal path, and the only one
|
|
173
|
+
* for a service that reviews for other people — a subscription token from `claude setup-token`, for
|
|
174
|
+
* your own use, or this machine's `claude login`. A value may be passed already wrapped in a
|
|
175
|
+
* `SecretValue`; a bare string is wrapped on the way in, so nothing this package hands on prints it. */
|
|
176
|
+
type CredentialOption = {
|
|
177
|
+
apiKey: string | SecretValue;
|
|
178
|
+
} | {
|
|
179
|
+
oauthToken: string | SecretValue;
|
|
180
|
+
} | 'claude-login';
|
|
181
|
+
/** The deployment settings (docs/configuration.md in the repository), any of them left out — at any
|
|
182
|
+
* depth. `settings: { report: { checkRunName: 'review' } }` changes the one name and keeps
|
|
183
|
+
* every other report setting; see `mergeSettings` for why the merge goes exactly this deep.
|
|
184
|
+
*
|
|
185
|
+
* Every `Partial<DeploymentConfig>` is one of these (spec 0.2.0 §4.2 names that type), so a host can
|
|
186
|
+
* pass a whole deployment object it already keeps. `command` and `autoReview` are read only by the
|
|
187
|
+
* App's receiver, and are accepted here and ignored. */
|
|
188
|
+
type ReviewSettings = {
|
|
189
|
+
command?: string;
|
|
190
|
+
model?: string;
|
|
191
|
+
effort?: Effort;
|
|
192
|
+
tiers?: {
|
|
193
|
+
[T in Tier]?: Partial<TierSettings>;
|
|
194
|
+
};
|
|
195
|
+
severity?: {
|
|
196
|
+
bands?: Partial<SeverityBands>;
|
|
197
|
+
postInlineAtOrAbove?: number;
|
|
198
|
+
};
|
|
199
|
+
report?: Partial<DeploymentConfig['report']>;
|
|
200
|
+
skills?: SkillSource[];
|
|
201
|
+
instructions?: string;
|
|
202
|
+
autoReview?: DeploymentConfig['autoReview'];
|
|
203
|
+
allowMulti?: boolean;
|
|
204
|
+
};
|
|
205
|
+
/** What `authorize` is asked about, before anything is spent (spec 0.2.0 §4.2). It carries what
|
|
206
|
+
* `pushAccessOf` reads — `octokit`, `owner`, `repo`, `username` — so `authorize: pushAccessOf` is the
|
|
207
|
+
* App's own gate, as it is. */
|
|
208
|
+
type AuthorizeRequest = {
|
|
209
|
+
/** The client this review reads GitHub with. */
|
|
210
|
+
octokit: Octokit;
|
|
211
|
+
owner: string;
|
|
212
|
+
repo: string;
|
|
213
|
+
pr: number;
|
|
214
|
+
/** `requestedBy`: the login whose request this is. */
|
|
215
|
+
username: string;
|
|
216
|
+
/** The commit this review would read, as GitHub answered just now. */
|
|
217
|
+
headSha: string;
|
|
218
|
+
};
|
|
219
|
+
/** Several focused reviewers instead of one (spec 0.2.0 §8.3), and how many of them run at once: all
|
|
220
|
+
* three when `true`. Each is a Claude process of its own, and one review peaks around 400 MiB, so a
|
|
221
|
+
* host short of memory says `{ concurrency: 1 }`. */
|
|
222
|
+
type MultiOption = boolean | {
|
|
223
|
+
concurrency: number;
|
|
224
|
+
};
|
|
225
|
+
type ReviewOptions = {
|
|
226
|
+
/** The repository, `'owner/name'`. */
|
|
227
|
+
repo: string;
|
|
228
|
+
/** The pull request's number. */
|
|
229
|
+
pr: number;
|
|
230
|
+
github: GitHubAccess;
|
|
231
|
+
/** Left out: from this process's environment, exactly one of `ANTHROPIC_API_KEY` and
|
|
232
|
+
* `CLAUDE_CODE_OAUTH_TOKEN` (`modelCredentialFromEnv`, as the App's runner reads it). */
|
|
233
|
+
credential?: CredentialOption;
|
|
234
|
+
/** Post the review, its inline comments and its summary, and report on the commit. Default `false`:
|
|
235
|
+
* review, return the result, and write nothing to GitHub at all. */
|
|
236
|
+
post?: boolean;
|
|
237
|
+
/** How to report the outcome on the commit when posting (spec 0.2.0 §2.2). `'auto'`, the default:
|
|
238
|
+
* a check run when the token can create one, a commit status when it cannot. `'check-run'` with a
|
|
239
|
+
* token that cannot create one is a `failed` result before any money is spent. Read only when
|
|
240
|
+
* posting. */
|
|
241
|
+
report?: ReportWant;
|
|
242
|
+
/** This request, as the CLI's flags say it; the settings' own when left out. */
|
|
243
|
+
tier?: Tier;
|
|
244
|
+
effort?: Effort;
|
|
245
|
+
/** The model's name, e.g. `claude-opus-5`. */
|
|
246
|
+
model?: string;
|
|
247
|
+
/** Run the disprove-it pass, whatever the tier says (`true`), or skip it (`false`). */
|
|
248
|
+
check?: boolean;
|
|
249
|
+
/** Extra context for the reviewer. It is data, never instruction. */
|
|
250
|
+
brief?: string;
|
|
251
|
+
/** Lower this review's budget, in US dollars at per-token API prices. It can never be raised past
|
|
252
|
+
* the tier's. */
|
|
253
|
+
budgetUsd?: number;
|
|
254
|
+
multi?: MultiOption;
|
|
255
|
+
settings?: ReviewSettings;
|
|
256
|
+
/** Whose request this is: the login `authorize` is asked about, and `record.request.requestedBy`. */
|
|
257
|
+
requestedBy?: string;
|
|
258
|
+
/** A gate asked after the pull request is read and before anything is downloaded or spent. `false`
|
|
259
|
+
* is a `refused` result. It must answer `true` or `false`; it needs `requestedBy`. */
|
|
260
|
+
authorize?: (who: AuthorizeRequest) => boolean | Promise<boolean>;
|
|
261
|
+
/** Which Claude Code to run: an absolute path. Left out, `REVIEW_AGENT_CLAUDE_BIN`, else `claude`
|
|
262
|
+
* on `PATH`. */
|
|
263
|
+
claude?: {
|
|
264
|
+
bin?: string;
|
|
265
|
+
};
|
|
266
|
+
/** Where checkouts go, each in a new directory of its own that is removed when the review ends.
|
|
267
|
+
* Absolute. Left out, the OS temp directory. */
|
|
268
|
+
workDir?: string;
|
|
269
|
+
/** Where skills are cached. Absolute. Left out, this OS's cache directory. */
|
|
270
|
+
skillCacheDir?: string;
|
|
271
|
+
/** The review stops itself this long after it starts — after the pull request has been read.
|
|
272
|
+
* Default 30 minutes. */
|
|
273
|
+
deadlineMs?: number;
|
|
274
|
+
signal?: AbortSignal;
|
|
275
|
+
onProgress?: (event: ProgressEvent) => void;
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
/** Reviews one pull request, and returns what happened (spec 0.2.0 §4): the same review the GitHub
|
|
279
|
+
* App and the CLI run — `prepareReview` then `runReviewSession`, in `@review-agent/github` — called
|
|
280
|
+
* from a host's own code.
|
|
281
|
+
*
|
|
282
|
+
* It RETURNS every outcome — `reviewed`, `unfinished`, `refused`, `failed` — so a host can tell "the
|
|
283
|
+
* reviewer found nothing" from "the reviewer did not run" (§4.3). It THROWS only what is the
|
|
284
|
+
* programmer's to fix, before anything is fetched or spent: a missing or wrong-typed option
|
|
285
|
+
* (`TypeError`), or a process no review can run in — no model credential, two, or
|
|
286
|
+
* `ANTHROPIC_BASE_URL` set (`Error`). One more `TypeError` can come later, still before anything is
|
|
287
|
+
* spent: an `authorize` that answers something other than `true` or `false`.
|
|
288
|
+
*
|
|
289
|
+
* Nothing is shared between calls but the skill cache (§4.4): each builds its own client, credential
|
|
290
|
+
* and checkout, and this module keeps no state. Every checkout it made is removed before it returns,
|
|
291
|
+
* whichever way the review ended. */
|
|
292
|
+
declare function reviewPullRequest(options: ReviewOptions): Promise<ReviewResult>;
|
|
293
|
+
|
|
294
|
+
export { type AuthorizeRequest, type CredentialOption, type FailedResult, type GitHubAccess, type MultiOption, type NoteEvent, type PostedSummary, type ProgressEvent, type ProgressStage, type RefusedResult, type ReportResult, type ReviewOptions, type ReviewResult, type ReviewSettings, type ReviewedResult, type StageEvent, type UnfinishedResult, reviewPullRequest };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
// src/review.ts
|
|
2
|
+
import {
|
|
3
|
+
defaultRunReviewDeps,
|
|
4
|
+
prepareReview,
|
|
5
|
+
removeCheckout,
|
|
6
|
+
reporterFor,
|
|
7
|
+
runReviewSession,
|
|
8
|
+
saidSafely,
|
|
9
|
+
tokenIdentityOf
|
|
10
|
+
} from "@review-agent/github";
|
|
11
|
+
|
|
12
|
+
// src/options.ts
|
|
13
|
+
import { isAbsolute } from "path";
|
|
14
|
+
import {
|
|
15
|
+
defaultDeploymentConfig,
|
|
16
|
+
gatewayBaseUrlRefusal,
|
|
17
|
+
modelCredentialFromEnv,
|
|
18
|
+
SecretValue
|
|
19
|
+
} from "@review-agent/core";
|
|
20
|
+
import { DEFAULT_SESSION_DEADLINE_MS, octokitFor } from "@review-agent/github";
|
|
21
|
+
var TIER_SET = { basic: true, normal: true, thorough: true };
|
|
22
|
+
var EFFORT_SET = { low: true, medium: true, high: true, xhigh: true, max: true };
|
|
23
|
+
var MODE_SET = {
|
|
24
|
+
"inline+summary": true,
|
|
25
|
+
"summary-only": true,
|
|
26
|
+
"check-only": true,
|
|
27
|
+
silent: true
|
|
28
|
+
};
|
|
29
|
+
var AUTO_REVIEW_SET = { "open+ready": true, off: true };
|
|
30
|
+
var REPORT_SET = { auto: true, "check-run": true, "commit-status": true, none: true };
|
|
31
|
+
var TIERS = Object.keys(TIER_SET);
|
|
32
|
+
var EFFORTS = Object.keys(EFFORT_SET);
|
|
33
|
+
var MODES = Object.keys(MODE_SET);
|
|
34
|
+
var AUTO_REVIEWS = Object.keys(AUTO_REVIEW_SET);
|
|
35
|
+
var REPORTS = Object.keys(REPORT_SET);
|
|
36
|
+
var OPTION_KEYS = /* @__PURE__ */ new Set([
|
|
37
|
+
"repo",
|
|
38
|
+
"pr",
|
|
39
|
+
"github",
|
|
40
|
+
"credential",
|
|
41
|
+
"post",
|
|
42
|
+
"report",
|
|
43
|
+
"tier",
|
|
44
|
+
"effort",
|
|
45
|
+
"model",
|
|
46
|
+
"check",
|
|
47
|
+
"brief",
|
|
48
|
+
"budgetUsd",
|
|
49
|
+
"multi",
|
|
50
|
+
"settings",
|
|
51
|
+
"requestedBy",
|
|
52
|
+
"authorize",
|
|
53
|
+
"claude",
|
|
54
|
+
"workDir",
|
|
55
|
+
"skillCacheDir",
|
|
56
|
+
"deadlineMs",
|
|
57
|
+
"signal",
|
|
58
|
+
"onProgress"
|
|
59
|
+
]);
|
|
60
|
+
var MAX_DEADLINE_MS = 2147483647;
|
|
61
|
+
var REPO = /^([A-Za-z0-9](?:[A-Za-z0-9-]{0,38}))\/([A-Za-z0-9._-]{1,100})$/;
|
|
62
|
+
var isRecord = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
63
|
+
var isPlainObject = (v) => {
|
|
64
|
+
if (!isRecord(v)) return false;
|
|
65
|
+
const proto = Object.getPrototypeOf(v);
|
|
66
|
+
return proto === Object.prototype || proto === null;
|
|
67
|
+
};
|
|
68
|
+
var oneOf = (name, v, allowed) => {
|
|
69
|
+
if (typeof v !== "string" || !allowed.includes(v)) {
|
|
70
|
+
throw new TypeError(`${name} must be one of ${allowed.join(", ")}, got ${JSON.stringify(v) ?? String(v)}`);
|
|
71
|
+
}
|
|
72
|
+
return v;
|
|
73
|
+
};
|
|
74
|
+
var nonEmptyString = (name, v) => {
|
|
75
|
+
if (typeof v !== "string" || v === "") throw new TypeError(`${name} must be a non-empty string`);
|
|
76
|
+
return v;
|
|
77
|
+
};
|
|
78
|
+
var absolutePath = (name, v) => {
|
|
79
|
+
const s = nonEmptyString(name, v);
|
|
80
|
+
if (!isAbsolute(s)) throw new TypeError(`${name} must be an absolute path, got ${JSON.stringify(s)}`);
|
|
81
|
+
return s;
|
|
82
|
+
};
|
|
83
|
+
var onlyKeys = (name, v, allowed) => {
|
|
84
|
+
for (const k of Object.keys(v)) {
|
|
85
|
+
if (!allowed.includes(k)) throw new TypeError(`${name}.${k} is not an option; ${name} takes ${allowed.join(", ")}`);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
var secretOf = (name, v) => {
|
|
89
|
+
if (isRecord(v) && typeof v.reveal === "function") {
|
|
90
|
+
const s = v;
|
|
91
|
+
if (typeof s.reveal() !== "string" || s.reveal() === "") throw new TypeError(`${name} is empty`);
|
|
92
|
+
return s;
|
|
93
|
+
}
|
|
94
|
+
if (typeof v !== "string" || v === "") {
|
|
95
|
+
throw new TypeError(`${name} must be a non-empty string or a SecretValue (its value is not shown)`);
|
|
96
|
+
}
|
|
97
|
+
return new SecretValue(v);
|
|
98
|
+
};
|
|
99
|
+
var credentialOf = (c, env) => {
|
|
100
|
+
let credential;
|
|
101
|
+
if (c === void 0) {
|
|
102
|
+
return modelCredentialFromEnv(env, { allowLogin: false });
|
|
103
|
+
} else if (c === "claude-login") {
|
|
104
|
+
credential = { kind: "claude-login" };
|
|
105
|
+
} else if (isRecord(c)) {
|
|
106
|
+
const keys = Object.keys(c);
|
|
107
|
+
if (keys.length !== 1 || keys[0] !== "apiKey" && keys[0] !== "oauthToken") {
|
|
108
|
+
throw new TypeError("credential must be exactly one of { apiKey }, { oauthToken } or 'claude-login'");
|
|
109
|
+
}
|
|
110
|
+
credential = keys[0] === "apiKey" ? { kind: "api-key", value: secretOf("credential.apiKey", c.apiKey) } : { kind: "oauth-token", value: secretOf("credential.oauthToken", c.oauthToken) };
|
|
111
|
+
} else {
|
|
112
|
+
throw new TypeError("credential must be { apiKey }, { oauthToken } or 'claude-login'");
|
|
113
|
+
}
|
|
114
|
+
const gateway = gatewayBaseUrlRefusal(env);
|
|
115
|
+
if (gateway !== null) throw new Error(gateway);
|
|
116
|
+
return credential;
|
|
117
|
+
};
|
|
118
|
+
var octokitOf = (g) => {
|
|
119
|
+
if (!isRecord(g)) throw new TypeError("github must be { token, baseUrl? } or { octokit }");
|
|
120
|
+
if (Object.hasOwn(g, "octokit")) {
|
|
121
|
+
onlyKeys("github", g, ["octokit"]);
|
|
122
|
+
if (!isRecord(g.octokit) || typeof g.octokit.request !== "function") {
|
|
123
|
+
throw new TypeError("github.octokit must be an Octokit client");
|
|
124
|
+
}
|
|
125
|
+
return g.octokit;
|
|
126
|
+
}
|
|
127
|
+
onlyKeys("github", g, ["token", "baseUrl"]);
|
|
128
|
+
if (typeof g.token !== "string" || g.token === "") {
|
|
129
|
+
throw new TypeError("github.token must be a non-empty string (its value is not shown)");
|
|
130
|
+
}
|
|
131
|
+
if (g.baseUrl === void 0) return octokitFor({ token: g.token });
|
|
132
|
+
const baseUrl = nonEmptyString("github.baseUrl", g.baseUrl);
|
|
133
|
+
let protocol;
|
|
134
|
+
try {
|
|
135
|
+
protocol = new URL(baseUrl).protocol;
|
|
136
|
+
} catch {
|
|
137
|
+
throw new TypeError(`github.baseUrl must be a URL, got ${JSON.stringify(baseUrl)}`);
|
|
138
|
+
}
|
|
139
|
+
if (protocol !== "https:" && protocol !== "http:") {
|
|
140
|
+
throw new TypeError(`github.baseUrl must be an http(s) URL, got ${JSON.stringify(baseUrl)}`);
|
|
141
|
+
}
|
|
142
|
+
return octokitFor({ token: g.token, baseUrl });
|
|
143
|
+
};
|
|
144
|
+
var ABSENT_BY_DEFAULT = { instructions: "string" };
|
|
145
|
+
var ENUM_AT = {
|
|
146
|
+
effort: EFFORTS,
|
|
147
|
+
"report.mode": MODES,
|
|
148
|
+
autoReview: AUTO_REVIEWS,
|
|
149
|
+
"tiers.basic.effort": EFFORTS,
|
|
150
|
+
"tiers.normal.effort": EFFORTS,
|
|
151
|
+
"tiers.thorough.effort": EFFORTS
|
|
152
|
+
};
|
|
153
|
+
var NULLABLE_NUMBER_AT = /* @__PURE__ */ new Set(["report.failAtOrAbove"]);
|
|
154
|
+
var leafOf = (base, over, path) => {
|
|
155
|
+
const at = path.join(".");
|
|
156
|
+
const name = `settings.${at}`;
|
|
157
|
+
const allowed = ENUM_AT[at];
|
|
158
|
+
if (allowed !== void 0) return oneOf(name, over, allowed);
|
|
159
|
+
if (ABSENT_BY_DEFAULT[at] === "string") {
|
|
160
|
+
if (typeof over !== "string") throw new TypeError(`${name} must be a string`);
|
|
161
|
+
return over;
|
|
162
|
+
}
|
|
163
|
+
if (NULLABLE_NUMBER_AT.has(at)) {
|
|
164
|
+
if (over !== null && (typeof over !== "number" || !Number.isFinite(over))) throw new TypeError(`${name} must be a number or null`);
|
|
165
|
+
return over;
|
|
166
|
+
}
|
|
167
|
+
if (Array.isArray(base)) {
|
|
168
|
+
if (!Array.isArray(over)) throw new TypeError(`${name} must be an array`);
|
|
169
|
+
return [...over];
|
|
170
|
+
}
|
|
171
|
+
if (typeof base === "number") {
|
|
172
|
+
if (typeof over !== "number" || !Number.isFinite(over)) throw new TypeError(`${name} must be a finite number`);
|
|
173
|
+
return over;
|
|
174
|
+
}
|
|
175
|
+
if (typeof over !== typeof base) throw new TypeError(`${name} must be a ${typeof base}`);
|
|
176
|
+
return over;
|
|
177
|
+
};
|
|
178
|
+
var mergeAt = (base, over, path) => {
|
|
179
|
+
if (over === void 0) return base;
|
|
180
|
+
if (isPlainObject(base)) {
|
|
181
|
+
const name = path.length === 0 ? "settings" : `settings.${path.join(".")}`;
|
|
182
|
+
if (!isPlainObject(over)) throw new TypeError(`${name} must be an object`);
|
|
183
|
+
const out = { ...base };
|
|
184
|
+
for (const [key, value] of Object.entries(over)) {
|
|
185
|
+
const at = [...path, key];
|
|
186
|
+
if (!Object.hasOwn(base, key) && !Object.hasOwn(ABSENT_BY_DEFAULT, at.join("."))) {
|
|
187
|
+
throw new TypeError(`settings.${at.join(".")} is not a setting`);
|
|
188
|
+
}
|
|
189
|
+
out[key] = mergeAt(base[key], value, at);
|
|
190
|
+
}
|
|
191
|
+
return out;
|
|
192
|
+
}
|
|
193
|
+
return leafOf(base, over, path);
|
|
194
|
+
};
|
|
195
|
+
var mergeSettings = (over) => {
|
|
196
|
+
const base = defaultDeploymentConfig();
|
|
197
|
+
if (over === void 0) return base;
|
|
198
|
+
return mergeAt(base, over, []);
|
|
199
|
+
};
|
|
200
|
+
var normalise = (options, env) => {
|
|
201
|
+
if (!isRecord(options)) throw new TypeError("reviewPullRequest takes one options object");
|
|
202
|
+
for (const k of Object.keys(options)) {
|
|
203
|
+
if (!OPTION_KEYS.has(k)) throw new TypeError(`${k} is not an option of reviewPullRequest`);
|
|
204
|
+
}
|
|
205
|
+
const o = options;
|
|
206
|
+
if (typeof o.repo !== "string") throw new TypeError("repo must be 'owner/name'");
|
|
207
|
+
const where = REPO.exec(o.repo);
|
|
208
|
+
if (where === null || where[2] === "." || where[2] === "..") {
|
|
209
|
+
throw new TypeError(`repo must be 'owner/name', got ${JSON.stringify(o.repo)}`);
|
|
210
|
+
}
|
|
211
|
+
const [, owner, repo] = where;
|
|
212
|
+
if (typeof o.pr !== "number" || !Number.isInteger(o.pr) || o.pr <= 0) {
|
|
213
|
+
throw new TypeError(`pr must be a pull request number, got ${JSON.stringify(o.pr) ?? String(o.pr)}`);
|
|
214
|
+
}
|
|
215
|
+
if (o.github === void 0) throw new TypeError("github is required: { token, baseUrl? } or { octokit }");
|
|
216
|
+
const octokit = octokitOf(o.github);
|
|
217
|
+
if (o.post !== void 0 && typeof o.post !== "boolean") throw new TypeError("post must be true or false");
|
|
218
|
+
const report = o.report === void 0 ? "auto" : oneOf("report", o.report, REPORTS);
|
|
219
|
+
let focusConcurrency = null;
|
|
220
|
+
let multi = false;
|
|
221
|
+
if (o.multi !== void 0 && o.multi !== false) {
|
|
222
|
+
if (o.multi === true) {
|
|
223
|
+
multi = true;
|
|
224
|
+
} else if (isRecord(o.multi)) {
|
|
225
|
+
onlyKeys("multi", o.multi, ["concurrency"]);
|
|
226
|
+
const c = o.multi.concurrency;
|
|
227
|
+
if (typeof c !== "number" || !Number.isInteger(c) || c < 1) {
|
|
228
|
+
throw new TypeError(`multi.concurrency must be a whole number of at least 1, got ${JSON.stringify(c) ?? String(c)}`);
|
|
229
|
+
}
|
|
230
|
+
multi = true;
|
|
231
|
+
focusConcurrency = c;
|
|
232
|
+
} else {
|
|
233
|
+
throw new TypeError("multi must be true, false or { concurrency }");
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (o.check !== void 0 && typeof o.check !== "boolean") throw new TypeError("check must be true or false");
|
|
237
|
+
if (o.brief !== void 0 && typeof o.brief !== "string") throw new TypeError("brief must be a string");
|
|
238
|
+
if (o.budgetUsd !== void 0 && (typeof o.budgetUsd !== "number" || !Number.isFinite(o.budgetUsd) || o.budgetUsd <= 0)) {
|
|
239
|
+
throw new TypeError(`budgetUsd must be a positive number of US dollars, got ${JSON.stringify(o.budgetUsd) ?? String(o.budgetUsd)}`);
|
|
240
|
+
}
|
|
241
|
+
const requestedBy = o.requestedBy === void 0 ? null : nonEmptyString("requestedBy", o.requestedBy);
|
|
242
|
+
const ask = {
|
|
243
|
+
...o.tier !== void 0 && { tier: oneOf("tier", o.tier, TIERS) },
|
|
244
|
+
...o.effort !== void 0 && { effort: oneOf("effort", o.effort, EFFORTS) },
|
|
245
|
+
...o.model !== void 0 && { model: nonEmptyString("model", o.model) },
|
|
246
|
+
...o.check !== void 0 && { check: o.check },
|
|
247
|
+
...o.brief !== void 0 && { brief: o.brief },
|
|
248
|
+
...o.budgetUsd !== void 0 && { budgetUsd: o.budgetUsd },
|
|
249
|
+
// Only `true` is carried, as `refuseUnbuilt` carries it: false and absent mean the same.
|
|
250
|
+
...multi && { multi: true },
|
|
251
|
+
...requestedBy !== null && { requestedBy }
|
|
252
|
+
};
|
|
253
|
+
const deployment = mergeSettings(o.settings);
|
|
254
|
+
if (o.authorize !== void 0 && typeof o.authorize !== "function") throw new TypeError("authorize must be a function");
|
|
255
|
+
if (o.authorize !== void 0 && requestedBy === null) {
|
|
256
|
+
throw new TypeError("authorize needs requestedBy: the login whose request it decides");
|
|
257
|
+
}
|
|
258
|
+
let bin = null;
|
|
259
|
+
if (o.claude !== void 0) {
|
|
260
|
+
if (!isRecord(o.claude)) throw new TypeError("claude must be { bin? }");
|
|
261
|
+
onlyKeys("claude", o.claude, ["bin"]);
|
|
262
|
+
if (o.claude.bin !== void 0) bin = absolutePath("claude.bin", o.claude.bin);
|
|
263
|
+
}
|
|
264
|
+
const workDir = o.workDir === void 0 ? null : absolutePath("workDir", o.workDir);
|
|
265
|
+
const skillCacheDir = o.skillCacheDir === void 0 ? null : absolutePath("skillCacheDir", o.skillCacheDir);
|
|
266
|
+
const deadlineMs = o.deadlineMs ?? DEFAULT_SESSION_DEADLINE_MS;
|
|
267
|
+
if (typeof deadlineMs !== "number" || !Number.isFinite(deadlineMs) || deadlineMs <= 0 || deadlineMs > MAX_DEADLINE_MS) {
|
|
268
|
+
throw new TypeError(`deadlineMs must be a positive number of milliseconds, at most ${MAX_DEADLINE_MS}, got ${JSON.stringify(o.deadlineMs) ?? String(o.deadlineMs)}`);
|
|
269
|
+
}
|
|
270
|
+
if (o.signal !== void 0) {
|
|
271
|
+
const s = o.signal;
|
|
272
|
+
if (!isRecord(s) || typeof s.aborted !== "boolean" || typeof s.addEventListener !== "function") {
|
|
273
|
+
throw new TypeError("signal must be an AbortSignal");
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (o.onProgress !== void 0 && typeof o.onProgress !== "function") throw new TypeError("onProgress must be a function");
|
|
277
|
+
const credential = credentialOf(o.credential, env);
|
|
278
|
+
return {
|
|
279
|
+
owner,
|
|
280
|
+
repo,
|
|
281
|
+
pr: o.pr,
|
|
282
|
+
octokit,
|
|
283
|
+
credential,
|
|
284
|
+
post: o.post ?? false,
|
|
285
|
+
report,
|
|
286
|
+
ask,
|
|
287
|
+
deployment,
|
|
288
|
+
requestedBy,
|
|
289
|
+
authorize: o.authorize ?? null,
|
|
290
|
+
bin,
|
|
291
|
+
workDir,
|
|
292
|
+
skillCacheDir,
|
|
293
|
+
focusConcurrency,
|
|
294
|
+
deadlineMs,
|
|
295
|
+
signal: o.signal ?? null,
|
|
296
|
+
onProgress: o.onProgress ?? null
|
|
297
|
+
};
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
// src/progress.ts
|
|
301
|
+
import {
|
|
302
|
+
REVIEW_LENSES,
|
|
303
|
+
reviewSystemPrompt
|
|
304
|
+
} from "@review-agent/core";
|
|
305
|
+
import {
|
|
306
|
+
publishReview
|
|
307
|
+
} from "@review-agent/github";
|
|
308
|
+
var progressFor = (a) => {
|
|
309
|
+
const about = { repo: a.repo, pr: a.pr };
|
|
310
|
+
let current = "preparing";
|
|
311
|
+
const send = (e) => {
|
|
312
|
+
if (a.onProgress === null) return;
|
|
313
|
+
try {
|
|
314
|
+
const r = a.onProgress(e);
|
|
315
|
+
if (typeof r === "object" && r !== null && typeof r.then === "function") {
|
|
316
|
+
r.then(void 0, () => {
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
} catch {
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
return {
|
|
323
|
+
stage: (e) => {
|
|
324
|
+
current = e.stage;
|
|
325
|
+
send({ ...about, ...e });
|
|
326
|
+
},
|
|
327
|
+
note: (line) => {
|
|
328
|
+
send({ ...about, stage: current, note: line, details: {} });
|
|
329
|
+
},
|
|
330
|
+
log: (message, fields) => {
|
|
331
|
+
send({ ...about, stage: current, note: message, details: { ...fields } });
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
};
|
|
335
|
+
var focusOf = (input) => {
|
|
336
|
+
if (!input.settings.multi) return null;
|
|
337
|
+
return REVIEW_LENSES.find((lens) => reviewSystemPrompt({ settings: input.settings, lens }) === input.systemPrompt) ?? null;
|
|
338
|
+
};
|
|
339
|
+
var watchedDeps = (base, progress, made) => ({
|
|
340
|
+
...base,
|
|
341
|
+
makeCheckoutDir: async () => {
|
|
342
|
+
const dir = await base.makeCheckoutDir();
|
|
343
|
+
made.push(dir);
|
|
344
|
+
return dir;
|
|
345
|
+
},
|
|
346
|
+
downloadArchive: async (a) => {
|
|
347
|
+
progress.stage({ stage: "downloading" });
|
|
348
|
+
return base.downloadArchive(a);
|
|
349
|
+
},
|
|
350
|
+
runEngine: async (input) => {
|
|
351
|
+
progress.stage({ stage: "reviewing", focus: focusOf(input) });
|
|
352
|
+
return base.runEngine(input);
|
|
353
|
+
},
|
|
354
|
+
runCheckPass: async (x) => {
|
|
355
|
+
if (x.findings.length > 0) progress.stage({ stage: "checking", findings: x.findings.length });
|
|
356
|
+
return base.runCheckPass(x);
|
|
357
|
+
},
|
|
358
|
+
runRecheckPass: async (x) => {
|
|
359
|
+
progress.stage({ stage: "rechecking", findings: x.previous.length });
|
|
360
|
+
return base.runRecheckPass(x);
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
var watchedPublish = (progress) => async (a) => {
|
|
364
|
+
progress.stage({ stage: "publishing" });
|
|
365
|
+
return publishReview(a);
|
|
366
|
+
};
|
|
367
|
+
var watchedReporter = (reporter, progress) => ({
|
|
368
|
+
kind: reporter.kind,
|
|
369
|
+
start: async (s) => {
|
|
370
|
+
const handle = await reporter.start(s);
|
|
371
|
+
if (handle === null) return null;
|
|
372
|
+
const watched = {
|
|
373
|
+
get kind() {
|
|
374
|
+
return handle.kind;
|
|
375
|
+
},
|
|
376
|
+
get id() {
|
|
377
|
+
return handle.id;
|
|
378
|
+
},
|
|
379
|
+
get url() {
|
|
380
|
+
return handle.url;
|
|
381
|
+
},
|
|
382
|
+
finish: async (f) => {
|
|
383
|
+
progress.stage({ stage: "reporting", report: handle.kind });
|
|
384
|
+
return handle.finish(f);
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
return watched;
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
// src/result.ts
|
|
392
|
+
import {
|
|
393
|
+
writesSummaryComment
|
|
394
|
+
} from "@review-agent/core";
|
|
395
|
+
import {
|
|
396
|
+
anchorFindings,
|
|
397
|
+
duplicatesIn,
|
|
398
|
+
postedCommentsOf,
|
|
399
|
+
selectForPosting
|
|
400
|
+
} from "@review-agent/github";
|
|
401
|
+
var wouldSay = (review2) => {
|
|
402
|
+
const output = review2.result.output;
|
|
403
|
+
if (output === null) return [];
|
|
404
|
+
const { findings } = output;
|
|
405
|
+
const completed = review2.result.failure === null;
|
|
406
|
+
const mode = review2.settings.report.mode;
|
|
407
|
+
const sayingMode = !completed ? "summary-only" : writesSummaryComment(mode) ? mode : "inline+summary";
|
|
408
|
+
const checked = review2.check === null ? null : { findings, verdicts: review2.check.verdicts };
|
|
409
|
+
const duplicates = duplicatesIn(checked);
|
|
410
|
+
const anchored = anchorFindings({
|
|
411
|
+
findings: findings.filter((_, i) => !duplicates.has(i)),
|
|
412
|
+
pr: review2.pr,
|
|
413
|
+
checkoutDir: review2.checkoutDir
|
|
414
|
+
});
|
|
415
|
+
const plan = selectForPosting({
|
|
416
|
+
anchored,
|
|
417
|
+
settings: { ...review2.settings, report: { ...review2.settings.report, mode: sayingMode } },
|
|
418
|
+
previous: null,
|
|
419
|
+
check: checked
|
|
420
|
+
});
|
|
421
|
+
return [...plan.inline.map((x) => x.finding), ...plan.summaryOnly];
|
|
422
|
+
};
|
|
423
|
+
var reportOf = (r) => r === null ? null : { kind: r.kind, conclusion: r.conclusion, url: r.url, id: r.id };
|
|
424
|
+
var postedOf = (p) => ({
|
|
425
|
+
mode: p.mode,
|
|
426
|
+
summaryUrl: p.summaryUrl,
|
|
427
|
+
summaryCommentId: p.summaryCommentId,
|
|
428
|
+
reviewId: p.reviewId,
|
|
429
|
+
comments: postedCommentsOf(p),
|
|
430
|
+
counts: { ...p.counts }
|
|
431
|
+
});
|
|
432
|
+
var resultOf = (s) => {
|
|
433
|
+
switch (s.kind) {
|
|
434
|
+
case "reviewed":
|
|
435
|
+
return {
|
|
436
|
+
kind: "reviewed",
|
|
437
|
+
record: s.record,
|
|
438
|
+
findings: s.published === null ? wouldSay(s.review) : s.published.findings,
|
|
439
|
+
posted: s.published === null ? null : postedOf(s.published),
|
|
440
|
+
report: reportOf(s.report)
|
|
441
|
+
};
|
|
442
|
+
case "unfinished":
|
|
443
|
+
return {
|
|
444
|
+
kind: "unfinished",
|
|
445
|
+
record: s.record,
|
|
446
|
+
reason: s.reason,
|
|
447
|
+
spentUsd: s.spentUsd,
|
|
448
|
+
retryable: s.retryable,
|
|
449
|
+
stopped: s.stopped,
|
|
450
|
+
posted: s.published === null ? null : postedOf(s.published),
|
|
451
|
+
report: reportOf(s.report)
|
|
452
|
+
};
|
|
453
|
+
case "failed":
|
|
454
|
+
return {
|
|
455
|
+
kind: "failed",
|
|
456
|
+
stage: s.stage,
|
|
457
|
+
reason: s.reason,
|
|
458
|
+
spentUsd: s.spentUsd,
|
|
459
|
+
retryable: s.retryable,
|
|
460
|
+
stopped: s.stopped,
|
|
461
|
+
record: s.review?.record ?? null,
|
|
462
|
+
findings: s.review === null ? null : wouldSay(s.review),
|
|
463
|
+
report: reportOf(s.report),
|
|
464
|
+
cause: s.cause
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
// src/review.ts
|
|
470
|
+
var messageOf = (e) => e instanceof Error ? e.message : String(e);
|
|
471
|
+
var failedBeforeModel = (reason, cause) => ({
|
|
472
|
+
kind: "failed",
|
|
473
|
+
stage: "before-model",
|
|
474
|
+
reason,
|
|
475
|
+
spentUsd: 0,
|
|
476
|
+
retryable: true,
|
|
477
|
+
stopped: null,
|
|
478
|
+
record: null,
|
|
479
|
+
findings: null,
|
|
480
|
+
report: null,
|
|
481
|
+
cause
|
|
482
|
+
});
|
|
483
|
+
var cancelled = () => ({
|
|
484
|
+
kind: "failed",
|
|
485
|
+
stage: "before-model",
|
|
486
|
+
reason: "the review was cancelled before it started",
|
|
487
|
+
spentUsd: 0,
|
|
488
|
+
retryable: false,
|
|
489
|
+
stopped: "cancelled",
|
|
490
|
+
record: null,
|
|
491
|
+
findings: null,
|
|
492
|
+
report: null,
|
|
493
|
+
cause: void 0
|
|
494
|
+
});
|
|
495
|
+
var fromPrepare = (f) => ({
|
|
496
|
+
kind: "failed",
|
|
497
|
+
stage: f.stage,
|
|
498
|
+
reason: f.reason,
|
|
499
|
+
spentUsd: f.spentUsd,
|
|
500
|
+
retryable: f.retryable,
|
|
501
|
+
stopped: f.stopped,
|
|
502
|
+
record: null,
|
|
503
|
+
findings: null,
|
|
504
|
+
report: null,
|
|
505
|
+
cause: f.cause
|
|
506
|
+
});
|
|
507
|
+
async function reviewPullRequest(options) {
|
|
508
|
+
const o = normalise(options, process.env);
|
|
509
|
+
const progress = progressFor({ repo: `${o.owner}/${o.repo}`, pr: o.pr, onProgress: o.onProgress });
|
|
510
|
+
const result = await review(o, progress);
|
|
511
|
+
progress.stage({ stage: "done", result: result.kind });
|
|
512
|
+
return result;
|
|
513
|
+
}
|
|
514
|
+
var review = async (o, progress) => {
|
|
515
|
+
const aborted = () => o.signal?.aborted === true;
|
|
516
|
+
const made = [];
|
|
517
|
+
try {
|
|
518
|
+
if (aborted()) return cancelled();
|
|
519
|
+
progress.stage({ stage: "preparing" });
|
|
520
|
+
const prepared = await prepareReview({
|
|
521
|
+
octokit: o.octokit,
|
|
522
|
+
owner: o.owner,
|
|
523
|
+
repo: o.repo,
|
|
524
|
+
number: o.pr,
|
|
525
|
+
deployment: o.deployment,
|
|
526
|
+
ask: o.ask
|
|
527
|
+
});
|
|
528
|
+
if (prepared.kind === "failed") return fromPrepare(prepared);
|
|
529
|
+
if (aborted()) return cancelled();
|
|
530
|
+
if (o.authorize !== null && o.requestedBy !== null) {
|
|
531
|
+
let allowed;
|
|
532
|
+
try {
|
|
533
|
+
allowed = await o.authorize({
|
|
534
|
+
octokit: o.octokit,
|
|
535
|
+
owner: o.owner,
|
|
536
|
+
repo: o.repo,
|
|
537
|
+
pr: o.pr,
|
|
538
|
+
username: o.requestedBy,
|
|
539
|
+
headSha: prepared.pr.headSha
|
|
540
|
+
});
|
|
541
|
+
} catch (e) {
|
|
542
|
+
return failedBeforeModel(`the review could not be authorized: ${saidSafely(messageOf(e))}`, e);
|
|
543
|
+
}
|
|
544
|
+
if (typeof allowed !== "boolean") {
|
|
545
|
+
throw new TypeError(`authorize must answer true or false, and answered ${typeof allowed}`);
|
|
546
|
+
}
|
|
547
|
+
if (!allowed) {
|
|
548
|
+
return { kind: "refused", reason: `authorize did not allow @${o.requestedBy}'s request; nothing was downloaded or spent` };
|
|
549
|
+
}
|
|
550
|
+
if (aborted()) return cancelled();
|
|
551
|
+
}
|
|
552
|
+
let reporter;
|
|
553
|
+
let selfLogin;
|
|
554
|
+
if (o.post) {
|
|
555
|
+
try {
|
|
556
|
+
const identity = await tokenIdentityOf(o.octokit);
|
|
557
|
+
reporter = watchedReporter(await reporterFor({ octokit: o.octokit, want: o.report, identity }), progress);
|
|
558
|
+
if (identity.kind === "user") selfLogin = identity.login;
|
|
559
|
+
} catch (e) {
|
|
560
|
+
return failedBeforeModel(`GitHub would not say whose token this is: ${saidSafely(messageOf(e))}`, e);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
const runReviewDeps = watchedDeps(defaultRunReviewDeps({
|
|
564
|
+
note: progress.note,
|
|
565
|
+
...o.workDir !== null && { workDir: o.workDir },
|
|
566
|
+
...o.skillCacheDir !== null && { skillCacheDir: o.skillCacheDir },
|
|
567
|
+
...o.focusConcurrency !== null && { concurrency: o.focusConcurrency }
|
|
568
|
+
}), progress, made);
|
|
569
|
+
const session = await runReviewSession({
|
|
570
|
+
prepared,
|
|
571
|
+
post: o.post,
|
|
572
|
+
...reporter !== void 0 && { reporter },
|
|
573
|
+
via: { kind: "sdk" },
|
|
574
|
+
...selfLogin !== void 0 && { selfLogin },
|
|
575
|
+
// The credential travels with this review and no other (spec 0.2.0 §3.2): the Claude process
|
|
576
|
+
// gets it and nothing ambient. Never written to `process.env`, where a concurrent review would
|
|
577
|
+
// read it.
|
|
578
|
+
runtime: { credential: o.credential, ...o.bin !== null && { bin: o.bin } },
|
|
579
|
+
...o.signal !== null && { signal: o.signal },
|
|
580
|
+
deadlineMs: o.deadlineMs,
|
|
581
|
+
// Removed below, with every other checkout this review made.
|
|
582
|
+
keepCheckout: true,
|
|
583
|
+
deps: { runReviewDeps, publishReview: watchedPublish(progress), log: progress.log }
|
|
584
|
+
});
|
|
585
|
+
return resultOf(session);
|
|
586
|
+
} finally {
|
|
587
|
+
for (const dir of made) await removeCheckout(dir);
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
|
|
591
|
+
// src/index.ts
|
|
592
|
+
import {
|
|
593
|
+
DEFAULT_SESSION_DEADLINE_MS as DEFAULT_SESSION_DEADLINE_MS2,
|
|
594
|
+
pushAccessOf
|
|
595
|
+
} from "@review-agent/github";
|
|
596
|
+
import {
|
|
597
|
+
defaultDeploymentConfig as defaultDeploymentConfig2,
|
|
598
|
+
SecretValue as SecretValue2
|
|
599
|
+
} from "@review-agent/core";
|
|
600
|
+
export {
|
|
601
|
+
DEFAULT_SESSION_DEADLINE_MS2 as DEFAULT_SESSION_DEADLINE_MS,
|
|
602
|
+
SecretValue2 as SecretValue,
|
|
603
|
+
defaultDeploymentConfig2 as defaultDeploymentConfig,
|
|
604
|
+
pushAccessOf,
|
|
605
|
+
reviewPullRequest
|
|
606
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@review-agent/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=22"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/crissmoldovan/review-agent.git",
|
|
12
|
+
"directory": "packages/sdk"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"main": "./dist/index.js",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@octokit/core": "^7.0.8",
|
|
29
|
+
"@review-agent/core": "0.2.1",
|
|
30
|
+
"@review-agent/github": "0.2.1"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"tar": "^7.4.3"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsup",
|
|
37
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
38
|
+
},
|
|
39
|
+
"types": "./dist/index.d.ts"
|
|
6
40
|
}
|