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,217 @@
|
|
|
1
|
+
// File placement for generated images.
|
|
2
|
+
//
|
|
3
|
+
// Every image lands in ONE canonical directory (`imageGen.outputDir`),
|
|
4
|
+
// no matter which agent produced it — that's what the gallery and the
|
|
5
|
+
// /images/:id/file route index. A caller-chosen destination is an
|
|
6
|
+
// additional NAME for the same bytes, created with link(2): two paths,
|
|
7
|
+
// one inode, no second copy. Across filesystems link(2) fails with
|
|
8
|
+
// EXDEV and we fall back to a real copy, which is the only case where
|
|
9
|
+
// disk usage doubles.
|
|
10
|
+
//
|
|
11
|
+
// Deleting either name leaves the other intact — that's a property of
|
|
12
|
+
// hardlinks, and the desirable one here: cleaning up a project folder
|
|
13
|
+
// must not blank the gallery, and vice versa.
|
|
14
|
+
|
|
15
|
+
import { constants } from 'node:fs';
|
|
16
|
+
import { copyFile, link, mkdir, access, writeFile } from 'node:fs/promises';
|
|
17
|
+
import { dirname, join, resolve } from 'node:path';
|
|
18
|
+
import type { Config } from '../config/types.ts';
|
|
19
|
+
import { detectMimeFromBuffer } from '../multimodal/mime.ts';
|
|
20
|
+
import { expandHome } from '../tools/file/policy.ts';
|
|
21
|
+
import { logger } from '../server/logger.ts';
|
|
22
|
+
import type { MediaKind } from './types.ts';
|
|
23
|
+
|
|
24
|
+
/** Fallbacks when the operator left the block out entirely. Images and
|
|
25
|
+
* video get separate directories on purpose: these are folders a
|
|
26
|
+
* person browses, and mixing a 600 KB still with a 60 MB render makes
|
|
27
|
+
* both harder to find. */
|
|
28
|
+
const DEFAULT_OUTPUT_DIR: Record<MediaKind, string> = {
|
|
29
|
+
image: '~/somoraworkspace/images',
|
|
30
|
+
video: '~/somoraworkspace/videos',
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const EXT_BY_MIME: Record<string, string> = {
|
|
34
|
+
'image/png': 'png',
|
|
35
|
+
'image/jpeg': 'jpg',
|
|
36
|
+
'image/webp': 'webp',
|
|
37
|
+
'image/gif': 'gif',
|
|
38
|
+
'image/svg+xml': 'svg',
|
|
39
|
+
'video/mp4': 'mp4',
|
|
40
|
+
'video/webm': 'webm',
|
|
41
|
+
'video/quicktime': 'mov',
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** Canonical directory for one medium, ~ expanded, month bucket
|
|
45
|
+
* applied. */
|
|
46
|
+
export function mediaDir(config: Config, kind: MediaKind, now = new Date()): string {
|
|
47
|
+
const cfg = kind === 'video' ? config.videoGen : config.imageGen;
|
|
48
|
+
const base = expandHome(cfg?.outputDir ?? DEFAULT_OUTPUT_DIR[kind]);
|
|
49
|
+
if (!cfg?.monthlyFolders) return base;
|
|
50
|
+
const bucket = `${now.getFullYear()}-${two(now.getMonth() + 1)}`;
|
|
51
|
+
return join(base, bucket);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function two(n: number): string {
|
|
55
|
+
return String(n).padStart(2, '0');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Prompt → filename fragment. Latin-1 diacritics are folded rather than
|
|
60
|
+
* dropped so a German prompt stays readable ("größe" → "groesse", not
|
|
61
|
+
* "gre"). Everything else collapses to single hyphens.
|
|
62
|
+
*/
|
|
63
|
+
export function slugFromPrompt(prompt: string, maxLen = 40): string {
|
|
64
|
+
const folded = prompt
|
|
65
|
+
.toLowerCase()
|
|
66
|
+
.replace(/ä/g, 'ae')
|
|
67
|
+
.replace(/ö/g, 'oe')
|
|
68
|
+
.replace(/ü/g, 'ue')
|
|
69
|
+
.replace(/ß/g, 'ss')
|
|
70
|
+
.normalize('NFD')
|
|
71
|
+
.replace(/[\u0300-\u036f]/g, '');
|
|
72
|
+
const slug = folded
|
|
73
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
74
|
+
.replace(/^-+|-+$/g, '')
|
|
75
|
+
.slice(0, maxLen)
|
|
76
|
+
.replace(/-+$/g, '');
|
|
77
|
+
// A prompt of pure punctuation or non-Latin script leaves nothing
|
|
78
|
+
// usable — the timestamp still makes the name unique.
|
|
79
|
+
return slug || 'bild';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** `2026-08-26_143012_koala-im-weltraum.png` — sorts chronologically in
|
|
83
|
+
* any file browser and stays recognizable without opening it. */
|
|
84
|
+
export function buildFilename(prompt: string, ext: string, now = new Date()): string {
|
|
85
|
+
const stamp =
|
|
86
|
+
`${now.getFullYear()}-${two(now.getMonth() + 1)}-${two(now.getDate())}` +
|
|
87
|
+
`_${two(now.getHours())}${two(now.getMinutes())}${two(now.getSeconds())}`;
|
|
88
|
+
return `${stamp}_${slugFromPrompt(prompt)}.${ext}`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function extForMime(mime: string, fallbackFormat?: string): string {
|
|
92
|
+
return EXT_BY_MIME[mime] ?? fallbackFormat ?? 'png';
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function exists(path: string): Promise<boolean> {
|
|
96
|
+
try {
|
|
97
|
+
await access(path, constants.F_OK);
|
|
98
|
+
return true;
|
|
99
|
+
} catch {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* First free variant of `<dir>/<name>`: `foo.png`, then `foo_2.png`, …
|
|
106
|
+
* Two images generated within the same second from the same prompt
|
|
107
|
+
* (n > 1) would otherwise overwrite each other.
|
|
108
|
+
*/
|
|
109
|
+
export async function freePath(dir: string, filename: string): Promise<string> {
|
|
110
|
+
const dot = filename.lastIndexOf('.');
|
|
111
|
+
const stem = dot > 0 ? filename.slice(0, dot) : filename;
|
|
112
|
+
const ext = dot > 0 ? filename.slice(dot) : '';
|
|
113
|
+
let candidate = join(dir, filename);
|
|
114
|
+
let n = 2;
|
|
115
|
+
while (await exists(candidate)) {
|
|
116
|
+
candidate = join(dir, `${stem}_${n}${ext}`);
|
|
117
|
+
n++;
|
|
118
|
+
// Pathological only if something else is writing the same names in
|
|
119
|
+
// a loop; bail out rather than spin.
|
|
120
|
+
if (n > 999) throw new Error(`no free filename for '${filename}' in ${dir}`);
|
|
121
|
+
}
|
|
122
|
+
return candidate;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export interface StoredMedia {
|
|
126
|
+
path: string;
|
|
127
|
+
filename: string;
|
|
128
|
+
mime: string;
|
|
129
|
+
bytes: number;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Write media bytes into the canonical directory. MIME comes from the
|
|
134
|
+
* bytes themselves, not from what the upstream claimed — a provider
|
|
135
|
+
* that mislabels its output would otherwise leave us with a `.png` that
|
|
136
|
+
* no viewer opens.
|
|
137
|
+
*/
|
|
138
|
+
export async function storeMedia(args: {
|
|
139
|
+
bytes: Buffer;
|
|
140
|
+
prompt: string;
|
|
141
|
+
config: Config;
|
|
142
|
+
kind: MediaKind;
|
|
143
|
+
declaredMime?: string;
|
|
144
|
+
outputFormat?: string;
|
|
145
|
+
/** Overrides the prompt-derived name — used for a video's thumbnail,
|
|
146
|
+
* which belongs beside its video rather than under a name of its
|
|
147
|
+
* own. */
|
|
148
|
+
filenameStem?: string;
|
|
149
|
+
now?: Date;
|
|
150
|
+
}): Promise<StoredMedia> {
|
|
151
|
+
const now = args.now ?? new Date();
|
|
152
|
+
const detected = detectMimeFromBuffer(args.bytes);
|
|
153
|
+
// SVG is text, so magic-byte detection can't see it; trust the
|
|
154
|
+
// declared type in that one case.
|
|
155
|
+
const wanted = args.kind === 'video' ? 'video' : 'image';
|
|
156
|
+
const mime =
|
|
157
|
+
detected.kind === wanted
|
|
158
|
+
? detected.mimeType
|
|
159
|
+
: (args.declaredMime ??
|
|
160
|
+
(args.kind === 'video'
|
|
161
|
+
? 'video/mp4'
|
|
162
|
+
: args.outputFormat === 'svg'
|
|
163
|
+
? 'image/svg+xml'
|
|
164
|
+
: 'image/png'));
|
|
165
|
+
const dir = mediaDir(args.config, args.kind, now);
|
|
166
|
+
await mkdir(dir, { recursive: true });
|
|
167
|
+
const ext = extForMime(mime, args.outputFormat);
|
|
168
|
+
const filename = args.filenameStem
|
|
169
|
+
? `${args.filenameStem}.${ext}`
|
|
170
|
+
: buildFilename(args.prompt, ext, now);
|
|
171
|
+
const path = await freePath(dir, filename);
|
|
172
|
+
await writeFile(path, args.bytes);
|
|
173
|
+
return { path, filename: path.slice(dir.length + 1), mime, bytes: args.bytes.length };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Give the stored file a second name at `dest`. A destination ending
|
|
178
|
+
* in `/`, or naming an existing directory, keeps the canonical
|
|
179
|
+
* filename; anything else is treated as the full target path.
|
|
180
|
+
*
|
|
181
|
+
* Returns the path actually created.
|
|
182
|
+
*/
|
|
183
|
+
export async function linkMedia(canonicalPath: string, dest: string): Promise<string> {
|
|
184
|
+
const expanded = expandHome(dest);
|
|
185
|
+
const looksLikeDir = expanded.endsWith('/') || (await isDir(expanded));
|
|
186
|
+
const target = looksLikeDir
|
|
187
|
+
? join(resolve(expanded), canonicalPath.slice(canonicalPath.lastIndexOf('/') + 1))
|
|
188
|
+
: resolve(expanded);
|
|
189
|
+
|
|
190
|
+
await mkdir(dirname(target), { recursive: true });
|
|
191
|
+
const finalTarget = await freePath(dirname(target), target.slice(target.lastIndexOf('/') + 1));
|
|
192
|
+
|
|
193
|
+
try {
|
|
194
|
+
await link(canonicalPath, finalTarget);
|
|
195
|
+
return finalTarget;
|
|
196
|
+
} catch (err) {
|
|
197
|
+
const code = (err as NodeJS.ErrnoException).code;
|
|
198
|
+
// EXDEV: different filesystem — the one case where a real copy is
|
|
199
|
+
// unavoidable. EPERM: some filesystems (exFAT, many network mounts)
|
|
200
|
+
// reject hardlinks outright.
|
|
201
|
+
if (code === 'EXDEV' || code === 'EPERM' || code === 'ENOTSUP' || code === 'EOPNOTSUPP') {
|
|
202
|
+
logger.debug({ msg: 'media.link_fallback_copy', code, target: finalTarget });
|
|
203
|
+
await copyFile(canonicalPath, finalTarget);
|
|
204
|
+
return finalTarget;
|
|
205
|
+
}
|
|
206
|
+
throw err;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async function isDir(path: string): Promise<boolean> {
|
|
211
|
+
try {
|
|
212
|
+
const { stat } = await import('node:fs/promises');
|
|
213
|
+
return (await stat(path)).isDirectory();
|
|
214
|
+
} catch {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// Shared shapes for generated media — images today, video alongside
|
|
2
|
+
// them, and whatever comes next.
|
|
3
|
+
//
|
|
4
|
+
// One record type rather than one per medium: the questions a caller
|
|
5
|
+
// asks ("what did I make, when, with which model, where is the file")
|
|
6
|
+
// are identical, and two near-identical types would drift the moment
|
|
7
|
+
// one of them gained a field.
|
|
8
|
+
|
|
9
|
+
export type MediaKind = 'image' | 'video';
|
|
10
|
+
|
|
11
|
+
/** One generated file, as persisted and as handed back to callers. */
|
|
12
|
+
export interface MediaRecord {
|
|
13
|
+
id: string;
|
|
14
|
+
/**
|
|
15
|
+
* Absent on records written before video existed — those are all
|
|
16
|
+
* images, so a missing value reads as `image` rather than as
|
|
17
|
+
* "unknown". Use `mediaKind()` instead of touching this directly.
|
|
18
|
+
*/
|
|
19
|
+
kind?: MediaKind;
|
|
20
|
+
/** ISO-8601, local-time offset preserved. */
|
|
21
|
+
createdAt: string;
|
|
22
|
+
prompt: string;
|
|
23
|
+
/** Config handle (`zimage`, `h3`). */
|
|
24
|
+
modelName: string;
|
|
25
|
+
/** Wire id sent to the provider. */
|
|
26
|
+
modelId: string;
|
|
27
|
+
/** providers.<name> the request went through. */
|
|
28
|
+
provider: string;
|
|
29
|
+
/** Whatever spec fields the request carried, as sent. */
|
|
30
|
+
specs: Record<string, unknown>;
|
|
31
|
+
/** Absolute path in the canonical media directory. */
|
|
32
|
+
path: string;
|
|
33
|
+
filename: string;
|
|
34
|
+
mime: string;
|
|
35
|
+
bytes: number;
|
|
36
|
+
/** Real pixel dimensions, read from the bytes. Absent for a format
|
|
37
|
+
* we can't measure. */
|
|
38
|
+
width?: number;
|
|
39
|
+
height?: number;
|
|
40
|
+
/** Video only: playing time from the file's own header. */
|
|
41
|
+
durationSec?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Video only: a still saved next to the file. Providers that publish
|
|
44
|
+
* one (OpenAI's `variant=thumbnail`, and ours) let a gallery show a
|
|
45
|
+
* frame instead of a black box — and because it is an ordinary
|
|
46
|
+
* image, `analyze_file` reads it, which is how an agent judges a
|
|
47
|
+
* video it just made without anyone shipping a video decoder.
|
|
48
|
+
*/
|
|
49
|
+
thumbPath?: string;
|
|
50
|
+
thumbMime?: string;
|
|
51
|
+
/** Additional locations the caller asked for, as hardlinks (or
|
|
52
|
+
* copies, across filesystems). */
|
|
53
|
+
linkedTo: string[];
|
|
54
|
+
/** Upstream-reported cost in USD, when available. */
|
|
55
|
+
costUsd?: number;
|
|
56
|
+
/** Who triggered it — agent name, or undefined for a UI request. */
|
|
57
|
+
agent?: string;
|
|
58
|
+
session?: string;
|
|
59
|
+
/** Number of reference images passed in. */
|
|
60
|
+
references?: number;
|
|
61
|
+
/** Groups the outputs of one call. */
|
|
62
|
+
batchId: string;
|
|
63
|
+
batchIndex: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** A record's medium, treating the pre-video absence as `image`. */
|
|
67
|
+
export function mediaKind(record: Pick<MediaRecord, 'kind'>): MediaKind {
|
|
68
|
+
return record.kind ?? 'image';
|
|
69
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// Markdown chunking. Splits content into ~targetTokens chunks with
|
|
2
|
+
// overlapTokens of overlap between adjacent chunks. Token estimate is the
|
|
3
|
+
// project-wide 4-chars/token heuristic.
|
|
4
|
+
//
|
|
5
|
+
// We chunk by markdown structure first (split on blank lines, then on
|
|
6
|
+
// heading boundaries), then pack chunks up to the target size. Overlap is
|
|
7
|
+
// done as a sliding window over the *paragraph list*, not character-level —
|
|
8
|
+
// keeps chunks readable.
|
|
9
|
+
//
|
|
10
|
+
// Frontmatter (--- ... ---) at the top is stripped before chunking; metadata
|
|
11
|
+
// from the frontmatter is the caller's job.
|
|
12
|
+
|
|
13
|
+
export interface MarkdownChunk {
|
|
14
|
+
/** Concatenated paragraph text. */
|
|
15
|
+
text: string;
|
|
16
|
+
/** 1-based line numbers (inclusive). */
|
|
17
|
+
startLine: number;
|
|
18
|
+
endLine: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface Paragraph {
|
|
22
|
+
text: string;
|
|
23
|
+
startLine: number;
|
|
24
|
+
endLine: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Strip leading frontmatter block if present. Returns body + offset (line count consumed). */
|
|
28
|
+
export function stripFrontmatter(input: string): { body: string; bodyStartLine: number } {
|
|
29
|
+
if (!input.startsWith('---')) return { body: input, bodyStartLine: 1 };
|
|
30
|
+
const lines = input.split('\n');
|
|
31
|
+
if (lines.length < 2) return { body: input, bodyStartLine: 1 };
|
|
32
|
+
let endIdx = -1;
|
|
33
|
+
for (let i = 1; i < lines.length; i++) {
|
|
34
|
+
if (lines[i] === '---') {
|
|
35
|
+
endIdx = i;
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (endIdx < 0) return { body: input, bodyStartLine: 1 };
|
|
40
|
+
return {
|
|
41
|
+
body: lines.slice(endIdx + 1).join('\n'),
|
|
42
|
+
bodyStartLine: endIdx + 2, // 1-based, line after closing ---
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function estimateTokens(text: string): number {
|
|
47
|
+
// 4 chars/token is the project-wide heuristic. Code is denser, prose
|
|
48
|
+
// looser, but it's close enough for chunk-size budgeting.
|
|
49
|
+
return Math.ceil(text.length / 4);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function splitParagraphs(body: string, lineOffset: number): Paragraph[] {
|
|
53
|
+
const lines = body.split('\n');
|
|
54
|
+
const paragraphs: Paragraph[] = [];
|
|
55
|
+
let buf: string[] = [];
|
|
56
|
+
let start = -1;
|
|
57
|
+
const flush = (endIdx: number) => {
|
|
58
|
+
if (buf.length === 0) return;
|
|
59
|
+
const text = buf.join('\n').trim();
|
|
60
|
+
if (text.length > 0) {
|
|
61
|
+
paragraphs.push({
|
|
62
|
+
text,
|
|
63
|
+
startLine: start + lineOffset,
|
|
64
|
+
endLine: endIdx + lineOffset,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
buf = [];
|
|
68
|
+
start = -1;
|
|
69
|
+
};
|
|
70
|
+
for (let i = 0; i < lines.length; i++) {
|
|
71
|
+
const line = lines[i]!;
|
|
72
|
+
if (line.trim() === '') {
|
|
73
|
+
flush(i - 1);
|
|
74
|
+
} else {
|
|
75
|
+
if (start < 0) start = i;
|
|
76
|
+
buf.push(line);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
flush(lines.length - 1);
|
|
80
|
+
return paragraphs;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface ChunkOptions {
|
|
84
|
+
targetTokens: number;
|
|
85
|
+
overlapTokens: number;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Pack paragraphs into chunks of ~targetTokens. Overlap is implemented at
|
|
90
|
+
* paragraph granularity: each chunk starts with the trailing paragraphs of
|
|
91
|
+
* the previous chunk that together cover overlapTokens.
|
|
92
|
+
*/
|
|
93
|
+
export function chunkMarkdown(
|
|
94
|
+
input: string,
|
|
95
|
+
opts: ChunkOptions = { targetTokens: 400, overlapTokens: 80 },
|
|
96
|
+
): MarkdownChunk[] {
|
|
97
|
+
const { body, bodyStartLine } = stripFrontmatter(input);
|
|
98
|
+
const paragraphs = splitParagraphs(body, bodyStartLine);
|
|
99
|
+
if (paragraphs.length === 0) return [];
|
|
100
|
+
|
|
101
|
+
const chunks: MarkdownChunk[] = [];
|
|
102
|
+
let current: Paragraph[] = [];
|
|
103
|
+
let currentTokens = 0;
|
|
104
|
+
|
|
105
|
+
const pushChunk = () => {
|
|
106
|
+
if (current.length === 0) return;
|
|
107
|
+
const text = current.map((p) => p.text).join('\n\n');
|
|
108
|
+
chunks.push({
|
|
109
|
+
text,
|
|
110
|
+
startLine: current[0]!.startLine,
|
|
111
|
+
endLine: current[current.length - 1]!.endLine,
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
for (const para of paragraphs) {
|
|
116
|
+
const paraTokens = estimateTokens(para.text);
|
|
117
|
+
|
|
118
|
+
// If a single paragraph exceeds the target, it becomes its own chunk —
|
|
119
|
+
// we don't split mid-paragraph (would corrupt code blocks, lists, etc.)
|
|
120
|
+
if (paraTokens >= opts.targetTokens && current.length === 0) {
|
|
121
|
+
current.push(para);
|
|
122
|
+
pushChunk();
|
|
123
|
+
// An oversized paragraph is already a complete standalone chunk.
|
|
124
|
+
// Do NOT carry it forward as overlap: buildOverlap would return the
|
|
125
|
+
// whole paragraph (it alone exceeds overlapTokens), leaving `current`
|
|
126
|
+
// at >= target — so the very next paragraph would immediately
|
|
127
|
+
// re-emit it as a byte-identical duplicate chunk AND glue the entire
|
|
128
|
+
// giant block onto the following chunk. Start fresh instead; normal
|
|
129
|
+
// (sub-target) paragraphs still get their tail overlap below.
|
|
130
|
+
current = [];
|
|
131
|
+
currentTokens = 0;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (currentTokens + paraTokens > opts.targetTokens && current.length > 0) {
|
|
136
|
+
pushChunk();
|
|
137
|
+
current = buildOverlap(current, opts.overlapTokens);
|
|
138
|
+
currentTokens = current.reduce((s, p) => s + estimateTokens(p.text), 0);
|
|
139
|
+
}
|
|
140
|
+
current.push(para);
|
|
141
|
+
currentTokens += paraTokens;
|
|
142
|
+
}
|
|
143
|
+
pushChunk();
|
|
144
|
+
return chunks;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function buildOverlap(prev: Paragraph[], overlapTokens: number): Paragraph[] {
|
|
148
|
+
if (overlapTokens <= 0) return [];
|
|
149
|
+
const out: Paragraph[] = [];
|
|
150
|
+
let acc = 0;
|
|
151
|
+
for (let i = prev.length - 1; i >= 0; i--) {
|
|
152
|
+
const p = prev[i]!;
|
|
153
|
+
out.unshift(p);
|
|
154
|
+
acc += estimateTokens(p.text);
|
|
155
|
+
if (acc >= overlapTokens) break;
|
|
156
|
+
}
|
|
157
|
+
// The overlap must be a STRICT suffix of the previous chunk. When the
|
|
158
|
+
// previous chunk is shorter than overlapTokens (a lone heading, a short
|
|
159
|
+
// paragraph before a big one), the loop above hands back the whole
|
|
160
|
+
// chunk — and the next chunk then starts at the same line and CONTAINS
|
|
161
|
+
// the previous one. Observed live 2026-09-01: a 96-line wiki page
|
|
162
|
+
// indexed as 17-18 ⊂ 17-33 and 35-60 ⊂ 35-68, and both halves of each
|
|
163
|
+
// pair matched the same query, so the same text went into the prompt
|
|
164
|
+
// twice. Dropping the first paragraph keeps the next chunk's start
|
|
165
|
+
// strictly later; a single-paragraph chunk carries no overlap at all.
|
|
166
|
+
if (out.length === prev.length) out.shift();
|
|
167
|
+
return out;
|
|
168
|
+
}
|