@github-tools/sdk 1.4.0 → 1.6.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.
@@ -1,15 +1,64 @@
1
- import { createGithubTools } from "./index.mjs";
1
+ import { createHash } from "crypto";
2
2
  import { ToolLoopAgent, tool } from "ai";
3
3
  import { z } from "zod";
4
4
  import { Octokit } from "octokit";
5
-
6
5
  //#region src/client.ts
7
6
  function createOctokit(token) {
8
7
  return new Octokit({ auth: token });
9
8
  }
10
-
11
9
  //#endregion
12
10
  //#region src/tools/repository.ts
11
+ const CREATE_COMMIT_ON_BRANCH_MUTATION = `
12
+ mutation CreateCommitOnBranch($input: CreateCommitOnBranchInput!) {
13
+ createCommitOnBranch(input: $input) { commit { oid url } }
14
+ }
15
+ `;
16
+ var CreateCommitOnBranchError = class extends Error {
17
+ constructor(message, reason) {
18
+ super(message);
19
+ this.reason = reason;
20
+ this.name = "CreateCommitOnBranchError";
21
+ }
22
+ };
23
+ async function createCommitOnBranch(octokit, input) {
24
+ try {
25
+ const result = await octokit.graphql(CREATE_COMMIT_ON_BRANCH_MUTATION, { input: {
26
+ branch: {
27
+ repositoryNameWithOwner: `${input.owner}/${input.repo}`,
28
+ branchName: input.branch
29
+ },
30
+ expectedHeadOid: input.expectedHeadOid,
31
+ message: input.body ? {
32
+ headline: input.headline,
33
+ body: input.body
34
+ } : { headline: input.headline },
35
+ fileChanges: {
36
+ additions: input.additions,
37
+ deletions: input.deletions
38
+ }
39
+ } });
40
+ return {
41
+ commitSha: result.createCommitOnBranch.commit.oid,
42
+ commitUrl: result.createCommitOnBranch.commit.url
43
+ };
44
+ } catch (error) {
45
+ const err = error;
46
+ const types = new Set((err.errors ?? []).map((e) => e.type));
47
+ if (types.has("STALE_DATA")) throw new CreateCommitOnBranchError(err.message ?? "Stale data", "stale_data");
48
+ if (types.has("FORBIDDEN") || types.has("INSUFFICIENT_SCOPES") || err.status === 401 || err.status === 403) throw new CreateCommitOnBranchError(err.message ?? "Forbidden", "forbidden");
49
+ throw error;
50
+ }
51
+ }
52
+ /** Compute the git blob SHA (SHA-1 of "blob {size}\0{content}") for a given string. */
53
+ function gitBlobSha(content) {
54
+ const buf = Buffer.from(content);
55
+ return createHash("sha1").update(`blob ${buf.length}\0`).update(buf).digest("hex");
56
+ }
57
+ function composeCommitMessage(message, coAuthors) {
58
+ if (!coAuthors?.length) return message;
59
+ const trailers = coAuthors.map(({ name, email }) => `Co-authored-by: ${name} <${email}>`).join("\n");
60
+ return message ? `${message}\n\n${trailers}` : trailers;
61
+ }
13
62
  async function getRepositoryStep({ token, owner, repo }) {
14
63
  "use step";
15
64
  const { data } = await createOctokit(token).rest.repos.get({
@@ -230,18 +279,56 @@ const createRepository = (token, { needsApproval = true } = {}) => tool({
230
279
  ...args
231
280
  })
232
281
  });
233
- async function createOrUpdateFileStep({ token, owner, repo, path, message, content, branch, sha }) {
282
+ async function createOrUpdateFileStep({ token, owner, repo, path, message, content, branch, sha, author, committer, coAuthors }) {
234
283
  "use step";
235
284
  const octokit = createOctokit(token);
285
+ if (!author && !committer) try {
286
+ const branchName = branch || (await octokit.rest.repos.get({
287
+ owner,
288
+ repo
289
+ })).data.default_branch;
290
+ const { data: ref } = await octokit.rest.git.getRef({
291
+ owner,
292
+ repo,
293
+ ref: `heads/${branchName}`
294
+ });
295
+ const [headline, ...rest] = composeCommitMessage(message, coAuthors).split("\n");
296
+ const body = rest.join("\n").replace(/^\n+/, "") || void 0;
297
+ const result = await createCommitOnBranch(octokit, {
298
+ owner,
299
+ repo,
300
+ branch: branchName,
301
+ expectedHeadOid: ref.object.sha,
302
+ headline,
303
+ body,
304
+ additions: [{
305
+ path,
306
+ contents: Buffer.from(content).toString("base64")
307
+ }]
308
+ });
309
+ return {
310
+ path,
311
+ sha: gitBlobSha(content),
312
+ commitSha: result.commitSha,
313
+ commitUrl: result.commitUrl
314
+ };
315
+ } catch (error) {
316
+ if (error instanceof CreateCommitOnBranchError && error.reason === "stale_data") throw error;
317
+ const msg = error instanceof Error ? error.message : String(error);
318
+ console.warn(`[github-tools] Signed commit failed, using REST API: ${msg}`);
319
+ }
236
320
  const encoded = Buffer.from(content).toString("base64");
321
+ const finalMessage = composeCommitMessage(message, coAuthors);
237
322
  const { data } = await octokit.rest.repos.createOrUpdateFileContents({
238
323
  owner,
239
324
  repo,
240
325
  path,
241
- message,
326
+ message: finalMessage,
242
327
  content: encoded,
243
328
  branch,
244
- sha
329
+ sha,
330
+ author,
331
+ committer
245
332
  });
246
333
  return {
247
334
  path: data.content?.path,
@@ -250,7 +337,7 @@ async function createOrUpdateFileStep({ token, owner, repo, path, message, conte
250
337
  commitUrl: data.commit.html_url
251
338
  };
252
339
  }
253
- const createOrUpdateFile = (token, { needsApproval = true } = {}) => tool({
340
+ const createOrUpdateFile = (token, { needsApproval = true, author, committer, coAuthors } = {}) => tool({
254
341
  description: "Create or update a file in a GitHub repository. Provide the SHA when updating an existing file.",
255
342
  needsApproval,
256
343
  inputSchema: z.object({
@@ -264,10 +351,12 @@ const createOrUpdateFile = (token, { needsApproval = true } = {}) => tool({
264
351
  }),
265
352
  execute: async (args) => createOrUpdateFileStep({
266
353
  token,
354
+ author,
355
+ committer,
356
+ coAuthors,
267
357
  ...args
268
358
  })
269
359
  });
270
-
271
360
  //#endregion
272
361
  //#region src/tools/pull-requests.ts
273
362
  async function listPullRequestsStep({ token, owner, repo, state, perPage }) {
@@ -385,14 +474,16 @@ const createPullRequest = (token, { needsApproval = true } = {}) => tool({
385
474
  ...args
386
475
  })
387
476
  });
388
- async function mergePullRequestStep({ token, owner, repo, pullNumber, commitTitle, commitMessage, mergeMethod }) {
477
+ async function mergePullRequestStep({ token, owner, repo, pullNumber, commitTitle, commitMessage, mergeMethod, coAuthors }) {
389
478
  "use step";
390
- const { data } = await createOctokit(token).rest.pulls.merge({
479
+ const octokit = createOctokit(token);
480
+ const finalMessage = composeCommitMessage(commitMessage ?? "", coAuthors) || void 0;
481
+ const { data } = await octokit.rest.pulls.merge({
391
482
  owner,
392
483
  repo,
393
484
  pull_number: pullNumber,
394
485
  commit_title: commitTitle,
395
- commit_message: commitMessage,
486
+ commit_message: finalMessage,
396
487
  merge_method: mergeMethod
397
488
  });
398
489
  return {
@@ -401,7 +492,7 @@ async function mergePullRequestStep({ token, owner, repo, pullNumber, commitTitl
401
492
  sha: data.sha
402
493
  };
403
494
  }
404
- const mergePullRequest = (token, { needsApproval = true } = {}) => tool({
495
+ const mergePullRequest = (token, { needsApproval = true, coAuthors } = {}) => tool({
405
496
  description: "Merge a pull request",
406
497
  needsApproval,
407
498
  inputSchema: z.object({
@@ -418,6 +509,7 @@ const mergePullRequest = (token, { needsApproval = true } = {}) => tool({
418
509
  }),
419
510
  execute: async (args) => mergePullRequestStep({
420
511
  token,
512
+ coAuthors,
421
513
  ...args
422
514
  })
423
515
  });
@@ -559,7 +651,6 @@ const createPullRequestReview = (token, { needsApproval = true } = {}) => tool({
559
651
  ...args
560
652
  })
561
653
  });
562
-
563
654
  //#endregion
564
655
  //#region src/tools/issues.ts
565
656
  async function listIssuesStep({ token, owner, repo, state, labels, perPage }) {
@@ -812,7 +903,6 @@ const removeLabel = (token, { needsApproval = true } = {}) => tool({
812
903
  ...args
813
904
  })
814
905
  });
815
-
816
906
  //#endregion
817
907
  //#region src/tools/search.ts
818
908
  async function searchCodeStep({ token, query, perPage }) {
@@ -883,7 +973,6 @@ const searchRepositories = (token) => tool({
883
973
  ...args
884
974
  })
885
975
  });
886
-
887
976
  //#endregion
888
977
  //#region src/tools/commits.ts
889
978
  const BLAME_QUERY = `
@@ -1061,7 +1150,6 @@ const getBlame = (token) => tool({
1061
1150
  ...args
1062
1151
  })
1063
1152
  });
1064
-
1065
1153
  //#endregion
1066
1154
  //#region src/tools/gists.ts
1067
1155
  async function listGistsStep({ token, username, perPage, page }) {
@@ -1259,7 +1347,6 @@ const createGistComment = (token, { needsApproval = true } = {}) => tool({
1259
1347
  ...args
1260
1348
  })
1261
1349
  });
1262
-
1263
1350
  //#endregion
1264
1351
  //#region src/tools/workflows.ts
1265
1352
  async function listWorkflowsStep({ token, owner, repo, perPage, page }) {
@@ -1538,7 +1625,6 @@ const rerunWorkflowRun = (token, { needsApproval = true } = {}) => tool({
1538
1625
  ...args
1539
1626
  })
1540
1627
  });
1541
-
1542
1628
  //#endregion
1543
1629
  //#region src/agents.ts
1544
1630
  const SHARED_RULES = `When a tool execution is denied by the user, do not retry it. Briefly acknowledge the decision and move on.`;
@@ -1627,11 +1713,14 @@ function resolveInstructions(options) {
1627
1713
  * const result = await agent.generate({ prompt: 'Review PR #42 on vercel/ai' })
1628
1714
  * ```
1629
1715
  */
1630
- function createGithubAgent({ token, preset, requireApproval, instructions, additionalInstructions, ...agentOptions }) {
1716
+ function createGithubAgent({ token, preset, requireApproval, instructions, additionalInstructions, author, committer, coAuthors, ...agentOptions }) {
1631
1717
  const tools = createGithubTools({
1632
1718
  token,
1633
1719
  requireApproval,
1634
- preset
1720
+ preset,
1721
+ author,
1722
+ committer,
1723
+ coAuthors
1635
1724
  });
1636
1725
  return new ToolLoopAgent({
1637
1726
  ...agentOptions,
@@ -1643,7 +1732,230 @@ function createGithubAgent({ token, preset, requireApproval, instructions, addit
1643
1732
  })
1644
1733
  });
1645
1734
  }
1646
-
1647
1735
  //#endregion
1648
- export { addPullRequestComment as A, createRepository as B, addLabels as C, listIssues as D, getIssue as E, listPullRequestReviews as F, createOctokit as G, getFileContent as H, listPullRequests as I, mergePullRequest as L, createPullRequestReview as M, getPullRequest as N, listLabels as O, listPullRequestFiles as P, createBranch as R, addIssueComment as S, createIssue as T, getRepository as U, forkRepository as V, listBranches as W, getBlame as _, listWorkflowJobs as a, searchCode as b, rerunWorkflowRun as c, createGistComment as d, deleteGist as f, updateGist as g, listGists as h, getWorkflowRun as i, createPullRequest as j, removeLabel as k, triggerWorkflow as l, listGistComments as m, resolveInstructions as n, listWorkflowRuns as o, getGist as p, cancelWorkflowRun as r, listWorkflows as s, createGithubAgent as t, createGist as u, getCommit as v, closeIssue as w, searchRepositories as x, listCommits as y, createOrUpdateFile as z };
1649
- //# sourceMappingURL=agents-D9aKsd_7.mjs.map
1736
+ //#region src/index.ts
1737
+ const PRESET_TOOLS = {
1738
+ "code-review": [
1739
+ "getPullRequest",
1740
+ "listPullRequests",
1741
+ "listPullRequestFiles",
1742
+ "listPullRequestReviews",
1743
+ "getFileContent",
1744
+ "listCommits",
1745
+ "getCommit",
1746
+ "getBlame",
1747
+ "getRepository",
1748
+ "listBranches",
1749
+ "searchCode",
1750
+ "addPullRequestComment",
1751
+ "createPullRequestReview"
1752
+ ],
1753
+ "issue-triage": [
1754
+ "listIssues",
1755
+ "getIssue",
1756
+ "createIssue",
1757
+ "addIssueComment",
1758
+ "closeIssue",
1759
+ "listLabels",
1760
+ "addLabels",
1761
+ "removeLabel",
1762
+ "getRepository",
1763
+ "searchRepositories",
1764
+ "searchCode"
1765
+ ],
1766
+ "ci-ops": [
1767
+ "getRepository",
1768
+ "listBranches",
1769
+ "listCommits",
1770
+ "getCommit",
1771
+ "listWorkflows",
1772
+ "listWorkflowRuns",
1773
+ "getWorkflowRun",
1774
+ "listWorkflowJobs",
1775
+ "triggerWorkflow",
1776
+ "cancelWorkflowRun",
1777
+ "rerunWorkflowRun"
1778
+ ],
1779
+ "repo-explorer": [
1780
+ "getRepository",
1781
+ "listBranches",
1782
+ "getFileContent",
1783
+ "listPullRequests",
1784
+ "getPullRequest",
1785
+ "listPullRequestFiles",
1786
+ "listPullRequestReviews",
1787
+ "listIssues",
1788
+ "getIssue",
1789
+ "listLabels",
1790
+ "listCommits",
1791
+ "getCommit",
1792
+ "getBlame",
1793
+ "searchCode",
1794
+ "searchRepositories",
1795
+ "listGists",
1796
+ "getGist",
1797
+ "listGistComments",
1798
+ "listWorkflows",
1799
+ "listWorkflowRuns",
1800
+ "getWorkflowRun",
1801
+ "listWorkflowJobs"
1802
+ ],
1803
+ "maintainer": [
1804
+ "getRepository",
1805
+ "listBranches",
1806
+ "getFileContent",
1807
+ "createBranch",
1808
+ "forkRepository",
1809
+ "createRepository",
1810
+ "createOrUpdateFile",
1811
+ "listPullRequests",
1812
+ "getPullRequest",
1813
+ "listPullRequestFiles",
1814
+ "listPullRequestReviews",
1815
+ "createPullRequest",
1816
+ "mergePullRequest",
1817
+ "addPullRequestComment",
1818
+ "createPullRequestReview",
1819
+ "listIssues",
1820
+ "getIssue",
1821
+ "createIssue",
1822
+ "addIssueComment",
1823
+ "closeIssue",
1824
+ "listLabels",
1825
+ "addLabels",
1826
+ "removeLabel",
1827
+ "listCommits",
1828
+ "getCommit",
1829
+ "getBlame",
1830
+ "searchCode",
1831
+ "searchRepositories",
1832
+ "listGists",
1833
+ "getGist",
1834
+ "listGistComments",
1835
+ "createGist",
1836
+ "updateGist",
1837
+ "deleteGist",
1838
+ "createGistComment",
1839
+ "listWorkflows",
1840
+ "listWorkflowRuns",
1841
+ "getWorkflowRun",
1842
+ "listWorkflowJobs",
1843
+ "triggerWorkflow",
1844
+ "cancelWorkflowRun",
1845
+ "rerunWorkflowRun"
1846
+ ]
1847
+ };
1848
+ function resolveApproval(toolName, config) {
1849
+ if (typeof config === "boolean") return config;
1850
+ return config[toolName] ?? true;
1851
+ }
1852
+ function resolvePresetTools(preset) {
1853
+ if (!preset) return null;
1854
+ const presets = Array.isArray(preset) ? preset : [preset];
1855
+ const tools = /* @__PURE__ */ new Set();
1856
+ for (const p of presets) for (const t of PRESET_TOOLS[p]) tools.add(t);
1857
+ return tools;
1858
+ }
1859
+ /**
1860
+ * Create a set of GitHub tools for the Vercel AI SDK.
1861
+ *
1862
+ * Write operations require user approval by default.
1863
+ * Control this globally or per-tool via `requireApproval`.
1864
+ * Use `preset` to get only the tools you need.
1865
+ *
1866
+ * @example
1867
+ * ```ts
1868
+ * // All tools (default)
1869
+ * createGithubTools({ token })
1870
+ *
1871
+ * // Code-review agent — only PR & commit tools
1872
+ * createGithubTools({ token, preset: 'code-review' })
1873
+ *
1874
+ * // Combine presets
1875
+ * createGithubTools({ token, preset: ['code-review', 'issue-triage'] })
1876
+ *
1877
+ * // Granular approval
1878
+ * createGithubTools({
1879
+ * token,
1880
+ * preset: 'maintainer',
1881
+ * requireApproval: {
1882
+ * mergePullRequest: true,
1883
+ * createOrUpdateFile: true,
1884
+ * addPullRequestComment: false,
1885
+ * }
1886
+ * })
1887
+ * ```
1888
+ */
1889
+ function createGithubTools({ token, requireApproval = true, preset, overrides, author, committer, coAuthors } = {}) {
1890
+ const resolvedToken = token || process.env.GITHUB_TOKEN;
1891
+ if (!resolvedToken) throw new Error("GitHub token is required. Pass it as `token` or set the GITHUB_TOKEN environment variable.");
1892
+ const approval = (name) => ({ needsApproval: resolveApproval(name, requireApproval) });
1893
+ const allowed = preset ? resolvePresetTools(preset) : null;
1894
+ const allTools = {
1895
+ getRepository: getRepository(resolvedToken),
1896
+ listBranches: listBranches(resolvedToken),
1897
+ getFileContent: getFileContent(resolvedToken),
1898
+ listPullRequests: listPullRequests(resolvedToken),
1899
+ getPullRequest: getPullRequest(resolvedToken),
1900
+ listIssues: listIssues(resolvedToken),
1901
+ getIssue: getIssue(resolvedToken),
1902
+ searchCode: searchCode(resolvedToken),
1903
+ searchRepositories: searchRepositories(resolvedToken),
1904
+ listCommits: listCommits(resolvedToken),
1905
+ getCommit: getCommit(resolvedToken),
1906
+ getBlame: getBlame(resolvedToken),
1907
+ createBranch: createBranch(resolvedToken, approval("createBranch")),
1908
+ forkRepository: forkRepository(resolvedToken, approval("forkRepository")),
1909
+ createRepository: createRepository(resolvedToken, approval("createRepository")),
1910
+ createOrUpdateFile: createOrUpdateFile(resolvedToken, {
1911
+ ...approval("createOrUpdateFile"),
1912
+ author,
1913
+ committer,
1914
+ coAuthors
1915
+ }),
1916
+ createPullRequest: createPullRequest(resolvedToken, approval("createPullRequest")),
1917
+ mergePullRequest: mergePullRequest(resolvedToken, {
1918
+ ...approval("mergePullRequest"),
1919
+ coAuthors
1920
+ }),
1921
+ addPullRequestComment: addPullRequestComment(resolvedToken, approval("addPullRequestComment")),
1922
+ listPullRequestFiles: listPullRequestFiles(resolvedToken),
1923
+ listPullRequestReviews: listPullRequestReviews(resolvedToken),
1924
+ createPullRequestReview: createPullRequestReview(resolvedToken, approval("createPullRequestReview")),
1925
+ createIssue: createIssue(resolvedToken, approval("createIssue")),
1926
+ addIssueComment: addIssueComment(resolvedToken, approval("addIssueComment")),
1927
+ closeIssue: closeIssue(resolvedToken, approval("closeIssue")),
1928
+ listLabels: listLabels(resolvedToken),
1929
+ addLabels: addLabels(resolvedToken, approval("addLabels")),
1930
+ removeLabel: removeLabel(resolvedToken, approval("removeLabel")),
1931
+ listGists: listGists(resolvedToken),
1932
+ getGist: getGist(resolvedToken),
1933
+ listGistComments: listGistComments(resolvedToken),
1934
+ createGist: createGist(resolvedToken, approval("createGist")),
1935
+ updateGist: updateGist(resolvedToken, approval("updateGist")),
1936
+ deleteGist: deleteGist(resolvedToken, approval("deleteGist")),
1937
+ createGistComment: createGistComment(resolvedToken, approval("createGistComment")),
1938
+ listWorkflows: listWorkflows(resolvedToken),
1939
+ listWorkflowRuns: listWorkflowRuns(resolvedToken),
1940
+ getWorkflowRun: getWorkflowRun(resolvedToken),
1941
+ listWorkflowJobs: listWorkflowJobs(resolvedToken),
1942
+ triggerWorkflow: triggerWorkflow(resolvedToken, approval("triggerWorkflow")),
1943
+ cancelWorkflowRun: cancelWorkflowRun(resolvedToken, approval("cancelWorkflowRun")),
1944
+ rerunWorkflowRun: rerunWorkflowRun(resolvedToken, approval("rerunWorkflowRun"))
1945
+ };
1946
+ if (overrides) {
1947
+ for (const [name, toolOverrides] of Object.entries(overrides)) if (name in allTools && toolOverrides) {
1948
+ const key = name;
1949
+ Object.assign(allTools, { [key]: {
1950
+ ...allTools[key],
1951
+ ...toolOverrides
1952
+ } });
1953
+ }
1954
+ }
1955
+ if (!allowed) return allTools;
1956
+ return Object.fromEntries(Object.entries(allTools).filter(([name]) => allowed.has(name)));
1957
+ }
1958
+ //#endregion
1959
+ export { removeLabel as A, createOrUpdateFile as B, addIssueComment as C, getIssue as D, createIssue as E, listPullRequestFiles as F, listBranches as G, forkRepository as H, listPullRequestReviews as I, createOctokit as K, listPullRequests as L, createPullRequest as M, createPullRequestReview as N, listIssues as O, getPullRequest as P, mergePullRequest as R, searchRepositories as S, closeIssue as T, getFileContent as U, createRepository as V, getRepository as W, updateGist as _, getWorkflowRun as a, listCommits as b, listWorkflows as c, createGist as d, createGistComment as f, listGists as g, listGistComments as h, cancelWorkflowRun as i, addPullRequestComment as j, listLabels as k, rerunWorkflowRun as l, getGist as m, createGithubAgent as n, listWorkflowJobs as o, deleteGist as p, resolveInstructions as r, listWorkflowRuns as s, createGithubTools as t, triggerWorkflow as u, getBlame as v, addLabels as w, searchCode as x, getCommit as y, createBranch as z };
1960
+
1961
+ //# sourceMappingURL=src--pAdGYqY.mjs.map