ima2-gen 1.1.21 → 1.1.23

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 (214) hide show
  1. package/README.md +44 -7
  2. package/bin/commands/video.js +14 -0
  3. package/bin/ima2.js +14 -4
  4. package/bin/lib/platform.js +34 -5
  5. package/docs/README.ko.md +43 -2
  6. package/lib/agentQueueWorker.js +6 -0
  7. package/lib/agentRuntime.js +3 -2
  8. package/lib/atomicWrite.js +14 -0
  9. package/lib/grokImageAdapter.js +6 -0
  10. package/lib/grokProxyLauncher.js +5 -3
  11. package/lib/grokVideoAdapter.js +1 -1
  12. package/lib/grokVideoPlannerPrompt.js +10 -0
  13. package/lib/inflight.js +1 -1
  14. package/lib/oauthLauncher.js +5 -0
  15. package/lib/videoFrameExtract.js +3 -3
  16. package/package.json +5 -7
  17. package/routes/capabilities.js +13 -0
  18. package/routes/edit.js +2 -1
  19. package/routes/generate.js +32 -6
  20. package/routes/health.js +4 -3
  21. package/routes/multimode.js +2 -1
  22. package/routes/video.js +35 -3
  23. package/server.js +29 -2
  24. package/skills/ima2/SKILL.md +48 -6
  25. package/ui/dist/.vite/manifest.json +12 -12
  26. package/ui/dist/assets/{AgentWorkspace-B_hq9CLg.js → AgentWorkspace-C21zqdTZ.js} +1 -1
  27. package/ui/dist/assets/{CardNewsWorkspace-wD12J7qk.js → CardNewsWorkspace-BN-ga1lG.js} +1 -1
  28. package/ui/dist/assets/{NodeCanvas-CI_wuPMf.js → NodeCanvas-BbMa4IhI.js} +1 -1
  29. package/ui/dist/assets/{PromptBuilderPanel-CUTujJUV.js → PromptBuilderPanel-DRwBJRDQ.js} +1 -1
  30. package/ui/dist/assets/{PromptImportDialog-CUi66jPK.js → PromptImportDialog-Dp85kHCq.js} +2 -2
  31. package/ui/dist/assets/{PromptImportDiscoverySection-Cm3vrjY4.js → PromptImportDiscoverySection-BE8Q8MLD.js} +1 -1
  32. package/ui/dist/assets/{PromptImportFolderSection-DOtWTD9n.js → PromptImportFolderSection-PtH5x0sc.js} +1 -1
  33. package/ui/dist/assets/{PromptLibraryPanel-BMjQegRa.js → PromptLibraryPanel-FnM9tHI9.js} +2 -2
  34. package/ui/dist/assets/SettingsWorkspace-MARPGyBL.js +1 -0
  35. package/ui/dist/assets/index-BAFI6htx.js +42 -0
  36. package/ui/dist/assets/{index-31uVIdt4.js → index-BSXxr_Bt.js} +1 -1
  37. package/ui/dist/assets/index-DS-ADE7U.css +1 -0
  38. package/ui/dist/index.html +2 -2
  39. package/bin/commands/annotate.ts +0 -119
  40. package/bin/commands/cancel.ts +0 -48
  41. package/bin/commands/canvas-versions.ts +0 -80
  42. package/bin/commands/capabilities.ts +0 -110
  43. package/bin/commands/cardnews.ts +0 -249
  44. package/bin/commands/comfy.ts +0 -54
  45. package/bin/commands/config.ts +0 -186
  46. package/bin/commands/defaults.ts +0 -192
  47. package/bin/commands/doctor.ts +0 -202
  48. package/bin/commands/edit.ts +0 -150
  49. package/bin/commands/gen.ts +0 -214
  50. package/bin/commands/grok.ts +0 -90
  51. package/bin/commands/history.ts +0 -146
  52. package/bin/commands/ls.ts +0 -64
  53. package/bin/commands/metadata.ts +0 -39
  54. package/bin/commands/multimode.ts +0 -196
  55. package/bin/commands/node.ts +0 -166
  56. package/bin/commands/observability.ts +0 -176
  57. package/bin/commands/ping.ts +0 -31
  58. package/bin/commands/prompt-sub/build.ts +0 -101
  59. package/bin/commands/prompt.ts +0 -492
  60. package/bin/commands/ps.ts +0 -81
  61. package/bin/commands/session.ts +0 -266
  62. package/bin/commands/show.ts +0 -72
  63. package/bin/commands/skill.ts +0 -70
  64. package/bin/commands/video.ts +0 -442
  65. package/bin/ima2.ts +0 -430
  66. package/bin/lib/args.ts +0 -92
  67. package/bin/lib/browser-id.ts +0 -16
  68. package/bin/lib/client.ts +0 -122
  69. package/bin/lib/config-store.ts +0 -120
  70. package/bin/lib/destructive-confirm.ts +0 -19
  71. package/bin/lib/doctor-checks.ts +0 -91
  72. package/bin/lib/error-hints.ts +0 -23
  73. package/bin/lib/files.ts +0 -39
  74. package/bin/lib/output.ts +0 -73
  75. package/bin/lib/platform.ts +0 -99
  76. package/bin/lib/recover-output.ts +0 -139
  77. package/bin/lib/sse.ts +0 -73
  78. package/bin/lib/star-prompt.ts +0 -97
  79. package/bin/lib/storage-doctor.ts +0 -39
  80. package/bin/lib/ui-build.ts +0 -85
  81. package/config.ts +0 -354
  82. package/lib/agentCommandParser.ts +0 -69
  83. package/lib/agentGenerationPlanner.ts +0 -273
  84. package/lib/agentQuestionResponder.ts +0 -266
  85. package/lib/agentQueueStore.ts +0 -270
  86. package/lib/agentQueueWorker.ts +0 -89
  87. package/lib/agentRuntime.ts +0 -604
  88. package/lib/agentSettings.ts +0 -72
  89. package/lib/agentStore.ts +0 -422
  90. package/lib/agentStoreRows.ts +0 -136
  91. package/lib/agentTypes.ts +0 -154
  92. package/lib/apiCachePolicy.ts +0 -11
  93. package/lib/assetLifecycle.ts +0 -146
  94. package/lib/canvasVersionStore.ts +0 -223
  95. package/lib/capabilities.ts +0 -126
  96. package/lib/cardNewsGenerator.ts +0 -271
  97. package/lib/cardNewsJobStore.ts +0 -142
  98. package/lib/cardNewsManifestStore.ts +0 -154
  99. package/lib/cardNewsPlanner.ts +0 -236
  100. package/lib/cardNewsPlannerClient.ts +0 -155
  101. package/lib/cardNewsPlannerPrompt.ts +0 -62
  102. package/lib/cardNewsPlannerSchema.ts +0 -321
  103. package/lib/cardNewsRoleTemplateStore.ts +0 -47
  104. package/lib/cardNewsTemplateStore.ts +0 -252
  105. package/lib/codexDetect.ts +0 -71
  106. package/lib/comfyBridge.ts +0 -235
  107. package/lib/composerSnapshot.ts +0 -33
  108. package/lib/configKeys.ts +0 -62
  109. package/lib/db.ts +0 -295
  110. package/lib/errInfo.ts +0 -43
  111. package/lib/errorClassify.ts +0 -100
  112. package/lib/generationCancel.ts +0 -28
  113. package/lib/generationErrors.ts +0 -238
  114. package/lib/grokImageAdapter.ts +0 -513
  115. package/lib/grokMultimodeAdapter.ts +0 -84
  116. package/lib/grokProxyLauncher.ts +0 -153
  117. package/lib/grokRuntime.ts +0 -23
  118. package/lib/grokSizeMapper.ts +0 -71
  119. package/lib/grokVideoAdapter.ts +0 -458
  120. package/lib/grokVideoCanvas.ts +0 -26
  121. package/lib/grokVideoDownload.ts +0 -59
  122. package/lib/grokVideoPlannerPrompt.ts +0 -67
  123. package/lib/historyIndex.ts +0 -51
  124. package/lib/historyList.ts +0 -181
  125. package/lib/imageMetadata.ts +0 -113
  126. package/lib/imageMetadataStore.ts +0 -67
  127. package/lib/imageModels.ts +0 -165
  128. package/lib/inflight.ts +0 -281
  129. package/lib/localImportStore.ts +0 -114
  130. package/lib/logger.ts +0 -161
  131. package/lib/nodeStore.ts +0 -91
  132. package/lib/oauthLauncher.ts +0 -94
  133. package/lib/oauthNormalize.ts +0 -30
  134. package/lib/oauthProxy/errors.ts +0 -128
  135. package/lib/oauthProxy/generators.ts +0 -494
  136. package/lib/oauthProxy/index.ts +0 -28
  137. package/lib/oauthProxy/prompts.ts +0 -123
  138. package/lib/oauthProxy/references.ts +0 -45
  139. package/lib/oauthProxy/runtime.ts +0 -115
  140. package/lib/oauthProxy/streams.ts +0 -232
  141. package/lib/oauthProxy/types.ts +0 -9
  142. package/lib/oauthProxy.ts +0 -3
  143. package/lib/openDirectory.ts +0 -47
  144. package/lib/pngInfo.ts +0 -26
  145. package/lib/promptBuilder/attachments.ts +0 -74
  146. package/lib/promptBuilder/client.ts +0 -130
  147. package/lib/promptBuilder/constants.ts +0 -9
  148. package/lib/promptBuilder/context.ts +0 -36
  149. package/lib/promptBuilder/errors.ts +0 -12
  150. package/lib/promptBuilder/requestSchema.ts +0 -56
  151. package/lib/promptBuilder/responseParser.ts +0 -219
  152. package/lib/promptBuilder/systemPrompt.ts +0 -135
  153. package/lib/promptBuilder/transport.ts +0 -94
  154. package/lib/promptBuilder/types.ts +0 -109
  155. package/lib/promptImport/curatedSources.ts +0 -141
  156. package/lib/promptImport/discoveryRegistry.ts +0 -329
  157. package/lib/promptImport/errors.ts +0 -18
  158. package/lib/promptImport/githubDiscovery.ts +0 -309
  159. package/lib/promptImport/githubFolder.ts +0 -397
  160. package/lib/promptImport/githubSource.ts +0 -257
  161. package/lib/promptImport/gptImageHints.ts +0 -70
  162. package/lib/promptImport/parsePromptCandidates.ts +0 -179
  163. package/lib/promptImport/promptIndex.ts +0 -326
  164. package/lib/promptImport/rankPromptCandidates.ts +0 -65
  165. package/lib/promptImport/types.ts +0 -103
  166. package/lib/promptSafetyPolicy.ts +0 -5
  167. package/lib/providerOptions.ts +0 -56
  168. package/lib/referenceImageCompress.ts +0 -84
  169. package/lib/refs.ts +0 -133
  170. package/lib/requestLogger.ts +0 -49
  171. package/lib/responsesDoctor.ts +0 -456
  172. package/lib/responsesErrors.ts +0 -83
  173. package/lib/responsesFallback.ts +0 -114
  174. package/lib/responsesImageAdapter.ts +0 -466
  175. package/lib/responsesParse.ts +0 -452
  176. package/lib/responsesTools.ts +0 -28
  177. package/lib/runtimeContext.ts +0 -146
  178. package/lib/runtimePorts.ts +0 -105
  179. package/lib/sessionStore.ts +0 -308
  180. package/lib/storageMigration.ts +0 -310
  181. package/lib/styleSheet.ts +0 -139
  182. package/lib/systemTrash.ts +0 -20
  183. package/lib/videoContinuity.ts +0 -180
  184. package/lib/videoFrameExtract.ts +0 -78
  185. package/lib/videoSeriesChain.ts +0 -29
  186. package/lib/visibleTextLanguagePolicy.ts +0 -7
  187. package/routes/agent.ts +0 -308
  188. package/routes/annotations.ts +0 -118
  189. package/routes/canvasVersions.ts +0 -69
  190. package/routes/capabilities.ts +0 -18
  191. package/routes/cardNews.ts +0 -211
  192. package/routes/comfy.ts +0 -43
  193. package/routes/edit.ts +0 -352
  194. package/routes/generate.ts +0 -492
  195. package/routes/grok.ts +0 -24
  196. package/routes/health.ts +0 -123
  197. package/routes/history.ts +0 -221
  198. package/routes/imageImport.ts +0 -37
  199. package/routes/index.ts +0 -52
  200. package/routes/metadata.ts +0 -77
  201. package/routes/multimode.ts +0 -499
  202. package/routes/nodes.ts +0 -578
  203. package/routes/promptBuilder.ts +0 -37
  204. package/routes/promptImport.ts +0 -379
  205. package/routes/prompts.ts +0 -428
  206. package/routes/quota.ts +0 -89
  207. package/routes/sessions.ts +0 -317
  208. package/routes/storage.ts +0 -47
  209. package/routes/video.ts +0 -300
  210. package/routes/videoExtended.ts +0 -284
  211. package/server.ts +0 -293
  212. package/ui/dist/assets/SettingsWorkspace-PiaVnsdA.js +0 -1
  213. package/ui/dist/assets/index-CjgnNtgt.css +0 -1
  214. package/ui/dist/assets/index-Da2s4_-5.js +0 -36
@@ -1,379 +0,0 @@
1
- import type { Express, Request, Response } from "express";
2
- import { getDb } from "../lib/db.js";
3
- import { logError, logEvent } from "../lib/logger.js";
4
- import { isPromptImportError, promptImportError } from "../lib/promptImport/errors.js";
5
- import {
6
- fetchGitHubSourceText,
7
- isSupportedPromptFileName,
8
- normalizeGitHubSource,
9
- } from "../lib/promptImport/githubSource.js";
10
- import {
11
- fetchGitHubFolderFiles,
12
- fetchSelectedGitHubFolderFiles,
13
- normalizeGitHubFolderSource,
14
- } from "../lib/promptImport/githubFolder.js";
15
- import { parsePromptCandidates } from "../lib/promptImport/parsePromptCandidates.js";
16
- import {
17
- getPromptImportSources,
18
- refreshCuratedSource,
19
- searchCuratedPrompts,
20
- } from "../lib/promptImport/promptIndex.js";
21
- import { searchGitHubDiscovery } from "../lib/promptImport/githubDiscovery.js";
22
- import {
23
- listDiscoveryCandidates,
24
- reviewDiscoveryCandidate,
25
- } from "../lib/promptImport/discoveryRegistry.js";
26
- import { requireRuntimeContext, type RouteRuntimeContext, type RuntimeContext } from "../lib/runtimeContext.js";
27
- import type { GitHubFileSource } from "../lib/promptImport/types.js";
28
-
29
- interface LocalSource {
30
- kind: "local";
31
- filename: string;
32
- extension: string;
33
- text: string;
34
- tags: string[];
35
- }
36
-
37
- type ParsedSource = LocalSource | (GitHubFileSource & { kind?: string; filename?: string });
38
-
39
- type ImportLimits = ReturnType<typeof promptImportLimits>;
40
-
41
- interface CandidateLike {
42
- text?: string;
43
- name?: string;
44
- tags?: unknown;
45
- mode?: string;
46
- }
47
-
48
- function promptImportLimits(ctx: RuntimeContext) {
49
- return {
50
- maxFileBytesForPreview: ctx.config.limits.promptImportMaxFileBytes,
51
- maxPromptCandidatesPerFile: ctx.config.limits.promptImportMaxCandidatesPerFile,
52
- maxPromptCandidatesPerImport: ctx.config.limits.promptImportMaxCandidatesPerImport,
53
- fetchTimeoutMs: ctx.config.limits.promptImportFetchTimeoutMs,
54
- maxCandidateChars: ctx.config.limits.promptImportMaxCandidateChars,
55
- minCandidateChars: ctx.config.limits.promptImportMinCandidateChars,
56
- maxSourceCharsScanned: ctx.config.limits.promptImportMaxSourceCharsScanned,
57
- maxRepoIndexFiles: ctx.config.limits.promptImportMaxRepoIndexFiles,
58
- curatedSearchLimit: ctx.config.limits.promptImportCuratedSearchLimit,
59
- indexCacheTtlMs: ctx.config.limits.promptImportIndexCacheTtlMs,
60
- maxFolderFiles: ctx.config.limits.promptImportMaxFolderFiles,
61
- maxFolderPreviewFiles: ctx.config.limits.promptImportMaxFolderPreviewFiles,
62
- discoverySearchLimit: ctx.config.limits.promptImportDiscoverySearchLimit,
63
- discoveryMaxQueries: ctx.config.limits.promptImportDiscoveryMaxQueries,
64
- };
65
- }
66
-
67
- function sendPromptImportError(res: Response, error: unknown) {
68
- const errAsAny = (error ?? {}) as { code?: unknown; status?: unknown; message?: unknown };
69
- const status = isPromptImportError(error) && typeof errAsAny.status === "number" ? errAsAny.status : 500;
70
- const code = isPromptImportError(error) && typeof errAsAny.code === "string" ? errAsAny.code : "PROMPT_IMPORT_FAILED";
71
- const message = typeof errAsAny.message === "string" ? errAsAny.message : "Prompt import failed";
72
- res.status(status).json({ error: { code, message } });
73
- }
74
-
75
- function generateId() {
76
- return `${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
77
- }
78
-
79
- function sourceFilename(source: ParsedSource): string {
80
- if (source.kind === "local" && "filename" in source && typeof source.filename === "string") return source.filename;
81
- const gh = source as GitHubFileSource & { filename?: string };
82
- if (typeof gh.filename === "string" && gh.filename) return gh.filename;
83
- if (typeof gh.path === "string" && gh.path) return gh.path.split("/").pop() ?? "";
84
- return "";
85
- }
86
-
87
- function normalizeLocalSource(source: { filename?: unknown; text?: unknown }): LocalSource {
88
- const filename = typeof source?.filename === "string" ? source.filename.trim() : "";
89
- const text = typeof source?.text === "string" ? source.text : "";
90
- if (!filename || !isSupportedPromptFileName(filename)) {
91
- throw promptImportError("UNSUPPORTED_EXTENSION", "Only .md, .markdown, and .txt files are supported");
92
- }
93
- if (!text.trim()) {
94
- throw promptImportError("PROMPT_IMPORT_EMPTY", "Prompt source is empty", 422);
95
- }
96
- const ext = (filename.split(".").pop() ?? "").toLowerCase();
97
- return {
98
- kind: "local",
99
- filename,
100
- extension: ext,
101
- text,
102
- tags: [`file:${filename}`, `ext:${ext}`],
103
- };
104
- }
105
-
106
- async function buildPreview(req: Request, ctx: RuntimeContext) {
107
- const body = (req.body ?? {}) as { source?: { kind?: string; input?: unknown } & Record<string, unknown> } & Record<string, unknown>;
108
- const rawSource = (body.source ?? body) as { kind?: string; input?: unknown; filename?: unknown; text?: unknown };
109
- const kind = rawSource.kind === "github" ? "github" : "local";
110
- const limits = promptImportLimits(ctx);
111
- let source: ParsedSource;
112
- let text: string;
113
-
114
- if (kind === "github") {
115
- source = normalizeGitHubSource(rawSource.input);
116
- text = await fetchGitHubSourceText(source, limits);
117
- } else {
118
- source = normalizeLocalSource(rawSource);
119
- text = source.text;
120
- }
121
-
122
- if (text.length > limits.maxSourceCharsScanned) {
123
- text = text.slice(0, limits.maxSourceCharsScanned);
124
- }
125
-
126
- const candidates = parsePromptCandidates({
127
- text,
128
- filename: sourceFilename(source),
129
- source: {
130
- kind: source.kind ?? "github",
131
- owner: (source as GitHubFileSource).owner,
132
- repo: (source as GitHubFileSource).repo,
133
- ref: (source as GitHubFileSource).ref,
134
- path: (source as GitHubFileSource).path,
135
- htmlUrl: (source as GitHubFileSource).htmlUrl,
136
- filename: (source as ParsedSource).filename,
137
- },
138
- tags: source.tags,
139
- limits,
140
- });
141
-
142
- if (candidates.length === 0) {
143
- throw promptImportError("PROMPT_IMPORT_EMPTY", "No prompt candidates were found", 422);
144
- }
145
- return { source, candidates, warnings: [] };
146
- }
147
-
148
- function normalizeFolderInput(body: { source?: { input?: unknown }; input?: unknown }) {
149
- const input = typeof body?.source?.input === "string" ? body.source.input : body?.input;
150
- return normalizeGitHubFolderSource(input);
151
- }
152
-
153
- async function buildFolderFiles(req: Request, ctx: RuntimeContext) {
154
- const limits = promptImportLimits(ctx);
155
- const source = normalizeFolderInput((req.body ?? {}) as { source?: { input?: unknown }; input?: unknown });
156
- return fetchGitHubFolderFiles(source, limits);
157
- }
158
-
159
- async function buildFolderPreview(req: Request, ctx: RuntimeContext) {
160
- const limits = promptImportLimits(ctx);
161
- const body = (req.body ?? {}) as { source?: { input?: unknown }; input?: unknown; paths?: unknown };
162
- const source = normalizeFolderInput(body);
163
- const paths = Array.isArray(body.paths) ? (body.paths as string[]) : [];
164
- const selected = await fetchSelectedGitHubFolderFiles(source, paths, limits);
165
- const candidates: ReturnType<typeof parsePromptCandidates> = [];
166
- const warnings = [...selected.warnings];
167
-
168
- for (const file of selected.files) {
169
- const text = file.text.length > limits.maxSourceCharsScanned
170
- ? file.text.slice(0, limits.maxSourceCharsScanned)
171
- : file.text;
172
- const parsed = parsePromptCandidates({
173
- text,
174
- filename: file.path,
175
- source: {
176
- kind: "github",
177
- owner: source.owner,
178
- repo: source.repo,
179
- ref: source.ref,
180
- path: file.path,
181
- htmlUrl: file.htmlUrl,
182
- },
183
- tags: [...(source.tags ?? []), `file:${file.name}`, `ext:${file.extension}`],
184
- limits,
185
- });
186
- if (parsed.length === 0) warnings.push(`${file.path}: no prompt candidates`);
187
- candidates.push(...parsed);
188
- }
189
-
190
- if (candidates.length === 0) {
191
- throw promptImportError("PROMPT_IMPORT_EMPTY", "No prompt candidates were found", 422);
192
- }
193
- return {
194
- source,
195
- files: selected.files.map(({ text: _t, contentHash: _h, ...file }) => file),
196
- candidates: candidates.slice(0, limits.maxPromptCandidatesPerImport),
197
- warnings,
198
- };
199
- }
200
-
201
- function assertCommitCandidateText(text: string, limits: ImportLimits) {
202
- if (text.length < limits.minCandidateChars) {
203
- throw promptImportError("PROMPT_IMPORT_EMPTY", "Prompt candidate is too short", 422);
204
- }
205
- if (text.length > limits.maxCandidateChars) {
206
- throw promptImportError("PROMPT_IMPORT_TOO_MANY_CANDIDATES", "Prompt candidate is too large", 413);
207
- }
208
- }
209
-
210
- function commitCandidates(candidates: CandidateLike[], folderId: unknown, limits: ImportLimits) {
211
- const db = getDb();
212
- const result = { foldersCreated: 0, promptsImported: 0, duplicatesSkipped: 0 };
213
- const now = Math.floor(Date.now() / 1000);
214
- const targetFolder = typeof folderId === "string" && folderId ? folderId : "__root__";
215
- const folderExists = db.prepare("SELECT 1 FROM prompt_folders WHERE id = ? LIMIT 1").get(targetFolder);
216
- const resolvedFolderId = folderExists ? targetFolder : "__root__";
217
-
218
- for (const candidate of candidates) {
219
- if (!candidate?.text || typeof candidate.text !== "string") continue;
220
- const text = candidate.text.trim();
221
- if (!text) continue;
222
- assertCommitCandidateText(text, limits);
223
- const dup = db.prepare("SELECT 1 FROM prompts WHERE text = ? AND folder_id = ? LIMIT 1").get(text, resolvedFolderId);
224
- if (dup) {
225
- result.duplicatesSkipped++;
226
- continue;
227
- }
228
- const tagsJson = Array.isArray(candidate.tags) ? JSON.stringify([...new Set(candidate.tags)]) : null;
229
- db.prepare(
230
- `INSERT INTO prompts (id, folder_id, name, text, tags, mode, is_favorite, created_at, updated_at)
231
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
232
- ).run(
233
- generateId(),
234
- resolvedFolderId,
235
- typeof candidate.name === "string" && candidate.name.trim() ? candidate.name.trim() : text.slice(0, 30),
236
- text,
237
- tagsJson,
238
- candidate.mode === "direct" || candidate.mode === "auto" ? candidate.mode : null,
239
- 0,
240
- now,
241
- now,
242
- );
243
- result.promptsImported++;
244
- }
245
- return result;
246
- }
247
-
248
- export function registerPromptImportRoutes(app: Express, ctxRaw: RouteRuntimeContext) {
249
- const ctx = requireRuntimeContext(ctxRaw);
250
- app.get("/api/prompts/import/curated-sources", async (_req: Request, res: Response) => {
251
- try {
252
- res.json(await getPromptImportSources(ctx));
253
- } catch (error) {
254
- logError("promptImport", "curated_sources_error", error);
255
- sendPromptImportError(res, error);
256
- }
257
- });
258
-
259
- app.get("/api/prompts/import/discovery", async (req: Request, res: Response) => {
260
- try {
261
- const status = req.query?.status;
262
- const candidates = await listDiscoveryCandidates(ctx, {
263
- status: typeof status === "string" ? status : undefined,
264
- });
265
- res.json({ candidates, warnings: [] });
266
- } catch (error) {
267
- logError("promptImport", "discovery_list_error", error);
268
- sendPromptImportError(res, error);
269
- }
270
- });
271
-
272
- app.post("/api/prompts/import/discovery-search", async (req: Request, res: Response) => {
273
- try {
274
- const limits = promptImportLimits(ctx);
275
- const body = (req.body ?? {}) as { seeds?: unknown; q?: unknown; limit?: unknown };
276
- const seeds = Array.isArray(body.seeds) ? (body.seeds as string[]) : [];
277
- if (seeds.length > limits.discoveryMaxQueries) {
278
- throw promptImportError("GITHUB_DISCOVERY_TOO_MANY_QUERIES", "Too many discovery queries", 413);
279
- }
280
- const result = await searchGitHubDiscovery(ctx, {
281
- q: typeof body.q === "string" ? body.q : undefined,
282
- seeds,
283
- limit: typeof body.limit === "number" ? body.limit : undefined,
284
- maxQueries: limits.discoveryMaxQueries,
285
- });
286
- res.json(result);
287
- } catch (error) {
288
- logError("promptImport", "discovery_search_error", error);
289
- sendPromptImportError(res, error);
290
- }
291
- });
292
-
293
- app.post("/api/prompts/import/discovery-review", async (req: Request, res: Response) => {
294
- try {
295
- const body = (req.body ?? {}) as Record<string, unknown>;
296
- const result = await reviewDiscoveryCandidate(ctx, body as Parameters<typeof reviewDiscoveryCandidate>[1]);
297
- res.json(result);
298
- } catch (error) {
299
- logError("promptImport", "discovery_review_error", error);
300
- sendPromptImportError(res, error);
301
- }
302
- });
303
-
304
- app.post("/api/prompts/import/curated-search", async (req: Request, res: Response) => {
305
- try {
306
- const body = (req.body ?? {}) as Record<string, unknown>;
307
- const result = await searchCuratedPrompts(ctx, body as Parameters<typeof searchCuratedPrompts>[1]);
308
- res.json(result);
309
- } catch (error) {
310
- logError("promptImport", "curated_search_error", error);
311
- sendPromptImportError(res, error);
312
- }
313
- });
314
-
315
- app.post("/api/prompts/import/curated-refresh", async (req: Request, res: Response) => {
316
- try {
317
- const body = (req.body ?? {}) as { sourceId?: unknown };
318
- const sourceId = typeof body.sourceId === "string" ? body.sourceId : "";
319
- if (!sourceId) {
320
- throw promptImportError("INVALID_GITHUB_SOURCE", "Curated source is required", 400);
321
- }
322
- const result = await refreshCuratedSource(ctx, sourceId);
323
- res.json(result);
324
- } catch (error) {
325
- logError("promptImport", "curated_refresh_error", error);
326
- sendPromptImportError(res, error);
327
- }
328
- });
329
-
330
- app.post("/api/prompts/import/folder-files", async (req: Request, res: Response) => {
331
- try {
332
- const result = await buildFolderFiles(req, ctx);
333
- res.json(result);
334
- } catch (error) {
335
- logError("promptImport", "folder_files_error", error);
336
- sendPromptImportError(res, error);
337
- }
338
- });
339
-
340
- app.post("/api/prompts/import/folder-preview", async (req: Request, res: Response) => {
341
- try {
342
- const result = await buildFolderPreview(req, ctx);
343
- res.json(result);
344
- } catch (error) {
345
- logError("promptImport", "folder_preview_error", error);
346
- sendPromptImportError(res, error);
347
- }
348
- });
349
-
350
- app.post("/api/prompts/import/preview", async (req: Request, res: Response) => {
351
- try {
352
- const preview = await buildPreview(req, ctx);
353
- res.json(preview);
354
- } catch (error) {
355
- logError("promptImport", "preview_error", error);
356
- sendPromptImportError(res, error);
357
- }
358
- });
359
-
360
- app.post("/api/prompts/import/commit", async (req: Request, res: Response) => {
361
- try {
362
- const limits = promptImportLimits(ctx);
363
- const body = (req.body ?? {}) as { candidates?: unknown; folderId?: unknown };
364
- const candidates = Array.isArray(body.candidates) ? (body.candidates as CandidateLike[]) : [];
365
- if (candidates.length === 0) {
366
- throw promptImportError("PROMPT_IMPORT_EMPTY", "Select at least one prompt to import", 422);
367
- }
368
- if (candidates.length > limits.maxPromptCandidatesPerImport) {
369
- throw promptImportError("PROMPT_IMPORT_TOO_MANY_CANDIDATES", "Too many prompt candidates", 413);
370
- }
371
- const result = commitCandidates(candidates, body.folderId, limits);
372
- logEvent("promptImport", "committed", result);
373
- res.json(result);
374
- } catch (error) {
375
- logError("promptImport", "commit_error", error);
376
- sendPromptImportError(res, error);
377
- }
378
- });
379
- }