ima2-gen 1.1.7 → 1.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/phase-a-bg-cleanup-test.png +0 -0
- package/config.js +53 -0
- package/lib/localImportStore.js +111 -0
- package/lib/promptImport/curatedSources.js +139 -0
- package/lib/promptImport/discoveryRegistry.js +236 -0
- package/lib/promptImport/githubDiscovery.js +248 -0
- package/lib/promptImport/githubFolder.js +308 -0
- package/lib/promptImport/githubSource.js +36 -2
- package/lib/promptImport/gptImageHints.js +68 -0
- package/lib/promptImport/parsePromptCandidates.js +13 -0
- package/lib/promptImport/promptIndex.js +248 -0
- package/lib/promptImport/rankPromptCandidates.js +49 -0
- package/package.json +1 -1
- package/routes/imageImport.js +33 -0
- package/routes/index.js +2 -0
- package/routes/promptImport.js +179 -0
- package/ui/dist/assets/index-BDffwmLs.css +1 -0
- package/ui/dist/assets/index-D0fdHLkJ.js +31 -0
- package/ui/dist/assets/index-D0fdHLkJ.js.map +1 -0
- package/ui/dist/index.html +6 -3
- package/ui/dist/assets/index-DARPdT4Q.css +0 -1
- package/ui/dist/assets/index-ht80GMq4.js +0 -31
- package/ui/dist/assets/index-ht80GMq4.js.map +0 -1
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { promptImportError } from "./errors.js";
|
|
3
|
+
import { upsertDiscoveryCandidates } from "./discoveryRegistry.js";
|
|
4
|
+
|
|
5
|
+
const GITHUB_API_HOST = "api.github.com";
|
|
6
|
+
const DEFAULT_DISCOVERY_SEEDS = [
|
|
7
|
+
"gpt-image-2 prompt",
|
|
8
|
+
"image generation prompts",
|
|
9
|
+
"nano banana prompts",
|
|
10
|
+
"product photography prompt",
|
|
11
|
+
"typography image prompt",
|
|
12
|
+
"reference image prompt",
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
function tokenize(value) {
|
|
16
|
+
return String(value || "")
|
|
17
|
+
.toLowerCase()
|
|
18
|
+
.split(/[^a-z0-9가-힣-]+/i)
|
|
19
|
+
.filter(Boolean);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function discoveryLimits(ctx) {
|
|
23
|
+
return {
|
|
24
|
+
limit: ctx.config.limits.promptImportDiscoverySearchLimit,
|
|
25
|
+
maxQueries: ctx.config.limits.promptImportDiscoveryMaxQueries,
|
|
26
|
+
fetchTimeoutMs: ctx.config.limits.promptImportFetchTimeoutMs,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function validateGitHubApiUrl(rawUrl) {
|
|
31
|
+
let url;
|
|
32
|
+
try {
|
|
33
|
+
url = new URL(rawUrl);
|
|
34
|
+
} catch {
|
|
35
|
+
throw promptImportError("GITHUB_DISCOVERY_FAILED", "GitHub discovery returned an invalid URL");
|
|
36
|
+
}
|
|
37
|
+
if (!["https:", "http:"].includes(url.protocol) || url.hostname !== GITHUB_API_HOST) {
|
|
38
|
+
throw promptImportError("GITHUB_DISCOVERY_FAILED", "GitHub discovery redirected to an unsupported host");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function rateLimitFromHeaders(headers) {
|
|
43
|
+
const remaining = Number(headers.get("x-ratelimit-remaining") || Number.NaN);
|
|
44
|
+
const reset = Number(headers.get("x-ratelimit-reset") || Number.NaN);
|
|
45
|
+
const limit = Number(headers.get("x-ratelimit-limit") || Number.NaN);
|
|
46
|
+
return {
|
|
47
|
+
limit: Number.isFinite(limit) ? limit : null,
|
|
48
|
+
remaining: Number.isFinite(remaining) ? remaining : null,
|
|
49
|
+
resetAt: Number.isFinite(reset) ? new Date(reset * 1000).toISOString() : null,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function searchHeaders(ctx) {
|
|
54
|
+
const headers = {
|
|
55
|
+
Accept: "application/vnd.github+json",
|
|
56
|
+
"X-GitHub-Api-Version": "2022-11-28",
|
|
57
|
+
"User-Agent": "ima2-prompt-import-discovery",
|
|
58
|
+
};
|
|
59
|
+
const token = ctx.config.github?.token;
|
|
60
|
+
if (token) headers.Authorization = `Bearer ${token}`;
|
|
61
|
+
return headers;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function buildDiscoveryQueries({ q = "", seeds = [], limit = 5 } = {}) {
|
|
65
|
+
const raw = [q, ...seeds, ...DEFAULT_DISCOVERY_SEEDS]
|
|
66
|
+
.map((item) => String(item || "").trim())
|
|
67
|
+
.filter(Boolean);
|
|
68
|
+
const unique = [...new Set(raw)];
|
|
69
|
+
if (unique.length === 0) {
|
|
70
|
+
throw promptImportError("GITHUB_DISCOVERY_QUERY_EMPTY", "Discovery query is required", 422);
|
|
71
|
+
}
|
|
72
|
+
return unique.slice(0, Math.max(1, Number(limit) || 1));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function scoreDiscoveryRepository(repo, context = {}) {
|
|
76
|
+
const terms = tokenize([context.query, ...(context.seeds || [])].join(" "));
|
|
77
|
+
const nameText = `${repo.full_name || ""} ${repo.description || ""} ${(repo.topics || []).join(" ")}`.toLowerCase();
|
|
78
|
+
const pushedAt = repo.pushed_at ? Date.parse(repo.pushed_at) : 0;
|
|
79
|
+
const daysSincePush = pushedAt ? (Date.now() - pushedAt) / 86_400_000 : Number.POSITIVE_INFINITY;
|
|
80
|
+
const scoreReasons = [];
|
|
81
|
+
const warnings = [];
|
|
82
|
+
let score = 0;
|
|
83
|
+
|
|
84
|
+
const stars = Number(repo.stargazers_count || 0);
|
|
85
|
+
if (stars > 1000) {
|
|
86
|
+
score += 20;
|
|
87
|
+
scoreReasons.push("popular-repo");
|
|
88
|
+
} else if (stars > 100) {
|
|
89
|
+
score += 12;
|
|
90
|
+
scoreReasons.push("known-repo");
|
|
91
|
+
} else if (stars > 10) {
|
|
92
|
+
score += 5;
|
|
93
|
+
scoreReasons.push("some-stars");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (daysSincePush <= 365) {
|
|
97
|
+
score += 10;
|
|
98
|
+
scoreReasons.push("recently-updated");
|
|
99
|
+
} else {
|
|
100
|
+
score -= 6;
|
|
101
|
+
warnings.push("stale-repo");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (repo.license?.spdx_id) {
|
|
105
|
+
score += 8;
|
|
106
|
+
scoreReasons.push("license-present");
|
|
107
|
+
} else {
|
|
108
|
+
score -= 8;
|
|
109
|
+
warnings.push("no-license");
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (repo.archived || repo.disabled) {
|
|
113
|
+
score -= 20;
|
|
114
|
+
warnings.push("archived-or-disabled");
|
|
115
|
+
}
|
|
116
|
+
if (repo.fork) {
|
|
117
|
+
score -= 4;
|
|
118
|
+
warnings.push("fork-source");
|
|
119
|
+
}
|
|
120
|
+
if (!repo.default_branch) {
|
|
121
|
+
score -= 10;
|
|
122
|
+
warnings.push("missing-default-branch");
|
|
123
|
+
} else if (String(repo.default_branch).includes("/")) {
|
|
124
|
+
warnings.push("discovery-default-branch-unsupported");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const promptTerms = ["prompt", "image", "generation", "gpt-image", "nano", "typography", "reference"];
|
|
128
|
+
if (promptTerms.some((term) => nameText.includes(term))) {
|
|
129
|
+
score += 14;
|
|
130
|
+
scoreReasons.push("prompt-like");
|
|
131
|
+
}
|
|
132
|
+
for (const term of terms) {
|
|
133
|
+
if (term && nameText.includes(term)) score += 3;
|
|
134
|
+
}
|
|
135
|
+
if (!repo.html_url || !String(repo.html_url).startsWith("https://github.com/")) {
|
|
136
|
+
score -= 20;
|
|
137
|
+
warnings.push("non-github-url");
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return { score, scoreReasons: [...new Set(scoreReasons)], warnings: [...new Set(warnings)] };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function normalizeDiscoveryCandidate(repo, context = {}) {
|
|
144
|
+
const fullName = String(repo.full_name || "").trim();
|
|
145
|
+
const [owner, name] = fullName.split("/");
|
|
146
|
+
const scored = scoreDiscoveryRepository(repo, context);
|
|
147
|
+
return {
|
|
148
|
+
id: `github:${fullName}`,
|
|
149
|
+
repo: fullName,
|
|
150
|
+
owner,
|
|
151
|
+
name,
|
|
152
|
+
fullName,
|
|
153
|
+
htmlUrl: repo.html_url,
|
|
154
|
+
description: repo.description || "",
|
|
155
|
+
defaultBranch: repo.default_branch || "main",
|
|
156
|
+
stars: Number(repo.stargazers_count || 0),
|
|
157
|
+
forks: Number(repo.forks_count || 0),
|
|
158
|
+
openIssues: Number(repo.open_issues_count || 0),
|
|
159
|
+
updatedAt: repo.updated_at || null,
|
|
160
|
+
pushedAt: repo.pushed_at || null,
|
|
161
|
+
licenseSpdx: repo.license?.spdx_id || "NOASSERTION",
|
|
162
|
+
topics: Array.isArray(repo.topics) ? repo.topics : [],
|
|
163
|
+
language: repo.language || null,
|
|
164
|
+
score: scored.score,
|
|
165
|
+
scoreReasons: scored.scoreReasons,
|
|
166
|
+
warnings: scored.warnings,
|
|
167
|
+
status: "candidate",
|
|
168
|
+
query: context.query || "",
|
|
169
|
+
discoveredAt: new Date().toISOString(),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function discoveryCacheKey(input) {
|
|
174
|
+
return createHash("sha256").update(JSON.stringify(input)).digest("hex");
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async function searchOneQuery(ctx, query, perPage) {
|
|
178
|
+
const controller = new AbortController();
|
|
179
|
+
const timer = setTimeout(() => controller.abort(), discoveryLimits(ctx).fetchTimeoutMs);
|
|
180
|
+
const url = new URL("https://api.github.com/search/repositories");
|
|
181
|
+
url.searchParams.set("q", `${query} in:name,description`);
|
|
182
|
+
url.searchParams.set("sort", "stars");
|
|
183
|
+
url.searchParams.set("order", "desc");
|
|
184
|
+
url.searchParams.set("per_page", String(perPage));
|
|
185
|
+
try {
|
|
186
|
+
const response = await fetch(url, {
|
|
187
|
+
headers: searchHeaders(ctx),
|
|
188
|
+
signal: controller.signal,
|
|
189
|
+
});
|
|
190
|
+
validateGitHubApiUrl(response.url);
|
|
191
|
+
const rateLimit = rateLimitFromHeaders(response.headers);
|
|
192
|
+
if (response.status === 403 || response.status === 429) {
|
|
193
|
+
throw promptImportError("GITHUB_RATE_LIMITED", "GitHub discovery rate limit reached", 429);
|
|
194
|
+
}
|
|
195
|
+
if (!response.ok) {
|
|
196
|
+
throw promptImportError("GITHUB_DISCOVERY_FAILED", `GitHub discovery failed with ${response.status}`, 422);
|
|
197
|
+
}
|
|
198
|
+
const data = await response.json();
|
|
199
|
+
const items = Array.isArray(data.items) ? data.items : [];
|
|
200
|
+
return { items, rateLimit };
|
|
201
|
+
} catch (error) {
|
|
202
|
+
if (error?.name === "AbortError") {
|
|
203
|
+
throw promptImportError("REMOTE_FETCH_TIMEOUT", "GitHub discovery timed out", 504);
|
|
204
|
+
}
|
|
205
|
+
throw error;
|
|
206
|
+
} finally {
|
|
207
|
+
clearTimeout(timer);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export async function searchGitHubDiscovery(ctx, options = {}) {
|
|
212
|
+
const limits = discoveryLimits(ctx);
|
|
213
|
+
const queryLimit = Math.min(Number(options.maxQueries) || limits.maxQueries, limits.maxQueries);
|
|
214
|
+
const queries = buildDiscoveryQueries({
|
|
215
|
+
q: options.q,
|
|
216
|
+
seeds: options.seeds,
|
|
217
|
+
limit: queryLimit,
|
|
218
|
+
});
|
|
219
|
+
const requestedLimit = Math.min(Number(options.limit) || limits.limit, limits.limit);
|
|
220
|
+
const perQuery = Math.max(1, Math.ceil(requestedLimit / queries.length));
|
|
221
|
+
const warnings = [];
|
|
222
|
+
let rateLimit = null;
|
|
223
|
+
const byRepo = new Map();
|
|
224
|
+
|
|
225
|
+
for (const query of queries) {
|
|
226
|
+
const result = await searchOneQuery(ctx, query, perQuery);
|
|
227
|
+
rateLimit = result.rateLimit;
|
|
228
|
+
for (const repo of result.items) {
|
|
229
|
+
const candidate = normalizeDiscoveryCandidate(repo, {
|
|
230
|
+
query,
|
|
231
|
+
seeds: options.seeds,
|
|
232
|
+
});
|
|
233
|
+
const existing = byRepo.get(candidate.fullName);
|
|
234
|
+
if (!existing || candidate.score > existing.score) {
|
|
235
|
+
byRepo.set(candidate.fullName, candidate);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const candidates = [...byRepo.values()]
|
|
241
|
+
.filter((candidate) => candidate.fullName && candidate.htmlUrl?.startsWith("https://github.com/"))
|
|
242
|
+
.sort((a, b) => b.score - a.score || b.stars - a.stars)
|
|
243
|
+
.slice(0, requestedLimit);
|
|
244
|
+
|
|
245
|
+
await upsertDiscoveryCandidates(ctx, candidates);
|
|
246
|
+
if (rateLimit?.remaining === 0) warnings.push("github-rate-limit-exhausted");
|
|
247
|
+
return { candidates, warnings, rateLimit, cacheKey: discoveryCacheKey({ queries, requestedLimit }) };
|
|
248
|
+
}
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { promptImportError } from "./errors.js";
|
|
3
|
+
|
|
4
|
+
const GITHUB_HOST = "github.com";
|
|
5
|
+
const GITHUB_API_HOST = "api.github.com";
|
|
6
|
+
const RAW_HOST = "raw.githubusercontent.com";
|
|
7
|
+
const SUPPORTED_EXTENSIONS = new Set(["md", "markdown", "txt"]);
|
|
8
|
+
const OWNER_REPO_RE = /^[A-Za-z0-9_.-]+$/;
|
|
9
|
+
|
|
10
|
+
function safeDecode(value) {
|
|
11
|
+
try {
|
|
12
|
+
return decodeURIComponent(value);
|
|
13
|
+
} catch {
|
|
14
|
+
throw promptImportError("INVALID_GITHUB_SOURCE", "Invalid encoded GitHub folder path");
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function safePath(path) {
|
|
19
|
+
const raw = String(path || "").trim();
|
|
20
|
+
const lower = raw.toLowerCase();
|
|
21
|
+
if (raw.includes("\0") || lower.includes("%00")) {
|
|
22
|
+
throw promptImportError("INVALID_GITHUB_SOURCE", "GitHub folder path contains a null byte");
|
|
23
|
+
}
|
|
24
|
+
if (/%2f|%5c/i.test(raw)) {
|
|
25
|
+
throw promptImportError("INVALID_GITHUB_SOURCE", "GitHub folder path contains an encoded slash");
|
|
26
|
+
}
|
|
27
|
+
const decoded = safeDecode(raw);
|
|
28
|
+
if (decoded.includes("\\") || decoded.split("/").includes("..")) {
|
|
29
|
+
throw promptImportError("INVALID_GITHUB_SOURCE", "GitHub folder traversal is not allowed");
|
|
30
|
+
}
|
|
31
|
+
return decoded.replace(/^\/+|\/+$/g, "");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function assertOwnerRepo(owner, repo) {
|
|
35
|
+
if (!OWNER_REPO_RE.test(owner || "") || !OWNER_REPO_RE.test(repo || "")) {
|
|
36
|
+
throw promptImportError("INVALID_GITHUB_SOURCE", "Invalid GitHub owner or repository");
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function extensionForPath(path) {
|
|
41
|
+
const match = /\.([A-Za-z0-9]+)$/.exec(path);
|
|
42
|
+
return match?.[1]?.toLowerCase() ?? "";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function supportedExtension(path) {
|
|
46
|
+
const extension = extensionForPath(path);
|
|
47
|
+
return SUPPORTED_EXTENSIONS.has(extension) ? extension : "";
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function encodeApiPath(path) {
|
|
51
|
+
return path
|
|
52
|
+
.split("/")
|
|
53
|
+
.filter(Boolean)
|
|
54
|
+
.map((part) => encodeURIComponent(part))
|
|
55
|
+
.join("/");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function buildApiUrl({ owner, repo, ref, path }) {
|
|
59
|
+
const encodedPath = encodeApiPath(path);
|
|
60
|
+
const suffix = encodedPath ? `/${encodedPath}` : "";
|
|
61
|
+
return `https://${GITHUB_API_HOST}/repos/${owner}/${repo}/contents${suffix}?ref=${encodeURIComponent(ref)}`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function folderTags(source) {
|
|
65
|
+
return ["github", `repo:${source.owner}/${source.repo}`, `ref:${source.ref}`, `folder:${source.path || "/"}`];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function fromUrl(input) {
|
|
69
|
+
let url;
|
|
70
|
+
try {
|
|
71
|
+
url = new URL(input);
|
|
72
|
+
} catch {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
if (!["http:", "https:"].includes(url.protocol) || url.hostname !== GITHUB_HOST) {
|
|
76
|
+
throw promptImportError("GITHUB_FOLDER_UNSUPPORTED", "Only github.com folder URLs are supported", 400);
|
|
77
|
+
}
|
|
78
|
+
const [owner, repo, marker, rawRef, ...pathParts] = url.pathname.split("/").filter(Boolean);
|
|
79
|
+
assertOwnerRepo(owner, repo);
|
|
80
|
+
if (marker !== "tree") {
|
|
81
|
+
throw promptImportError("GITHUB_FOLDER_UNSUPPORTED", "Enter a GitHub folder URL or owner/repo:path/", 422);
|
|
82
|
+
}
|
|
83
|
+
const ref = safeDecode(rawRef || "main");
|
|
84
|
+
const path = safePath(pathParts.join("/"));
|
|
85
|
+
return makeSource({ owner, repo, ref, path, fromTreeUrl: true, ambiguousTree: path.includes("/") });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function fromShorthand(input) {
|
|
89
|
+
const match = /^([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)(?:@([^:]+))?:(.*)$/.exec(input);
|
|
90
|
+
if (!match) {
|
|
91
|
+
throw promptImportError("GITHUB_FOLDER_UNSUPPORTED", "Enter a GitHub folder URL or owner/repo:path/", 422);
|
|
92
|
+
}
|
|
93
|
+
const [, owner, repo, rawRef, rawPath] = match;
|
|
94
|
+
assertOwnerRepo(owner, repo);
|
|
95
|
+
const ref = rawRef ? safeDecode(rawRef.trim()) : "main";
|
|
96
|
+
if (ref.includes("/")) {
|
|
97
|
+
throw promptImportError("AMBIGUOUS_GITHUB_REF", "Branches with slashes need a later Git ref resolver", 422);
|
|
98
|
+
}
|
|
99
|
+
return makeSource({ owner, repo, ref, path: safePath(rawPath) });
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function makeSource({ owner, repo, ref, path, fromTreeUrl = false, ambiguousTree = false }) {
|
|
103
|
+
return {
|
|
104
|
+
kind: "github-folder",
|
|
105
|
+
owner,
|
|
106
|
+
repo,
|
|
107
|
+
ref,
|
|
108
|
+
path,
|
|
109
|
+
htmlUrl: `https://${GITHUB_HOST}/${owner}/${repo}/tree/${encodeURIComponent(ref)}${path ? `/${path}` : ""}`,
|
|
110
|
+
apiUrl: buildApiUrl({ owner, repo, ref, path }),
|
|
111
|
+
tags: [],
|
|
112
|
+
fromTreeUrl,
|
|
113
|
+
ambiguousTree,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function assertGithubApiUrl(rawUrl) {
|
|
118
|
+
let url;
|
|
119
|
+
try {
|
|
120
|
+
url = new URL(rawUrl);
|
|
121
|
+
} catch {
|
|
122
|
+
throw promptImportError("INVALID_GITHUB_SOURCE", "GitHub folder fetch returned an invalid URL");
|
|
123
|
+
}
|
|
124
|
+
if (!["http:", "https:"].includes(url.protocol) || url.hostname !== GITHUB_API_HOST) {
|
|
125
|
+
throw promptImportError("INVALID_GITHUB_SOURCE", "GitHub folder fetch used an unsupported host");
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function assertRawDownloadUrl(rawUrl) {
|
|
130
|
+
let url;
|
|
131
|
+
try {
|
|
132
|
+
url = new URL(rawUrl);
|
|
133
|
+
} catch {
|
|
134
|
+
throw promptImportError("INVALID_GITHUB_SOURCE", "GitHub folder file has an invalid download URL");
|
|
135
|
+
}
|
|
136
|
+
if (!["http:", "https:"].includes(url.protocol) || url.hostname !== RAW_HOST) {
|
|
137
|
+
throw promptImportError("INVALID_GITHUB_SOURCE", "GitHub folder file download host is unsupported");
|
|
138
|
+
}
|
|
139
|
+
const path = safePath(url.pathname.split("/").filter(Boolean).slice(3).join("/"));
|
|
140
|
+
if (!supportedExtension(path)) {
|
|
141
|
+
throw promptImportError("UNSUPPORTED_EXTENSION", "Only .md, .markdown, and .txt files are supported");
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function normalizeItem(source, item) {
|
|
146
|
+
if (!item || typeof item !== "object") return { warning: "invalid-item" };
|
|
147
|
+
const type = typeof item.type === "string" ? item.type : "";
|
|
148
|
+
const path = safePath(item.path || "");
|
|
149
|
+
const name = typeof item.name === "string" ? item.name : path.split("/").pop();
|
|
150
|
+
const extension = supportedExtension(path);
|
|
151
|
+
if (type !== "file") return { warning: `${path || name}: folder-deferred` };
|
|
152
|
+
if (!extension) return { warning: `${path || name}: unsupported-extension` };
|
|
153
|
+
if (!insideFolder(source.path, path)) return { warning: `${path || name}: outside-folder` };
|
|
154
|
+
if (typeof item.download_url !== "string" || !item.download_url) {
|
|
155
|
+
return { warning: `${path || name}: missing-download-url` };
|
|
156
|
+
}
|
|
157
|
+
return {
|
|
158
|
+
file: {
|
|
159
|
+
name,
|
|
160
|
+
path,
|
|
161
|
+
extension,
|
|
162
|
+
sizeBytes: Number(item.size || 0),
|
|
163
|
+
htmlUrl: typeof item.html_url === "string" ? item.html_url : "",
|
|
164
|
+
downloadUrl: item.download_url,
|
|
165
|
+
selected: false,
|
|
166
|
+
warnings: [],
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function insideFolder(folderPath, filePath) {
|
|
172
|
+
if (!folderPath) return true;
|
|
173
|
+
return filePath === folderPath || filePath.startsWith(`${folderPath}/`);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async function fetchJson(url, limits) {
|
|
177
|
+
assertGithubApiUrl(url);
|
|
178
|
+
const controller = new AbortController();
|
|
179
|
+
const timer = setTimeout(() => controller.abort(), limits.fetchTimeoutMs);
|
|
180
|
+
try {
|
|
181
|
+
const response = await fetch(url, {
|
|
182
|
+
signal: controller.signal,
|
|
183
|
+
headers: { Accept: "application/vnd.github+json" },
|
|
184
|
+
});
|
|
185
|
+
assertGithubApiUrl(response.url || url);
|
|
186
|
+
if (response.status === 404) return { notFound: true };
|
|
187
|
+
if (!response.ok) {
|
|
188
|
+
throw promptImportError("GITHUB_FOLDER_NOT_FOUND", `GitHub folder fetch failed with ${response.status}`, 422);
|
|
189
|
+
}
|
|
190
|
+
return { json: await response.json() };
|
|
191
|
+
} catch (error) {
|
|
192
|
+
if (error?.name === "AbortError") {
|
|
193
|
+
throw promptImportError("REMOTE_FETCH_TIMEOUT", "GitHub folder fetch timed out", 504);
|
|
194
|
+
}
|
|
195
|
+
throw error;
|
|
196
|
+
} finally {
|
|
197
|
+
clearTimeout(timer);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function normalizeGitHubFolderSource(input) {
|
|
202
|
+
const trimmed = typeof input === "string" ? input.trim() : "";
|
|
203
|
+
if (!trimmed) {
|
|
204
|
+
throw promptImportError("GITHUB_FOLDER_UNSUPPORTED", "GitHub folder source is required", 400);
|
|
205
|
+
}
|
|
206
|
+
const source = fromUrl(trimmed) ?? fromShorthand(trimmed);
|
|
207
|
+
source.tags = folderTags(source);
|
|
208
|
+
return source;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export async function fetchGitHubFolderFiles(source, limits) {
|
|
212
|
+
const fetched = await fetchJson(source.apiUrl, limits);
|
|
213
|
+
if (fetched.notFound && source.ambiguousTree) {
|
|
214
|
+
throw promptImportError("AMBIGUOUS_GITHUB_REF", "GitHub tree URL is ambiguous; slash branches are not resolved in PR3", 422);
|
|
215
|
+
}
|
|
216
|
+
if (fetched.notFound) {
|
|
217
|
+
throw promptImportError("GITHUB_FOLDER_NOT_FOUND", "GitHub folder was not found", 404);
|
|
218
|
+
}
|
|
219
|
+
if (!Array.isArray(fetched.json)) {
|
|
220
|
+
throw promptImportError("GITHUB_FOLDER_UNSUPPORTED", "GitHub source is not a folder", 422);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const warnings = [];
|
|
224
|
+
if (fetched.json.length > limits.maxFolderFiles) {
|
|
225
|
+
warnings.push(`folder-raw-too-large:${fetched.json.length}`);
|
|
226
|
+
}
|
|
227
|
+
const files = [];
|
|
228
|
+
for (const item of fetched.json) {
|
|
229
|
+
const normalized = normalizeItem(source, item);
|
|
230
|
+
if (normalized.warning) warnings.push(normalized.warning);
|
|
231
|
+
if (normalized.file) files.push(normalized.file);
|
|
232
|
+
}
|
|
233
|
+
if (files.length > limits.maxFolderFiles) {
|
|
234
|
+
warnings.push(`folder-too-large:${files.length}`);
|
|
235
|
+
}
|
|
236
|
+
return { source, files: files.slice(0, limits.maxFolderFiles), warnings };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function assertSelectedPath(source, rawPath, allowed) {
|
|
240
|
+
const path = safePath(rawPath);
|
|
241
|
+
if (!path || !supportedExtension(path) || !insideFolder(source.path, path) || !allowed.has(path)) {
|
|
242
|
+
throw promptImportError("GITHUB_FOLDER_SELECTION_EMPTY", `Selected file is not in the listed folder: ${path}`, 422);
|
|
243
|
+
}
|
|
244
|
+
return path;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export async function fetchSelectedGitHubFolderFiles(source, selectedPaths, limits) {
|
|
248
|
+
const selected = Array.isArray(selectedPaths) ? selectedPaths : [];
|
|
249
|
+
if (selected.length === 0) {
|
|
250
|
+
throw promptImportError("GITHUB_FOLDER_SELECTION_EMPTY", "Select at least one folder file to preview", 422);
|
|
251
|
+
}
|
|
252
|
+
if (selected.length > limits.maxFolderPreviewFiles) {
|
|
253
|
+
throw promptImportError("GITHUB_FOLDER_SELECTION_TOO_LARGE", "Too many folder files selected", 413);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const listing = await fetchGitHubFolderFiles(source, limits);
|
|
257
|
+
const allowed = new Map(listing.files.map((file) => [file.path, file]));
|
|
258
|
+
const paths = selected.map((path) => assertSelectedPath(source, path, allowed));
|
|
259
|
+
const warnings = [...listing.warnings];
|
|
260
|
+
const files = [];
|
|
261
|
+
let firstError = null;
|
|
262
|
+
|
|
263
|
+
for (const path of paths) {
|
|
264
|
+
const file = allowed.get(path);
|
|
265
|
+
try {
|
|
266
|
+
const fetched = await fetchRawFile(file.downloadUrl, limits);
|
|
267
|
+
files.push({ ...file, text: fetched.text, contentHash: fetched.contentHash });
|
|
268
|
+
} catch (error) {
|
|
269
|
+
if (!firstError) firstError = error;
|
|
270
|
+
warnings.push(`${path}: ${error?.message || "file fetch failed"}`);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (files.length === 0 && firstError) throw firstError;
|
|
274
|
+
return { source, files, warnings };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
async function fetchRawFile(rawUrl, limits) {
|
|
278
|
+
assertRawDownloadUrl(rawUrl);
|
|
279
|
+
const controller = new AbortController();
|
|
280
|
+
const timer = setTimeout(() => controller.abort(), limits.fetchTimeoutMs);
|
|
281
|
+
try {
|
|
282
|
+
const response = await fetch(rawUrl, { signal: controller.signal });
|
|
283
|
+
assertRawDownloadUrl(response.url || rawUrl);
|
|
284
|
+
if (!response.ok) {
|
|
285
|
+
throw promptImportError("INVALID_GITHUB_SOURCE", `GitHub folder file fetch failed with ${response.status}`, 422);
|
|
286
|
+
}
|
|
287
|
+
const contentLength = Number(response.headers.get("content-length") || 0);
|
|
288
|
+
if (contentLength > limits.maxFileBytesForPreview) {
|
|
289
|
+
throw promptImportError("GITHUB_FOLDER_FILE_TOO_LARGE", "Folder file is too large", 413);
|
|
290
|
+
}
|
|
291
|
+
const buffer = await response.arrayBuffer();
|
|
292
|
+
if (buffer.byteLength > limits.maxFileBytesForPreview) {
|
|
293
|
+
throw promptImportError("GITHUB_FOLDER_FILE_TOO_LARGE", "Folder file is too large", 413);
|
|
294
|
+
}
|
|
295
|
+
const text = new TextDecoder("utf-8", { fatal: false }).decode(buffer);
|
|
296
|
+
return {
|
|
297
|
+
text,
|
|
298
|
+
contentHash: createHash("sha256").update(Buffer.from(buffer)).digest("hex"),
|
|
299
|
+
};
|
|
300
|
+
} catch (error) {
|
|
301
|
+
if (error?.name === "AbortError") {
|
|
302
|
+
throw promptImportError("REMOTE_FETCH_TIMEOUT", "GitHub folder file fetch timed out", 504);
|
|
303
|
+
}
|
|
304
|
+
throw error;
|
|
305
|
+
} finally {
|
|
306
|
+
clearTimeout(timer);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
1
2
|
import { promptImportError } from "./errors.js";
|
|
2
3
|
|
|
3
4
|
const ALLOWED_HOSTS = new Set(["github.com", "raw.githubusercontent.com"]);
|
|
@@ -134,6 +135,27 @@ function normalizeShorthand(input) {
|
|
|
134
135
|
};
|
|
135
136
|
}
|
|
136
137
|
|
|
138
|
+
export function buildGitHubRawFileSource({ owner, repo, ref = "main", path }) {
|
|
139
|
+
assertOwnerRepo(owner, repo);
|
|
140
|
+
const cleanRef = safeDecode(String(ref || "main").trim());
|
|
141
|
+
if (!cleanRef || cleanRef.includes("/")) {
|
|
142
|
+
throw promptImportError("AMBIGUOUS_GITHUB_REF", "Branches with slashes need GitHub API folder support planned for PR3");
|
|
143
|
+
}
|
|
144
|
+
const cleanPath = assertCleanPath(String(path || "").trim());
|
|
145
|
+
const ext = assertSupportedFilePath(cleanPath);
|
|
146
|
+
return {
|
|
147
|
+
kind: "github",
|
|
148
|
+
owner,
|
|
149
|
+
repo,
|
|
150
|
+
ref: cleanRef,
|
|
151
|
+
path: cleanPath,
|
|
152
|
+
extension: ext,
|
|
153
|
+
htmlUrl: `https://github.com/${owner}/${repo}/blob/${cleanRef}/${cleanPath}`,
|
|
154
|
+
rawUrl: `https://raw.githubusercontent.com/${owner}/${repo}/${encodeURIComponent(cleanRef)}/${cleanPath}`,
|
|
155
|
+
tags: ["github", `repo:${owner}/${repo}`, `ref:${cleanRef}`, `file:${cleanPath.split("/").pop()}`, `ext:${ext}`],
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
137
159
|
function validateFinalFetchUrl(rawUrl) {
|
|
138
160
|
let url;
|
|
139
161
|
try {
|
|
@@ -172,7 +194,7 @@ export function normalizeGitHubSource(input) {
|
|
|
172
194
|
return normalizeUrlInput(trimmed) ?? normalizeShorthand(trimmed);
|
|
173
195
|
}
|
|
174
196
|
|
|
175
|
-
export async function
|
|
197
|
+
export async function fetchGitHubSource(source, limits) {
|
|
176
198
|
const controller = new AbortController();
|
|
177
199
|
const timer = setTimeout(() => controller.abort(), limits.fetchTimeoutMs);
|
|
178
200
|
try {
|
|
@@ -189,7 +211,14 @@ export async function fetchGitHubSourceText(source, limits) {
|
|
|
189
211
|
if (buffer.byteLength > limits.maxFileBytesForPreview) {
|
|
190
212
|
throw promptImportError("REMOTE_FILE_TOO_LARGE", "Remote file is too large", 413);
|
|
191
213
|
}
|
|
192
|
-
|
|
214
|
+
const text = new TextDecoder("utf-8", { fatal: false }).decode(buffer);
|
|
215
|
+
return {
|
|
216
|
+
text,
|
|
217
|
+
finalUrl: response.url,
|
|
218
|
+
etag: response.headers.get("etag"),
|
|
219
|
+
sizeBytes: buffer.byteLength,
|
|
220
|
+
contentHash: createHash("sha256").update(Buffer.from(buffer)).digest("hex"),
|
|
221
|
+
};
|
|
193
222
|
} catch (error) {
|
|
194
223
|
if (error?.name === "AbortError") {
|
|
195
224
|
throw promptImportError("REMOTE_FETCH_TIMEOUT", "GitHub fetch timed out", 504);
|
|
@@ -200,6 +229,11 @@ export async function fetchGitHubSourceText(source, limits) {
|
|
|
200
229
|
}
|
|
201
230
|
}
|
|
202
231
|
|
|
232
|
+
export async function fetchGitHubSourceText(source, limits) {
|
|
233
|
+
const result = await fetchGitHubSource(source, limits);
|
|
234
|
+
return result.text;
|
|
235
|
+
}
|
|
236
|
+
|
|
203
237
|
export function isSupportedPromptFileName(filename) {
|
|
204
238
|
return SUPPORTED_EXTENSIONS.has(extensionForPath(filename || ""));
|
|
205
239
|
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
const MODEL_HINTS = [
|
|
2
|
+
["gpt-image-2", /\bgpt[- ]?image[- ]?2\b/i],
|
|
3
|
+
["gpt-image", /\bgpt[- ]?image\b/i],
|
|
4
|
+
["nano-banana", /\bnano[- ]?banana\b/i],
|
|
5
|
+
["midjourney", /\bmidjourney\b/i],
|
|
6
|
+
["stable-diffusion", /\bstable[- ]?diffusion\b/i],
|
|
7
|
+
];
|
|
8
|
+
|
|
9
|
+
const TASK_HINTS = [
|
|
10
|
+
["image_generation", /\b(image generation|generate an image|text to image)\b/i],
|
|
11
|
+
["reference-image", /\b(reference image|keep the same|preserve|consistent character|same face)\b/i],
|
|
12
|
+
["mask-edit", /\b(mask|inpaint|outpaint|replace only|change only)\b/i],
|
|
13
|
+
["typography", /\b(typography|headline|readable text|lettering|poster text|title text)\b/i],
|
|
14
|
+
["layout", /\b(layout|grid|composition|poster|diagram|infographic)\b/i],
|
|
15
|
+
["product-shot", /\b(product photo|product shot|packaging|label|reflection)\b/i],
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
const SIZE_HINTS = [
|
|
19
|
+
["2k", /\b2k\b/i],
|
|
20
|
+
["4k", /\b4k\b/i],
|
|
21
|
+
["square", /\bsquare\b/i],
|
|
22
|
+
["portrait", /\bportrait|vertical\b/i],
|
|
23
|
+
["landscape", /\blandscape|horizontal\b/i],
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
const QUALITY_HINTS = [
|
|
27
|
+
["low", /\blow\b/i],
|
|
28
|
+
["medium", /\bmedium\b/i],
|
|
29
|
+
["high", /\bhigh\b/i],
|
|
30
|
+
["auto", /\bauto\b/i],
|
|
31
|
+
["draft", /\bdraft|thumbnail|iteration\b/i],
|
|
32
|
+
["final", /\bfinal|production[- ]ready\b/i],
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
function matches(text, patterns) {
|
|
36
|
+
return patterns.filter(([, pattern]) => pattern.test(text)).map(([name]) => name);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function extractGptImageHints(text) {
|
|
40
|
+
const value = String(text || "");
|
|
41
|
+
const modelHints = matches(value, MODEL_HINTS);
|
|
42
|
+
const taskHints = matches(value, TASK_HINTS);
|
|
43
|
+
const sizeHints = matches(value, SIZE_HINTS);
|
|
44
|
+
const qualityHints = matches(value, QUALITY_HINTS);
|
|
45
|
+
const warnings = [];
|
|
46
|
+
|
|
47
|
+
if (/\btransparent|alpha channel|no background|cutout\b/i.test(value)) {
|
|
48
|
+
warnings.push("transparent-unsupported-gpt-image-2");
|
|
49
|
+
}
|
|
50
|
+
if (/\bexact text|small text|dense text|legal copy\b/i.test(value)) {
|
|
51
|
+
warnings.push("text-rendering-sensitive");
|
|
52
|
+
}
|
|
53
|
+
if (/\bcomplex layout|multi[- ]panel|precise diagram\b/i.test(value)) {
|
|
54
|
+
warnings.push("layout-sensitive");
|
|
55
|
+
}
|
|
56
|
+
if (/\b4k|high resolution|ultra[- ]high\b/i.test(value)) {
|
|
57
|
+
warnings.push("high-res-cost-warning");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
modelHints,
|
|
62
|
+
generationSurfaceHints: taskHints.includes("image_generation") ? ["responses-image-tool"] : [],
|
|
63
|
+
taskHints,
|
|
64
|
+
sizeHints,
|
|
65
|
+
qualityHints,
|
|
66
|
+
warnings,
|
|
67
|
+
};
|
|
68
|
+
}
|