oda-engine 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +151 -2
- package/bin/nowhere.mjs +352 -0
- package/lib/account.mjs +57 -0
- package/lib/agentModules.mjs +57 -0
- package/lib/devServer.mjs +351 -0
- package/lib/headless.mjs +231 -0
- package/lib/headlessPreview.mjs +212 -0
- package/lib/keys.mjs +517 -0
- package/lib/make.mjs +855 -0
- package/lib/mcp.mjs +537 -0
- package/lib/open.mjs +22 -0
- package/lib/preview.mjs +87 -0
- package/lib/previewRpc.mjs +134 -0
- package/lib/project.mjs +359 -0
- package/lib/publishFlow.mjs +131 -0
- package/lib/resources.mjs +50 -0
- package/lib/rules.mjs +60 -0
- package/lib/scaffold.mjs +42 -0
- package/lib/setup.mjs +59 -0
- package/lib/tools.mjs +668 -0
- package/package.json +51 -4
- package/template/game.config.json +9 -0
- package/template/scene.json +4 -0
- package/template/src/main.ts +11 -0
- package/vendor/DOCS.md +447 -0
- package/vendor/agent/appRun.mjs +520 -0
- package/vendor/agent/assets/brief.mjs +242 -0
- package/vendor/agent/assets/collider.mjs +531 -0
- package/vendor/agent/assets/environments/index.mjs +12 -0
- package/vendor/agent/assets/environments/worldlabs.mjs +161 -0
- package/vendor/agent/assets/fetch.mjs +168 -0
- package/vendor/agent/assets/footprint.mjs +13 -0
- package/vendor/agent/assets/gltf.mjs +547 -0
- package/vendor/agent/assets/hdr.mjs +269 -0
- package/vendor/agent/assets/imageOps.mjs +171 -0
- package/vendor/agent/assets/images/codex.mjs +307 -0
- package/vendor/agent/assets/images/fal.mjs +75 -0
- package/vendor/agent/assets/images/gemini.mjs +84 -0
- package/vendor/agent/assets/images/index.mjs +25 -0
- package/vendor/agent/assets/images/openai.mjs +75 -0
- package/vendor/agent/assets/images/stability.mjs +71 -0
- package/vendor/agent/assets/images/views.mjs +31 -0
- package/vendor/agent/assets/import.mjs +1352 -0
- package/vendor/agent/assets/inspect.mjs +465 -0
- package/vendor/agent/assets/jobs.mjs +196 -0
- package/vendor/agent/assets/ktx2.mjs +290 -0
- package/vendor/agent/assets/ledger.mjs +466 -0
- package/vendor/agent/assets/net.mjs +254 -0
- package/vendor/agent/assets/prices.json +39 -0
- package/vendor/agent/assets/providers/fal.mjs +115 -0
- package/vendor/agent/assets/providers/index.mjs +22 -0
- package/vendor/agent/assets/providers/meshy.mjs +119 -0
- package/vendor/agent/assets/providers/tripo.mjs +188 -0
- package/vendor/agent/assets/sources/ambientcg.mjs +129 -0
- package/vendor/agent/assets/sources/index.mjs +23 -0
- package/vendor/agent/assets/sources/polyhaven.mjs +237 -0
- package/vendor/agent/assets/sources/polypizza.mjs +121 -0
- package/vendor/agent/assets/splat.mjs +631 -0
- package/vendor/agent/assets/targets.mjs +122 -0
- package/vendor/agent/assets/tile.mjs +261 -0
- package/vendor/agent/assets/unzip.mjs +180 -0
- package/vendor/agent/blender/cleanup.py +360 -0
- package/vendor/agent/blender/convert.py +167 -0
- package/vendor/agent/blender/find.mjs +192 -0
- package/vendor/agent/blender/nw_export.py +104 -0
- package/vendor/agent/blender/nw_rubric.py +426 -0
- package/vendor/agent/blender/run.mjs +193 -0
- package/vendor/agent/blender/runner.py +252 -0
- package/vendor/agent/blender/sheet.py +743 -0
- package/vendor/agent/depth.mjs +134 -0
- package/vendor/agent/director/board.mjs +451 -0
- package/vendor/agent/director/contracts.mjs +663 -0
- package/vendor/agent/director/director.mjs +2365 -0
- package/vendor/agent/director/estimate.mjs +200 -0
- package/vendor/agent/director/gate.mjs +802 -0
- package/vendor/agent/director/limits.json +16 -0
- package/vendor/agent/director/methods.mjs +457 -0
- package/vendor/agent/director/policy.json +33 -0
- package/vendor/agent/director/prompts.mjs +244 -0
- package/vendor/agent/director/reading.mjs +341 -0
- package/vendor/agent/director/router.mjs +184 -0
- package/vendor/agent/director/seams.mjs +243 -0
- package/vendor/agent/director/session.mjs +643 -0
- package/vendor/agent/director/store.mjs +119 -0
- package/vendor/agent/director/taskGate.mjs +110 -0
- package/vendor/agent/director/teamLedger.mjs +118 -0
- package/vendor/agent/director/triage.mjs +63 -0
- package/vendor/agent/downloads.cjs +363 -0
- package/vendor/agent/flow.mjs +78 -0
- package/vendor/agent/guard.mjs +613 -0
- package/vendor/agent/host.mjs +1736 -0
- package/vendor/agent/language.mjs +58 -0
- package/vendor/agent/ledger.mjs +360 -0
- package/vendor/agent/locale.cjs +186 -0
- package/vendor/agent/loop/capacity.mjs +74 -0
- package/vendor/agent/loop/compose.mjs +127 -0
- package/vendor/agent/loop/gate.mjs +205 -0
- package/vendor/agent/loop/judge.mjs +189 -0
- package/vendor/agent/loop/levels.mjs +273 -0
- package/vendor/agent/loop/state.mjs +358 -0
- package/vendor/agent/notes.cjs +278 -0
- package/vendor/agent/pace.mjs +258 -0
- package/vendor/agent/panels.mjs +431 -0
- package/vendor/agent/prompt.mjs +342 -0
- package/vendor/agent/reference/board.mjs +410 -0
- package/vendor/agent/reference/compose.mjs +364 -0
- package/vendor/agent/reference/decode.mjs +56 -0
- package/vendor/agent/reference/facts.mjs +1874 -0
- package/vendor/agent/reference/png.mjs +107 -0
- package/vendor/agent/reference/records.mjs +63 -0
- package/vendor/agent/reference/web.mjs +252 -0
- package/vendor/agent/roles/artist.mjs +69 -0
- package/vendor/agent/roles/builder.mjs +79 -0
- package/vendor/agent/roles/checks.mjs +223 -0
- package/vendor/agent/roles/common.mjs +188 -0
- package/vendor/agent/roles/critic.mjs +171 -0
- package/vendor/agent/roles/index.mjs +39 -0
- package/vendor/agent/roles/modeller.mjs +142 -0
- package/vendor/agent/roles/qa.mjs +58 -0
- package/vendor/agent/roles/rubrics.mjs +79 -0
- package/vendor/agent/roles/sound.mjs +69 -0
- package/vendor/agent/rounds.cjs +333 -0
- package/vendor/agent/shared.mjs +284 -0
- package/vendor/agent/stopGate.mjs +574 -0
- package/vendor/agent/subagents.mjs +129 -0
- package/vendor/agent/tools/assets.mjs +499 -0
- package/vendor/agent/tools/blender.mjs +754 -0
- package/vendor/agent/tools/cards.mjs +479 -0
- package/vendor/agent/tools/choices.mjs +479 -0
- package/vendor/agent/tools/compose.mjs +74 -0
- package/vendor/agent/tools/environment.mjs +488 -0
- package/vendor/agent/tools/eyes.mjs +733 -0
- package/vendor/agent/tools/generate.mjs +338 -0
- package/vendor/agent/tools/guide.mjs +46 -0
- package/vendor/agent/tools/harness.mjs +193 -0
- package/vendor/agent/tools/image.mjs +643 -0
- package/vendor/agent/tools/index.mjs +162 -0
- package/vendor/agent/tools/look.mjs +578 -0
- package/vendor/agent/tools/loop.mjs +858 -0
- package/vendor/agent/tools/others.mjs +123 -0
- package/vendor/agent/tools/pictures.mjs +535 -0
- package/vendor/agent/tools/reference.mjs +366 -0
- package/vendor/agent/transcripts.mjs +118 -0
- package/vendor/agent-loop.mjs +602 -0
- package/vendor/agent-plugin/.claude-plugin/plugin.json +5 -0
- package/vendor/agent-plugin/skills/art-direction/SKILL.md +82 -0
- package/vendor/agent-plugin/skills/asset-legality/SKILL.md +101 -0
- package/vendor/agent-plugin/skills/asset-sources/SKILL.md +111 -0
- package/vendor/agent-plugin/skills/blender-modeling/SKILL.md +97 -0
- package/vendor/agent-plugin/skills/look-dev/SKILL.md +104 -0
- package/vendor/agent-plugin/skills/playtesting/SKILL.md +74 -0
- package/vendor/agent-plugin/skills/reference-loop/SKILL.md +102 -0
- package/vendor/agent-plugin/skills/sound-design/SKILL.md +60 -0
- package/vendor/agent-plugin/skills/style/SKILL.md +35 -0
- package/vendor/agent-plugin/skills/tsl-r185/SKILL.md +107 -0
- package/vendor/auth.cjs +570 -0
- package/vendor/build.cjs +953 -0
- package/vendor/catalog.json +455 -0
- package/vendor/connectors.cjs +688 -0
- package/vendor/eyes.mjs +733 -0
- package/vendor/gameExports.json +1390 -0
- package/vendor/git.cjs +222 -0
- package/vendor/models.json +57 -0
- package/vendor/publish.cjs +1018 -0
- package/vendor/refBoard.cjs +301 -0
- package/vendor/schema/dist/assets.d.ts +335 -0
- package/vendor/schema/dist/assets.js +439 -0
- package/vendor/schema/dist/brief.d.ts +357 -0
- package/vendor/schema/dist/brief.js +302 -0
- package/vendor/schema/dist/common.d.ts +9 -0
- package/vendor/schema/dist/common.js +13 -0
- package/vendor/schema/dist/config.d.ts +406 -0
- package/vendor/schema/dist/config.js +355 -0
- package/vendor/schema/dist/contract.d.ts +599 -0
- package/vendor/schema/dist/contract.js +1146 -0
- package/vendor/schema/dist/index.d.ts +14 -0
- package/vendor/schema/dist/index.js +21 -0
- package/vendor/schema/dist/look.d.ts +463 -0
- package/vendor/schema/dist/look.js +681 -0
- package/vendor/schema/dist/lookStats.d.ts +230 -0
- package/vendor/schema/dist/lookStats.js +768 -0
- package/vendor/schema/dist/pilot.d.ts +32 -0
- package/vendor/schema/dist/pilot.js +44 -0
- package/vendor/schema/dist/publish.d.ts +214 -0
- package/vendor/schema/dist/publish.js +291 -0
- package/vendor/schema/dist/scene.d.ts +75 -0
- package/vendor/schema/dist/scene.js +120 -0
- package/vendor/schema/dist/skyLight.d.ts +141 -0
- package/vendor/schema/dist/skyLight.js +436 -0
- package/vendor/schema/dist/style.d.ts +131 -0
- package/vendor/schema/dist/style.js +155 -0
- package/vendor/schema/package.json +1 -0
- package/vendor/selectionPrompt.mjs +51 -0
- package/vendor/useTools.mjs +195 -0
- package/vendor/userMessage.mjs +26 -0
- package/web-dist/assets/GaussianSplatUtils-CptQkDVX.js +1 -0
- package/web-dist/assets/KTX2Loader-C53LJqss.js +2 -0
- package/web-dist/assets/SPZLoader-BQ3tjaTv.js +1 -0
- package/web-dist/assets/gaussianSplat-_9tcWegB.js +1 -0
- package/web-dist/assets/index-Baka-ZO7.js +693 -0
- package/web-dist/assets/index-CcjyyQco.css +1 -0
- package/web-dist/assets/meshopt_decoder.module-BvwdjiD3.js +1 -0
- package/web-dist/assets/zstddec.module-CfFeQW7d.js +1 -0
- package/web-dist/index.html +34 -0
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
// The Codex app's image generation, through the creator's own Codex CLI and
|
|
2
|
+
// the ChatGPT account it is logged in with: no key of the studio's, and
|
|
3
|
+
// nothing on the cost card, since it is the creator's plan that is spent,
|
|
4
|
+
// which is why it needs their consent (a host consent card, topic codex,
|
|
5
|
+
// asked once and saved) before the first picture.
|
|
6
|
+
//
|
|
7
|
+
// What one real run taught (2026-10-22, codex-cli 0.163.0-alpha.11 from the
|
|
8
|
+
// ChatGPT app, a plain mug on grey, no picture attached):
|
|
9
|
+
//
|
|
10
|
+
// codex exec --json --color never --skip-git-repo-check --ignore-user-config
|
|
11
|
+
// -s read-only -C <scratch> - (the prompt on stdin)
|
|
12
|
+
//
|
|
13
|
+
// took 28 s, exited 0 and printed JSONL: thread.started { thread_id },
|
|
14
|
+
// turn.started, item.completed (agent_message), turn.completed { usage }.
|
|
15
|
+
// No event names the picture: it lands as a PNG (that run: 1536 x 1024,
|
|
16
|
+
// RGB, 1.6 MB) at $CODEX_HOME/generated_images/<thread_id>/exec-<uuid>.png
|
|
17
|
+
// ($CODEX_HOME is ~/.codex unless set). So a picture is read from that
|
|
18
|
+
// folder after the run; none there, a non-zero exit, a turn.failed or an
|
|
19
|
+
// error event, or no end within the time is a failure, said as Codex said it.
|
|
20
|
+
//
|
|
21
|
+
// The binary is found the way it is installed: the creator's own
|
|
22
|
+
// NOWHERE_CODEX, then each codex on PATH, then the ChatGPT app's own copy
|
|
23
|
+
// and the usual install folders. Each is run for its version first: an npm
|
|
24
|
+
// shim on PATH can be there and not start (its platform binary missing), and
|
|
25
|
+
// is passed over. `codex login status` says whether it is logged in, and
|
|
26
|
+
// how; only a ChatGPT login is the creator's plan. Codex runs with an
|
|
27
|
+
// environment built from scratch (its own home, the path, the language), so
|
|
28
|
+
// no key the studio holds reaches it and it never bills an API key instead
|
|
29
|
+
// of the plan. Everything outside comes in through `deps`, so a check runs a
|
|
30
|
+
// fake binary on a fake machine.
|
|
31
|
+
|
|
32
|
+
import { execFile, spawn as spawnChild } from 'node:child_process'
|
|
33
|
+
import fs from 'node:fs'
|
|
34
|
+
import fsp from 'node:fs/promises'
|
|
35
|
+
import os from 'node:os'
|
|
36
|
+
import path from 'node:path'
|
|
37
|
+
|
|
38
|
+
export const realDeps = {
|
|
39
|
+
platform: process.platform,
|
|
40
|
+
env: process.env,
|
|
41
|
+
home: os.homedir(),
|
|
42
|
+
exists: (p) => fs.existsSync(p),
|
|
43
|
+
run: (cmd, args, timeoutMs, env) =>
|
|
44
|
+
new Promise((resolve) => {
|
|
45
|
+
execFile(cmd, args, { timeout: timeoutMs, windowsHide: true, maxBuffer: 1 << 20, ...(env ? { env } : {}) }, (err, stdout, stderr) => {
|
|
46
|
+
resolve({ code: err ? (typeof err.code === 'number' ? err.code : 1) : 0, stdout: String(stdout ?? ''), stderr: String(stderr ?? '') })
|
|
47
|
+
})
|
|
48
|
+
}),
|
|
49
|
+
spawn: spawnChild,
|
|
50
|
+
now: () => Date.now(),
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** How long one picture may take, and each picture after it. */
|
|
54
|
+
export const CODEX_MINUTES = { first: 4, more: 2 }
|
|
55
|
+
const PROBE_MS = 15_000
|
|
56
|
+
const MISS_MS = 60_000
|
|
57
|
+
const cache = new WeakMap()
|
|
58
|
+
const IMAGE = /\.(png|jpe?g|webp)$/i
|
|
59
|
+
/** The Codex runs going now, so a Stop ends them (stopAll). */
|
|
60
|
+
const running = new Set()
|
|
61
|
+
|
|
62
|
+
const envVar = (env, name) => {
|
|
63
|
+
if (env?.[name] !== undefined) return env[name]
|
|
64
|
+
const key = Object.keys(env ?? {}).find((k) => k.toLowerCase() === name.toLowerCase())
|
|
65
|
+
return key ? env[key] : undefined
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Where Codex keeps its home (its login and the pictures it makes). */
|
|
69
|
+
export const codexHome = (deps = realDeps) => envVar(deps.env, 'CODEX_HOME') || path.join(deps.home, '.codex')
|
|
70
|
+
|
|
71
|
+
/** The only variables Codex sees. */
|
|
72
|
+
export function codexEnv(deps = realDeps) {
|
|
73
|
+
const out = { PATH: envVar(deps.env, 'PATH') ?? '', HOME: envVar(deps.env, 'HOME') ?? deps.home, LANG: envVar(deps.env, 'LANG') ?? 'en_US.UTF-8' }
|
|
74
|
+
for (const name of ['CODEX_HOME', 'TMPDIR', 'USER', 'LOGNAME', 'SystemRoot', 'USERPROFILE', 'LOCALAPPDATA', 'APPDATA', 'TEMP', 'TMP']) {
|
|
75
|
+
const v = envVar(deps.env, name)
|
|
76
|
+
if (v !== undefined) out[name] = v
|
|
77
|
+
}
|
|
78
|
+
return out
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Every place a codex may be, in the order they are tried. */
|
|
82
|
+
export function codexPlaces(deps = realDeps) {
|
|
83
|
+
const P = deps.platform === 'win32' ? path.win32 : path.posix
|
|
84
|
+
const names = deps.platform === 'win32' ? ['codex.exe', 'codex.cmd'] : ['codex']
|
|
85
|
+
const own = envVar(deps.env, 'NOWHERE_CODEX')
|
|
86
|
+
const onPath = String(envVar(deps.env, 'PATH') ?? '')
|
|
87
|
+
.split(deps.platform === 'win32' ? ';' : ':')
|
|
88
|
+
.filter(Boolean)
|
|
89
|
+
.flatMap((dir) => names.map((n) => P.join(dir, n)))
|
|
90
|
+
const home = deps.home
|
|
91
|
+
const known =
|
|
92
|
+
deps.platform === 'darwin'
|
|
93
|
+
? ['/Applications/ChatGPT.app/Contents/Resources/codex-cli/bin/codex', P.join(home, 'Applications', 'ChatGPT.app', 'Contents', 'Resources', 'codex-cli', 'bin', 'codex'), '/opt/homebrew/bin/codex', '/usr/local/bin/codex', P.join(home, '.local', 'bin', 'codex'), P.join(home, '.npm-global', 'bin', 'codex')]
|
|
94
|
+
: deps.platform === 'win32'
|
|
95
|
+
? [P.join(envVar(deps.env, 'LOCALAPPDATA') ?? P.join(home, 'AppData', 'Local'), 'Programs', 'codex', 'codex.exe'), P.join(envVar(deps.env, 'APPDATA') ?? P.join(home, 'AppData', 'Roaming'), 'npm', 'codex.cmd')]
|
|
96
|
+
: [P.join(home, '.local', 'bin', 'codex'), '/usr/local/bin/codex', '/usr/bin/codex', P.join(home, '.npm-global', 'bin', 'codex')]
|
|
97
|
+
return [...new Set([...(own ? [own] : []), ...onPath, ...known])]
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** What `codex login status` said: chatgpt, api-key, or null (not logged in). */
|
|
101
|
+
export function loginOf(text) {
|
|
102
|
+
const t = String(text ?? '')
|
|
103
|
+
if (/not logged in/i.test(t)) return null
|
|
104
|
+
if (/logged in using chatgpt/i.test(t)) return 'chatgpt'
|
|
105
|
+
if (/logged in using an api key/i.test(t)) return 'api-key'
|
|
106
|
+
return /logged in/i.test(t) ? 'other' : null
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The creator's Codex: { path, version, login, places } where one starts
|
|
111
|
+
* (login chatgpt, api-key, other or null), or { error, searched, broken }.
|
|
112
|
+
* Found once per deps; a miss is kept a minute, so a Codex installed or
|
|
113
|
+
* logged into after being told is found on the next request.
|
|
114
|
+
*/
|
|
115
|
+
export async function findCodex(deps = realDeps) {
|
|
116
|
+
const hit = cache.get(deps)
|
|
117
|
+
if (hit && ((hit.found && hit.login && deps.exists(hit.found)) || deps.now() - hit.at < MISS_MS)) return hit.promise
|
|
118
|
+
const promise = search(deps)
|
|
119
|
+
cache.set(deps, { promise, at: deps.now(), found: null })
|
|
120
|
+
const result = await promise
|
|
121
|
+
cache.set(deps, { promise, at: deps.now(), found: result.path ?? null, login: result.login ?? null })
|
|
122
|
+
return result
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function search(deps) {
|
|
126
|
+
const searched = []
|
|
127
|
+
const broken = []
|
|
128
|
+
const env = codexEnv(deps)
|
|
129
|
+
for (const exe of codexPlaces(deps)) {
|
|
130
|
+
searched.push(exe)
|
|
131
|
+
if (!deps.exists(exe)) continue
|
|
132
|
+
const v = await deps.run(exe, ['--version'], PROBE_MS, env)
|
|
133
|
+
const version = /codex[\w-]*\s+([\w.+-]+)/i.exec(v.stdout)?.[1] ?? null
|
|
134
|
+
if (v.code !== 0 || !version) {
|
|
135
|
+
broken.push(exe)
|
|
136
|
+
continue
|
|
137
|
+
}
|
|
138
|
+
const s = await deps.run(exe, ['login', 'status'], PROBE_MS, env)
|
|
139
|
+
return { path: exe, version, login: loginOf(`${s.stdout}\n${s.stderr}`), searched, broken }
|
|
140
|
+
}
|
|
141
|
+
return { error: broken.length ? `Codex is on this computer but does not start (${broken.join(', ')}); reinstall it, or install the ChatGPT app.` : 'Codex is not installed on this computer (the ChatGPT app ships it).', searched, broken }
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** The one message Codex is sent: the pictures to make, each with its own
|
|
145
|
+
* words, made with its image tool and nothing else. */
|
|
146
|
+
export function codexPrompt(prompts, inputs = 0) {
|
|
147
|
+
const each = prompts.length === 1 ? prompts[0] : prompts.map((p, i) => `Picture ${i + 1}:\n${p}`).join('\n\n')
|
|
148
|
+
return [
|
|
149
|
+
`Use your image generation tool to make ${prompts.length === 1 ? 'one picture' : `${prompts.length} pictures, one call of the tool each`}, and do nothing else: run no shell command and write no file.`,
|
|
150
|
+
inputs ? `The attached picture${inputs === 1 ? ' is' : 's are'} what to start from, as the words below say.` : '',
|
|
151
|
+
each,
|
|
152
|
+
`When ${prompts.length === 1 ? 'the picture is' : 'the pictures are'} made, answer with the one word DONE.`,
|
|
153
|
+
]
|
|
154
|
+
.filter(Boolean)
|
|
155
|
+
.join('\n\n')
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** The pictures a Codex thread made since `since`, oldest first. */
|
|
159
|
+
async function picturesOf(home, threadId, since) {
|
|
160
|
+
const dirs = []
|
|
161
|
+
const root = path.join(home, 'generated_images')
|
|
162
|
+
if (threadId) dirs.push(path.join(root, threadId))
|
|
163
|
+
else {
|
|
164
|
+
// No thread id came back: the folders written since the run began.
|
|
165
|
+
for (const name of await fsp.readdir(root).catch(() => [])) {
|
|
166
|
+
const st = await fsp.stat(path.join(root, name)).catch(() => null)
|
|
167
|
+
if (st?.isDirectory() && st.mtimeMs >= since) dirs.push(path.join(root, name))
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const found = []
|
|
171
|
+
for (const dir of dirs) {
|
|
172
|
+
for (const name of await fsp.readdir(dir).catch(() => [])) {
|
|
173
|
+
if (!IMAGE.test(name)) continue
|
|
174
|
+
const st = await fsp.stat(path.join(dir, name)).catch(() => null)
|
|
175
|
+
if (st?.isFile() && st.mtimeMs >= since) found.push({ file: path.join(dir, name), at: st.mtimeMs })
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return found.sort((a, b) => a.at - b.at).map((f) => f.file)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** What Codex said went wrong, from its events and its error output. */
|
|
182
|
+
function failureOf(events, stderr) {
|
|
183
|
+
for (const e of [...events].reverse()) {
|
|
184
|
+
if (e?.type === 'turn.failed') return String(e.error?.message ?? e.message ?? 'the turn failed')
|
|
185
|
+
if (e?.type === 'error') return String(e.message ?? e.error?.message ?? 'an error')
|
|
186
|
+
}
|
|
187
|
+
const last = String(stderr ?? '').trim().split('\n').filter(Boolean).at(-1)
|
|
188
|
+
return last ? last.slice(0, 300) : null
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* One run of Codex for `prompts` (one picture each), starting from the
|
|
193
|
+
* picture files `inputs` (absolute paths). Answers [{ mime, data, file }]
|
|
194
|
+
* or throws with what went wrong. `signal` stops it.
|
|
195
|
+
*/
|
|
196
|
+
export async function runCodex({ bin, prompts, inputs = [], workDir, deps = realDeps, signal = null, timeoutMs = null }) {
|
|
197
|
+
const home = codexHome(deps)
|
|
198
|
+
const since = deps.now() - 2000
|
|
199
|
+
await fsp.mkdir(workDir, { recursive: true })
|
|
200
|
+
const args = ['exec', '--json', '--color', 'never', '--skip-git-repo-check', '--ignore-user-config', '-s', 'read-only', '-C', workDir, ...inputs.map((f) => `--image=${f}`), '-']
|
|
201
|
+
const limit = timeoutMs ?? (CODEX_MINUTES.first + CODEX_MINUTES.more * Math.max(0, prompts.length - 1)) * 60_000
|
|
202
|
+
const out = await new Promise((resolve) => {
|
|
203
|
+
let stdout = ''
|
|
204
|
+
let stderr = ''
|
|
205
|
+
let settled = false
|
|
206
|
+
const child = deps.spawn(bin, args, { cwd: workDir, env: codexEnv(deps), stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true })
|
|
207
|
+
running.add(child)
|
|
208
|
+
const finish = (r) => {
|
|
209
|
+
if (settled) return
|
|
210
|
+
settled = true
|
|
211
|
+
running.delete(child)
|
|
212
|
+
clearTimeout(timer)
|
|
213
|
+
signal?.removeEventListener?.('abort', abort)
|
|
214
|
+
resolve({ stdout, stderr, ...r, ...(child.nwStopped ? { stopped: true } : {}) })
|
|
215
|
+
}
|
|
216
|
+
const abort = () => {
|
|
217
|
+
child.kill('SIGTERM')
|
|
218
|
+
finish({ code: null, stopped: true })
|
|
219
|
+
}
|
|
220
|
+
const timer = setTimeout(() => {
|
|
221
|
+
child.kill('SIGTERM')
|
|
222
|
+
finish({ code: null, timedOut: true })
|
|
223
|
+
}, limit)
|
|
224
|
+
timer.unref?.()
|
|
225
|
+
signal?.addEventListener?.('abort', abort)
|
|
226
|
+
child.stdout.on('data', (d) => {
|
|
227
|
+
if (stdout.length < 1 << 20) stdout += d
|
|
228
|
+
})
|
|
229
|
+
child.stderr.on('data', (d) => {
|
|
230
|
+
if (stderr.length < 64 << 10) stderr += d
|
|
231
|
+
})
|
|
232
|
+
child.on('error', (err) => finish({ code: null, error: err.message }))
|
|
233
|
+
child.on('close', (code) => finish({ code }))
|
|
234
|
+
child.stdin.on('error', () => {})
|
|
235
|
+
child.stdin.end(codexPrompt(prompts, inputs.length))
|
|
236
|
+
})
|
|
237
|
+
if (out.stopped) throw new Error('The request was stopped, so Codex was stopped too.')
|
|
238
|
+
if (out.error) throw new Error(`Codex could not be started (${out.error}).`)
|
|
239
|
+
if (out.timedOut) throw new Error(`Codex did not finish within ${Math.round(limit / 60_000)} minutes, so it was stopped.`)
|
|
240
|
+
const events = out.stdout
|
|
241
|
+
.split('\n')
|
|
242
|
+
.map((l) => {
|
|
243
|
+
try {
|
|
244
|
+
return JSON.parse(l)
|
|
245
|
+
} catch {
|
|
246
|
+
return null
|
|
247
|
+
}
|
|
248
|
+
})
|
|
249
|
+
.filter(Boolean)
|
|
250
|
+
const threadId = events.find((e) => e.type === 'thread.started')?.thread_id ?? null
|
|
251
|
+
const why = failureOf(events, out.stderr)?.replace(/\.+$/, '') ?? null
|
|
252
|
+
if (out.code !== 0) throw new Error(`Codex stopped with exit code ${out.code}${why ? `: ${why}` : ''}.`)
|
|
253
|
+
if (events.some((e) => e.type === 'turn.failed' || e.type === 'error')) throw new Error(`Codex could not make it: ${why}.`)
|
|
254
|
+
const files = (await picturesOf(home, threadId, since)).slice(-prompts.length)
|
|
255
|
+
if (!files.length) {
|
|
256
|
+
const said = events.filter((e) => e.type === 'item.completed' && e.item?.type === 'agent_message').map((e) => e.item.text).at(-1)
|
|
257
|
+
throw new Error(`Codex finished but made no picture${said ? ` (it said: ${String(said).trim().slice(0, 200).replace(/\.+$/, '')})` : ''}.`)
|
|
258
|
+
}
|
|
259
|
+
const pictures = []
|
|
260
|
+
for (const file of files) {
|
|
261
|
+
const data = await fsp.readFile(file)
|
|
262
|
+
pictures.push({ mime: /\.jpe?g$/i.test(file) ? 'image/jpeg' : /\.webp$/i.test(file) ? 'image/webp' : 'image/png', data: data.toString('base64'), file })
|
|
263
|
+
}
|
|
264
|
+
return pictures
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** Every Codex run going now is stopped (the creator's Stop). */
|
|
268
|
+
export function stopAll() {
|
|
269
|
+
for (const child of running) {
|
|
270
|
+
child.nwStopped = true
|
|
271
|
+
try {
|
|
272
|
+
child.kill('SIGTERM')
|
|
273
|
+
} catch {
|
|
274
|
+
// already gone
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export const codex = {
|
|
280
|
+
id: 'codex',
|
|
281
|
+
label: 'Codex',
|
|
282
|
+
connector: null,
|
|
283
|
+
envVar: null,
|
|
284
|
+
hosts: [],
|
|
285
|
+
paid: false,
|
|
286
|
+
|
|
287
|
+
/** Codex picks its own image model; the studio names none. */
|
|
288
|
+
defaultModel() {
|
|
289
|
+
return 'codex'
|
|
290
|
+
},
|
|
291
|
+
|
|
292
|
+
licenseOf() {
|
|
293
|
+
return { spdx: 'LicenseRef-Generated-Owned', url: 'https://openai.com/policies/terms-of-use' }
|
|
294
|
+
},
|
|
295
|
+
|
|
296
|
+
refuse(model, { inputs = 0 } = {}) {
|
|
297
|
+
if (model !== 'codex') return 'Codex chooses its own image model: leave model out.'
|
|
298
|
+
if (inputs > 4) return 'Codex here takes at most four pictures to start from.'
|
|
299
|
+
return null
|
|
300
|
+
},
|
|
301
|
+
|
|
302
|
+
price(_model, _options, prices) {
|
|
303
|
+
const p = prices?.images?.codex
|
|
304
|
+
if (!p) return null
|
|
305
|
+
return { low: Number(p.usd) || 0, high: Number(p.usd) || 0, unit: `the creator's ${p.plan ?? 'ChatGPT'} plan` }
|
|
306
|
+
},
|
|
307
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// fal's hosted FLUX.2 [pro], text to picture and picture plus text to
|
|
2
|
+
// picture, with the creator's key (connector fal, env FAL_KEY, Authorization:
|
|
3
|
+
// Key ...), through fal.run, the synchronous form of the queue the 3D side
|
|
4
|
+
// uses (assets/providers/fal.mjs). The models' pages (read 2026-10-22) give
|
|
5
|
+
// their inputs, outputs and prices and mark them for commercial use; NOT
|
|
6
|
+
// exercised against the live API from here: the checks use a fake. Verify
|
|
7
|
+
// one real picture before release.
|
|
8
|
+
//
|
|
9
|
+
// POST https://fal.run/fal-ai/flux-2-pro { prompt, image_size, output_format, num_images }
|
|
10
|
+
// POST https://fal.run/fal-ai/flux-2-pro/edit the same and image_urls (data URIs, at most 9)
|
|
11
|
+
// both answer { images: [{ url, content_type }] }, files on fal.media
|
|
12
|
+
//
|
|
13
|
+
// The key goes only to fal.run; the files are fetched without it.
|
|
14
|
+
|
|
15
|
+
import { fetchJson, safeFetch } from '../net.mjs'
|
|
16
|
+
import { eachView } from './views.mjs'
|
|
17
|
+
|
|
18
|
+
const RUN = 'https://fal.run'
|
|
19
|
+
|
|
20
|
+
/** The models image_generate may run on fal: one makes a picture from
|
|
21
|
+
* words, the other from pictures and words. */
|
|
22
|
+
export const MODELS = {
|
|
23
|
+
'fal-ai/flux-2-pro': { inputs: false, license: { spdx: 'LicenseRef-Generated-Owned', url: 'https://fal.ai/terms' } },
|
|
24
|
+
'fal-ai/flux-2-pro/edit': { inputs: true, license: { spdx: 'LicenseRef-Generated-Owned', url: 'https://fal.ai/terms' } },
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const SIZES = { square: 'square_hd', tall: 'portrait_4_3', wide: 'landscape_4_3' }
|
|
28
|
+
const MAX_PICTURE = 20 * 1024 * 1024
|
|
29
|
+
|
|
30
|
+
export const fal = {
|
|
31
|
+
id: 'fal',
|
|
32
|
+
label: 'fal',
|
|
33
|
+
connector: 'fal',
|
|
34
|
+
envVar: 'FAL_KEY',
|
|
35
|
+
hosts: ['fal.run', 'fal.media', '*.fal.media'],
|
|
36
|
+
paid: true,
|
|
37
|
+
|
|
38
|
+
/** The model for a request: the edit model when it starts from pictures. */
|
|
39
|
+
defaultModel({ inputs = 0 } = {}) {
|
|
40
|
+
return inputs > 0 ? 'fal-ai/flux-2-pro/edit' : 'fal-ai/flux-2-pro'
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
licenseOf(model) {
|
|
44
|
+
return MODELS[model]?.license ?? { spdx: 'LicenseRef-Unknown' }
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
refuse(model, { inputs = 0 } = {}) {
|
|
48
|
+
const m = MODELS[model]
|
|
49
|
+
if (!m) return `fal model ${model} is not in the studio's table, so what it costs and whose its pictures are is unknown. Use one of: ${Object.keys(MODELS).join(', ')}.`
|
|
50
|
+
if (inputs > 0 && !m.inputs) return `${model} makes a picture from words only; start from pictures with fal-ai/flux-2-pro/edit.`
|
|
51
|
+
if (inputs === 0 && m.inputs) return `${model} starts from pictures; with none, use fal-ai/flux-2-pro.`
|
|
52
|
+
if (inputs > 2) return 'fal here takes at most two pictures to start from.'
|
|
53
|
+
return null
|
|
54
|
+
},
|
|
55
|
+
|
|
56
|
+
price(model, _options, prices) {
|
|
57
|
+
const p = prices?.images?.fal?.[model]
|
|
58
|
+
if (!Array.isArray(p) || !p.every(Number.isFinite)) return null
|
|
59
|
+
return { low: p[0], high: p[1], unit: 'per picture' }
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
generate({ model, prompts, inputs = [], size = 'square', key, fetch, ua }) {
|
|
63
|
+
return eachView(prompts, async (prompt) => {
|
|
64
|
+
const body = { prompt, image_size: SIZES[size] ?? SIZES.square, output_format: 'png', num_images: 1, ...(inputs.length ? { image_urls: inputs.map((p) => `data:${p.mime};base64,${p.data}`) } : {}) }
|
|
65
|
+
const r = await fetchJson(`${RUN}/${model}`, { fetch, hosts: ['fal.run'], ua, method: 'POST', headers: { authorization: `Key ${key}`, 'content-type': 'application/json' }, body: JSON.stringify(body) })
|
|
66
|
+
const url = r?.images?.[0]?.url
|
|
67
|
+
if (typeof url !== 'string') throw new Error(`fal answered without a picture${r?.detail ? `: ${String(typeof r.detail === 'string' ? r.detail : JSON.stringify(r.detail)).slice(0, 200)}` : ''}.`)
|
|
68
|
+
const { res } = await safeFetch(url, { fetch, hosts: fal.hosts, ua })
|
|
69
|
+
if (!res.ok) throw new Error(`fal's picture could not be fetched (${res.status}).`)
|
|
70
|
+
const buf = Buffer.from(await res.arrayBuffer())
|
|
71
|
+
if (buf.length > MAX_PICTURE) throw new Error('fal\'s picture is over 20 MB.')
|
|
72
|
+
return { mime: r.images[0].content_type === 'image/jpeg' ? 'image/jpeg' : 'image/png', data: buf.toString('base64') }
|
|
73
|
+
})
|
|
74
|
+
},
|
|
75
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Google's Gemini image models, through the Interactions API with the
|
|
2
|
+
// creator's key (connector gemini, env GEMINI_API_KEY, x-goog-api-key). The
|
|
3
|
+
// shape is Google's documented one (ai.google.dev image generation guide and
|
|
4
|
+
// Interactions API reference, read 2026-10-22), NOT exercised against the
|
|
5
|
+
// live API from here: the checks use a fake. Verify one real picture before
|
|
6
|
+
// release.
|
|
7
|
+
//
|
|
8
|
+
// POST https://generativelanguage.googleapis.com/v1beta/interactions
|
|
9
|
+
// { model, input: [{ type: 'text', text }, { type: 'image', mime_type, data }...],
|
|
10
|
+
// response_format: { type: 'image', mime_type, aspect_ratio, image_size } }
|
|
11
|
+
// answers { status, steps: [{ type: 'model_output', content: [{ type: 'image', data, mime_type }] }], errors? }
|
|
12
|
+
//
|
|
13
|
+
// The Gemini API's terms leave what it makes to the caller, so a picture is
|
|
14
|
+
// the creator's (LicenseRef-Generated-Owned).
|
|
15
|
+
|
|
16
|
+
import { fetchJson } from '../net.mjs'
|
|
17
|
+
import { eachView } from './views.mjs'
|
|
18
|
+
|
|
19
|
+
const API = 'https://generativelanguage.googleapis.com/v1beta/interactions'
|
|
20
|
+
|
|
21
|
+
/** The models image_generate may run on Gemini. */
|
|
22
|
+
export const MODELS = {
|
|
23
|
+
'gemini-nano-banana-2.1': { license: { spdx: 'LicenseRef-Generated-Owned', url: 'https://ai.google.dev/gemini-api/terms' } },
|
|
24
|
+
'gemini-3-pro-image': { license: { spdx: 'LicenseRef-Generated-Owned', url: 'https://ai.google.dev/gemini-api/terms' } },
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const RATIOS = { square: '1:1', tall: '2:3', wide: '3:2' }
|
|
28
|
+
|
|
29
|
+
/** The last picture an interaction made, or null. */
|
|
30
|
+
function pictureOf(r) {
|
|
31
|
+
let found = null
|
|
32
|
+
for (const step of Array.isArray(r?.steps) ? r.steps : []) {
|
|
33
|
+
if (step?.type !== 'model_output') continue
|
|
34
|
+
for (const block of Array.isArray(step.content) ? step.content : []) {
|
|
35
|
+
if (block?.type === 'image' && typeof block.data === 'string' && block.data) found = { mime: block.mime_type === 'image/jpeg' ? 'image/jpeg' : 'image/png', data: block.data }
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return found ?? (typeof r?.output_image?.data === 'string' ? { mime: r.output_image.mime_type === 'image/jpeg' ? 'image/jpeg' : 'image/png', data: r.output_image.data } : null)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const gemini = {
|
|
42
|
+
id: 'gemini',
|
|
43
|
+
label: 'Gemini',
|
|
44
|
+
connector: 'gemini',
|
|
45
|
+
envVar: 'GEMINI_API_KEY',
|
|
46
|
+
hosts: ['generativelanguage.googleapis.com'],
|
|
47
|
+
paid: true,
|
|
48
|
+
|
|
49
|
+
defaultModel() {
|
|
50
|
+
return 'gemini-nano-banana-2.1'
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
licenseOf(model) {
|
|
54
|
+
return MODELS[model]?.license ?? { spdx: 'LicenseRef-Unknown' }
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
refuse(model) {
|
|
58
|
+
return MODELS[model] ? null : `Gemini model ${model} is not in the studio's table, so what it costs and whose its pictures are is unknown. Use one of: ${Object.keys(MODELS).join(', ')}.`
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
/** One 1K picture, with its inputs, from prices.json. */
|
|
62
|
+
price(model, { inputs = 0 } = {}, prices) {
|
|
63
|
+
const p = prices?.images?.gemini?.[model]
|
|
64
|
+
const one = Number(p?.['1K'])
|
|
65
|
+
if (!Number.isFinite(one)) return null
|
|
66
|
+
const usd = one + (Number(p.input) || 0) * inputs
|
|
67
|
+
return { low: usd, high: usd, unit: '1K picture' }
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
generate({ model, prompts, inputs = [], size = 'square', key, fetch, ua }) {
|
|
71
|
+
return eachView(prompts, async (prompt) => {
|
|
72
|
+
const body = {
|
|
73
|
+
model,
|
|
74
|
+
input: [{ type: 'text', text: prompt }, ...inputs.map((p) => ({ type: 'image', mime_type: p.mime, data: p.data }))],
|
|
75
|
+
response_format: { type: 'image', mime_type: 'image/png', aspect_ratio: RATIOS[size] ?? RATIOS.square, image_size: '1K' },
|
|
76
|
+
}
|
|
77
|
+
const r = await fetchJson(API, { fetch, hosts: gemini.hosts, ua, method: 'POST', headers: { 'x-goog-api-key': key, 'content-type': 'application/json' }, body: JSON.stringify(body) })
|
|
78
|
+
if (r?.status === 'failed' || (Array.isArray(r?.errors) && r.errors.length)) throw new Error(`Gemini could not make it: ${String(r.errors?.[0]?.message ?? r.status).slice(0, 200)}`)
|
|
79
|
+
const pic = pictureOf(r)
|
|
80
|
+
if (!pic) throw new Error('Gemini answered without a picture.')
|
|
81
|
+
return pic
|
|
82
|
+
})
|
|
83
|
+
},
|
|
84
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// The image generators image_generate can run, behind one shape:
|
|
2
|
+
// { id, label, connector, envVar, hosts, paid, defaultModel({ inputs }),
|
|
3
|
+
// licenseOf(model), refuse(model, { inputs }), price(model, options, prices),
|
|
4
|
+
// generate({ model, prompts, inputs, size, quality, key, fetch, ua }) }
|
|
5
|
+
// where generate makes one picture a prompt (views.mjs eachView), and one
|
|
6
|
+
// that fails after some throws with those it made (err.made).
|
|
7
|
+
// Codex has no generate of its own here: it runs the creator's CLI
|
|
8
|
+
// (codex.mjs runCodex), on their ChatGPT plan, after their consent. The paid
|
|
9
|
+
// ones run only after the creator approved a cost card that covers them, and
|
|
10
|
+
// every check drives them with a fake fetch or a fake binary.
|
|
11
|
+
//
|
|
12
|
+
// A picture made for image-to-3D is one object, whole: what a reconstruction
|
|
13
|
+
// needs is in the words of the prompt, written for each asset. Several views
|
|
14
|
+
// of it are made one picture each (views.mjs).
|
|
15
|
+
|
|
16
|
+
import { codex } from './codex.mjs'
|
|
17
|
+
import { fal } from './fal.mjs'
|
|
18
|
+
import { gemini } from './gemini.mjs'
|
|
19
|
+
import { openai } from './openai.mjs'
|
|
20
|
+
import { stability } from './stability.mjs'
|
|
21
|
+
|
|
22
|
+
export { VIEW_WORDS, viewPrompts } from './views.mjs'
|
|
23
|
+
|
|
24
|
+
export const IMAGE_PROVIDERS = { codex, openai, gemini, fal, stability }
|
|
25
|
+
export const IMAGE_PROVIDER_IDS = Object.keys(IMAGE_PROVIDERS)
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// OpenAI's GPT Image models, through the Image API with the creator's key
|
|
2
|
+
// (connector openai, env OPENAI_API_KEY, Authorization: Bearer). The shapes
|
|
3
|
+
// are OpenAI's documented ones (developers.openai.com image generation
|
|
4
|
+
// guide, read 2026-10-22), NOT exercised against the live API from here:
|
|
5
|
+
// the checks use a fake. Verify one real picture before release.
|
|
6
|
+
//
|
|
7
|
+
// POST https://api.openai.com/v1/images/generations JSON { model, prompt, n, size, quality, output_format }
|
|
8
|
+
// POST https://api.openai.com/v1/images/edits multipart, the same fields and image[] (the pictures to start from)
|
|
9
|
+
// both answer { data: [{ b64_json }] }
|
|
10
|
+
//
|
|
11
|
+
// OpenAI's terms give what the models make to the caller, so a picture is
|
|
12
|
+
// the creator's (LicenseRef-Generated-Owned).
|
|
13
|
+
|
|
14
|
+
import { fetchJson } from '../net.mjs'
|
|
15
|
+
import { eachView } from './views.mjs'
|
|
16
|
+
|
|
17
|
+
const API = 'https://api.openai.com/v1/images'
|
|
18
|
+
|
|
19
|
+
/** The models image_generate may run on OpenAI. */
|
|
20
|
+
export const MODELS = {
|
|
21
|
+
'gpt-image-2.5-flare': { license: { spdx: 'LicenseRef-Generated-Owned', url: 'https://openai.com/policies/terms-of-use' } },
|
|
22
|
+
'gpt-image-2.5-sunburst': { license: { spdx: 'LicenseRef-Generated-Owned', url: 'https://openai.com/policies/terms-of-use' } },
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The picture shapes as the API names them. */
|
|
26
|
+
const SIZES = { square: '1024x1024', tall: '1024x1536', wide: '1536x1024' }
|
|
27
|
+
|
|
28
|
+
export const openai = {
|
|
29
|
+
id: 'openai',
|
|
30
|
+
label: 'OpenAI',
|
|
31
|
+
connector: 'openai',
|
|
32
|
+
envVar: 'OPENAI_API_KEY',
|
|
33
|
+
hosts: ['api.openai.com'],
|
|
34
|
+
paid: true,
|
|
35
|
+
|
|
36
|
+
defaultModel() {
|
|
37
|
+
return 'gpt-image-2.5-flare'
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
licenseOf(model) {
|
|
41
|
+
return MODELS[model]?.license ?? { spdx: 'LicenseRef-Unknown' }
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
refuse(model) {
|
|
45
|
+
return MODELS[model] ? null : `OpenAI model ${model} is not in the studio's table, so what it costs and whose its pictures are is unknown. Use one of: ${Object.keys(MODELS).join(', ')}.`
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
/** One picture, lower and upper, with its inputs, from prices.json. */
|
|
49
|
+
price(model, { quality = 'medium', inputs = 0 } = {}, prices) {
|
|
50
|
+
const p = prices?.images?.openai?.[model]
|
|
51
|
+
const one = Number(p?.[quality])
|
|
52
|
+
if (!Number.isFinite(one)) return null
|
|
53
|
+
const extra = (Number(p.input) || 0) * inputs
|
|
54
|
+
return { low: Number(p.low) + extra, high: one + extra, unit: `${quality} quality, per picture` }
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
/** `views` pictures, one request each. */
|
|
58
|
+
generate({ model, prompts, inputs = [], size = 'square', quality = 'medium', key, fetch, ua }) {
|
|
59
|
+
return eachView(prompts, async (prompt) => {
|
|
60
|
+
const fields = { model, prompt, n: 1, size: SIZES[size] ?? SIZES.square, quality, output_format: 'png' }
|
|
61
|
+
let r
|
|
62
|
+
if (inputs.length) {
|
|
63
|
+
const form = new FormData()
|
|
64
|
+
for (const [k, v] of Object.entries(fields)) form.append(k, String(v))
|
|
65
|
+
inputs.forEach((pic, i) => form.append('image[]', new Blob([Buffer.from(pic.data, 'base64')], { type: pic.mime }), `input-${i + 1}.${pic.mime === 'image/jpeg' ? 'jpg' : 'png'}`))
|
|
66
|
+
r = await fetchJson(`${API}/edits`, { fetch, hosts: openai.hosts, ua, method: 'POST', headers: { authorization: `Bearer ${key}` }, body: form })
|
|
67
|
+
} else {
|
|
68
|
+
r = await fetchJson(`${API}/generations`, { fetch, hosts: openai.hosts, ua, method: 'POST', headers: { authorization: `Bearer ${key}`, 'content-type': 'application/json' }, body: JSON.stringify(fields) })
|
|
69
|
+
}
|
|
70
|
+
const b64 = r?.data?.[0]?.b64_json
|
|
71
|
+
if (typeof b64 !== 'string' || !b64) throw new Error('OpenAI answered without a picture.')
|
|
72
|
+
return { mime: 'image/png', data: b64 }
|
|
73
|
+
})
|
|
74
|
+
},
|
|
75
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// Stability AI's Stable Image Ultra, with the creator's key (connector
|
|
2
|
+
// stability, env STABILITY_API_KEY, Authorization: Bearer). The shape is the
|
|
3
|
+
// documented v2beta one (docs.stability.ai API reference and pricing, read
|
|
4
|
+
// 2026-10-22), NOT exercised against the live API from here: the checks use
|
|
5
|
+
// a fake. Verify one real picture before release.
|
|
6
|
+
//
|
|
7
|
+
// POST https://api.stability.ai/v2beta/stable-image/generate/ultra
|
|
8
|
+
// multipart: prompt, output_format, aspect_ratio (from words) or
|
|
9
|
+
// image and strength (from a picture); Accept: application/json
|
|
10
|
+
// answers { image (base64), finish_reason, seed }; finish_reason
|
|
11
|
+
// CONTENT_FILTERED is a refusal
|
|
12
|
+
//
|
|
13
|
+
// Stability's terms give a paying caller what it makes, so a picture is the
|
|
14
|
+
// creator's (LicenseRef-Generated-Owned).
|
|
15
|
+
|
|
16
|
+
import { fetchJson } from '../net.mjs'
|
|
17
|
+
import { eachView } from './views.mjs'
|
|
18
|
+
|
|
19
|
+
const API = 'https://api.stability.ai/v2beta/stable-image/generate'
|
|
20
|
+
|
|
21
|
+
export const MODELS = { ultra: { license: { spdx: 'LicenseRef-Generated-Owned' } } }
|
|
22
|
+
|
|
23
|
+
const RATIOS = { square: '1:1', tall: '2:3', wide: '3:2' }
|
|
24
|
+
|
|
25
|
+
export const stability = {
|
|
26
|
+
id: 'stability',
|
|
27
|
+
label: 'Stability AI',
|
|
28
|
+
connector: 'stability',
|
|
29
|
+
envVar: 'STABILITY_API_KEY',
|
|
30
|
+
hosts: ['api.stability.ai'],
|
|
31
|
+
paid: true,
|
|
32
|
+
|
|
33
|
+
defaultModel() {
|
|
34
|
+
return 'ultra'
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
licenseOf(model) {
|
|
38
|
+
return MODELS[model]?.license ?? { spdx: 'LicenseRef-Unknown' }
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
refuse(model, { inputs = 0 } = {}) {
|
|
42
|
+
if (!MODELS[model]) return `Stability model ${model} is not in the studio's table. Use one of: ${Object.keys(MODELS).join(', ')}.`
|
|
43
|
+
if (inputs > 1) return 'Stable Image Ultra starts from one picture at most.'
|
|
44
|
+
return null
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
price(model, _options, prices) {
|
|
48
|
+
const credits = Number(prices?.images?.stability?.[model])
|
|
49
|
+
const credit = Number(prices?.images?.stability?.creditUsd)
|
|
50
|
+
if (!Number.isFinite(credits) || !Number.isFinite(credit)) return null
|
|
51
|
+
return { low: credits * credit, high: credits * credit, unit: `${credits} credits` }
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
generate({ model, prompts, inputs = [], size = 'square', key, fetch, ua }) {
|
|
55
|
+
return eachView(prompts, async (prompt) => {
|
|
56
|
+
const form = new FormData()
|
|
57
|
+
form.append('prompt', prompt)
|
|
58
|
+
form.append('output_format', 'png')
|
|
59
|
+
if (inputs.length) {
|
|
60
|
+
form.append('image', new Blob([Buffer.from(inputs[0].data, 'base64')], { type: inputs[0].mime }), inputs[0].mime === 'image/jpeg' ? 'input.jpg' : 'input.png')
|
|
61
|
+
// How far the picture may move from the one it starts from (0 to 1):
|
|
62
|
+
// a clean view of the same object, not a copy of the crop.
|
|
63
|
+
form.append('strength', '0.6')
|
|
64
|
+
} else form.append('aspect_ratio', RATIOS[size] ?? RATIOS.square)
|
|
65
|
+
const r = await fetchJson(`${API}/${model}`, { fetch, hosts: stability.hosts, ua, method: 'POST', headers: { authorization: `Bearer ${key}`, accept: 'application/json' }, body: form })
|
|
66
|
+
if (r?.finish_reason && r.finish_reason !== 'SUCCESS') throw new Error(`Stability did not make it (${r.finish_reason}).`)
|
|
67
|
+
if (typeof r?.image !== 'string' || !r.image) throw new Error('Stability answered without a picture.')
|
|
68
|
+
return { mime: 'image/png', data: r.image }
|
|
69
|
+
})
|
|
70
|
+
},
|
|
71
|
+
}
|