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,266 @@
|
|
|
1
|
+
// Build engine-specific user-message content shapes from resolved
|
|
2
|
+
// user-attachments. Each engine has its own native shape:
|
|
3
|
+
// - claude-cli (Anthropic SDK): MessageParam.content as
|
|
4
|
+
// ContentBlockParam[] with image / document / text blocks
|
|
5
|
+
// - openai-compatible: chat-completion content parts (text +
|
|
6
|
+
// image_url + file)
|
|
7
|
+
// - codex-cli: --image flag list + text-only stdin (PDFs are
|
|
8
|
+
// rasterised; text attachments are inlined as a header block)
|
|
9
|
+
//
|
|
10
|
+
// Shared invariant: attachment blocks come BEFORE the text block
|
|
11
|
+
// (natural reading order, matches Anthropic's recommendation). The
|
|
12
|
+
// text block carries replay-prefix + memory + user-typed text exactly
|
|
13
|
+
// like today — Phase Y.B is purely additive on top of that.
|
|
14
|
+
|
|
15
|
+
import { readFileSync } from 'node:fs';
|
|
16
|
+
import { renderPdfToPngs } from './pdf-render.ts';
|
|
17
|
+
import type { ResolvedAttachment } from '../engine/types.ts';
|
|
18
|
+
import type { ModelCapability } from '../config/types.ts';
|
|
19
|
+
import type { ContentBlockParam } from '@anthropic-ai/sdk/resources/messages/messages.js';
|
|
20
|
+
import { logger } from '../server/logger.ts';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* claude-cli (Anthropic SDK). PDFs ride as native DocumentBlock —
|
|
24
|
+
* Anthropic supports inline PDF up to 32 MB / 100 pages, and the
|
|
25
|
+
* SDK type system already exposes ImageBlockParam +
|
|
26
|
+
* DocumentBlockParam. Text attachments become a TextBlock with a
|
|
27
|
+
* `[Attached <name>:\n…\n]` header.
|
|
28
|
+
*/
|
|
29
|
+
export function buildAnthropicUserContent(
|
|
30
|
+
text: string,
|
|
31
|
+
attachments: ResolvedAttachment[],
|
|
32
|
+
): string | ContentBlockParam[] {
|
|
33
|
+
if (attachments.length === 0) return text;
|
|
34
|
+
const blocks: ContentBlockParam[] = [];
|
|
35
|
+
for (const a of attachments) {
|
|
36
|
+
const bytes = readFileSync(a.path);
|
|
37
|
+
if (a.mime.kind === 'image') {
|
|
38
|
+
blocks.push({
|
|
39
|
+
type: 'image',
|
|
40
|
+
source: {
|
|
41
|
+
type: 'base64',
|
|
42
|
+
media_type: a.mime.mimeType as 'image/png' | 'image/jpeg' | 'image/gif' | 'image/webp',
|
|
43
|
+
data: bytes.toString('base64'),
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
} else if (a.mime.kind === 'pdf') {
|
|
47
|
+
blocks.push({
|
|
48
|
+
type: 'document',
|
|
49
|
+
source: {
|
|
50
|
+
type: 'base64',
|
|
51
|
+
media_type: 'application/pdf',
|
|
52
|
+
data: bytes.toString('base64'),
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
} else if (a.mime.kind === 'text') {
|
|
56
|
+
blocks.push({
|
|
57
|
+
type: 'text',
|
|
58
|
+
text: `[Attached ${a.name}]\n\n${bytes.toString('utf8')}\n[/Attached]`,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
blocks.push({ type: 'text', text });
|
|
63
|
+
return blocks;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** OpenAI chat-completion content-part shape for the `messages` array.
|
|
67
|
+
* Used by the openai-compatible adapter for both the latest user
|
|
68
|
+
* turn AND replayed history user messages. */
|
|
69
|
+
export type OpenAiContentPart =
|
|
70
|
+
| { type: 'text'; text: string }
|
|
71
|
+
| { type: 'image_url'; image_url: { url: string } }
|
|
72
|
+
| { type: 'file'; file: { file_data: string; filename?: string } };
|
|
73
|
+
|
|
74
|
+
/** Size for the not-shown markers. Rounded — this is orientation for a
|
|
75
|
+
* language model, not an accounting figure. */
|
|
76
|
+
function humanSize(bytes: number): string {
|
|
77
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
78
|
+
if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`;
|
|
79
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Stand-in for an attachment the active model cannot be shown. Names
|
|
84
|
+
* the file, its type and its size so the model can talk about what it
|
|
85
|
+
* is missing ("the screenshot you sent earlier") instead of behaving as
|
|
86
|
+
* though the turn never had an attachment at all.
|
|
87
|
+
*
|
|
88
|
+
* English on purpose — every model-facing and user-facing string in
|
|
89
|
+
* somora is.
|
|
90
|
+
*/
|
|
91
|
+
function notShownMarker(a: ResolvedAttachment, kind: 'image' | 'pdf'): string {
|
|
92
|
+
const needed = kind === 'image' ? "'image'" : "'pdf' or 'image'";
|
|
93
|
+
const what = kind === 'image' ? 'Image' : 'PDF';
|
|
94
|
+
return (
|
|
95
|
+
`[${what} attachment "${a.name}" (${a.mime.mimeType}, ${humanSize(a.size)}) ` +
|
|
96
|
+
`— not shown: the active model has no ${needed} capability. ` +
|
|
97
|
+
`It can be described with analyze_file({path:"${a.path}"}).]`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* openai-compatible. PDF behaviour depends on the provider config:
|
|
103
|
+
* - 'native': PDF → file content block (Anthropic via OpenRouter,
|
|
104
|
+
* OpenAI direct accept this). Single block per PDF.
|
|
105
|
+
* - 'rasterize' (default): PDF → page-PNGs via pdf-to-img → one
|
|
106
|
+
* image_url per page. Works against omlx, ollama, anything
|
|
107
|
+
* image-capable.
|
|
108
|
+
* Text attachments inline via a header text part.
|
|
109
|
+
*
|
|
110
|
+
* `caps` are the ACTIVE model's capabilities. Attachments it cannot
|
|
111
|
+
* process degrade to a text marker rather than being sent and
|
|
112
|
+
* rejected — see the note in the image branch.
|
|
113
|
+
*/
|
|
114
|
+
export async function buildOpenAiUserContent(
|
|
115
|
+
text: string,
|
|
116
|
+
attachments: ResolvedAttachment[],
|
|
117
|
+
pdfMode: 'native' | 'rasterize',
|
|
118
|
+
caps: readonly ModelCapability[],
|
|
119
|
+
): Promise<string | OpenAiContentPart[]> {
|
|
120
|
+
if (attachments.length === 0) return text;
|
|
121
|
+
const parts: OpenAiContentPart[] = [];
|
|
122
|
+
const canSeeImages = caps.includes('image');
|
|
123
|
+
const canReadPdf = caps.includes('pdf');
|
|
124
|
+
for (const a of attachments) {
|
|
125
|
+
if (a.mime.kind === 'image') {
|
|
126
|
+
// Capability-aware: a model without vision must not be handed an
|
|
127
|
+
// image_url block. run-turn refuses a NEW image attachment for
|
|
128
|
+
// such a model, but history is replayed on every turn — once a
|
|
129
|
+
// session has gone multimodal, every later turn re-sends those
|
|
130
|
+
// blocks and a text-only model 400s on all of them. Degrade to a
|
|
131
|
+
// marker so the session stays usable instead: the model is told
|
|
132
|
+
// what was there and that it cannot see it.
|
|
133
|
+
if (!canSeeImages) {
|
|
134
|
+
parts.push({ type: 'text', text: notShownMarker(a, 'image') });
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
const bytes = readFileSync(a.path);
|
|
138
|
+
parts.push({
|
|
139
|
+
type: 'image_url',
|
|
140
|
+
image_url: {
|
|
141
|
+
url: `data:${a.mime.mimeType};base64,${bytes.toString('base64')}`,
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
} else if (a.mime.kind === 'pdf') {
|
|
145
|
+
// A PDF rides either as a native document block (needs 'pdf') or
|
|
146
|
+
// as rasterised page-PNGs (needs 'image'). Neither ⇒ marker.
|
|
147
|
+
if (!canReadPdf && !canSeeImages) {
|
|
148
|
+
parts.push({ type: 'text', text: notShownMarker(a, 'pdf') });
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
// `native` on a model that has 'image' but not 'pdf' would send a
|
|
152
|
+
// file block the backend rejects for the same reason as above.
|
|
153
|
+
// Rasterise instead of failing — the pages are readable either way.
|
|
154
|
+
if (pdfMode === 'native' && canReadPdf) {
|
|
155
|
+
const bytes = readFileSync(a.path);
|
|
156
|
+
parts.push({
|
|
157
|
+
type: 'file',
|
|
158
|
+
file: {
|
|
159
|
+
file_data: `data:application/pdf;base64,${bytes.toString('base64')}`,
|
|
160
|
+
filename: a.name,
|
|
161
|
+
},
|
|
162
|
+
});
|
|
163
|
+
} else {
|
|
164
|
+
const result = await renderPdfToPngs(a.path);
|
|
165
|
+
for (const pageBase64 of result.pages) {
|
|
166
|
+
parts.push({
|
|
167
|
+
type: 'image_url',
|
|
168
|
+
image_url: {
|
|
169
|
+
url: `data:image/png;base64,${pageBase64}`,
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
} else if (a.mime.kind === 'text') {
|
|
175
|
+
const bytes = readFileSync(a.path);
|
|
176
|
+
parts.push({
|
|
177
|
+
type: 'text',
|
|
178
|
+
text: `[Attached ${a.name}]\n\n${bytes.toString('utf8')}\n[/Attached]`,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
parts.push({ type: 'text', text });
|
|
183
|
+
// Everything degraded to text ⇒ hand back a plain string. A text-only
|
|
184
|
+
// backend then sees exactly the message shape it saw before the
|
|
185
|
+
// session ever went multimodal, rather than a content array it may
|
|
186
|
+
// handle differently (some local servers are picky about those).
|
|
187
|
+
if (parts.every((p) => p.type === 'text')) {
|
|
188
|
+
return parts.map((p) => (p as { text: string }).text).join('\n\n');
|
|
189
|
+
}
|
|
190
|
+
return parts;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* codex-cli. Its `exec --image <PATH>` accepts only image files (PNG/
|
|
195
|
+
* JPEG/GIF/WebP). PDFs get rasterised to per-page PNGs and the page
|
|
196
|
+
* paths feed --image instead. Text attachments inline into the
|
|
197
|
+
* stdin prompt as a `[Attached <name>: …]` block — codex doesn't
|
|
198
|
+
* have a native non-image attachment surface.
|
|
199
|
+
*
|
|
200
|
+
* Returns:
|
|
201
|
+
* imagePaths — one absolute path per --image flag the caller pushes
|
|
202
|
+
* promptPrefix — a text block to prepend to the stdin prompt;
|
|
203
|
+
* empty when there are no text attachments
|
|
204
|
+
*/
|
|
205
|
+
export async function buildCodexAttachments(
|
|
206
|
+
attachments: ResolvedAttachment[],
|
|
207
|
+
): Promise<{ imagePaths: string[]; promptPrefix: string }> {
|
|
208
|
+
if (attachments.length === 0) return { imagePaths: [], promptPrefix: '' };
|
|
209
|
+
const imagePaths: string[] = [];
|
|
210
|
+
let promptPrefix = '';
|
|
211
|
+
for (const a of attachments) {
|
|
212
|
+
if (a.mime.kind === 'image') {
|
|
213
|
+
imagePaths.push(a.path);
|
|
214
|
+
} else if (a.mime.kind === 'pdf') {
|
|
215
|
+
// codex's --image accepts only images, so we render the PDF
|
|
216
|
+
// pages out to PNGs first. The temp PNGs live next to the PDF
|
|
217
|
+
// under a content-addressed cache so repeat sends of the same
|
|
218
|
+
// PDF (same hash) reuse the rendered pages.
|
|
219
|
+
try {
|
|
220
|
+
const pages = await renderPdfToPngsToDisk(a);
|
|
221
|
+
for (const p of pages) imagePaths.push(p);
|
|
222
|
+
} catch (err) {
|
|
223
|
+
logger.warn({
|
|
224
|
+
msg: 'attachments.codex.pdf_rasterize_failed',
|
|
225
|
+
name: a.name,
|
|
226
|
+
err: (err as Error).message,
|
|
227
|
+
});
|
|
228
|
+
// Don't kill the turn over a failed rasterise — surface as a
|
|
229
|
+
// text marker so the model knows what was attempted.
|
|
230
|
+
promptPrefix += `[Attached ${a.name} (PDF) — failed to rasterise]\n\n`;
|
|
231
|
+
}
|
|
232
|
+
} else if (a.mime.kind === 'text') {
|
|
233
|
+
const bytes = readFileSync(a.path);
|
|
234
|
+
promptPrefix += `[Attached ${a.name}]\n\n${bytes.toString('utf8')}\n[/Attached]\n\n`;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return { imagePaths, promptPrefix };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Render a PDF attachment's pages into PNG files on disk under a
|
|
242
|
+
* content-addressed cache, returning the page paths in order. Used
|
|
243
|
+
* for codex-cli where `--image` needs file paths (not bytes). The
|
|
244
|
+
* cache shares the attachment hash, so repeat turns referencing the
|
|
245
|
+
* same PDF re-use the existing pages without re-rendering.
|
|
246
|
+
*/
|
|
247
|
+
async function renderPdfToPngsToDisk(a: ResolvedAttachment): Promise<string[]> {
|
|
248
|
+
const { join, dirname } = await import('node:path');
|
|
249
|
+
const { existsSync, mkdirSync, writeFileSync, readdirSync } = await import('node:fs');
|
|
250
|
+
const cacheDir = join(dirname(a.path), `${a.hash}.pdf-pages`);
|
|
251
|
+
if (existsSync(cacheDir)) {
|
|
252
|
+
const files = readdirSync(cacheDir)
|
|
253
|
+
.filter((f) => f.endsWith('.png'))
|
|
254
|
+
.sort();
|
|
255
|
+
if (files.length > 0) return files.map((f) => join(cacheDir, f));
|
|
256
|
+
}
|
|
257
|
+
mkdirSync(cacheDir, { recursive: true });
|
|
258
|
+
const result = await renderPdfToPngs(a.path);
|
|
259
|
+
const out: string[] = [];
|
|
260
|
+
result.pages.forEach((b64, i) => {
|
|
261
|
+
const p = join(cacheDir, `page-${String(i + 1).padStart(4, '0')}.png`);
|
|
262
|
+
writeFileSync(p, Buffer.from(b64, 'base64'));
|
|
263
|
+
out.push(p);
|
|
264
|
+
});
|
|
265
|
+
return out;
|
|
266
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// Persona files for the web Agent window: read AGENTS.md / SOUL.md /
|
|
2
|
+
// USER.md (+ agent.yaml read-only) with content hashes and sizes, and
|
|
3
|
+
// write one of the editable three under an optimistic lock — the
|
|
4
|
+
// agents edit these files themselves (self-edit), so a human save must
|
|
5
|
+
// never silently overwrite what an agent wrote in between. Same
|
|
6
|
+
// backup discipline as team.yaml (last five kept).
|
|
7
|
+
|
|
8
|
+
import { createHash } from 'node:crypto';
|
|
9
|
+
import { existsSync, readdirSync, unlinkSync } from 'node:fs';
|
|
10
|
+
import { copyFile, readFile, rename, stat, writeFile } from 'node:fs/promises';
|
|
11
|
+
import { homedir } from 'node:os';
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
import { validateAgentsMd } from './loader.ts';
|
|
14
|
+
|
|
15
|
+
const SOMORA_HOME = process.env.SOMORA_HOME ?? join(homedir(), '.somora');
|
|
16
|
+
const VALID_NAME = /^[A-Za-z0-9_-][A-Za-z0-9_.-]*$/;
|
|
17
|
+
const KEEP_BACKUPS = 5;
|
|
18
|
+
|
|
19
|
+
export const PERSONA_EDITABLE_FILES = ['AGENTS.md', 'SOUL.md', 'USER.md'] as const;
|
|
20
|
+
export type PersonaEditableFile = (typeof PERSONA_EDITABLE_FILES)[number];
|
|
21
|
+
export const PERSONA_FILES = [...PERSONA_EDITABLE_FILES, 'agent.yaml'] as const;
|
|
22
|
+
export type PersonaFileName = (typeof PERSONA_FILES)[number];
|
|
23
|
+
|
|
24
|
+
export interface PersonaFileInfo {
|
|
25
|
+
name: PersonaFileName;
|
|
26
|
+
exists: boolean;
|
|
27
|
+
content: string;
|
|
28
|
+
/** sha1 of the content on disk; the write side's optimistic lock. */
|
|
29
|
+
hash: string;
|
|
30
|
+
chars: number;
|
|
31
|
+
bytes: number;
|
|
32
|
+
mtime: string | null;
|
|
33
|
+
readOnly: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const hashOf = (s: string): string => createHash('sha1').update(s).digest('hex').slice(0, 16);
|
|
37
|
+
|
|
38
|
+
export function agentDir(agent: string): string {
|
|
39
|
+
if (!VALID_NAME.test(agent)) throw new Error(`invalid agent name '${agent}'`);
|
|
40
|
+
return join(SOMORA_HOME, 'agents', agent);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function readPersonaFiles(agent: string): Promise<PersonaFileInfo[]> {
|
|
44
|
+
const dir = agentDir(agent);
|
|
45
|
+
const out: PersonaFileInfo[] = [];
|
|
46
|
+
for (const name of PERSONA_FILES) {
|
|
47
|
+
const path = join(dir, name);
|
|
48
|
+
let content = '';
|
|
49
|
+
let exists = false;
|
|
50
|
+
let mtime: string | null = null;
|
|
51
|
+
try {
|
|
52
|
+
content = await readFile(path, 'utf8');
|
|
53
|
+
exists = true;
|
|
54
|
+
mtime = (await stat(path)).mtime.toISOString();
|
|
55
|
+
} catch (err) {
|
|
56
|
+
if ((err as NodeJS.ErrnoException).code !== 'ENOENT') throw err;
|
|
57
|
+
}
|
|
58
|
+
out.push({
|
|
59
|
+
name,
|
|
60
|
+
exists,
|
|
61
|
+
content,
|
|
62
|
+
hash: hashOf(content),
|
|
63
|
+
chars: content.length,
|
|
64
|
+
bytes: Buffer.byteLength(content, 'utf8'),
|
|
65
|
+
mtime,
|
|
66
|
+
readOnly: name === 'agent.yaml',
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type PersonaWriteResult =
|
|
73
|
+
| { ok: true; hash: string; backup: string | null }
|
|
74
|
+
| { ok: false; status: 400 | 404 | 409; error: string; currentHash?: string; currentContent?: string };
|
|
75
|
+
|
|
76
|
+
function rotateBackups(dir: string, name: string): void {
|
|
77
|
+
const baks = readdirSync(dir)
|
|
78
|
+
.filter((f) => f.startsWith(`${name}.bak-`))
|
|
79
|
+
.sort();
|
|
80
|
+
while (baks.length > KEEP_BACKUPS) {
|
|
81
|
+
const victim = baks.shift()!;
|
|
82
|
+
try {
|
|
83
|
+
unlinkSync(join(dir, victim));
|
|
84
|
+
} catch {
|
|
85
|
+
/* best effort */
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const ts = (): string =>
|
|
91
|
+
new Date().toISOString().replace(/[-:]/g, '').replace(/\..+$/, '').replace('T', '-');
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Write one editable persona file. `baseHash` must equal the hash of the
|
|
95
|
+
* content currently on disk (as served by readPersonaFiles); otherwise
|
|
96
|
+
* the agent (or another client) changed it meanwhile → 409 with the
|
|
97
|
+
* current content so the editor can reload. AGENTS.md must keep a
|
|
98
|
+
* parseable frontmatter whose `name` (if set) matches the directory —
|
|
99
|
+
* a persona that fails to load would take the agent offline.
|
|
100
|
+
*/
|
|
101
|
+
export async function writePersonaFile(
|
|
102
|
+
agent: string,
|
|
103
|
+
name: string,
|
|
104
|
+
content: string,
|
|
105
|
+
baseHash: string,
|
|
106
|
+
): Promise<PersonaWriteResult> {
|
|
107
|
+
if (!(PERSONA_EDITABLE_FILES as readonly string[]).includes(name)) {
|
|
108
|
+
return { ok: false, status: 400, error: `'${name}' is not editable here (AGENTS.md, SOUL.md, USER.md)` };
|
|
109
|
+
}
|
|
110
|
+
const dir = agentDir(agent);
|
|
111
|
+
if (!existsSync(join(dir, 'AGENTS.md'))) return { ok: false, status: 404, error: `agent '${agent}' not found` };
|
|
112
|
+
const path = join(dir, name);
|
|
113
|
+
let current = '';
|
|
114
|
+
try {
|
|
115
|
+
current = await readFile(path, 'utf8');
|
|
116
|
+
} catch (err) {
|
|
117
|
+
if ((err as NodeJS.ErrnoException).code !== 'ENOENT') throw err;
|
|
118
|
+
}
|
|
119
|
+
const currentHash = hashOf(current);
|
|
120
|
+
if (currentHash !== baseHash) {
|
|
121
|
+
return {
|
|
122
|
+
ok: false,
|
|
123
|
+
status: 409,
|
|
124
|
+
error: `${name} changed on disk since you loaded it (the agent may have edited it) — reload and re-apply your change`,
|
|
125
|
+
currentHash,
|
|
126
|
+
currentContent: current,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
if (name === 'AGENTS.md') {
|
|
130
|
+
if (content.trim().length === 0) return { ok: false, status: 400, error: 'AGENTS.md cannot be empty' };
|
|
131
|
+
const v = validateAgentsMd(content, agent);
|
|
132
|
+
if (!v.ok) return { ok: false, status: 400, error: v.error };
|
|
133
|
+
}
|
|
134
|
+
let backup: string | null = null;
|
|
135
|
+
if (existsSync(path)) {
|
|
136
|
+
backup = `${path}.bak-${ts()}`;
|
|
137
|
+
await copyFile(path, backup);
|
|
138
|
+
rotateBackups(dir, name);
|
|
139
|
+
}
|
|
140
|
+
const tmp = `${path}.tmp-${process.pid}`;
|
|
141
|
+
await writeFile(tmp, content, 'utf8');
|
|
142
|
+
await rename(tmp, path);
|
|
143
|
+
return { ok: true, hash: hashOf(content), backup };
|
|
144
|
+
}
|