ima2-gen 1.1.21 → 1.1.22

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 (208) hide show
  1. package/README.md +30 -4
  2. package/bin/ima2.js +14 -4
  3. package/bin/lib/platform.js +34 -5
  4. package/docs/README.ko.md +31 -0
  5. package/lib/agentQueueWorker.js +6 -0
  6. package/lib/agentRuntime.js +3 -2
  7. package/lib/atomicWrite.js +14 -0
  8. package/lib/grokProxyLauncher.js +5 -3
  9. package/lib/inflight.js +1 -1
  10. package/lib/oauthLauncher.js +5 -0
  11. package/lib/videoFrameExtract.js +3 -3
  12. package/package.json +5 -7
  13. package/routes/edit.js +2 -1
  14. package/routes/generate.js +4 -3
  15. package/routes/health.js +4 -3
  16. package/routes/multimode.js +2 -1
  17. package/routes/video.js +4 -2
  18. package/server.js +29 -2
  19. package/ui/dist/.vite/manifest.json +12 -12
  20. package/ui/dist/assets/{AgentWorkspace-B_hq9CLg.js → AgentWorkspace-COxQ5TjU.js} +1 -1
  21. package/ui/dist/assets/{CardNewsWorkspace-wD12J7qk.js → CardNewsWorkspace-B0OkcuVz.js} +1 -1
  22. package/ui/dist/assets/{NodeCanvas-CI_wuPMf.js → NodeCanvas-BSsclEBh.js} +1 -1
  23. package/ui/dist/assets/{PromptBuilderPanel-CUTujJUV.js → PromptBuilderPanel-DpC9A5Rz.js} +1 -1
  24. package/ui/dist/assets/{PromptImportDialog-CUi66jPK.js → PromptImportDialog-CVwT0rLd.js} +2 -2
  25. package/ui/dist/assets/{PromptImportDiscoverySection-Cm3vrjY4.js → PromptImportDiscoverySection-BDCkRCRs.js} +1 -1
  26. package/ui/dist/assets/{PromptImportFolderSection-DOtWTD9n.js → PromptImportFolderSection-QoKbZD83.js} +1 -1
  27. package/ui/dist/assets/{PromptLibraryPanel-BMjQegRa.js → PromptLibraryPanel-BhFgeKnY.js} +2 -2
  28. package/ui/dist/assets/SettingsWorkspace-CfjrlH5R.js +1 -0
  29. package/ui/dist/assets/index-C-mur7pa.css +1 -0
  30. package/ui/dist/assets/index-CCP5nUOj.js +42 -0
  31. package/ui/dist/assets/{index-31uVIdt4.js → index-Cxhzi3bs.js} +1 -1
  32. package/ui/dist/index.html +2 -2
  33. package/bin/commands/annotate.ts +0 -119
  34. package/bin/commands/cancel.ts +0 -48
  35. package/bin/commands/canvas-versions.ts +0 -80
  36. package/bin/commands/capabilities.ts +0 -110
  37. package/bin/commands/cardnews.ts +0 -249
  38. package/bin/commands/comfy.ts +0 -54
  39. package/bin/commands/config.ts +0 -186
  40. package/bin/commands/defaults.ts +0 -192
  41. package/bin/commands/doctor.ts +0 -202
  42. package/bin/commands/edit.ts +0 -150
  43. package/bin/commands/gen.ts +0 -214
  44. package/bin/commands/grok.ts +0 -90
  45. package/bin/commands/history.ts +0 -146
  46. package/bin/commands/ls.ts +0 -64
  47. package/bin/commands/metadata.ts +0 -39
  48. package/bin/commands/multimode.ts +0 -196
  49. package/bin/commands/node.ts +0 -166
  50. package/bin/commands/observability.ts +0 -176
  51. package/bin/commands/ping.ts +0 -31
  52. package/bin/commands/prompt-sub/build.ts +0 -101
  53. package/bin/commands/prompt.ts +0 -492
  54. package/bin/commands/ps.ts +0 -81
  55. package/bin/commands/session.ts +0 -266
  56. package/bin/commands/show.ts +0 -72
  57. package/bin/commands/skill.ts +0 -70
  58. package/bin/commands/video.ts +0 -442
  59. package/bin/ima2.ts +0 -430
  60. package/bin/lib/args.ts +0 -92
  61. package/bin/lib/browser-id.ts +0 -16
  62. package/bin/lib/client.ts +0 -122
  63. package/bin/lib/config-store.ts +0 -120
  64. package/bin/lib/destructive-confirm.ts +0 -19
  65. package/bin/lib/doctor-checks.ts +0 -91
  66. package/bin/lib/error-hints.ts +0 -23
  67. package/bin/lib/files.ts +0 -39
  68. package/bin/lib/output.ts +0 -73
  69. package/bin/lib/platform.ts +0 -99
  70. package/bin/lib/recover-output.ts +0 -139
  71. package/bin/lib/sse.ts +0 -73
  72. package/bin/lib/star-prompt.ts +0 -97
  73. package/bin/lib/storage-doctor.ts +0 -39
  74. package/bin/lib/ui-build.ts +0 -85
  75. package/config.ts +0 -354
  76. package/lib/agentCommandParser.ts +0 -69
  77. package/lib/agentGenerationPlanner.ts +0 -273
  78. package/lib/agentQuestionResponder.ts +0 -266
  79. package/lib/agentQueueStore.ts +0 -270
  80. package/lib/agentQueueWorker.ts +0 -89
  81. package/lib/agentRuntime.ts +0 -604
  82. package/lib/agentSettings.ts +0 -72
  83. package/lib/agentStore.ts +0 -422
  84. package/lib/agentStoreRows.ts +0 -136
  85. package/lib/agentTypes.ts +0 -154
  86. package/lib/apiCachePolicy.ts +0 -11
  87. package/lib/assetLifecycle.ts +0 -146
  88. package/lib/canvasVersionStore.ts +0 -223
  89. package/lib/capabilities.ts +0 -126
  90. package/lib/cardNewsGenerator.ts +0 -271
  91. package/lib/cardNewsJobStore.ts +0 -142
  92. package/lib/cardNewsManifestStore.ts +0 -154
  93. package/lib/cardNewsPlanner.ts +0 -236
  94. package/lib/cardNewsPlannerClient.ts +0 -155
  95. package/lib/cardNewsPlannerPrompt.ts +0 -62
  96. package/lib/cardNewsPlannerSchema.ts +0 -321
  97. package/lib/cardNewsRoleTemplateStore.ts +0 -47
  98. package/lib/cardNewsTemplateStore.ts +0 -252
  99. package/lib/codexDetect.ts +0 -71
  100. package/lib/comfyBridge.ts +0 -235
  101. package/lib/composerSnapshot.ts +0 -33
  102. package/lib/configKeys.ts +0 -62
  103. package/lib/db.ts +0 -295
  104. package/lib/errInfo.ts +0 -43
  105. package/lib/errorClassify.ts +0 -100
  106. package/lib/generationCancel.ts +0 -28
  107. package/lib/generationErrors.ts +0 -238
  108. package/lib/grokImageAdapter.ts +0 -513
  109. package/lib/grokMultimodeAdapter.ts +0 -84
  110. package/lib/grokProxyLauncher.ts +0 -153
  111. package/lib/grokRuntime.ts +0 -23
  112. package/lib/grokSizeMapper.ts +0 -71
  113. package/lib/grokVideoAdapter.ts +0 -458
  114. package/lib/grokVideoCanvas.ts +0 -26
  115. package/lib/grokVideoDownload.ts +0 -59
  116. package/lib/grokVideoPlannerPrompt.ts +0 -67
  117. package/lib/historyIndex.ts +0 -51
  118. package/lib/historyList.ts +0 -181
  119. package/lib/imageMetadata.ts +0 -113
  120. package/lib/imageMetadataStore.ts +0 -67
  121. package/lib/imageModels.ts +0 -165
  122. package/lib/inflight.ts +0 -281
  123. package/lib/localImportStore.ts +0 -114
  124. package/lib/logger.ts +0 -161
  125. package/lib/nodeStore.ts +0 -91
  126. package/lib/oauthLauncher.ts +0 -94
  127. package/lib/oauthNormalize.ts +0 -30
  128. package/lib/oauthProxy/errors.ts +0 -128
  129. package/lib/oauthProxy/generators.ts +0 -494
  130. package/lib/oauthProxy/index.ts +0 -28
  131. package/lib/oauthProxy/prompts.ts +0 -123
  132. package/lib/oauthProxy/references.ts +0 -45
  133. package/lib/oauthProxy/runtime.ts +0 -115
  134. package/lib/oauthProxy/streams.ts +0 -232
  135. package/lib/oauthProxy/types.ts +0 -9
  136. package/lib/oauthProxy.ts +0 -3
  137. package/lib/openDirectory.ts +0 -47
  138. package/lib/pngInfo.ts +0 -26
  139. package/lib/promptBuilder/attachments.ts +0 -74
  140. package/lib/promptBuilder/client.ts +0 -130
  141. package/lib/promptBuilder/constants.ts +0 -9
  142. package/lib/promptBuilder/context.ts +0 -36
  143. package/lib/promptBuilder/errors.ts +0 -12
  144. package/lib/promptBuilder/requestSchema.ts +0 -56
  145. package/lib/promptBuilder/responseParser.ts +0 -219
  146. package/lib/promptBuilder/systemPrompt.ts +0 -135
  147. package/lib/promptBuilder/transport.ts +0 -94
  148. package/lib/promptBuilder/types.ts +0 -109
  149. package/lib/promptImport/curatedSources.ts +0 -141
  150. package/lib/promptImport/discoveryRegistry.ts +0 -329
  151. package/lib/promptImport/errors.ts +0 -18
  152. package/lib/promptImport/githubDiscovery.ts +0 -309
  153. package/lib/promptImport/githubFolder.ts +0 -397
  154. package/lib/promptImport/githubSource.ts +0 -257
  155. package/lib/promptImport/gptImageHints.ts +0 -70
  156. package/lib/promptImport/parsePromptCandidates.ts +0 -179
  157. package/lib/promptImport/promptIndex.ts +0 -326
  158. package/lib/promptImport/rankPromptCandidates.ts +0 -65
  159. package/lib/promptImport/types.ts +0 -103
  160. package/lib/promptSafetyPolicy.ts +0 -5
  161. package/lib/providerOptions.ts +0 -56
  162. package/lib/referenceImageCompress.ts +0 -84
  163. package/lib/refs.ts +0 -133
  164. package/lib/requestLogger.ts +0 -49
  165. package/lib/responsesDoctor.ts +0 -456
  166. package/lib/responsesErrors.ts +0 -83
  167. package/lib/responsesFallback.ts +0 -114
  168. package/lib/responsesImageAdapter.ts +0 -466
  169. package/lib/responsesParse.ts +0 -452
  170. package/lib/responsesTools.ts +0 -28
  171. package/lib/runtimeContext.ts +0 -146
  172. package/lib/runtimePorts.ts +0 -105
  173. package/lib/sessionStore.ts +0 -308
  174. package/lib/storageMigration.ts +0 -310
  175. package/lib/styleSheet.ts +0 -139
  176. package/lib/systemTrash.ts +0 -20
  177. package/lib/videoContinuity.ts +0 -180
  178. package/lib/videoFrameExtract.ts +0 -78
  179. package/lib/videoSeriesChain.ts +0 -29
  180. package/lib/visibleTextLanguagePolicy.ts +0 -7
  181. package/routes/agent.ts +0 -308
  182. package/routes/annotations.ts +0 -118
  183. package/routes/canvasVersions.ts +0 -69
  184. package/routes/capabilities.ts +0 -18
  185. package/routes/cardNews.ts +0 -211
  186. package/routes/comfy.ts +0 -43
  187. package/routes/edit.ts +0 -352
  188. package/routes/generate.ts +0 -492
  189. package/routes/grok.ts +0 -24
  190. package/routes/health.ts +0 -123
  191. package/routes/history.ts +0 -221
  192. package/routes/imageImport.ts +0 -37
  193. package/routes/index.ts +0 -52
  194. package/routes/metadata.ts +0 -77
  195. package/routes/multimode.ts +0 -499
  196. package/routes/nodes.ts +0 -578
  197. package/routes/promptBuilder.ts +0 -37
  198. package/routes/promptImport.ts +0 -379
  199. package/routes/prompts.ts +0 -428
  200. package/routes/quota.ts +0 -89
  201. package/routes/sessions.ts +0 -317
  202. package/routes/storage.ts +0 -47
  203. package/routes/video.ts +0 -300
  204. package/routes/videoExtended.ts +0 -284
  205. package/server.ts +0 -293
  206. package/ui/dist/assets/SettingsWorkspace-PiaVnsdA.js +0 -1
  207. package/ui/dist/assets/index-CjgnNtgt.css +0 -1
  208. package/ui/dist/assets/index-Da2s4_-5.js +0 -36
@@ -1,309 +0,0 @@
1
- import { createHash } from "node:crypto";
2
- import { promptImportError } from "./errors.js";
3
- import { upsertDiscoveryCandidates } from "./discoveryRegistry.js";
4
- import { requireRuntimeContext } from "../runtimeContext.js";
5
- import type { DiscoveryRepo, PromptImportCtx } from "./types.js";
6
-
7
- import { errInfo } from "../errInfo.js";
8
- const GITHUB_API_HOST = "api.github.com";
9
- const DEFAULT_DISCOVERY_SEEDS = [
10
- "gpt-image-2 prompt",
11
- "image generation prompts",
12
- "nano banana prompts",
13
- "product photography prompt",
14
- "typography image prompt",
15
- "reference image prompt",
16
- ];
17
-
18
- interface DiscoveryRateLimit {
19
- limit: number | null;
20
- remaining: number | null;
21
- resetAt: string | null;
22
- }
23
-
24
- interface DiscoveryCandidate {
25
- id: string;
26
- repo: string;
27
- owner: string | undefined;
28
- name: string | undefined;
29
- fullName: string;
30
- htmlUrl: string | null | undefined;
31
- description: string;
32
- defaultBranch: string;
33
- stars: number;
34
- forks: number;
35
- openIssues: number;
36
- updatedAt: string | null | undefined;
37
- pushedAt: string | null | undefined;
38
- licenseSpdx: string;
39
- topics: string[];
40
- language: string | null | undefined;
41
- score: number;
42
- scoreReasons: string[];
43
- warnings: string[];
44
- status: string;
45
- query: string;
46
- discoveredAt: string;
47
- }
48
-
49
- interface DiscoveryContext {
50
- query?: string;
51
- seeds?: string[];
52
- }
53
-
54
- interface DiscoveryQueryOptions {
55
- q?: string;
56
- seeds?: string[];
57
- limit?: number;
58
- }
59
-
60
- interface DiscoverySearchOptions {
61
- q?: string;
62
- seeds?: string[];
63
- limit?: number;
64
- maxQueries?: number;
65
- }
66
-
67
- function tokenize(value: unknown): string[] {
68
- return String(value || "")
69
- .toLowerCase()
70
- .split(/[^a-z0-9가-힣-]+/i)
71
- .filter(Boolean);
72
- }
73
-
74
- function discoveryLimits(ctx: PromptImportCtx) {
75
- const limits = ctx.config?.limits as Record<string, number> | undefined;
76
- return {
77
- limit: limits?.promptImportDiscoverySearchLimit ?? 0,
78
- maxQueries: limits?.promptImportDiscoveryMaxQueries ?? 0,
79
- fetchTimeoutMs: limits?.promptImportFetchTimeoutMs ?? 0,
80
- };
81
- }
82
-
83
- function validateGitHubApiUrl(rawUrl: string): void {
84
- let url;
85
- try {
86
- url = new URL(rawUrl);
87
- } catch {
88
- throw promptImportError("GITHUB_DISCOVERY_FAILED", "GitHub discovery returned an invalid URL");
89
- }
90
- if (!["https:", "http:"].includes(url.protocol) || url.hostname !== GITHUB_API_HOST) {
91
- throw promptImportError("GITHUB_DISCOVERY_FAILED", "GitHub discovery redirected to an unsupported host");
92
- }
93
- }
94
-
95
- function rateLimitFromHeaders(headers: Headers): DiscoveryRateLimit {
96
- const remaining = Number(headers.get("x-ratelimit-remaining") || Number.NaN);
97
- const reset = Number(headers.get("x-ratelimit-reset") || Number.NaN);
98
- const limit = Number(headers.get("x-ratelimit-limit") || Number.NaN);
99
- return {
100
- limit: Number.isFinite(limit) ? limit : null,
101
- remaining: Number.isFinite(remaining) ? remaining : null,
102
- resetAt: Number.isFinite(reset) ? new Date(reset * 1000).toISOString() : null,
103
- };
104
- }
105
-
106
- function searchHeaders(ctx: PromptImportCtx): Record<string, string> {
107
- const headers: Record<string, string> = {
108
- Accept: "application/vnd.github+json",
109
- "X-GitHub-Api-Version": "2022-11-28",
110
- "User-Agent": "ima2-prompt-import-discovery",
111
- };
112
- const github = (ctx.config as { github?: { token?: string } } | undefined)?.github;
113
- const token = github?.token;
114
- if (token) headers.Authorization = `Bearer ${token}`;
115
- return headers;
116
- }
117
-
118
- export function buildDiscoveryQueries({ q = "", seeds = [], limit = 5 }: DiscoveryQueryOptions = {}): string[] {
119
- const raw = [q, ...seeds, ...DEFAULT_DISCOVERY_SEEDS]
120
- .map((item) => String(item || "").trim())
121
- .filter(Boolean);
122
- const unique = [...new Set(raw)];
123
- if (unique.length === 0) {
124
- throw promptImportError("GITHUB_DISCOVERY_QUERY_EMPTY", "Discovery query is required", 422);
125
- }
126
- return unique.slice(0, Math.max(1, Number(limit) || 1));
127
- }
128
-
129
- export function scoreDiscoveryRepository(repo: DiscoveryRepo, context: DiscoveryContext = {}) {
130
- const terms = tokenize([context.query, ...(context.seeds || [])].join(" "));
131
- const nameText = `${repo.full_name || ""} ${repo.description || ""} ${(repo.topics || []).join(" ")}`.toLowerCase();
132
- const pushedAt = repo.pushed_at ? Date.parse(repo.pushed_at) : 0;
133
- const daysSincePush = pushedAt ? (Date.now() - pushedAt) / 86_400_000 : Number.POSITIVE_INFINITY;
134
- const scoreReasons: string[] = [];
135
- const warnings: string[] = [];
136
- let score = 0;
137
-
138
- const stars = Number(repo.stargazers_count || 0);
139
- if (stars > 1000) {
140
- score += 20;
141
- scoreReasons.push("popular-repo");
142
- } else if (stars > 100) {
143
- score += 12;
144
- scoreReasons.push("known-repo");
145
- } else if (stars > 10) {
146
- score += 5;
147
- scoreReasons.push("some-stars");
148
- }
149
-
150
- if (daysSincePush <= 365) {
151
- score += 10;
152
- scoreReasons.push("recently-updated");
153
- } else {
154
- score -= 6;
155
- warnings.push("stale-repo");
156
- }
157
-
158
- if (repo.license?.spdx_id) {
159
- score += 8;
160
- scoreReasons.push("license-present");
161
- } else {
162
- score -= 8;
163
- warnings.push("no-license");
164
- }
165
-
166
- if (repo.archived || repo.disabled) {
167
- score -= 20;
168
- warnings.push("archived-or-disabled");
169
- }
170
- if (repo.fork) {
171
- score -= 4;
172
- warnings.push("fork-source");
173
- }
174
- if (!repo.default_branch) {
175
- score -= 10;
176
- warnings.push("missing-default-branch");
177
- } else if (String(repo.default_branch).includes("/")) {
178
- warnings.push("discovery-default-branch-unsupported");
179
- }
180
-
181
- const promptTerms = ["prompt", "image", "generation", "gpt-image", "nano", "typography", "reference"];
182
- if (promptTerms.some((term) => nameText.includes(term))) {
183
- score += 14;
184
- scoreReasons.push("prompt-like");
185
- }
186
- for (const term of terms) {
187
- if (term && nameText.includes(term)) score += 3;
188
- }
189
- if (!repo.html_url || !String(repo.html_url).startsWith("https://github.com/")) {
190
- score -= 20;
191
- warnings.push("non-github-url");
192
- }
193
-
194
- return { score, scoreReasons: [...new Set(scoreReasons)], warnings: [...new Set(warnings)] };
195
- }
196
-
197
- export function normalizeDiscoveryCandidate(repo: DiscoveryRepo, context: DiscoveryContext = {}): DiscoveryCandidate {
198
- const fullName = String(repo.full_name || "").trim();
199
- const [owner, name] = fullName.split("/");
200
- const scored = scoreDiscoveryRepository(repo, context);
201
- return {
202
- id: `github:${fullName}`,
203
- repo: fullName,
204
- owner,
205
- name,
206
- fullName,
207
- htmlUrl: repo.html_url,
208
- description: repo.description || "",
209
- defaultBranch: repo.default_branch || "main",
210
- stars: Number(repo.stargazers_count || 0),
211
- forks: Number(repo.forks_count || 0),
212
- openIssues: Number(repo.open_issues_count || 0),
213
- updatedAt: repo.updated_at || null,
214
- pushedAt: repo.pushed_at || null,
215
- licenseSpdx: repo.license?.spdx_id || "NOASSERTION",
216
- topics: Array.isArray(repo.topics) ? repo.topics : [],
217
- language: repo.language || null,
218
- score: scored.score,
219
- scoreReasons: scored.scoreReasons,
220
- warnings: scored.warnings,
221
- status: "candidate",
222
- query: context.query || "",
223
- discoveredAt: new Date().toISOString(),
224
- };
225
- }
226
-
227
- export function discoveryCacheKey(input: unknown): string {
228
- return createHash("sha256").update(JSON.stringify(input)).digest("hex");
229
- }
230
-
231
- interface SearchOneQueryResult {
232
- items: DiscoveryRepo[];
233
- rateLimit: DiscoveryRateLimit;
234
- }
235
-
236
- async function searchOneQuery(ctx: PromptImportCtx, query: string, perPage: number): Promise<SearchOneQueryResult> {
237
- const controller = new AbortController();
238
- const timer = setTimeout(() => controller.abort(), discoveryLimits(ctx).fetchTimeoutMs);
239
- const url = new URL("https://api.github.com/search/repositories");
240
- url.searchParams.set("q", `${query} in:name,description`);
241
- url.searchParams.set("sort", "stars");
242
- url.searchParams.set("order", "desc");
243
- url.searchParams.set("per_page", String(perPage));
244
- try {
245
- const response = await fetch(url, {
246
- headers: searchHeaders(ctx),
247
- signal: controller.signal,
248
- });
249
- validateGitHubApiUrl(response.url);
250
- const rateLimit = rateLimitFromHeaders(response.headers);
251
- if (response.status === 403 || response.status === 429) {
252
- throw promptImportError("GITHUB_RATE_LIMITED", "GitHub discovery rate limit reached", 429);
253
- }
254
- if (!response.ok) {
255
- throw promptImportError("GITHUB_DISCOVERY_FAILED", `GitHub discovery failed with ${response.status}`, 422);
256
- }
257
- const data = (await response.json()) as { items?: DiscoveryRepo[] };
258
- const items = Array.isArray(data.items) ? data.items : [];
259
- return { items, rateLimit };
260
- } catch (error) {
261
- const err = errInfo(error);
262
- if (err.name === "AbortError") {
263
- throw promptImportError("REMOTE_FETCH_TIMEOUT", "GitHub discovery timed out", 504);
264
- }
265
- throw error;
266
- } finally {
267
- clearTimeout(timer);
268
- }
269
- }
270
-
271
- export async function searchGitHubDiscovery(ctxIn: PromptImportCtx, options: DiscoverySearchOptions = {}) {
272
- const ctx = requireRuntimeContext(ctxIn);
273
- const limits = discoveryLimits(ctx);
274
- const queryLimit = Math.min(Number(options.maxQueries) || limits.maxQueries, limits.maxQueries);
275
- const queries = buildDiscoveryQueries({
276
- q: options.q,
277
- seeds: options.seeds,
278
- limit: queryLimit,
279
- });
280
- const requestedLimit = Math.min(Number(options.limit) || limits.limit, limits.limit);
281
- const perQuery = Math.max(1, Math.ceil(requestedLimit / queries.length));
282
- const warnings: string[] = [];
283
- let rateLimit: DiscoveryRateLimit | null = null;
284
- const byRepo = new Map<string, DiscoveryCandidate>();
285
-
286
- for (const query of queries) {
287
- const result = await searchOneQuery(ctx, query, perQuery);
288
- rateLimit = result.rateLimit;
289
- for (const repo of result.items) {
290
- const candidate = normalizeDiscoveryCandidate(repo, {
291
- query,
292
- seeds: options.seeds,
293
- });
294
- const existing = byRepo.get(candidate.fullName);
295
- if (!existing || candidate.score > existing.score) {
296
- byRepo.set(candidate.fullName, candidate);
297
- }
298
- }
299
- }
300
-
301
- const candidates = [...byRepo.values()]
302
- .filter((candidate) => candidate.fullName && candidate.htmlUrl?.startsWith("https://github.com/"))
303
- .sort((a, b) => b.score - a.score || b.stars - a.stars)
304
- .slice(0, requestedLimit);
305
-
306
- await upsertDiscoveryCandidates(ctx, candidates);
307
- if (rateLimit?.remaining === 0) warnings.push("github-rate-limit-exhausted");
308
- return { candidates, warnings, rateLimit, cacheKey: discoveryCacheKey({ queries, requestedLimit }) };
309
- }
@@ -1,397 +0,0 @@
1
- import { createHash } from "node:crypto";
2
- import { promptImportError } from "./errors.js";
3
- import type { PromptImportLimits } from "./types.js";
4
-
5
- import { errInfo } from "../errInfo.js";
6
- const GITHUB_HOST = "github.com";
7
- const GITHUB_API_HOST = "api.github.com";
8
- const RAW_HOST = "raw.githubusercontent.com";
9
- const SUPPORTED_EXTENSIONS = new Set(["md", "markdown", "txt"]);
10
- const OWNER_REPO_RE = /^[A-Za-z0-9_.-]+$/;
11
-
12
- export interface GitHubFolderSource {
13
- kind: "github-folder";
14
- owner: string;
15
- repo: string;
16
- ref: string;
17
- path: string;
18
- htmlUrl: string;
19
- apiUrl: string;
20
- tags: string[];
21
- fromTreeUrl: boolean;
22
- ambiguousTree: boolean;
23
- }
24
-
25
- export interface GitHubFolderFile {
26
- name: string;
27
- path: string;
28
- extension: string;
29
- sizeBytes: number;
30
- htmlUrl: string;
31
- downloadUrl: string;
32
- selected: boolean;
33
- warnings: string[];
34
- }
35
-
36
- interface GitHubFolderItemRaw {
37
- type?: unknown;
38
- path?: unknown;
39
- name?: unknown;
40
- size?: unknown;
41
- html_url?: unknown;
42
- download_url?: unknown;
43
- }
44
-
45
- function safeDecode(value: string): string {
46
- try {
47
- return decodeURIComponent(value);
48
- } catch {
49
- throw promptImportError("INVALID_GITHUB_SOURCE", "Invalid encoded GitHub folder path");
50
- }
51
- }
52
-
53
- function safePath(path: unknown): string {
54
- const raw = String(path || "").trim();
55
- const lower = raw.toLowerCase();
56
- if (raw.includes("\0") || lower.includes("%00")) {
57
- throw promptImportError("INVALID_GITHUB_SOURCE", "GitHub folder path contains a null byte");
58
- }
59
- if (/%2f|%5c/i.test(raw)) {
60
- throw promptImportError("INVALID_GITHUB_SOURCE", "GitHub folder path contains an encoded slash");
61
- }
62
- const decoded = safeDecode(raw);
63
- if (decoded.includes("\\") || decoded.split("/").includes("..")) {
64
- throw promptImportError("INVALID_GITHUB_SOURCE", "GitHub folder traversal is not allowed");
65
- }
66
- return decoded.replace(/^\/+|\/+$/g, "");
67
- }
68
-
69
- function assertOwnerRepo(owner: string | undefined, repo: string | undefined): void {
70
- if (!OWNER_REPO_RE.test(owner || "") || !OWNER_REPO_RE.test(repo || "")) {
71
- throw promptImportError("INVALID_GITHUB_SOURCE", "Invalid GitHub owner or repository");
72
- }
73
- }
74
-
75
- function extensionForPath(path: string): string {
76
- const match = /\.([A-Za-z0-9]+)$/.exec(path);
77
- return match?.[1]?.toLowerCase() ?? "";
78
- }
79
-
80
- function supportedExtension(path: string): string {
81
- const extension = extensionForPath(path);
82
- return SUPPORTED_EXTENSIONS.has(extension) ? extension : "";
83
- }
84
-
85
- function encodeApiPath(path: string): string {
86
- return path
87
- .split("/")
88
- .filter(Boolean)
89
- .map((part) => encodeURIComponent(part))
90
- .join("/");
91
- }
92
-
93
- interface BuildApiUrlInput {
94
- owner: string;
95
- repo: string;
96
- ref: string;
97
- path: string;
98
- }
99
-
100
- function buildApiUrl({ owner, repo, ref, path }: BuildApiUrlInput): string {
101
- const encodedPath = encodeApiPath(path);
102
- const suffix = encodedPath ? `/${encodedPath}` : "";
103
- return `https://${GITHUB_API_HOST}/repos/${owner}/${repo}/contents${suffix}?ref=${encodeURIComponent(ref)}`;
104
- }
105
-
106
- function folderTags(source: GitHubFolderSource): string[] {
107
- return ["github", `repo:${source.owner}/${source.repo}`, `ref:${source.ref}`, `folder:${source.path || "/"}`];
108
- }
109
-
110
- function fromUrl(input: string): GitHubFolderSource | null {
111
- let url;
112
- try {
113
- url = new URL(input);
114
- } catch {
115
- return null;
116
- }
117
- if (!["http:", "https:"].includes(url.protocol) || url.hostname !== GITHUB_HOST) {
118
- throw promptImportError("GITHUB_FOLDER_UNSUPPORTED", "Only github.com folder URLs are supported", 400);
119
- }
120
- const [owner, repo, marker, rawRef, ...pathParts] = url.pathname.split("/").filter(Boolean);
121
- assertOwnerRepo(owner, repo);
122
- if (marker !== "tree") {
123
- throw promptImportError("GITHUB_FOLDER_UNSUPPORTED", "Enter a GitHub folder URL or owner/repo:path/", 422);
124
- }
125
- const ref = safeDecode(rawRef || "main");
126
- const path = safePath(pathParts.join("/"));
127
- return makeSource({ owner, repo, ref, path, fromTreeUrl: true, ambiguousTree: path.includes("/") });
128
- }
129
-
130
- function fromShorthand(input: string): GitHubFolderSource {
131
- const match = /^([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)(?:@([^:]+))?:(.*)$/.exec(input);
132
- if (!match) {
133
- throw promptImportError("GITHUB_FOLDER_UNSUPPORTED", "Enter a GitHub folder URL or owner/repo:path/", 422);
134
- }
135
- const [, owner, repo, rawRef, rawPath] = match;
136
- assertOwnerRepo(owner, repo);
137
- const ref = rawRef ? safeDecode(rawRef.trim()) : "main";
138
- if (ref.includes("/")) {
139
- throw promptImportError("AMBIGUOUS_GITHUB_REF", "Branches with slashes need a later Git ref resolver", 422);
140
- }
141
- return makeSource({ owner, repo, ref, path: safePath(rawPath) });
142
- }
143
-
144
- interface MakeSourceInput {
145
- owner: string;
146
- repo: string;
147
- ref: string;
148
- path: string;
149
- fromTreeUrl?: boolean;
150
- ambiguousTree?: boolean;
151
- }
152
-
153
- function makeSource({ owner, repo, ref, path, fromTreeUrl = false, ambiguousTree = false }: MakeSourceInput): GitHubFolderSource {
154
- return {
155
- kind: "github-folder",
156
- owner,
157
- repo,
158
- ref,
159
- path,
160
- htmlUrl: `https://${GITHUB_HOST}/${owner}/${repo}/tree/${encodeURIComponent(ref)}${path ? `/${path}` : ""}`,
161
- apiUrl: buildApiUrl({ owner, repo, ref, path }),
162
- tags: [],
163
- fromTreeUrl,
164
- ambiguousTree,
165
- };
166
- }
167
-
168
- function assertGithubApiUrl(rawUrl: string): void {
169
- let url;
170
- try {
171
- url = new URL(rawUrl);
172
- } catch {
173
- throw promptImportError("INVALID_GITHUB_SOURCE", "GitHub folder fetch returned an invalid URL");
174
- }
175
- if (!["http:", "https:"].includes(url.protocol) || url.hostname !== GITHUB_API_HOST) {
176
- throw promptImportError("INVALID_GITHUB_SOURCE", "GitHub folder fetch used an unsupported host");
177
- }
178
- }
179
-
180
- function assertRawDownloadUrl(rawUrl: string): void {
181
- let url;
182
- try {
183
- url = new URL(rawUrl);
184
- } catch {
185
- throw promptImportError("INVALID_GITHUB_SOURCE", "GitHub folder file has an invalid download URL");
186
- }
187
- if (!["http:", "https:"].includes(url.protocol) || url.hostname !== RAW_HOST) {
188
- throw promptImportError("INVALID_GITHUB_SOURCE", "GitHub folder file download host is unsupported");
189
- }
190
- const path = safePath(url.pathname.split("/").filter(Boolean).slice(3).join("/"));
191
- if (!supportedExtension(path)) {
192
- throw promptImportError("UNSUPPORTED_EXTENSION", "Only .md, .markdown, and .txt files are supported");
193
- }
194
- }
195
-
196
- interface NormalizeItemResult {
197
- warning?: string;
198
- file?: GitHubFolderFile;
199
- }
200
-
201
- function normalizeItem(source: GitHubFolderSource, item: unknown): NormalizeItemResult {
202
- if (!item || typeof item !== "object") return { warning: "invalid-item" };
203
- const raw = item as GitHubFolderItemRaw;
204
- const type = typeof raw.type === "string" ? raw.type : "";
205
- const path = safePath(raw.path);
206
- const name = typeof raw.name === "string" ? raw.name : (path.split("/").pop() || "");
207
- const extension = supportedExtension(path);
208
- if (type !== "file") return { warning: `${path || name}: folder-deferred` };
209
- if (!extension) return { warning: `${path || name}: unsupported-extension` };
210
- if (!insideFolder(source.path, path)) return { warning: `${path || name}: outside-folder` };
211
- if (typeof raw.download_url !== "string" || !raw.download_url) {
212
- return { warning: `${path || name}: missing-download-url` };
213
- }
214
- return {
215
- file: {
216
- name,
217
- path,
218
- extension,
219
- sizeBytes: Number(raw.size || 0),
220
- htmlUrl: typeof raw.html_url === "string" ? raw.html_url : "",
221
- downloadUrl: raw.download_url,
222
- selected: false,
223
- warnings: [],
224
- },
225
- };
226
- }
227
-
228
- function insideFolder(folderPath: string, filePath: string): boolean {
229
- if (!folderPath) return true;
230
- return filePath === folderPath || filePath.startsWith(`${folderPath}/`);
231
- }
232
-
233
- interface FetchJsonResult {
234
- notFound?: boolean;
235
- json?: unknown;
236
- }
237
-
238
- async function fetchJson(url: string, limits: PromptImportLimits): Promise<FetchJsonResult> {
239
- assertGithubApiUrl(url);
240
- const controller = new AbortController();
241
- const timer = setTimeout(() => controller.abort(), limits.fetchTimeoutMs);
242
- try {
243
- const response = await fetch(url, {
244
- signal: controller.signal,
245
- headers: { Accept: "application/vnd.github+json" },
246
- });
247
- assertGithubApiUrl(response.url || url);
248
- if (response.status === 404) return { notFound: true };
249
- if (!response.ok) {
250
- throw promptImportError("GITHUB_FOLDER_NOT_FOUND", `GitHub folder fetch failed with ${response.status}`, 422);
251
- }
252
- return { json: await response.json() };
253
- } catch (error) {
254
- const err = errInfo(error);
255
- if (err.name === "AbortError") {
256
- throw promptImportError("REMOTE_FETCH_TIMEOUT", "GitHub folder fetch timed out", 504);
257
- }
258
- throw error;
259
- } finally {
260
- clearTimeout(timer);
261
- }
262
- }
263
-
264
- export function normalizeGitHubFolderSource(input: unknown): GitHubFolderSource {
265
- const trimmed = typeof input === "string" ? input.trim() : "";
266
- if (!trimmed) {
267
- throw promptImportError("GITHUB_FOLDER_UNSUPPORTED", "GitHub folder source is required", 400);
268
- }
269
- const source = fromUrl(trimmed) ?? fromShorthand(trimmed);
270
- source.tags = folderTags(source);
271
- return source;
272
- }
273
-
274
- interface FolderFetchResult {
275
- source: GitHubFolderSource;
276
- files: GitHubFolderFile[];
277
- warnings: string[];
278
- }
279
-
280
- export async function fetchGitHubFolderFiles(source: GitHubFolderSource, limits: PromptImportLimits): Promise<FolderFetchResult> {
281
- const fetched = await fetchJson(source.apiUrl, limits);
282
- if (fetched.notFound && source.ambiguousTree) {
283
- throw promptImportError("AMBIGUOUS_GITHUB_REF", "GitHub tree URL is ambiguous; slash branches are not resolved in PR3", 422);
284
- }
285
- if (fetched.notFound) {
286
- throw promptImportError("GITHUB_FOLDER_NOT_FOUND", "GitHub folder was not found", 404);
287
- }
288
- if (!Array.isArray(fetched.json)) {
289
- throw promptImportError("GITHUB_FOLDER_UNSUPPORTED", "GitHub source is not a folder", 422);
290
- }
291
-
292
- const warnings: string[] = [];
293
- const maxFolderFiles = limits.maxFolderFiles ?? Infinity;
294
- if (fetched.json.length > maxFolderFiles) {
295
- warnings.push(`folder-raw-too-large:${fetched.json.length}`);
296
- }
297
- const files: GitHubFolderFile[] = [];
298
- for (const item of fetched.json) {
299
- const normalized = normalizeItem(source, item);
300
- if (normalized.warning) warnings.push(normalized.warning);
301
- if (normalized.file) files.push(normalized.file);
302
- }
303
- if (files.length > maxFolderFiles) {
304
- warnings.push(`folder-too-large:${files.length}`);
305
- }
306
- return { source, files: files.slice(0, maxFolderFiles), warnings };
307
- }
308
-
309
- function assertSelectedPath(source: GitHubFolderSource, rawPath: unknown, allowed: Map<string, GitHubFolderFile>): string {
310
- const path = safePath(rawPath);
311
- if (!path || !supportedExtension(path) || !insideFolder(source.path, path) || !allowed.has(path)) {
312
- throw promptImportError("GITHUB_FOLDER_SELECTION_EMPTY", `Selected file is not in the listed folder: ${path}`, 422);
313
- }
314
- return path;
315
- }
316
-
317
- export interface SelectedGitHubFolderFile extends GitHubFolderFile {
318
- text: string;
319
- contentHash: string;
320
- }
321
-
322
- interface SelectedFolderResult {
323
- source: GitHubFolderSource;
324
- files: SelectedGitHubFolderFile[];
325
- warnings: string[];
326
- }
327
-
328
- export async function fetchSelectedGitHubFolderFiles(source: GitHubFolderSource, selectedPaths: unknown, limits: PromptImportLimits): Promise<SelectedFolderResult> {
329
- const selected = Array.isArray(selectedPaths) ? selectedPaths : [];
330
- if (selected.length === 0) {
331
- throw promptImportError("GITHUB_FOLDER_SELECTION_EMPTY", "Select at least one folder file to preview", 422);
332
- }
333
- if (selected.length > (limits.maxFolderPreviewFiles ?? Infinity)) {
334
- throw promptImportError("GITHUB_FOLDER_SELECTION_TOO_LARGE", "Too many folder files selected", 413);
335
- }
336
-
337
- const listing = await fetchGitHubFolderFiles(source, limits);
338
- const allowed = new Map<string, GitHubFolderFile>(listing.files.map((file) => [file.path, file]));
339
- const paths = selected.map((path) => assertSelectedPath(source, path, allowed));
340
- const warnings: string[] = [...listing.warnings];
341
- const files: SelectedGitHubFolderFile[] = [];
342
- let firstError: unknown = null;
343
-
344
- for (const path of paths) {
345
- const file = allowed.get(path);
346
- if (!file) continue;
347
- try {
348
- const fetched = await fetchRawFile(file.downloadUrl, limits);
349
- files.push({ ...file, text: fetched.text, contentHash: fetched.contentHash });
350
- } catch (error) {
351
- const err = errInfo(error);
352
- if (!firstError) firstError = error;
353
- warnings.push(`${path}: ${err.message || "file fetch failed"}`);
354
- }
355
- }
356
- if (files.length === 0 && firstError) throw firstError;
357
- return { source, files, warnings };
358
- }
359
-
360
- interface RawFileResult {
361
- text: string;
362
- contentHash: string;
363
- }
364
-
365
- async function fetchRawFile(rawUrl: string, limits: PromptImportLimits): Promise<RawFileResult> {
366
- assertRawDownloadUrl(rawUrl);
367
- const controller = new AbortController();
368
- const timer = setTimeout(() => controller.abort(), limits.fetchTimeoutMs);
369
- try {
370
- const response = await fetch(rawUrl, { signal: controller.signal });
371
- assertRawDownloadUrl(response.url || rawUrl);
372
- if (!response.ok) {
373
- throw promptImportError("INVALID_GITHUB_SOURCE", `GitHub folder file fetch failed with ${response.status}`, 422);
374
- }
375
- const contentLength = Number(response.headers.get("content-length") || 0);
376
- if (contentLength > limits.maxFileBytesForPreview) {
377
- throw promptImportError("GITHUB_FOLDER_FILE_TOO_LARGE", "Folder file is too large", 413);
378
- }
379
- const buffer = await response.arrayBuffer();
380
- if (buffer.byteLength > limits.maxFileBytesForPreview) {
381
- throw promptImportError("GITHUB_FOLDER_FILE_TOO_LARGE", "Folder file is too large", 413);
382
- }
383
- const text = new TextDecoder("utf-8", { fatal: false }).decode(buffer);
384
- return {
385
- text,
386
- contentHash: createHash("sha256").update(Buffer.from(buffer)).digest("hex"),
387
- };
388
- } catch (error) {
389
- const err = errInfo(error);
390
- if (err.name === "AbortError") {
391
- throw promptImportError("REMOTE_FETCH_TIMEOUT", "GitHub folder file fetch timed out", 504);
392
- }
393
- throw error;
394
- } finally {
395
- clearTimeout(timer);
396
- }
397
- }