@loopstack/github-module 0.1.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.
Files changed (142) hide show
  1. package/README.md +67 -0
  2. package/dist/github-oauth.provider.d.ts +19 -0
  3. package/dist/github-oauth.provider.d.ts.map +1 -0
  4. package/dist/github-oauth.provider.js +91 -0
  5. package/dist/github-oauth.provider.js.map +1 -0
  6. package/dist/github.module.d.ts +3 -0
  7. package/dist/github.module.d.ts.map +1 -0
  8. package/dist/github.module.js +75 -0
  9. package/dist/github.module.js.map +1 -0
  10. package/dist/index.d.ts +28 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/index.js +44 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/tools/actions/github-get-workflow-run.tool.d.ts +13 -0
  15. package/dist/tools/actions/github-get-workflow-run.tool.d.ts.map +1 -0
  16. package/dist/tools/actions/github-get-workflow-run.tool.js +106 -0
  17. package/dist/tools/actions/github-get-workflow-run.tool.js.map +1 -0
  18. package/dist/tools/actions/github-list-workflow-runs.tool.d.ts +16 -0
  19. package/dist/tools/actions/github-list-workflow-runs.tool.d.ts.map +1 -0
  20. package/dist/tools/actions/github-list-workflow-runs.tool.js +129 -0
  21. package/dist/tools/actions/github-list-workflow-runs.tool.js.map +1 -0
  22. package/dist/tools/actions/github-trigger-workflow.tool.d.ts +15 -0
  23. package/dist/tools/actions/github-trigger-workflow.tool.d.ts.map +1 -0
  24. package/dist/tools/actions/github-trigger-workflow.tool.js +101 -0
  25. package/dist/tools/actions/github-trigger-workflow.tool.js.map +1 -0
  26. package/dist/tools/content/github-create-or-update-file.tool.d.ts +17 -0
  27. package/dist/tools/content/github-create-or-update-file.tool.d.ts.map +1 -0
  28. package/dist/tools/content/github-create-or-update-file.tool.js +115 -0
  29. package/dist/tools/content/github-create-or-update-file.tool.js.map +1 -0
  30. package/dist/tools/content/github-get-commit.tool.d.ts +13 -0
  31. package/dist/tools/content/github-get-commit.tool.d.ts.map +1 -0
  32. package/dist/tools/content/github-get-commit.tool.js +113 -0
  33. package/dist/tools/content/github-get-commit.tool.js.map +1 -0
  34. package/dist/tools/content/github-get-file-content.tool.d.ts +14 -0
  35. package/dist/tools/content/github-get-file-content.tool.d.ts.map +1 -0
  36. package/dist/tools/content/github-get-file-content.tool.js +107 -0
  37. package/dist/tools/content/github-get-file-content.tool.js.map +1 -0
  38. package/dist/tools/content/github-list-directory.tool.d.ts +14 -0
  39. package/dist/tools/content/github-list-directory.tool.d.ts.map +1 -0
  40. package/dist/tools/content/github-list-directory.tool.js +102 -0
  41. package/dist/tools/content/github-list-directory.tool.js.map +1 -0
  42. package/dist/tools/issues/github-create-issue-comment.tool.d.ts +14 -0
  43. package/dist/tools/issues/github-create-issue-comment.tool.d.ts.map +1 -0
  44. package/dist/tools/issues/github-create-issue-comment.tool.js +100 -0
  45. package/dist/tools/issues/github-create-issue-comment.tool.js.map +1 -0
  46. package/dist/tools/issues/github-create-issue.tool.d.ts +16 -0
  47. package/dist/tools/issues/github-create-issue.tool.d.ts.map +1 -0
  48. package/dist/tools/issues/github-create-issue.tool.js +112 -0
  49. package/dist/tools/issues/github-create-issue.tool.js.map +1 -0
  50. package/dist/tools/issues/github-get-issue.tool.d.ts +13 -0
  51. package/dist/tools/issues/github-get-issue.tool.d.ts.map +1 -0
  52. package/dist/tools/issues/github-get-issue.tool.js +106 -0
  53. package/dist/tools/issues/github-get-issue.tool.js.map +1 -0
  54. package/dist/tools/issues/github-list-issues.tool.d.ts +17 -0
  55. package/dist/tools/issues/github-list-issues.tool.d.ts.map +1 -0
  56. package/dist/tools/issues/github-list-issues.tool.js +115 -0
  57. package/dist/tools/issues/github-list-issues.tool.js.map +1 -0
  58. package/dist/tools/pull-requests/github-create-pull-request.tool.d.ts +17 -0
  59. package/dist/tools/pull-requests/github-create-pull-request.tool.d.ts.map +1 -0
  60. package/dist/tools/pull-requests/github-create-pull-request.tool.js +113 -0
  61. package/dist/tools/pull-requests/github-create-pull-request.tool.js.map +1 -0
  62. package/dist/tools/pull-requests/github-get-pull-request.tool.d.ts +13 -0
  63. package/dist/tools/pull-requests/github-get-pull-request.tool.d.ts.map +1 -0
  64. package/dist/tools/pull-requests/github-get-pull-request.tool.js +113 -0
  65. package/dist/tools/pull-requests/github-get-pull-request.tool.js.map +1 -0
  66. package/dist/tools/pull-requests/github-list-pr-reviews.tool.d.ts +13 -0
  67. package/dist/tools/pull-requests/github-list-pr-reviews.tool.d.ts.map +1 -0
  68. package/dist/tools/pull-requests/github-list-pr-reviews.tool.js +97 -0
  69. package/dist/tools/pull-requests/github-list-pr-reviews.tool.js.map +1 -0
  70. package/dist/tools/pull-requests/github-list-pull-requests.tool.d.ts +16 -0
  71. package/dist/tools/pull-requests/github-list-pull-requests.tool.d.ts.map +1 -0
  72. package/dist/tools/pull-requests/github-list-pull-requests.tool.js +113 -0
  73. package/dist/tools/pull-requests/github-list-pull-requests.tool.js.map +1 -0
  74. package/dist/tools/pull-requests/github-merge-pull-request.tool.d.ts +16 -0
  75. package/dist/tools/pull-requests/github-merge-pull-request.tool.d.ts.map +1 -0
  76. package/dist/tools/pull-requests/github-merge-pull-request.tool.js +108 -0
  77. package/dist/tools/pull-requests/github-merge-pull-request.tool.js.map +1 -0
  78. package/dist/tools/repos/github-create-repo.tool.d.ts +14 -0
  79. package/dist/tools/repos/github-create-repo.tool.d.ts.map +1 -0
  80. package/dist/tools/repos/github-create-repo.tool.js +108 -0
  81. package/dist/tools/repos/github-create-repo.tool.js.map +1 -0
  82. package/dist/tools/repos/github-get-repo.tool.d.ts +12 -0
  83. package/dist/tools/repos/github-get-repo.tool.d.ts.map +1 -0
  84. package/dist/tools/repos/github-get-repo.tool.js +107 -0
  85. package/dist/tools/repos/github-get-repo.tool.js.map +1 -0
  86. package/dist/tools/repos/github-list-branches.tool.d.ts +13 -0
  87. package/dist/tools/repos/github-list-branches.tool.d.ts.map +1 -0
  88. package/dist/tools/repos/github-list-branches.tool.js +97 -0
  89. package/dist/tools/repos/github-list-branches.tool.js.map +1 -0
  90. package/dist/tools/repos/github-list-repos.tool.d.ts +14 -0
  91. package/dist/tools/repos/github-list-repos.tool.d.ts.map +1 -0
  92. package/dist/tools/repos/github-list-repos.tool.js +107 -0
  93. package/dist/tools/repos/github-list-repos.tool.js.map +1 -0
  94. package/dist/tools/search/github-search-code.tool.d.ts +13 -0
  95. package/dist/tools/search/github-search-code.tool.d.ts.map +1 -0
  96. package/dist/tools/search/github-search-code.tool.js +100 -0
  97. package/dist/tools/search/github-search-code.tool.js.map +1 -0
  98. package/dist/tools/search/github-search-issues.tool.d.ts +14 -0
  99. package/dist/tools/search/github-search-issues.tool.d.ts.map +1 -0
  100. package/dist/tools/search/github-search-issues.tool.js +109 -0
  101. package/dist/tools/search/github-search-issues.tool.js.map +1 -0
  102. package/dist/tools/search/github-search-repos.tool.d.ts +14 -0
  103. package/dist/tools/search/github-search-repos.tool.d.ts.map +1 -0
  104. package/dist/tools/search/github-search-repos.tool.js +106 -0
  105. package/dist/tools/search/github-search-repos.tool.js.map +1 -0
  106. package/dist/tools/users/github-get-authenticated-user.tool.d.ts +9 -0
  107. package/dist/tools/users/github-get-authenticated-user.tool.d.ts.map +1 -0
  108. package/dist/tools/users/github-get-authenticated-user.tool.js +96 -0
  109. package/dist/tools/users/github-get-authenticated-user.tool.js.map +1 -0
  110. package/dist/tools/users/github-list-user-orgs.tool.d.ts +11 -0
  111. package/dist/tools/users/github-list-user-orgs.tool.d.ts.map +1 -0
  112. package/dist/tools/users/github-list-user-orgs.tool.js +95 -0
  113. package/dist/tools/users/github-list-user-orgs.tool.js.map +1 -0
  114. package/package.json +81 -0
  115. package/src/github-oauth.provider.ts +85 -0
  116. package/src/github.module.ts +63 -0
  117. package/src/index.ts +27 -0
  118. package/src/tools/actions/github-get-workflow-run.tool.ts +115 -0
  119. package/src/tools/actions/github-list-workflow-runs.tool.ts +141 -0
  120. package/src/tools/actions/github-trigger-workflow.tool.ts +97 -0
  121. package/src/tools/content/github-create-or-update-file.tool.ts +124 -0
  122. package/src/tools/content/github-get-commit.tool.ts +124 -0
  123. package/src/tools/content/github-get-file-content.tool.ts +112 -0
  124. package/src/tools/content/github-list-directory.tool.ts +105 -0
  125. package/src/tools/issues/github-create-issue-comment.tool.ts +100 -0
  126. package/src/tools/issues/github-create-issue.tool.ts +114 -0
  127. package/src/tools/issues/github-get-issue.tool.ts +115 -0
  128. package/src/tools/issues/github-list-issues.tool.ts +126 -0
  129. package/src/tools/pull-requests/github-create-pull-request.tool.ts +119 -0
  130. package/src/tools/pull-requests/github-get-pull-request.tool.ts +128 -0
  131. package/src/tools/pull-requests/github-list-pr-reviews.tool.ts +99 -0
  132. package/src/tools/pull-requests/github-list-pull-requests.tool.ts +124 -0
  133. package/src/tools/pull-requests/github-merge-pull-request.tool.ts +108 -0
  134. package/src/tools/repos/github-create-repo.tool.ts +111 -0
  135. package/src/tools/repos/github-get-repo.tool.ts +120 -0
  136. package/src/tools/repos/github-list-branches.tool.ts +97 -0
  137. package/src/tools/repos/github-list-repos.tool.ts +115 -0
  138. package/src/tools/search/github-search-code.tool.ts +105 -0
  139. package/src/tools/search/github-search-issues.tool.ts +120 -0
  140. package/src/tools/search/github-search-repos.tool.ts +115 -0
  141. package/src/tools/users/github-get-authenticated-user.tool.ts +98 -0
  142. package/src/tools/users/github-list-user-orgs.tool.ts +94 -0
@@ -0,0 +1,124 @@
1
+ import { Inject, Logger } from '@nestjs/common';
2
+ import { z } from 'zod';
3
+ import { Input, RunContext, Tool, ToolInterface, ToolResult } from '@loopstack/common';
4
+ import { OAuthTokenStore } from '@loopstack/oauth-module';
5
+
6
+ export type GitHubCreateOrUpdateFileArgs = {
7
+ owner: string;
8
+ repo: string;
9
+ path: string;
10
+ content: string;
11
+ message: string;
12
+ sha?: string;
13
+ branch?: string;
14
+ };
15
+
16
+ @Tool({
17
+ config: {
18
+ description:
19
+ 'Creates or updates a file in a GitHub repository. Content is provided as plain text and encoded to base64 before sending. To update an existing file, provide the sha of the file being replaced. Returns { error: "unauthorized" } if no valid token is available.',
20
+ },
21
+ })
22
+ export class GitHubCreateOrUpdateFileTool implements ToolInterface {
23
+ private readonly logger = new Logger(GitHubCreateOrUpdateFileTool.name);
24
+
25
+ @Inject()
26
+ private tokenStore: OAuthTokenStore;
27
+
28
+ @Input({
29
+ schema: z
30
+ .object({
31
+ owner: z.string(),
32
+ repo: z.string(),
33
+ path: z.string(),
34
+ content: z.string(),
35
+ message: z.string(),
36
+ sha: z.string().optional(),
37
+ branch: z.string().optional(),
38
+ })
39
+ .strict(),
40
+ })
41
+ args: GitHubCreateOrUpdateFileArgs;
42
+
43
+ async execute(args: GitHubCreateOrUpdateFileArgs, ctx: RunContext): Promise<ToolResult> {
44
+ const accessToken = await this.tokenStore.getValidAccessToken(ctx.userId, 'github');
45
+
46
+ if (!accessToken) {
47
+ return {
48
+ data: {
49
+ error: 'unauthorized',
50
+ message: 'No valid GitHub token found. Please authenticate first.',
51
+ },
52
+ };
53
+ }
54
+
55
+ const requestBody: Record<string, unknown> = {
56
+ message: args.message,
57
+ content: Buffer.from(args.content).toString('base64'),
58
+ };
59
+
60
+ if (args.sha) requestBody.sha = args.sha;
61
+ if (args.branch) requestBody.branch = args.branch;
62
+
63
+ const url = `https://api.github.com/repos/${encodeURIComponent(args.owner)}/${encodeURIComponent(args.repo)}/contents/${args.path}`;
64
+ const response = await fetch(url, {
65
+ method: 'PUT',
66
+ headers: {
67
+ Authorization: `Bearer ${accessToken}`,
68
+ Accept: 'application/vnd.github+json',
69
+ 'X-GitHub-Api-Version': '2022-11-28',
70
+ 'Content-Type': 'application/json',
71
+ },
72
+ body: JSON.stringify(requestBody),
73
+ });
74
+
75
+ if (response.status === 401 || response.status === 403) {
76
+ this.logger.warn(`GitHub API returned ${response.status} for user ${ctx.userId}`);
77
+ return {
78
+ data: {
79
+ error: '401',
80
+ message: 'GitHub token was rejected. Please re-authenticate.',
81
+ },
82
+ };
83
+ }
84
+
85
+ if (!response.ok) {
86
+ const errorBody = await response.text();
87
+ this.logger.error(`GitHub API error: ${response.status} ${errorBody}`);
88
+ return {
89
+ data: {
90
+ error: 'api_error',
91
+ message: `GitHub API error: ${response.statusText}`,
92
+ },
93
+ };
94
+ }
95
+
96
+ const data = (await response.json()) as {
97
+ content: {
98
+ name: string;
99
+ path: string;
100
+ sha: string;
101
+ html_url: string;
102
+ };
103
+ commit: {
104
+ sha: string;
105
+ message: string;
106
+ };
107
+ };
108
+
109
+ return {
110
+ data: {
111
+ file: {
112
+ name: data.content.name,
113
+ path: data.content.path,
114
+ sha: data.content.sha,
115
+ htmlUrl: data.content.html_url,
116
+ },
117
+ commit: {
118
+ sha: data.commit.sha,
119
+ message: data.commit.message,
120
+ },
121
+ },
122
+ };
123
+ }
124
+ }
@@ -0,0 +1,124 @@
1
+ import { Inject, Logger } from '@nestjs/common';
2
+ import { z } from 'zod';
3
+ import { Input, RunContext, Tool, ToolInterface, ToolResult } from '@loopstack/common';
4
+ import { OAuthTokenStore } from '@loopstack/oauth-module';
5
+
6
+ export type GitHubGetCommitArgs = {
7
+ owner: string;
8
+ repo: string;
9
+ ref: string;
10
+ };
11
+
12
+ @Tool({
13
+ config: {
14
+ description:
15
+ 'Gets detailed information about a specific commit in a GitHub repository. Returns { error: "unauthorized" } if no valid token is available.',
16
+ },
17
+ })
18
+ export class GitHubGetCommitTool implements ToolInterface {
19
+ private readonly logger = new Logger(GitHubGetCommitTool.name);
20
+
21
+ @Inject()
22
+ private tokenStore: OAuthTokenStore;
23
+
24
+ @Input({
25
+ schema: z
26
+ .object({
27
+ owner: z.string(),
28
+ repo: z.string(),
29
+ ref: z.string(),
30
+ })
31
+ .strict(),
32
+ })
33
+ args: GitHubGetCommitArgs;
34
+
35
+ async execute(args: GitHubGetCommitArgs, ctx: RunContext): Promise<ToolResult> {
36
+ const accessToken = await this.tokenStore.getValidAccessToken(ctx.userId, 'github');
37
+
38
+ if (!accessToken) {
39
+ return {
40
+ data: {
41
+ error: 'unauthorized',
42
+ message: 'No valid GitHub token found. Please authenticate first.',
43
+ },
44
+ };
45
+ }
46
+
47
+ const url = `https://api.github.com/repos/${encodeURIComponent(args.owner)}/${encodeURIComponent(args.repo)}/commits/${encodeURIComponent(args.ref)}`;
48
+ const response = await fetch(url, {
49
+ headers: {
50
+ Authorization: `Bearer ${accessToken}`,
51
+ Accept: 'application/vnd.github+json',
52
+ 'X-GitHub-Api-Version': '2022-11-28',
53
+ },
54
+ });
55
+
56
+ if (response.status === 401 || response.status === 403) {
57
+ this.logger.warn(`GitHub API returned ${response.status} for user ${ctx.userId}`);
58
+ return {
59
+ data: {
60
+ error: '401',
61
+ message: 'GitHub token was rejected. Please re-authenticate.',
62
+ },
63
+ };
64
+ }
65
+
66
+ if (!response.ok) {
67
+ const body = await response.text();
68
+ this.logger.error(`GitHub API error: ${response.status} ${body}`);
69
+ return {
70
+ data: {
71
+ error: 'api_error',
72
+ message: `GitHub API error: ${response.statusText}`,
73
+ },
74
+ };
75
+ }
76
+
77
+ const data = (await response.json()) as {
78
+ sha: string;
79
+ commit: {
80
+ message: string;
81
+ author: { name: string; email: string; date: string };
82
+ committer: { name: string; email: string; date: string };
83
+ };
84
+ author: { login: string } | null;
85
+ html_url: string;
86
+ stats: { additions: number; deletions: number; total: number };
87
+ files: Array<{
88
+ filename: string;
89
+ status: string;
90
+ additions: number;
91
+ deletions: number;
92
+ changes: number;
93
+ }>;
94
+ };
95
+
96
+ return {
97
+ data: {
98
+ commit: {
99
+ sha: data.sha,
100
+ message: data.commit.message,
101
+ author: {
102
+ name: data.commit.author.name,
103
+ email: data.commit.author.email,
104
+ date: data.commit.author.date,
105
+ login: data.author?.login ?? null,
106
+ },
107
+ committer: {
108
+ name: data.commit.committer.name,
109
+ date: data.commit.committer.date,
110
+ },
111
+ htmlUrl: data.html_url,
112
+ stats: data.stats,
113
+ files: data.files.map((f) => ({
114
+ filename: f.filename,
115
+ status: f.status,
116
+ additions: f.additions,
117
+ deletions: f.deletions,
118
+ changes: f.changes,
119
+ })),
120
+ },
121
+ },
122
+ };
123
+ }
124
+ }
@@ -0,0 +1,112 @@
1
+ import { Inject, Logger } from '@nestjs/common';
2
+ import { z } from 'zod';
3
+ import { Input, RunContext, Tool, ToolInterface, ToolResult } from '@loopstack/common';
4
+ import { OAuthTokenStore } from '@loopstack/oauth-module';
5
+
6
+ export type GitHubGetFileContentArgs = {
7
+ owner: string;
8
+ repo: string;
9
+ path: string;
10
+ ref?: string;
11
+ };
12
+
13
+ @Tool({
14
+ config: {
15
+ description:
16
+ 'Gets the content of a file from a GitHub repository. Decodes base64-encoded content from the API. Returns { error: "unauthorized" } if no valid token is available.',
17
+ },
18
+ })
19
+ export class GitHubGetFileContentTool implements ToolInterface {
20
+ private readonly logger = new Logger(GitHubGetFileContentTool.name);
21
+
22
+ @Inject()
23
+ private tokenStore: OAuthTokenStore;
24
+
25
+ @Input({
26
+ schema: z
27
+ .object({
28
+ owner: z.string(),
29
+ repo: z.string(),
30
+ path: z.string(),
31
+ ref: z.string().optional(),
32
+ })
33
+ .strict(),
34
+ })
35
+ args: GitHubGetFileContentArgs;
36
+
37
+ async execute(args: GitHubGetFileContentArgs, ctx: RunContext): Promise<ToolResult> {
38
+ const accessToken = await this.tokenStore.getValidAccessToken(ctx.userId, 'github');
39
+
40
+ if (!accessToken) {
41
+ return {
42
+ data: {
43
+ error: 'unauthorized',
44
+ message: 'No valid GitHub token found. Please authenticate first.',
45
+ },
46
+ };
47
+ }
48
+
49
+ const params = new URLSearchParams();
50
+ if (args.ref) params.set('ref', args.ref);
51
+
52
+ const url = `https://api.github.com/repos/${encodeURIComponent(args.owner)}/${encodeURIComponent(args.repo)}/contents/${args.path}${params.toString() ? `?${params.toString()}` : ''}`;
53
+ const response = await fetch(url, {
54
+ headers: {
55
+ Authorization: `Bearer ${accessToken}`,
56
+ Accept: 'application/vnd.github+json',
57
+ 'X-GitHub-Api-Version': '2022-11-28',
58
+ },
59
+ });
60
+
61
+ if (response.status === 401 || response.status === 403) {
62
+ this.logger.warn(`GitHub API returned ${response.status} for user ${ctx.userId}`);
63
+ return {
64
+ data: {
65
+ error: '401',
66
+ message: 'GitHub token was rejected. Please re-authenticate.',
67
+ },
68
+ };
69
+ }
70
+
71
+ if (!response.ok) {
72
+ const body = await response.text();
73
+ this.logger.error(`GitHub API error: ${response.status} ${body}`);
74
+ return {
75
+ data: {
76
+ error: 'api_error',
77
+ message: `GitHub API error: ${response.statusText}`,
78
+ },
79
+ };
80
+ }
81
+
82
+ const data = (await response.json()) as {
83
+ name: string;
84
+ path: string;
85
+ sha: string;
86
+ size: number;
87
+ type: string;
88
+ content?: string;
89
+ encoding?: string;
90
+ html_url: string;
91
+ };
92
+
93
+ let content: string | null = null;
94
+ if (data.content && data.encoding === 'base64') {
95
+ content = Buffer.from(data.content, 'base64').toString('utf-8');
96
+ }
97
+
98
+ return {
99
+ data: {
100
+ file: {
101
+ name: data.name,
102
+ path: data.path,
103
+ sha: data.sha,
104
+ size: data.size,
105
+ type: data.type,
106
+ content,
107
+ htmlUrl: data.html_url,
108
+ },
109
+ },
110
+ };
111
+ }
112
+ }
@@ -0,0 +1,105 @@
1
+ import { Inject, Logger } from '@nestjs/common';
2
+ import { z } from 'zod';
3
+ import { Input, RunContext, Tool, ToolInterface, ToolResult } from '@loopstack/common';
4
+ import { OAuthTokenStore } from '@loopstack/oauth-module';
5
+
6
+ export type GitHubListDirectoryArgs = {
7
+ owner: string;
8
+ repo: string;
9
+ path?: string;
10
+ ref?: string;
11
+ };
12
+
13
+ @Tool({
14
+ config: {
15
+ description:
16
+ 'Lists the contents of a directory in a GitHub repository. Returns { error: "unauthorized" } if no valid token is available.',
17
+ },
18
+ })
19
+ export class GitHubListDirectoryTool implements ToolInterface {
20
+ private readonly logger = new Logger(GitHubListDirectoryTool.name);
21
+
22
+ @Inject()
23
+ private tokenStore: OAuthTokenStore;
24
+
25
+ @Input({
26
+ schema: z
27
+ .object({
28
+ owner: z.string(),
29
+ repo: z.string(),
30
+ path: z.string().default(''),
31
+ ref: z.string().optional(),
32
+ })
33
+ .strict(),
34
+ })
35
+ args: GitHubListDirectoryArgs;
36
+
37
+ async execute(args: GitHubListDirectoryArgs, ctx: RunContext): Promise<ToolResult> {
38
+ const accessToken = await this.tokenStore.getValidAccessToken(ctx.userId, 'github');
39
+
40
+ if (!accessToken) {
41
+ return {
42
+ data: {
43
+ error: 'unauthorized',
44
+ message: 'No valid GitHub token found. Please authenticate first.',
45
+ },
46
+ };
47
+ }
48
+
49
+ const params = new URLSearchParams();
50
+ if (args.ref) params.set('ref', args.ref);
51
+
52
+ const dirPath = args.path ?? '';
53
+ const url = `https://api.github.com/repos/${encodeURIComponent(args.owner)}/${encodeURIComponent(args.repo)}/contents/${dirPath}${params.toString() ? `?${params.toString()}` : ''}`;
54
+ const response = await fetch(url, {
55
+ headers: {
56
+ Authorization: `Bearer ${accessToken}`,
57
+ Accept: 'application/vnd.github+json',
58
+ 'X-GitHub-Api-Version': '2022-11-28',
59
+ },
60
+ });
61
+
62
+ if (response.status === 401 || response.status === 403) {
63
+ this.logger.warn(`GitHub API returned ${response.status} for user ${ctx.userId}`);
64
+ return {
65
+ data: {
66
+ error: '401',
67
+ message: 'GitHub token was rejected. Please re-authenticate.',
68
+ },
69
+ };
70
+ }
71
+
72
+ if (!response.ok) {
73
+ const body = await response.text();
74
+ this.logger.error(`GitHub API error: ${response.status} ${body}`);
75
+ return {
76
+ data: {
77
+ error: 'api_error',
78
+ message: `GitHub API error: ${response.statusText}`,
79
+ },
80
+ };
81
+ }
82
+
83
+ const data = (await response.json()) as Array<{
84
+ name: string;
85
+ path: string;
86
+ sha: string;
87
+ size: number;
88
+ type: string;
89
+ html_url: string;
90
+ }>;
91
+
92
+ const entries = data.map((entry) => ({
93
+ name: entry.name,
94
+ path: entry.path,
95
+ sha: entry.sha,
96
+ size: entry.size,
97
+ type: entry.type,
98
+ htmlUrl: entry.html_url,
99
+ }));
100
+
101
+ return {
102
+ data: { entries },
103
+ };
104
+ }
105
+ }
@@ -0,0 +1,100 @@
1
+ import { Inject, Logger } from '@nestjs/common';
2
+ import { z } from 'zod';
3
+ import { Input, RunContext, Tool, ToolInterface, ToolResult } from '@loopstack/common';
4
+ import { OAuthTokenStore } from '@loopstack/oauth-module';
5
+
6
+ export type GitHubCreateIssueCommentArgs = {
7
+ owner: string;
8
+ repo: string;
9
+ issueNumber: number;
10
+ body: string;
11
+ };
12
+
13
+ @Tool({
14
+ config: {
15
+ description:
16
+ 'Creates a comment on a GitHub issue or pull request. Returns { error: "unauthorized" } if no valid token is available.',
17
+ },
18
+ })
19
+ export class GitHubCreateIssueCommentTool implements ToolInterface {
20
+ private readonly logger = new Logger(GitHubCreateIssueCommentTool.name);
21
+
22
+ @Inject()
23
+ private tokenStore: OAuthTokenStore;
24
+
25
+ @Input({
26
+ schema: z
27
+ .object({
28
+ owner: z.string(),
29
+ repo: z.string(),
30
+ issueNumber: z.number(),
31
+ body: z.string(),
32
+ })
33
+ .strict(),
34
+ })
35
+ args: GitHubCreateIssueCommentArgs;
36
+
37
+ async execute(args: GitHubCreateIssueCommentArgs, ctx: RunContext): Promise<ToolResult> {
38
+ const accessToken = await this.tokenStore.getValidAccessToken(ctx.userId, 'github');
39
+
40
+ if (!accessToken) {
41
+ return {
42
+ data: {
43
+ error: 'unauthorized',
44
+ message: 'No valid GitHub token found. Please authenticate first.',
45
+ },
46
+ };
47
+ }
48
+
49
+ const url = `https://api.github.com/repos/${encodeURIComponent(args.owner)}/${encodeURIComponent(args.repo)}/issues/${args.issueNumber}/comments`;
50
+ const response = await fetch(url, {
51
+ method: 'POST',
52
+ headers: {
53
+ Authorization: `Bearer ${accessToken}`,
54
+ Accept: 'application/vnd.github+json',
55
+ 'X-GitHub-Api-Version': '2022-11-28',
56
+ 'Content-Type': 'application/json',
57
+ },
58
+ body: JSON.stringify({ body: args.body }),
59
+ });
60
+
61
+ if (response.status === 401 || response.status === 403) {
62
+ this.logger.warn(`GitHub API returned ${response.status} for user ${ctx.userId}`);
63
+ return {
64
+ data: {
65
+ error: '401',
66
+ message: 'GitHub token was rejected. Please re-authenticate.',
67
+ },
68
+ };
69
+ }
70
+
71
+ if (!response.ok) {
72
+ const errorBody = await response.text();
73
+ this.logger.error(`GitHub API error: ${response.status} ${errorBody}`);
74
+ return {
75
+ data: {
76
+ error: 'api_error',
77
+ message: `GitHub API error: ${response.statusText}`,
78
+ },
79
+ };
80
+ }
81
+
82
+ const comment = (await response.json()) as {
83
+ id: number;
84
+ html_url: string;
85
+ created_at: string;
86
+ user: { login: string };
87
+ };
88
+
89
+ return {
90
+ data: {
91
+ comment: {
92
+ id: comment.id,
93
+ htmlUrl: comment.html_url,
94
+ createdAt: comment.created_at,
95
+ user: comment.user.login,
96
+ },
97
+ },
98
+ };
99
+ }
100
+ }
@@ -0,0 +1,114 @@
1
+ import { Inject, Logger } from '@nestjs/common';
2
+ import { z } from 'zod';
3
+ import { Input, RunContext, Tool, ToolInterface, ToolResult } from '@loopstack/common';
4
+ import { OAuthTokenStore } from '@loopstack/oauth-module';
5
+
6
+ export type GitHubCreateIssueArgs = {
7
+ owner: string;
8
+ repo: string;
9
+ title: string;
10
+ body?: string;
11
+ labels?: string[];
12
+ assignees?: string[];
13
+ };
14
+
15
+ @Tool({
16
+ config: {
17
+ description:
18
+ 'Creates a new issue in a GitHub repository. Returns { error: "unauthorized" } if no valid token is available.',
19
+ },
20
+ })
21
+ export class GitHubCreateIssueTool implements ToolInterface {
22
+ private readonly logger = new Logger(GitHubCreateIssueTool.name);
23
+
24
+ @Inject()
25
+ private tokenStore: OAuthTokenStore;
26
+
27
+ @Input({
28
+ schema: z
29
+ .object({
30
+ owner: z.string(),
31
+ repo: z.string(),
32
+ title: z.string(),
33
+ body: z.string().optional(),
34
+ labels: z.array(z.string()).optional(),
35
+ assignees: z.array(z.string()).optional(),
36
+ })
37
+ .strict(),
38
+ })
39
+ args: GitHubCreateIssueArgs;
40
+
41
+ async execute(args: GitHubCreateIssueArgs, ctx: RunContext): Promise<ToolResult> {
42
+ const accessToken = await this.tokenStore.getValidAccessToken(ctx.userId, 'github');
43
+
44
+ if (!accessToken) {
45
+ return {
46
+ data: {
47
+ error: 'unauthorized',
48
+ message: 'No valid GitHub token found. Please authenticate first.',
49
+ },
50
+ };
51
+ }
52
+
53
+ const requestBody: Record<string, unknown> = {
54
+ title: args.title,
55
+ };
56
+
57
+ if (args.body) requestBody.body = args.body;
58
+ if (args.labels) requestBody.labels = args.labels;
59
+ if (args.assignees) requestBody.assignees = args.assignees;
60
+
61
+ const url = `https://api.github.com/repos/${encodeURIComponent(args.owner)}/${encodeURIComponent(args.repo)}/issues`;
62
+ const response = await fetch(url, {
63
+ method: 'POST',
64
+ headers: {
65
+ Authorization: `Bearer ${accessToken}`,
66
+ Accept: 'application/vnd.github+json',
67
+ 'X-GitHub-Api-Version': '2022-11-28',
68
+ 'Content-Type': 'application/json',
69
+ },
70
+ body: JSON.stringify(requestBody),
71
+ });
72
+
73
+ if (response.status === 401 || response.status === 403) {
74
+ this.logger.warn(`GitHub API returned ${response.status} for user ${ctx.userId}`);
75
+ return {
76
+ data: {
77
+ error: '401',
78
+ message: 'GitHub token was rejected. Please re-authenticate.',
79
+ },
80
+ };
81
+ }
82
+
83
+ if (!response.ok) {
84
+ const errorBody = await response.text();
85
+ this.logger.error(`GitHub API error: ${response.status} ${errorBody}`);
86
+ return {
87
+ data: {
88
+ error: 'api_error',
89
+ message: `GitHub API error: ${response.statusText}`,
90
+ },
91
+ };
92
+ }
93
+
94
+ const issue = (await response.json()) as {
95
+ id: number;
96
+ number: number;
97
+ title: string;
98
+ html_url: string;
99
+ state: string;
100
+ };
101
+
102
+ return {
103
+ data: {
104
+ issue: {
105
+ id: issue.id,
106
+ number: issue.number,
107
+ title: issue.title,
108
+ htmlUrl: issue.html_url,
109
+ state: issue.state,
110
+ },
111
+ },
112
+ };
113
+ }
114
+ }