@toon-protocol/core 1.1.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.
@@ -0,0 +1,350 @@
1
+ import { Event } from 'nostr-tools/pure';
2
+
3
+ /**
4
+ * NIP-34: Git Stuff
5
+ * https://github.com/nostr-protocol/nips/blob/master/34.md
6
+ *
7
+ * Event kinds for decentralized code collaboration via Nostr.
8
+ */
9
+ /**
10
+ * Repository Announcement (kind 30617)
11
+ * Replaceable event announcing a Git repository's existence.
12
+ * Contains clone URLs, maintainers, and metadata.
13
+ */
14
+ declare const REPOSITORY_ANNOUNCEMENT_KIND = 30617;
15
+ /**
16
+ * Patch (kind 1617)
17
+ * Direct patch submission for files under 60KB.
18
+ * Contains git format-patch output and commit metadata.
19
+ */
20
+ declare const PATCH_KIND = 1617;
21
+ /**
22
+ * Pull Request (kind 1618)
23
+ * Larger submissions or branch-based changes.
24
+ * References remote repository and commit range.
25
+ */
26
+ declare const PULL_REQUEST_KIND = 1618;
27
+ /**
28
+ * Pull Request Status Update (kind 1619)
29
+ * Updates to existing pull requests.
30
+ */
31
+ declare const PR_STATUS_UPDATE_KIND = 1619;
32
+ /**
33
+ * Issue (kind 1621)
34
+ * Bug reports and feature requests in Markdown format.
35
+ */
36
+ declare const ISSUE_KIND = 1621;
37
+ /**
38
+ * Status: Open (kind 1630)
39
+ */
40
+ declare const STATUS_OPEN_KIND = 1630;
41
+ /**
42
+ * Status: Applied/Merged (kind 1631)
43
+ */
44
+ declare const STATUS_APPLIED_KIND = 1631;
45
+ /**
46
+ * Status: Closed (kind 1632)
47
+ */
48
+ declare const STATUS_CLOSED_KIND = 1632;
49
+ /**
50
+ * Status: Draft (kind 1633)
51
+ */
52
+ declare const STATUS_DRAFT_KIND = 1633;
53
+ /**
54
+ * All NIP-34 event kinds
55
+ */
56
+ declare const NIP34_EVENT_KINDS: readonly [30617, 1617, 1618, 1619, 1621, 1630, 1631, 1632, 1633];
57
+ /**
58
+ * Check if an event kind is a NIP-34 event
59
+ */
60
+ declare function isNIP34Event(kind: number): boolean;
61
+
62
+ /**
63
+ * Helper to get tag value by name
64
+ */
65
+ declare function getTag(event: Event, tagName: string): string | undefined;
66
+ /**
67
+ * Helper to get all tag values by name
68
+ */
69
+ declare function getTags(event: Event, tagName: string): string[];
70
+ /**
71
+ * Repository Announcement Event (Kind 30617)
72
+ */
73
+ interface RepositoryAnnouncement extends Event {
74
+ kind: 30617;
75
+ tags: (['d', string] | ['name', string] | ['description', string] | ['web', string] | ['clone', string] | ['relays', ...string[]] | ['r', string, 'euc'] | ['maintainers', ...string[]])[];
76
+ }
77
+ /**
78
+ * Patch Event (Kind 1617)
79
+ */
80
+ interface PatchEvent extends Event {
81
+ kind: 1617;
82
+ content: string;
83
+ tags: (['a', string] | ['r', string] | ['p', string] | ['commit', string] | ['parent-commit', string] | ['commit-pgp-sig', string] | ['committer', string] | ['t', 'root' | 'reply'])[];
84
+ }
85
+ /**
86
+ * Pull Request Event (Kind 1618)
87
+ */
88
+ interface PullRequestEvent extends Event {
89
+ kind: 1618;
90
+ tags: (['a', string] | ['r', string] | ['p', string] | ['clone', string] | ['c', string] | ['merge-base', string] | ['subject', string] | ['t', 'root' | 'reply'])[];
91
+ }
92
+ /**
93
+ * Issue Event (Kind 1621)
94
+ */
95
+ interface IssueEvent extends Event {
96
+ kind: 1621;
97
+ content: string;
98
+ tags: (['a', string] | ['p', string] | ['subject', string] | ['t', string])[];
99
+ }
100
+ /**
101
+ * Status Event (Kinds 1630-1633)
102
+ */
103
+ interface StatusEvent extends Event {
104
+ kind: 1630 | 1631 | 1632 | 1633;
105
+ tags: (['e', string] | ['p', string])[];
106
+ }
107
+ /**
108
+ * Union type of all NIP-34 events
109
+ */
110
+ type NIP34Event = RepositoryAnnouncement | PatchEvent | PullRequestEvent | IssueEvent | StatusEvent;
111
+ /**
112
+ * Parse repository reference from 'a' tag
113
+ * Format: "30617:pubkey:repo-id"
114
+ */
115
+ interface RepositoryReference {
116
+ kind: 30617;
117
+ pubkey: string;
118
+ repoId: string;
119
+ }
120
+ declare function parseRepositoryReference(aTag: string): RepositoryReference;
121
+ /**
122
+ * Extract commit message from git format-patch content
123
+ */
124
+ declare function extractCommitMessage(patchContent: string): string;
125
+
126
+ /**
127
+ * Forgejo API Client
128
+ *
129
+ * Wrapper around Forgejo REST API for repository and issue management.
130
+ * Uses fetch API for HTTP requests (compatible with gitea-js SDK structure).
131
+ */
132
+ interface ForgejoConfig {
133
+ /** Base URL of Forgejo instance (e.g., "http://forgejo:3000") */
134
+ baseUrl: string;
135
+ /** API token for authentication */
136
+ token: string;
137
+ /** Default owner/organization for repositories */
138
+ defaultOwner?: string;
139
+ }
140
+ interface CreateRepositoryOptions {
141
+ name: string;
142
+ description?: string;
143
+ private?: boolean;
144
+ auto_init?: boolean;
145
+ default_branch?: string;
146
+ }
147
+ interface CreatePullRequestOptions {
148
+ owner: string;
149
+ repo: string;
150
+ title: string;
151
+ head: string;
152
+ base: string;
153
+ body?: string;
154
+ }
155
+ interface CreateIssueOptions {
156
+ owner: string;
157
+ repo: string;
158
+ title: string;
159
+ body?: string;
160
+ labels?: number[];
161
+ }
162
+ interface ForgejoRepository {
163
+ id: number;
164
+ name: string;
165
+ full_name: string;
166
+ description: string;
167
+ html_url: string;
168
+ clone_url: string;
169
+ ssh_url: string;
170
+ }
171
+ interface ForgejoPullRequest {
172
+ id: number;
173
+ number: number;
174
+ title: string;
175
+ html_url: string;
176
+ state: 'open' | 'closed';
177
+ }
178
+ interface ForgejoIssue {
179
+ id: number;
180
+ number: number;
181
+ title: string;
182
+ html_url: string;
183
+ state: 'open' | 'closed';
184
+ }
185
+ interface CreateOrUpdateFileOptions {
186
+ owner: string;
187
+ repo: string;
188
+ filepath: string;
189
+ content: string;
190
+ message: string;
191
+ branch?: string;
192
+ sha?: string;
193
+ }
194
+ interface ForgejoFileResponse {
195
+ content: {
196
+ name: string;
197
+ path: string;
198
+ sha: string;
199
+ };
200
+ commit: {
201
+ sha: string;
202
+ };
203
+ }
204
+ /**
205
+ * Forgejo API Client
206
+ */
207
+ declare class ForgejoClient {
208
+ private baseUrl;
209
+ private token;
210
+ private defaultOwner?;
211
+ constructor(config: ForgejoConfig);
212
+ /**
213
+ * Make an authenticated API request
214
+ */
215
+ private request;
216
+ /**
217
+ * Create a new repository
218
+ */
219
+ createRepository(options: CreateRepositoryOptions): Promise<ForgejoRepository>;
220
+ /**
221
+ * Create a pull request
222
+ */
223
+ createPullRequest(options: CreatePullRequestOptions): Promise<ForgejoPullRequest>;
224
+ /**
225
+ * Create an issue
226
+ */
227
+ createIssue(options: CreateIssueOptions): Promise<ForgejoIssue>;
228
+ /**
229
+ * Get repository information
230
+ */
231
+ getRepository(owner: string, repo: string): Promise<ForgejoRepository>;
232
+ /**
233
+ * Check if repository exists
234
+ */
235
+ repositoryExists(owner: string, repo: string): Promise<boolean>;
236
+ /**
237
+ * Get clone URL for internal use (within Docker network)
238
+ */
239
+ getInternalCloneUrl(owner: string, repo: string): string;
240
+ /**
241
+ * Get clone URL for external use
242
+ */
243
+ getExternalCloneUrl(repo: ForgejoRepository): string;
244
+ /**
245
+ * Create or update a file in a repository
246
+ */
247
+ createOrUpdateFile(options: CreateOrUpdateFileOptions): Promise<ForgejoFileResponse>;
248
+ /**
249
+ * Create a new branch
250
+ */
251
+ createBranch(owner: string, repo: string, branchName: string, fromBranch?: string): Promise<void>;
252
+ /**
253
+ * Get file content from repository
254
+ */
255
+ getFileContent(owner: string, repo: string, filepath: string, branch?: string): Promise<{
256
+ content: string;
257
+ sha: string;
258
+ } | null>;
259
+ }
260
+
261
+ /**
262
+ * NIP-34 Handler
263
+ *
264
+ * Processes NIP-34 events (Git stuff on Nostr) and executes corresponding
265
+ * Git operations on a Forgejo instance.
266
+ *
267
+ * Flow:
268
+ * 1. TOON receives NIP-34 event via ILP payment
269
+ * 2. BLS validates payment and stores event
270
+ * 3. BLS calls NIP34Handler.handleEvent()
271
+ * 4. Handler maps event to Git operation
272
+ * 5. Operation executes on Forgejo
273
+ */
274
+
275
+ interface NIP34Config {
276
+ /** Forgejo base URL (e.g., "http://forgejo:3000") */
277
+ forgejoUrl: string;
278
+ /** Forgejo API token */
279
+ forgejoToken: string;
280
+ /** Default owner/org for repositories */
281
+ defaultOwner: string;
282
+ /** Git commit identity configuration */
283
+ gitConfig?: {
284
+ userName: string;
285
+ userEmail: string;
286
+ };
287
+ /** Enable verbose logging */
288
+ verbose?: boolean;
289
+ }
290
+ interface HandleEventResult {
291
+ success: boolean;
292
+ operation: 'repository' | 'patch' | 'pull_request' | 'issue' | 'status' | 'unsupported';
293
+ message: string;
294
+ metadata?: Record<string, unknown>;
295
+ }
296
+ /**
297
+ * NIP-34 Event Handler
298
+ *
299
+ * Maps Nostr events to Git operations on Forgejo.
300
+ */
301
+ declare class NIP34Handler {
302
+ private forgejo;
303
+ private verbose;
304
+ private defaultOwner;
305
+ constructor(config: NIP34Config);
306
+ /**
307
+ * Handle a NIP-34 event
308
+ *
309
+ * This is the main entry point called by the BLS after storing an event.
310
+ */
311
+ handleEvent(event: Event): Promise<HandleEventResult>;
312
+ /**
313
+ * Handle Repository Announcement (kind 30617)
314
+ *
315
+ * Creates a new repository in Forgejo.
316
+ */
317
+ private handleRepositoryAnnouncement;
318
+ /**
319
+ * Handle Patch (kind 1617)
320
+ *
321
+ * Applies a patch to a repository via Forgejo API and creates a pull request.
322
+ */
323
+ private handlePatch;
324
+ /**
325
+ * Handle Pull Request (kind 1618)
326
+ *
327
+ * Creates an issue documenting the pull request (simplified approach without git).
328
+ */
329
+ private handlePullRequest;
330
+ /**
331
+ * Handle Issue (kind 1621)
332
+ *
333
+ * Creates an issue in Forgejo.
334
+ */
335
+ private handleIssue;
336
+ /**
337
+ * Get operation type from event kind
338
+ */
339
+ private getOperationType;
340
+ /**
341
+ * Parse git format-patch output to extract file changes
342
+ */
343
+ private parsePatch;
344
+ /**
345
+ * Log message if verbose mode is enabled
346
+ */
347
+ private log;
348
+ }
349
+
350
+ export { type CreateIssueOptions, type CreateOrUpdateFileOptions, type CreatePullRequestOptions, type CreateRepositoryOptions, ForgejoClient, type ForgejoConfig, type ForgejoFileResponse, type ForgejoIssue, type ForgejoPullRequest, type ForgejoRepository, type HandleEventResult, ISSUE_KIND, type IssueEvent, type NIP34Config, type NIP34Event, NIP34Handler, NIP34_EVENT_KINDS, PATCH_KIND, PR_STATUS_UPDATE_KIND, PULL_REQUEST_KIND, type PatchEvent, type PullRequestEvent, REPOSITORY_ANNOUNCEMENT_KIND, type RepositoryAnnouncement, type RepositoryReference, STATUS_APPLIED_KIND, STATUS_CLOSED_KIND, STATUS_DRAFT_KIND, STATUS_OPEN_KIND, type StatusEvent, extractCommitMessage, getTag, getTags, isNIP34Event, parseRepositoryReference };