somora 2026.930.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/LICENSE +21 -0
- package/README.md +364 -0
- package/bin/node-version.d.mts +3 -0
- package/bin/node-version.mjs +45 -0
- package/bin/somora.mjs +77 -0
- package/config.example.yaml +896 -0
- package/docs/agents.md +677 -0
- package/docs/api.md +3194 -0
- package/docs/browser.md +317 -0
- package/docs/builder.md +331 -0
- package/docs/cache-strategy.md +373 -0
- package/docs/compaction.md +253 -0
- package/docs/display.md +154 -0
- package/docs/dream-phases.md +891 -0
- package/docs/files.md +387 -0
- package/docs/imagegen.md +366 -0
- package/docs/images/somora-hero.png +0 -0
- package/docs/images/somora-mobile-2026-05-23.png +0 -0
- package/docs/images/somora-tui-2026-05-23.png +0 -0
- package/docs/images/somora-web-2026-05-23.png +0 -0
- package/docs/index.md +178 -0
- package/docs/lsp.md +93 -0
- package/docs/mcp.md +332 -0
- package/docs/memory.md +407 -0
- package/docs/mobile.md +310 -0
- package/docs/models.md +379 -0
- package/docs/projects.md +425 -0
- package/docs/realtime-voice.md +293 -0
- package/docs/resources.md +230 -0
- package/docs/sampling.md +128 -0
- package/docs/security.md +189 -0
- package/docs/sentinel.md +295 -0
- package/docs/setup.md +1217 -0
- package/docs/skills.md +501 -0
- package/docs/team.md +189 -0
- package/docs/thinking.md +463 -0
- package/docs/tmux.md +399 -0
- package/docs/tools.md +117 -0
- package/docs/videogen.md +191 -0
- package/docs/voice.md +288 -0
- package/docs/web.md +893 -0
- package/docs/wiki.md +583 -0
- package/npm-shrinkwrap.json +5670 -0
- package/package.json +116 -0
- package/src/attachments/store.ts +196 -0
- package/src/browser/change-stream.ts +31 -0
- package/src/browser/display.ts +108 -0
- package/src/browser/policy.ts +121 -0
- package/src/browser/screencast.ts +374 -0
- package/src/browser/service.ts +1337 -0
- package/src/browser/session.ts +15 -0
- package/src/browser/snapshot.ts +40 -0
- package/src/cli/auth.ts +117 -0
- package/src/cli/codex.ts +41 -0
- package/src/cli/lsp.ts +71 -0
- package/src/cli/resolvers/clawhub.ts +400 -0
- package/src/cli/setup.ts +835 -0
- package/src/cli/skill.ts +619 -0
- package/src/cli/somora.ts +548 -0
- package/src/cli/systemd-unit.ts +72 -0
- package/src/cli/team.ts +148 -0
- package/src/cli/tui/activity-stream.ts +121 -0
- package/src/cli/tui/api.ts +479 -0
- package/src/cli/tui/app.tsx +1169 -0
- package/src/cli/tui/autocomplete.tsx +55 -0
- package/src/cli/tui/commands.ts +913 -0
- package/src/cli/tui/dream-glyphs.ts +22 -0
- package/src/cli/tui/footer.tsx +15 -0
- package/src/cli/tui/format.ts +28 -0
- package/src/cli/tui/header.tsx +279 -0
- package/src/cli/tui/index.tsx +120 -0
- package/src/cli/tui/markdown.tsx +63 -0
- package/src/cli/tui/sampling.ts +162 -0
- package/src/cli/tui/separator.tsx +13 -0
- package/src/cli/tui/state.ts +42 -0
- package/src/cli/tui/stream.ts +330 -0
- package/src/cli/tui/thinking-text.ts +54 -0
- package/src/cli/tui/turn-views.tsx +499 -0
- package/src/cli/tui/types.ts +410 -0
- package/src/cli/tui/work-queue.ts +161 -0
- package/src/cli/update-args.ts +55 -0
- package/src/cli/wiki.ts +406 -0
- package/src/compaction/index.ts +28 -0
- package/src/compaction/midturn.ts +210 -0
- package/src/compaction/policy.ts +154 -0
- package/src/compaction/summarize.ts +691 -0
- package/src/compaction/template.ts +90 -0
- package/src/compaction/types.ts +148 -0
- package/src/config/loader.ts +276 -0
- package/src/config/reload.ts +43 -0
- package/src/config/types.ts +2292 -0
- package/src/dream/deep-actions.ts +324 -0
- package/src/dream/deep-dispatcher.ts +237 -0
- package/src/dream/deep-llm.ts +388 -0
- package/src/dream/deep-prompts.ts +106 -0
- package/src/dream/deep-runner.ts +757 -0
- package/src/dream/deep-skip-cache.ts +199 -0
- package/src/dream/deep-worker.ts +229 -0
- package/src/dream/json-salvage.ts +34 -0
- package/src/dream/judge.ts +188 -0
- package/src/dream/loop-state.ts +286 -0
- package/src/dream/lucid-actions.ts +303 -0
- package/src/dream/lucid-batches.ts +94 -0
- package/src/dream/lucid-prompt.ts +82 -0
- package/src/dream/lucid-runner.ts +744 -0
- package/src/dream/lucid-storage.ts +277 -0
- package/src/dream/lucid-structure.ts +102 -0
- package/src/dream/lucid-types.ts +137 -0
- package/src/dream/lucid-worker.ts +224 -0
- package/src/dream/rem-dedup.ts +502 -0
- package/src/dream/rem-extract.ts +782 -0
- package/src/dream/rem-reset-run.ts +67 -0
- package/src/dream/rem-runner.ts +696 -0
- package/src/dream/rem-worker.ts +527 -0
- package/src/dream/review-window.ts +71 -0
- package/src/dream/scheduler-state.ts +129 -0
- package/src/dream/storage.ts +497 -0
- package/src/dream/types.ts +133 -0
- package/src/dream/wiki-context.ts +133 -0
- package/src/engine/a2a.ts +33 -0
- package/src/engine/availability.ts +63 -0
- package/src/engine/claude-cli.ts +969 -0
- package/src/engine/codex-app-server-client.ts +294 -0
- package/src/engine/codex-bin.ts +49 -0
- package/src/engine/codex-cli.ts +1014 -0
- package/src/engine/codex-dynamic-tools.ts +239 -0
- package/src/engine/codex-home.ts +63 -0
- package/src/engine/codex-thread-config.ts +74 -0
- package/src/engine/context-budget.ts +257 -0
- package/src/engine/engine-meta-labels.ts +211 -0
- package/src/engine/grok-cli.ts +941 -0
- package/src/engine/inline-think.ts +59 -0
- package/src/engine/model-availability.ts +105 -0
- package/src/engine/openai-compatible.ts +2464 -0
- package/src/engine/reasoning-retry.ts +83 -0
- package/src/engine/registry.ts +13 -0
- package/src/engine/replay.ts +195 -0
- package/src/engine/sampling.ts +90 -0
- package/src/engine/thinking-params.ts +264 -0
- package/src/engine/types.ts +269 -0
- package/src/engine/user-tag.ts +24 -0
- package/src/imagegen/aspect.ts +128 -0
- package/src/imagegen/generate.ts +642 -0
- package/src/imagegen/references.ts +75 -0
- package/src/imagegen/types.ts +154 -0
- package/src/lsp/client.ts +183 -0
- package/src/lsp/fake-server.helper.mjs +42 -0
- package/src/lsp/format.ts +29 -0
- package/src/lsp/index.ts +21 -0
- package/src/lsp/manager.ts +238 -0
- package/src/lsp/registry.ts +151 -0
- package/src/mcp/config.ts +117 -0
- package/src/mcp/hub/audit.ts +55 -0
- package/src/mcp/hub/bridge.ts +115 -0
- package/src/mcp/hub/catalog.ts +69 -0
- package/src/mcp/hub/credentials.ts +406 -0
- package/src/mcp/hub/env-expand.ts +14 -0
- package/src/mcp/hub/manager.ts +770 -0
- package/src/mcp/hub/normalize.ts +213 -0
- package/src/mcp/server.ts +368 -0
- package/src/mcp/tool-schema.ts +20 -0
- package/src/media/capabilities.ts +392 -0
- package/src/media/records.ts +175 -0
- package/src/media/store.ts +217 -0
- package/src/media/types.ts +69 -0
- package/src/memory/chunking.ts +168 -0
- package/src/memory/embeddings.ts +262 -0
- package/src/memory/index.ts +15 -0
- package/src/memory/inject.ts +197 -0
- package/src/memory/manager.ts +996 -0
- package/src/memory/registry.ts +176 -0
- package/src/memory/rescan.ts +101 -0
- package/src/memory/retrieval.ts +586 -0
- package/src/memory/shared-index.ts +372 -0
- package/src/memory/slug.ts +39 -0
- package/src/memory/storage.ts +385 -0
- package/src/memory/watcher.ts +134 -0
- package/src/multimodal/blocks.ts +101 -0
- package/src/multimodal/dimensions.ts +214 -0
- package/src/multimodal/load.ts +86 -0
- package/src/multimodal/mime.ts +135 -0
- package/src/multimodal/pdf-render.ts +80 -0
- package/src/multimodal/user-content.ts +266 -0
- package/src/persona/files.ts +144 -0
- package/src/persona/loader.ts +598 -0
- package/src/persona/skill-gating-store.ts +59 -0
- package/src/persona/tool-gating-store.ts +113 -0
- package/src/projects/focus.ts +186 -0
- package/src/projects/prompt-block.ts +106 -0
- package/src/projects/scheme.ts +107 -0
- package/src/projects/store.ts +144 -0
- package/src/projects/types.ts +73 -0
- package/src/sentinel/dispatcher.ts +97 -0
- package/src/sentinel/schedule.ts +271 -0
- package/src/sentinel/scheduler.ts +840 -0
- package/src/sentinel/store.ts +259 -0
- package/src/sentinel/types.ts +221 -0
- package/src/server/activity-stream.ts +160 -0
- package/src/server/ask-calls.ts +137 -0
- package/src/server/ask-wait-graph.ts +171 -0
- package/src/server/async-tasks.ts +199 -0
- package/src/server/builder-busy.ts +86 -0
- package/src/server/builder-prompt.ts +204 -0
- package/src/server/builder-questions.ts +117 -0
- package/src/server/builder-session.ts +249 -0
- package/src/server/chat-aborts.ts +83 -0
- package/src/server/claude-config-dir.ts +71 -0
- package/src/server/claude-credential-sync.ts +454 -0
- package/src/server/env-file.ts +100 -0
- package/src/server/env.ts +115 -0
- package/src/server/heal-session.ts +151 -0
- package/src/server/history-page.ts +46 -0
- package/src/server/index.ts +7244 -0
- package/src/server/lockfile.ts +89 -0
- package/src/server/logger.ts +67 -0
- package/src/server/logs.ts +187 -0
- package/src/server/loopback-fetch.ts +130 -0
- package/src/server/openai-client.ts +89 -0
- package/src/server/prompt-assembly.ts +334 -0
- package/src/server/restart-reconcile.ts +139 -0
- package/src/server/run-turn-fallback.ts +275 -0
- package/src/server/run-turn-types.ts +91 -0
- package/src/server/run-turn.ts +1474 -0
- package/src/server/sanitize-assistant-text.ts +112 -0
- package/src/server/session-export.ts +220 -0
- package/src/server/session-queue.ts +376 -0
- package/src/server/session-workdir.ts +49 -0
- package/src/server/sse-h2-safe.ts +61 -0
- package/src/server/sse-liveness.ts +162 -0
- package/src/server/sse-serializer.ts +161 -0
- package/src/server/start-turn.ts +249 -0
- package/src/server/steer-inbox.ts +107 -0
- package/src/server/tls-keeper.ts +153 -0
- package/src/server/tool-format.ts +167 -0
- package/src/server/turn-framing.ts +52 -0
- package/src/server/turn-origin-kind.ts +74 -0
- package/src/server/turn-origin.ts +40 -0
- package/src/server/work-ledger.ts +975 -0
- package/src/server/workspace.ts +114 -0
- package/src/setup/config-edit.ts +201 -0
- package/src/setup/presets.ts +71 -0
- package/src/setup/prompt.ts +163 -0
- package/src/setup/tailscale.ts +44 -0
- package/src/skills/bin-checks.ts +227 -0
- package/src/skills/bootstrap.ts +347 -0
- package/src/skills/env-scope.ts +175 -0
- package/src/skills/gating.ts +71 -0
- package/src/skills/lint.ts +202 -0
- package/src/skills/load.ts +368 -0
- package/src/skills/path-helpers.ts +102 -0
- package/src/skills/registry.ts +178 -0
- package/src/ssh/exec.ts +142 -0
- package/src/ssh/index.ts +12 -0
- package/src/ssh/known-hosts.ts +109 -0
- package/src/ssh/pool.ts +307 -0
- package/src/storage/sessions.ts +602 -0
- package/src/team/render.ts +149 -0
- package/src/team/resolve.ts +122 -0
- package/src/team/store.ts +124 -0
- package/src/team/types.ts +111 -0
- package/src/team/write.ts +95 -0
- package/src/testing/isolated-home.mts +46 -0
- package/src/tmux/attention-watcher.ts +272 -0
- package/src/tmux/attention.ts +336 -0
- package/src/tmux/list.ts +91 -0
- package/src/tmux/origin-store.ts +102 -0
- package/src/tmux/tui-markers.ts +267 -0
- package/src/tools/agents/ask-cancel.ts +94 -0
- package/src/tools/agents/ask-result.ts +235 -0
- package/src/tools/agents/ask.ts +704 -0
- package/src/tools/agents/builder-dispatch.ts +217 -0
- package/src/tools/agents/images.ts +104 -0
- package/src/tools/agents/index.ts +42 -0
- package/src/tools/agents/long-task-timeouts.ts +39 -0
- package/src/tools/agents/session-list.ts +136 -0
- package/src/tools/agents/session-model.ts +106 -0
- package/src/tools/agents/spawn.ts +845 -0
- package/src/tools/agents/status.ts +589 -0
- package/src/tools/browser/index.ts +6 -0
- package/src/tools/browser/ops.ts +145 -0
- package/src/tools/browser/tools.ts +184 -0
- package/src/tools/builder/short-descriptions.ts +80 -0
- package/src/tools/builder/tools.ts +222 -0
- package/src/tools/docs/index.ts +1 -0
- package/src/tools/docs/tools.ts +228 -0
- package/src/tools/dream/index.ts +11 -0
- package/src/tools/dream/tools.ts +1002 -0
- package/src/tools/exec/allowlist.ts +233 -0
- package/src/tools/exec/background-detach.helper.mts +27 -0
- package/src/tools/exec/blacklist.ts +336 -0
- package/src/tools/exec/concurrency.ts +150 -0
- package/src/tools/exec/fit-output.ts +82 -0
- package/src/tools/exec/index.ts +7 -0
- package/src/tools/exec/internal-env.ts +89 -0
- package/src/tools/exec/job-store.ts +403 -0
- package/src/tools/exec/local.ts +649 -0
- package/src/tools/exec/remote.ts +399 -0
- package/src/tools/exec/tools.ts +934 -0
- package/src/tools/file/analyze.ts +378 -0
- package/src/tools/file/index.ts +2 -0
- package/src/tools/file/local.ts +707 -0
- package/src/tools/file/lsp-hook.ts +72 -0
- package/src/tools/file/persona-guard.ts +61 -0
- package/src/tools/file/policy.ts +292 -0
- package/src/tools/file/read-format.ts +129 -0
- package/src/tools/file/remote.ts +680 -0
- package/src/tools/file/replace.ts +507 -0
- package/src/tools/file/search-window.ts +224 -0
- package/src/tools/file/tools.ts +658 -0
- package/src/tools/file/write-scope.ts +182 -0
- package/src/tools/gating.ts +106 -0
- package/src/tools/image/budget.ts +82 -0
- package/src/tools/image/index.ts +7 -0
- package/src/tools/image/tools.ts +601 -0
- package/src/tools/index.ts +99 -0
- package/src/tools/memory/index.ts +12 -0
- package/src/tools/memory/tools.ts +399 -0
- package/src/tools/projects/index.ts +1 -0
- package/src/tools/projects/tools.ts +643 -0
- package/src/tools/registry.ts +427 -0
- package/src/tools/resources/index.ts +8 -0
- package/src/tools/resources/tools.ts +176 -0
- package/src/tools/resources/visibility.ts +60 -0
- package/src/tools/sentinel/index.ts +1 -0
- package/src/tools/sentinel/tools.ts +592 -0
- package/src/tools/skill/index.ts +4 -0
- package/src/tools/skill/tools.ts +285 -0
- package/src/tools/time/index.ts +5 -0
- package/src/tools/time/tools.ts +123 -0
- package/src/tools/tmux/index.ts +1 -0
- package/src/tools/tmux/local.ts +277 -0
- package/src/tools/tmux/remote.ts +168 -0
- package/src/tools/tmux/tools.ts +1064 -0
- package/src/tools/tool-output.ts +97 -0
- package/src/tools/types.ts +227 -0
- package/src/tools/video/index.ts +1 -0
- package/src/tools/video/tools.ts +425 -0
- package/src/tools/web/fetch.ts +337 -0
- package/src/tools/web/index.ts +4 -0
- package/src/tools/web/tools.ts +186 -0
- package/src/tools/wiki/index.ts +1 -0
- package/src/tools/wiki/tools.ts +445 -0
- package/src/tts/cache-gc.ts +128 -0
- package/src/tts/prepare-for-tts.ts +114 -0
- package/src/tts/service.ts +332 -0
- package/src/types/events.ts +695 -0
- package/src/types/turn-origin.ts +53 -0
- package/src/version.ts +21 -0
- package/src/videogen/dialects.ts +222 -0
- package/src/videogen/generate.ts +484 -0
- package/src/videogen/jobs.ts +178 -0
- package/src/videogen/runner.ts +125 -0
- package/src/videogen/wake.ts +119 -0
- package/src/voice/realtime/call.ts +948 -0
- package/src/voice/realtime/consult.ts +293 -0
- package/src/voice/realtime/fake-provider.ts +134 -0
- package/src/voice/realtime/manager.ts +384 -0
- package/src/voice/realtime/openai-provider.ts +471 -0
- package/src/voice/realtime/persona.ts +227 -0
- package/src/voice/realtime/session-match.ts +116 -0
- package/src/voice/realtime/types.ts +182 -0
- package/src/wiki/conflict.ts +113 -0
- package/src/wiki/explorer.ts +578 -0
- package/src/wiki/index-builder.ts +172 -0
- package/src/wiki/language.ts +134 -0
- package/src/wiki/log-builder.ts +150 -0
- package/src/wiki/map.ts +258 -0
- package/src/wiki/migration/analyze.ts +304 -0
- package/src/wiki/migration/execute.ts +689 -0
- package/src/wiki/migration/refine.ts +402 -0
- package/src/wiki/migration/store.ts +100 -0
- package/src/wiki/move-page.ts +57 -0
- package/src/wiki/structure-file.ts +192 -0
- package/src/wiki/taxonomy.ts +140 -0
- package/src/wiki/templates.ts +163 -0
- package/src/wiki/types.ts +133 -0
- package/templates/skills/README.md +24 -0
- package/templates/skills/_templates/api-wrapper/SKILL.md +26 -0
- package/templates/skills/_templates/cli-wrapper/BOOTSTRAP.md +32 -0
- package/templates/skills/_templates/cli-wrapper/SKILL.md +24 -0
- package/templates/skills/_templates/default/SKILL.md +25 -0
- package/templates/skills/builder-handover/SKILL.md +96 -0
- package/templates/skills/builder-handover/assets/ORDER.md +12 -0
- package/templates/skills/skill-author/SKILL.md +110 -0
- package/tsconfig.json +22 -0
- package/web/dist/assets/index-BaVv6pB2.js +631 -0
- package/web/dist/assets/index-BaVv6pB2.js.map +1 -0
- package/web/dist/assets/index-V3FV3Z25.css +32 -0
- package/web/dist/favicon.svg +8 -0
- package/web/dist/index.html +20 -0
- package/web/package.json +36 -0
- package/web/tsconfig.json +25 -0
- package/web-mobile/dist/apple-touch-icon.png +0 -0
- package/web-mobile/dist/assets/index-BqKt7IMx.css +1 -0
- package/web-mobile/dist/assets/index-X_vRr6cd.js +88 -0
- package/web-mobile/dist/assets/index-X_vRr6cd.js.map +1 -0
- package/web-mobile/dist/favicon.svg +8 -0
- package/web-mobile/dist/icon-192.png +0 -0
- package/web-mobile/dist/icon-512.png +0 -0
- package/web-mobile/dist/index.html +27 -0
- package/web-mobile/dist/manifest.webmanifest +32 -0
- package/web-mobile/dist/service-worker.js +90 -0
- package/web-mobile/package.json +28 -0
- package/web-mobile/tsconfig.json +25 -0
|
@@ -0,0 +1,601 @@
|
|
|
1
|
+
// Image tools — text-to-image generation and an index over everything
|
|
2
|
+
// generated so far.
|
|
3
|
+
//
|
|
4
|
+
// Both tools sit on src/imagegen/, the same core the web app's Images
|
|
5
|
+
// window calls through POST /images/generate. Nothing model-facing
|
|
6
|
+
// lives here beyond argument shaping and the two decisions that are
|
|
7
|
+
// genuinely agent-specific: whether the image comes back into the
|
|
8
|
+
// agent's context, and whether this turn is still within its budget.
|
|
9
|
+
|
|
10
|
+
import { z } from 'zod';
|
|
11
|
+
import { generateImage, ImageGenError } from '../../imagegen/generate.ts';
|
|
12
|
+
import { listRecords } from '../../media/records.ts';
|
|
13
|
+
import { resolveCapabilities } from '../../media/capabilities.ts';
|
|
14
|
+
import { resolveImageModel } from '../../config/types.ts';
|
|
15
|
+
import type { ImageRecord, ImageSpecs } from '../../imagegen/types.ts';
|
|
16
|
+
import { loadPersona } from '../../persona/loader.ts';
|
|
17
|
+
import { logger } from '../../server/logger.ts';
|
|
18
|
+
import { checkReadAllowed, checkWriteAllowed, realpathSafeAncestor, resolveLocalPath } from '../file/policy.ts';
|
|
19
|
+
import { referenceFromBytes } from '../../imagegen/references.ts';
|
|
20
|
+
import { readFile } from 'node:fs/promises';
|
|
21
|
+
import type { MultimodalToolResult, ToolContext, ToolDefinition } from '../types.ts';
|
|
22
|
+
import { checkImageBudget, recordImagesInTurn } from './budget.ts';
|
|
23
|
+
|
|
24
|
+
function imageGenEnabled(ctx: ToolContext): boolean {
|
|
25
|
+
return ctx.config.imageGen?.enabled === true && (ctx.config.imageGen?.models.length ?? 0) > 0;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const GenerateInput = z
|
|
29
|
+
.object({
|
|
30
|
+
prompt: z.string().min(1).max(4000).describe('What to draw. Passed to the model verbatim.'),
|
|
31
|
+
model: z.string().min(1).optional(),
|
|
32
|
+
aspect_ratio: z.string().min(1).optional(),
|
|
33
|
+
resolution: z.string().min(1).optional(),
|
|
34
|
+
size: z.string().min(1).optional(),
|
|
35
|
+
quality: z.string().min(1).optional(),
|
|
36
|
+
output_format: z.string().min(1).optional(),
|
|
37
|
+
background: z.string().min(1).optional(),
|
|
38
|
+
output_compression: z.number().int().min(0).max(100).optional(),
|
|
39
|
+
seed: z.number().int().optional(),
|
|
40
|
+
n: z.number().int().min(1).max(10).optional(),
|
|
41
|
+
steps: z.number().int().positive().optional(),
|
|
42
|
+
cfg: z.number().optional(),
|
|
43
|
+
guidance: z.number().optional(),
|
|
44
|
+
/** Removed 2026-09-10: images live in one place. Still accepted so a
|
|
45
|
+
* persona or habit that still passes it does not fail the call — it
|
|
46
|
+
* is ignored and the result says where the image actually is. */
|
|
47
|
+
save_to: z.string().min(1).optional(),
|
|
48
|
+
reference_images: z.array(z.string().min(1)).max(16).optional(),
|
|
49
|
+
return_image: z.boolean().optional(),
|
|
50
|
+
extra: z.record(z.string(), z.unknown()).optional(),
|
|
51
|
+
})
|
|
52
|
+
.strict();
|
|
53
|
+
|
|
54
|
+
type GenerateArgs = z.infer<typeof GenerateInput>;
|
|
55
|
+
|
|
56
|
+
interface GeneratedRow {
|
|
57
|
+
id: string;
|
|
58
|
+
path: string;
|
|
59
|
+
filename: string;
|
|
60
|
+
linked_to: string[];
|
|
61
|
+
cost_usd?: number;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface GenerateOutput {
|
|
65
|
+
images: GeneratedRow[];
|
|
66
|
+
model: string;
|
|
67
|
+
specs: ImageSpecs;
|
|
68
|
+
cost_usd?: number;
|
|
69
|
+
/** Non-fatal remarks — a destination that couldn't be linked, the
|
|
70
|
+
* image withheld because the model can't see images, etc. */
|
|
71
|
+
notes?: string[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function toSpecs(input: GenerateArgs): ImageSpecs {
|
|
75
|
+
const specs: ImageSpecs = {};
|
|
76
|
+
if (input.aspect_ratio !== undefined) specs.aspect_ratio = input.aspect_ratio;
|
|
77
|
+
if (input.resolution !== undefined) specs.resolution = input.resolution;
|
|
78
|
+
if (input.size !== undefined) specs.size = input.size;
|
|
79
|
+
if (input.quality !== undefined) specs.quality = input.quality;
|
|
80
|
+
if (input.output_format !== undefined) specs.output_format = input.output_format;
|
|
81
|
+
if (input.background !== undefined) specs.background = input.background;
|
|
82
|
+
if (input.output_compression !== undefined) specs.output_compression = input.output_compression;
|
|
83
|
+
if (input.seed !== undefined) specs.seed = input.seed;
|
|
84
|
+
if (input.n !== undefined) specs.n = input.n;
|
|
85
|
+
if (input.steps !== undefined) specs.steps = input.steps;
|
|
86
|
+
if (input.cfg !== undefined) specs.cfg = input.cfg;
|
|
87
|
+
if (input.guidance !== undefined) specs.guidance = input.guidance;
|
|
88
|
+
return specs;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** agent.yaml `imageReview: always` makes looking at the result the
|
|
92
|
+
* default for this agent; an explicit `return_image` on the call still
|
|
93
|
+
* wins, so a task can opt out of the cost. */
|
|
94
|
+
async function wantsImageBack(input: GenerateArgs, ctx: ToolContext): Promise<boolean> {
|
|
95
|
+
if (input.return_image !== undefined) return input.return_image;
|
|
96
|
+
try {
|
|
97
|
+
const persona = await loadPersona(ctx.agent);
|
|
98
|
+
return persona?.imageReview === 'always';
|
|
99
|
+
} catch {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function modelCanSeeImages(ctx: ToolContext): boolean {
|
|
105
|
+
return ctx.activeModel?.model.capabilities.includes('image') === true;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// TOutput left unbound (like file_read): the handler returns either a
|
|
109
|
+
// plain payload or a MultimodalToolResult, and the registry
|
|
110
|
+
// discriminates on the brand at runtime.
|
|
111
|
+
export const imageGenerate: ToolDefinition<GenerateArgs> = {
|
|
112
|
+
name: 'image_generate',
|
|
113
|
+
toolset: 'image',
|
|
114
|
+
description:
|
|
115
|
+
'Generate an image from a text prompt and save it. Call image_models first if you need a ' +
|
|
116
|
+
'model other than the default, or to see which spec values a model accepts. ' +
|
|
117
|
+
'Returns the file path and metadata, ' +
|
|
118
|
+
'NOT the image itself — set return_image: true if you need to SEE the result (costs ~2k ' +
|
|
119
|
+
'tokens and requires a vision-capable model). The user sees every generated image in ' +
|
|
120
|
+
'their chat automatically, so you do not need to send it to them. ' +
|
|
121
|
+
'Specs are real request fields, not prompt text: size ("1024x1792"), aspect_ratio ("16:9"), ' +
|
|
122
|
+
'quality, n (how many), output_format, seed, and the sampling knobs steps/cfg/guidance. ' +
|
|
123
|
+
'Do NOT write them into the prompt. WHICH fields a model takes differs per model — ' +
|
|
124
|
+
'image_models tells you, and one it does not take is rejected before the request goes out, ' +
|
|
125
|
+
'naming what it does take. Every image lives in exactly ONE place, the configured images ' +
|
|
126
|
+
'directory, and the result gives you its path — use that path when you need the file, do ' +
|
|
127
|
+
'not copy it somewhere else. reference_images takes FILE PATHS of images to work from (image-to-image), ' +
|
|
128
|
+
'where the model supports it — the tool reads them itself, so never paste base64. Several ' +
|
|
129
|
+
'are allowed: that is how you combine multiple sources into one picture.',
|
|
130
|
+
inputSchema: GenerateInput,
|
|
131
|
+
jsonSchema: {
|
|
132
|
+
type: 'object',
|
|
133
|
+
properties: {
|
|
134
|
+
prompt: { type: 'string', description: 'What to draw. Passed to the model verbatim.' },
|
|
135
|
+
model: {
|
|
136
|
+
type: 'string',
|
|
137
|
+
description: 'Configured image-model handle. Omit for the default.',
|
|
138
|
+
},
|
|
139
|
+
aspect_ratio: { type: 'string', description: 'e.g. "1:1", "16:9", "9:16", "4:3".' },
|
|
140
|
+
resolution: { type: 'string', description: 'Tier, e.g. "512", "1K", "2K", "4K".' },
|
|
141
|
+
size: { type: 'string', description: 'Alternative to resolution: explicit "1024x1024".' },
|
|
142
|
+
quality: { type: 'string', description: 'e.g. "auto", "low", "medium", "high".' },
|
|
143
|
+
output_format: { type: 'string', description: '"png", "jpeg", "webp", "svg".' },
|
|
144
|
+
background: { type: 'string', description: '"auto", "transparent", "opaque".' },
|
|
145
|
+
output_compression: { type: 'number', description: '0-100, webp/jpeg only.' },
|
|
146
|
+
seed: { type: 'number', description: 'Repeat a previous result exactly.' },
|
|
147
|
+
n: { type: 'number', description: 'How many images (1-10, model-dependent).' },
|
|
148
|
+
steps: {
|
|
149
|
+
type: 'number',
|
|
150
|
+
description:
|
|
151
|
+
'Sampling steps. More = slower, usually sharper. Leave unset for the model default.',
|
|
152
|
+
},
|
|
153
|
+
cfg: {
|
|
154
|
+
type: 'number',
|
|
155
|
+
description:
|
|
156
|
+
'Prompt adherence. Too high looks over-cooked, too low looks washed out. ' +
|
|
157
|
+
'Leave unset for the model default.',
|
|
158
|
+
},
|
|
159
|
+
guidance: {
|
|
160
|
+
type: 'number',
|
|
161
|
+
description: 'Guidance scale, where the model uses one instead of cfg.',
|
|
162
|
+
},
|
|
163
|
+
reference_images: {
|
|
164
|
+
type: 'array',
|
|
165
|
+
items: { type: 'string' },
|
|
166
|
+
description:
|
|
167
|
+
'Paths of existing images to work from (image-to-image). Relative paths resolve ' +
|
|
168
|
+
'against your workspace. Pass several to combine them.',
|
|
169
|
+
},
|
|
170
|
+
return_image: {
|
|
171
|
+
type: 'boolean',
|
|
172
|
+
description: 'Also return the image so you can look at it. Default false.',
|
|
173
|
+
},
|
|
174
|
+
extra: {
|
|
175
|
+
type: 'object',
|
|
176
|
+
description: 'Provider-specific fields, passed through untouched.',
|
|
177
|
+
additionalProperties: true,
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
required: ['prompt'],
|
|
181
|
+
additionalProperties: false,
|
|
182
|
+
},
|
|
183
|
+
// Image models are slow by nature — a 2K render takes tens of seconds
|
|
184
|
+
// and 4K can take minutes. The engine-level 30s default would cancel
|
|
185
|
+
// a request the provider is still billing for.
|
|
186
|
+
defaultTimeoutMs: 300_000,
|
|
187
|
+
available: (ctx) => imageGenEnabled(ctx),
|
|
188
|
+
async handler(input, ctx): Promise<GenerateOutput | MultimodalToolResult> {
|
|
189
|
+
const cfg = ctx.config.imageGen;
|
|
190
|
+
if (!cfg?.enabled) {
|
|
191
|
+
throw new Error(
|
|
192
|
+
'image_generate: image generation is not enabled. Ask the user to set imageGen.enabled and configure a model in config.yaml.',
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const requested = input.n ?? 1;
|
|
197
|
+
const budget = checkImageBudget(ctx.turnId, requested, cfg.maxImagesPerTurn);
|
|
198
|
+
if (!budget.ok) {
|
|
199
|
+
throw new Error(`image_generate: ${budget.reason}`);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// One image, one place (Rene 2026-09-10). The second destination is
|
|
203
|
+
// gone: it produced two paths for the same file, one of which was
|
|
204
|
+
// routinely the nested `<workspace>/<workspace>/…` a relative path
|
|
205
|
+
// creates, and the gallery then showed the same picture under two
|
|
206
|
+
// names. An agent that needs the file uses the path in the result.
|
|
207
|
+
const saveTo: string | undefined = undefined;
|
|
208
|
+
|
|
209
|
+
const wantImage = await wantsImageBack(input, ctx);
|
|
210
|
+
const notes: string[] = [];
|
|
211
|
+
|
|
212
|
+
// Refuse early rather than after paying for the image: the caller
|
|
213
|
+
// asked to see it and can't, so it should know before deciding to
|
|
214
|
+
// generate at all.
|
|
215
|
+
if (wantImage && input.return_image === true && !modelCanSeeImages(ctx)) {
|
|
216
|
+
const active = ctx.activeModel
|
|
217
|
+
? `'${ctx.activeModel.providerName}/${ctx.activeModel.modelId}'`
|
|
218
|
+
: '(unknown)';
|
|
219
|
+
throw new Error(
|
|
220
|
+
`image_generate: return_image was requested but the active model ${active} lacks ` +
|
|
221
|
+
`'image' capability. Generate without return_image and inspect the file with ` +
|
|
222
|
+
`analyze_file({path:"..."}), which dispatches to the configured vision worker.`,
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Reference images arrive as paths and are read HERE, through the
|
|
227
|
+
// same two-pass policy file_read uses (raw path, then realpath of
|
|
228
|
+
// the nearest existing ancestor, so a symlink can't step outside a
|
|
229
|
+
// blocked root). Doing it in the tool rather than in the generator
|
|
230
|
+
// is what keeps base64 out of the agent's context entirely.
|
|
231
|
+
const references = [];
|
|
232
|
+
for (const ref of input.reference_images ?? []) {
|
|
233
|
+
const { absolute } = await resolveLocalPath(ref, ctx.agent, ctx.config, ctx.session);
|
|
234
|
+
const verdict = checkReadAllowed(absolute);
|
|
235
|
+
if (!verdict.ok) {
|
|
236
|
+
throw new Error(`image_generate: reference_images ${verdict.reason}`);
|
|
237
|
+
}
|
|
238
|
+
const real = await realpathSafeAncestor(absolute);
|
|
239
|
+
const verdictReal = checkReadAllowed(real);
|
|
240
|
+
if (!verdictReal.ok) {
|
|
241
|
+
throw new Error(`image_generate: reference_images ${verdictReal.reason}`);
|
|
242
|
+
}
|
|
243
|
+
let bytes;
|
|
244
|
+
try {
|
|
245
|
+
bytes = await readFile(absolute);
|
|
246
|
+
} catch (err) {
|
|
247
|
+
throw new Error(
|
|
248
|
+
`image_generate: could not read reference image '${ref}': ${(err as Error).message}`,
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
try {
|
|
252
|
+
references.push(referenceFromBytes(bytes, ref));
|
|
253
|
+
} catch (err) {
|
|
254
|
+
if (err instanceof ImageGenError) throw new Error(`image_generate: ${err.message}`);
|
|
255
|
+
throw err;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
let result;
|
|
260
|
+
try {
|
|
261
|
+
result = await generateImage(
|
|
262
|
+
{
|
|
263
|
+
prompt: input.prompt,
|
|
264
|
+
...(input.model ? { model: input.model } : {}),
|
|
265
|
+
specs: toSpecs(input),
|
|
266
|
+
...(saveTo ? { saveTo } : {}),
|
|
267
|
+
...(references.length > 0 ? { references } : {}),
|
|
268
|
+
...(input.extra ? { extra: input.extra } : {}),
|
|
269
|
+
agent: ctx.agent,
|
|
270
|
+
...(ctx.session ? { session: ctx.session } : {}),
|
|
271
|
+
},
|
|
272
|
+
ctx.config,
|
|
273
|
+
);
|
|
274
|
+
} catch (err) {
|
|
275
|
+
// ImageGenError messages are written to be acted on — relay them
|
|
276
|
+
// as-is rather than wrapping them in a generic failure.
|
|
277
|
+
if (err instanceof ImageGenError) throw new Error(`image_generate: ${err.message}`);
|
|
278
|
+
throw err;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
recordImagesInTurn(ctx.turnId, result.images.length);
|
|
282
|
+
|
|
283
|
+
if (result.warnings && result.warnings.length > 0) {
|
|
284
|
+
// Things the endpoint did differently than asked. Relayed
|
|
285
|
+
// verbatim: the caller is a model, and it cannot correct what it
|
|
286
|
+
// is not told.
|
|
287
|
+
notes.push(...result.warnings);
|
|
288
|
+
}
|
|
289
|
+
if (result.fellBackFrom && result.fellBackFrom.length > 0) {
|
|
290
|
+
// The caller asked for one model and got another. Saying so beats
|
|
291
|
+
// letting it wonder why the style changed.
|
|
292
|
+
notes.push(
|
|
293
|
+
`Used a fallback model — the requested one was unavailable: ${result.fellBackFrom.join('; ')}`,
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
if (input.save_to) {
|
|
297
|
+
// Still accepted, deliberately ignored: an old persona line or a
|
|
298
|
+
// habit must not fail the generation. Say where the image is.
|
|
299
|
+
notes.push(
|
|
300
|
+
`save_to is no longer used — every image lives in one place. This one is at ${result.images[0]?.path ?? 'the images directory'}.`,
|
|
301
|
+
);
|
|
302
|
+
logger.info({ msg: 'imagegen.save_to_ignored', agent: ctx.agent, requested: input.save_to });
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const payload: GenerateOutput = {
|
|
306
|
+
images: result.images.map((img) => ({
|
|
307
|
+
id: img.id,
|
|
308
|
+
path: img.path,
|
|
309
|
+
filename: img.filename,
|
|
310
|
+
linked_to: img.linkedTo,
|
|
311
|
+
...(img.costUsd !== undefined ? { cost_usd: img.costUsd } : {}),
|
|
312
|
+
})),
|
|
313
|
+
model: result.images[0]?.modelId ?? '',
|
|
314
|
+
specs: result.images[0]?.specs ?? {},
|
|
315
|
+
...(result.costUsd !== undefined ? { cost_usd: result.costUsd } : {}),
|
|
316
|
+
...(notes.length > 0 ? { notes } : {}),
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
if (!wantImage) return payload;
|
|
320
|
+
|
|
321
|
+
// imageReview: always on a text-only model — generate anyway and say
|
|
322
|
+
// why the image isn't attached, rather than failing a call the agent
|
|
323
|
+
// didn't explicitly ask to see the result of.
|
|
324
|
+
if (!modelCanSeeImages(ctx)) {
|
|
325
|
+
payload.notes = [
|
|
326
|
+
...(payload.notes ?? []),
|
|
327
|
+
`Image not attached: the active model cannot see images. Use analyze_file({path:"${result.images[0]?.path}"}) to have it described.`,
|
|
328
|
+
];
|
|
329
|
+
return payload;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const blocks: MultimodalToolResult['contentBlocks'] = [
|
|
333
|
+
{ type: 'text', text: JSON.stringify(payload) },
|
|
334
|
+
];
|
|
335
|
+
for (const img of result.images) {
|
|
336
|
+
try {
|
|
337
|
+
const bytes = await readFile(img.path);
|
|
338
|
+
blocks.push({
|
|
339
|
+
type: 'image',
|
|
340
|
+
source: { kind: 'base64', mediaType: img.mime, data: bytes.toString('base64') },
|
|
341
|
+
});
|
|
342
|
+
} catch (err) {
|
|
343
|
+
logger.warn({ msg: 'imagegen.readback_failed', path: img.path, err: (err as Error).message });
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return { _somoraMultimodal: true, contentBlocks: blocks };
|
|
347
|
+
},
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
const ListInput = z
|
|
351
|
+
.object({
|
|
352
|
+
type: z.enum(['image', 'video']).optional(),
|
|
353
|
+
query: z.string().min(1).max(200).optional(),
|
|
354
|
+
model: z.string().min(1).optional(),
|
|
355
|
+
agent: z.string().min(1).optional(),
|
|
356
|
+
mine_only: z.boolean().optional(),
|
|
357
|
+
since: z
|
|
358
|
+
.string()
|
|
359
|
+
.regex(/^\d{4}-\d{2}-\d{2}$/)
|
|
360
|
+
.optional(),
|
|
361
|
+
until: z
|
|
362
|
+
.string()
|
|
363
|
+
.regex(/^\d{4}-\d{2}-\d{2}$/)
|
|
364
|
+
.optional(),
|
|
365
|
+
limit: z.number().int().min(1).max(200).optional(),
|
|
366
|
+
offset: z.number().int().min(0).optional(),
|
|
367
|
+
})
|
|
368
|
+
.strict();
|
|
369
|
+
|
|
370
|
+
type ListArgs = z.infer<typeof ListInput>;
|
|
371
|
+
|
|
372
|
+
interface ListRow {
|
|
373
|
+
id: string;
|
|
374
|
+
created_at: string;
|
|
375
|
+
prompt: string;
|
|
376
|
+
model: string;
|
|
377
|
+
path: string;
|
|
378
|
+
specs: ImageSpecs;
|
|
379
|
+
agent?: string;
|
|
380
|
+
cost_usd?: number;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
interface ListOutput {
|
|
384
|
+
total: number;
|
|
385
|
+
returned: number;
|
|
386
|
+
images: ListRow[];
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function toRow(r: ImageRecord): ListRow {
|
|
390
|
+
return {
|
|
391
|
+
id: r.id,
|
|
392
|
+
created_at: r.createdAt,
|
|
393
|
+
prompt: r.prompt,
|
|
394
|
+
model: r.modelName,
|
|
395
|
+
path: r.path,
|
|
396
|
+
specs: r.specs,
|
|
397
|
+
...(r.agent ? { agent: r.agent } : {}),
|
|
398
|
+
...(r.costUsd !== undefined ? { cost_usd: r.costUsd } : {}),
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export const imageList: ToolDefinition<ListArgs, ListOutput> = {
|
|
403
|
+
// Named for the question, not for one medium: "find the thing I made
|
|
404
|
+
// earlier" is the same question for a picture and for a video, and
|
|
405
|
+
// the gallery behind it holds both. It was `image_list` until video
|
|
406
|
+
// existed — and then returned videos, which a tool with that name has
|
|
407
|
+
// no business doing.
|
|
408
|
+
name: 'media_list',
|
|
409
|
+
// Its own toolset rather than `image`: a video-only install needs to
|
|
410
|
+
// list what it made, and gating this on imageGen would have left it
|
|
411
|
+
// with no way to.
|
|
412
|
+
toolset: 'media',
|
|
413
|
+
description:
|
|
414
|
+
'Find images and video generated earlier, newest first. Use this to recover the path of ' +
|
|
415
|
+
'something whose tool result has scrolled out of the conversation — "the koala one", "the ' +
|
|
416
|
+
'last video I made". Filter by type (image | video), prompt substring (query), model, ' +
|
|
417
|
+
'agent, or date range. Returns paths and metadata; pass a path to file_read or ' +
|
|
418
|
+
'analyze_file to actually look at it — for a video, analyze_file reads the thumbnail ' +
|
|
419
|
+
'beside it.',
|
|
420
|
+
inputSchema: ListInput,
|
|
421
|
+
jsonSchema: {
|
|
422
|
+
type: 'object',
|
|
423
|
+
properties: {
|
|
424
|
+
type: { type: 'string', description: 'Restrict to "image" or "video". Omit for both.' },
|
|
425
|
+
query: { type: 'string', description: 'Case-insensitive substring of the prompt.' },
|
|
426
|
+
model: { type: 'string', description: 'Image-model handle.' },
|
|
427
|
+
agent: { type: 'string', description: 'Restrict to images generated by this agent.' },
|
|
428
|
+
mine_only: { type: 'boolean', description: 'Only images you generated yourself.' },
|
|
429
|
+
since: { type: 'string', description: 'YYYY-MM-DD, inclusive.' },
|
|
430
|
+
until: { type: 'string', description: 'YYYY-MM-DD, inclusive.' },
|
|
431
|
+
limit: { type: 'number', description: 'Default 20, max 200.' },
|
|
432
|
+
offset: { type: 'number', description: 'For paging through the total.' },
|
|
433
|
+
},
|
|
434
|
+
additionalProperties: false,
|
|
435
|
+
},
|
|
436
|
+
// Either surface: the gallery is shared, so listing must work
|
|
437
|
+
// wherever there is something to list.
|
|
438
|
+
available: (ctx) =>
|
|
439
|
+
imageGenEnabled(ctx) ||
|
|
440
|
+
(ctx.config.videoGen?.enabled === true && (ctx.config.videoGen?.models.length ?? 0) > 0),
|
|
441
|
+
async handler(input, ctx): Promise<ListOutput> {
|
|
442
|
+
const agent = input.mine_only ? ctx.agent : input.agent;
|
|
443
|
+
const { total, items } = await listRecords({
|
|
444
|
+
...(input.type ? { kind: input.type } : {}),
|
|
445
|
+
...(input.query ? { query: input.query } : {}),
|
|
446
|
+
...(input.model ? { model: input.model } : {}),
|
|
447
|
+
...(agent ? { agent } : {}),
|
|
448
|
+
...(input.since ? { since: input.since } : {}),
|
|
449
|
+
...(input.until ? { until: input.until } : {}),
|
|
450
|
+
limit: input.limit ?? 20,
|
|
451
|
+
offset: input.offset ?? 0,
|
|
452
|
+
});
|
|
453
|
+
return { total, returned: items.length, images: items.map(toRow) };
|
|
454
|
+
},
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
const ModelsInput = z
|
|
458
|
+
.object({
|
|
459
|
+
model: z
|
|
460
|
+
.string()
|
|
461
|
+
.min(1)
|
|
462
|
+
.optional()
|
|
463
|
+
.describe('Handle to describe in detail. Omit to just list what is configured.'),
|
|
464
|
+
})
|
|
465
|
+
.strict();
|
|
466
|
+
|
|
467
|
+
type ModelsArgs = z.infer<typeof ModelsInput>;
|
|
468
|
+
|
|
469
|
+
interface ModelRow {
|
|
470
|
+
name: string;
|
|
471
|
+
label?: string;
|
|
472
|
+
model: string;
|
|
473
|
+
provider: string;
|
|
474
|
+
is_default: boolean;
|
|
475
|
+
defaults?: Record<string, string>;
|
|
476
|
+
/** Only filled in when a specific model was asked about. */
|
|
477
|
+
accepts?: Record<string, string[] | string>;
|
|
478
|
+
/** Values the backend names as known-good for a free-text field
|
|
479
|
+
* (`accepts.<field>` = 'any value'). Not a restriction — but a
|
|
480
|
+
* value outside this list is the usual reason for an upstream
|
|
481
|
+
* 400, so prefer these. */
|
|
482
|
+
recommended?: Record<string, string[]>;
|
|
483
|
+
max_references?: number;
|
|
484
|
+
max_n?: number;
|
|
485
|
+
capability_source?: string;
|
|
486
|
+
note?: string;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Without this, a second configured model is a guessing game: the
|
|
491
|
+
* `model` argument on image_generate is a free string and nothing tells
|
|
492
|
+
* the caller which handles exist. The generate tool rejects an unknown
|
|
493
|
+
* one with the list, so it is recoverable — but only after a wasted
|
|
494
|
+
* call, and a wrong SPEC value costs a wasted call too.
|
|
495
|
+
*
|
|
496
|
+
* Deliberately two-speed: listing handles is free (it reads config),
|
|
497
|
+
* while the per-model detail hits the provider's catalog, so that only
|
|
498
|
+
* happens when a caller asks for one model by name.
|
|
499
|
+
*/
|
|
500
|
+
export const imageModels: ToolDefinition<ModelsArgs, { models: ModelRow[] }> = {
|
|
501
|
+
name: 'image_models',
|
|
502
|
+
toolset: 'image',
|
|
503
|
+
description:
|
|
504
|
+
'List the configured image models for image_generate. Call this before generating when you ' +
|
|
505
|
+
'need a model other than the default, or when you want to know which values a model accepts ' +
|
|
506
|
+
'for aspect_ratio, resolution, quality and the rest — those differ per model, and an ' +
|
|
507
|
+
'unsupported one is rejected. Pass model: "<handle>" for the detail of one model.',
|
|
508
|
+
inputSchema: ModelsInput,
|
|
509
|
+
jsonSchema: {
|
|
510
|
+
type: 'object',
|
|
511
|
+
properties: {
|
|
512
|
+
model: {
|
|
513
|
+
type: 'string',
|
|
514
|
+
description: 'Handle to describe in detail. Omit to list all configured models.',
|
|
515
|
+
},
|
|
516
|
+
},
|
|
517
|
+
additionalProperties: false,
|
|
518
|
+
},
|
|
519
|
+
available: (ctx) => imageGenEnabled(ctx),
|
|
520
|
+
async handler(input, ctx): Promise<{ models: ModelRow[] }> {
|
|
521
|
+
const configured = ctx.config.imageGen?.models ?? [];
|
|
522
|
+
if (configured.length === 0) {
|
|
523
|
+
throw new Error('image_models: no image models configured under imageGen.models.');
|
|
524
|
+
}
|
|
525
|
+
if (input.model && !configured.some((m) => m.name === input.model)) {
|
|
526
|
+
throw new Error(
|
|
527
|
+
`image_models: unknown image model '${input.model}'. Configured: ` +
|
|
528
|
+
configured.map((m) => m.name).join(', '),
|
|
529
|
+
);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
const wanted = input.model ? configured.filter((m) => m.name === input.model) : configured;
|
|
533
|
+
const rows: ModelRow[] = [];
|
|
534
|
+
for (const entry of wanted) {
|
|
535
|
+
const row: ModelRow = {
|
|
536
|
+
name: entry.name,
|
|
537
|
+
...(entry.label ? { label: entry.label } : {}),
|
|
538
|
+
model: entry.model,
|
|
539
|
+
provider: entry.provider,
|
|
540
|
+
is_default: entry.name === configured[0]?.name,
|
|
541
|
+
...(Object.keys(entry.defaults).length > 0
|
|
542
|
+
? { defaults: entry.defaults as Record<string, string> }
|
|
543
|
+
: {}),
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
// Detail only for a named model — this is the part that can reach
|
|
547
|
+
// out to the provider.
|
|
548
|
+
if (input.model) {
|
|
549
|
+
const resolved = resolveImageModel(ctx.config, entry.name);
|
|
550
|
+
if (!resolved || resolved.provider.engine !== 'openai-compatible') {
|
|
551
|
+
row.note =
|
|
552
|
+
`provider '${entry.provider}' is not an openai-compatible provider — ` +
|
|
553
|
+
'image_generate will refuse this model.';
|
|
554
|
+
} else {
|
|
555
|
+
try {
|
|
556
|
+
const caps = await resolveCapabilities(resolved.providerName, resolved.provider, entry);
|
|
557
|
+
const accepts: Record<string, string[] | string> = {};
|
|
558
|
+
for (const [field, values] of Object.entries(caps.values)) {
|
|
559
|
+
if (Array.isArray(values) && values.length > 0) accepts[field] = values;
|
|
560
|
+
}
|
|
561
|
+
// A field the catalog publishes without a value list takes
|
|
562
|
+
// free text; one missing from an exhaustive list is not
|
|
563
|
+
// supported at all. Saying so beats letting the caller find
|
|
564
|
+
// out by having the setting silently ignored.
|
|
565
|
+
if (caps.supported) {
|
|
566
|
+
for (const field of caps.supported) {
|
|
567
|
+
if (!(field in accepts)) accepts[field] = 'any value';
|
|
568
|
+
}
|
|
569
|
+
row.note =
|
|
570
|
+
'Fields not listed here are not supported by this model — passing them is an error.';
|
|
571
|
+
} else {
|
|
572
|
+
row.note = 'No published parameter list for this model; unlisted fields are passed through untouched.';
|
|
573
|
+
}
|
|
574
|
+
row.accepts = accepts;
|
|
575
|
+
if (caps.recommended && Object.keys(caps.recommended).length > 0) {
|
|
576
|
+
row.recommended = caps.recommended as Record<string, string[]>;
|
|
577
|
+
}
|
|
578
|
+
if (caps.maxReferences !== undefined) row.max_references = caps.maxReferences;
|
|
579
|
+
if (caps.maxN !== undefined) row.max_n = caps.maxN;
|
|
580
|
+
row.capability_source = caps.source;
|
|
581
|
+
} catch (err) {
|
|
582
|
+
// A catalog outage must not make this tool useless — the
|
|
583
|
+
// handle list is the part that always works.
|
|
584
|
+
row.note = `could not load capabilities: ${(err as Error).message}`;
|
|
585
|
+
logger.debug({ msg: 'imagegen.capabilities_lookup_failed', model: entry.name });
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
rows.push(row);
|
|
590
|
+
}
|
|
591
|
+
return { models: rows };
|
|
592
|
+
},
|
|
593
|
+
};
|
|
594
|
+
|
|
595
|
+
export function imageTools(): ToolDefinition[] {
|
|
596
|
+
return [
|
|
597
|
+
imageGenerate as ToolDefinition,
|
|
598
|
+
imageList as ToolDefinition,
|
|
599
|
+
imageModels as ToolDefinition,
|
|
600
|
+
];
|
|
601
|
+
}
|