Compare commits

...
8 Commits
Author SHA1 Message Date
tbenandClaude Opus 5.5 d8193d08b7 Release v0.7.0
CI / check (push) Canceled after 0s
Release image / image (release) Canceled after 0s
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-28 04:50:00 +01:00
tbenandClaude Opus 5.5 ba9705980e Keep logs for a set number of days
Logs grew forever. A daily prune-logs cron now deletes entries older
than the app setting logs_keep_days (Settings > General, super admin;
default AppData.LogsKeepDays, 90), with an index on logs.created_at.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-28 04:49:25 +01:00
tbenandClaude Opus 5.5 5f96004f13 Search files as the user types
The Files page and the conversation's files modal searched only on
Enter. Both now search by name or path a moment after the last
keystroke (useDebouncedValue, AppData.SearchDebounceMs), clearing shows
the whole list at once, and the modal drops an answer a newer request
overtook. The Files search box no longer locks while a folder loads.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-28 04:46:39 +01:00
tbenandClaude Opus 5.5 ac3ebc43d4 Keep searchable text only in knowledgebases
Every bot file was chunked into bot_file_chunks and embedded into
bot_file_vectors, and uploads kept their extracted text in
media.text_content, which together took most of the database. Bot
files are no longer indexed: bots find them by name or path with
list_files and read them with read_file, and search_files, ingest_file
and the files modal's re-read button are gone. Attachments keep no
text; a reply reads a document from the file. Only knowledgebase
documents keep text.

The schema drops both tables and the ingest columns. At boot,
dropStoredFileText removes what older versions stored (file jobs,
upload text) and vacuums once much of the database is free. The
media:prune-text script it replaces is removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-28 04:44:46 +01:00
tbenandClaude Opus 5.5 2896e76256 Stop computer bash commands on the computer itself
The sandbox ignores sandbox_execute_bash's timeout and runs every call in
one shared shell session, so a command that never ended (a network read
without a timeout, a prompt) kept running after the call gave up, and
every later command on that computer, from any run, timed out behind it.
Each command now runs in its own bash under coreutils timeout (at most
ComputerCommandMaxSeconds, below McpToolTimeoutMs), reading no input, and
says when it was stopped.

Replies' env list says where each key comes from and that workspace,
computer and install-wide envs reach every bot there, so bots stop
telling users a workspace's keys must be set per bot. The computer
prompt says installed programs may be gone after a restart. An empty
open-only task list says closed tasks aren't shown, and update_task says
when a status was already set.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-28 04:34:39 +01:00
tbenandClaude Opus 5.5 e51f2aa9a4 Catch up on replies missed while the app was in the background
The reply catch-up read the page's stale face, so after an interim
message it stopped while the thinking row stayed up. It now reads the
live face, checks at once when the page comes back into view or online,
and replays the bot's face and task too, so a reply that ended silently
can't leave the bot working on screen.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-28 04:14:13 +01:00
tbenandClaude Opus 5.5 4061a317bc Release v0.6.2
CI / check (push) Canceled after 0s
Release image / image (release) Canceled after 0s
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-27 19:42:10 +01:00
tbenandClaude Opus 5.5 a821013514 Clear finished jobs' steps a minute after they end
Job steps (bots_queue_updates) only feed the live working row, so the
new clear-job-steps cron deletes them shortly after a job ends, however
it ended, keeping their count on bots_queue.steps for the message
details. Not done the moment a job ends: the live relay would miss the
last ones.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-27 19:41:40 +01:00
84 changed files with 1039 additions and 1791 deletions
+10 -7
View File
@@ -60,7 +60,7 @@ Enables SQLite foreign keys (`enableForeignKeys`), `bunextInit()`, `ensureDocker
- **Passwords:** bcrypt in `user_passwords` (never in `users`; blocked from CRUD via `src/dict/private-tables.ts`).
- **User types** (`src/dict/user-types-dict.ts`, the only definition): `super_admin` (user #1), `admin` (only the super admin grants it), `member` (default), `read_only` (non-GET CRUD blocked), `revoked`. A user needs at least one type to sign in.
- **RBAC:** `src/utils/check-user-access.ts` plus the matrix engine in `src/utils/rbac/` (`userTypeAssignment` over `UserTypeAssignmentMatrix`/`UserTypeDeletionMatrix`).
- **Audit:** `src/functions/backend/logger.ts` (`logEvent`/`logError` -> `logs`). Auth events, cron runs, every unexpected API error (`safeErrorMessage` -> `logUnexpectedError`, not for `AppError`), every failed or lost bot job. WebSocket connects are not logged.
- **Audit:** `src/functions/backend/logger.ts` (`logEvent`/`logError` -> `logs`). Auth events, cron runs, every unexpected API error (`safeErrorMessage` -> `logUnexpectedError`, not for `AppError`), every failed or lost bot job. WebSocket connects are not logged. Kept for the app setting `logs_keep_days` (default `AppData.LogsKeepDays`), deleted by the daily `prune-logs`.
### WebSocket
@@ -68,7 +68,7 @@ Enables SQLite foreign keys (`enableForeignKeys`), `bunextInit()`, `ensureDocker
### Cron
`src/cron/index.ts` registers everything via `registerCronJob` (`src/cron/register-cron-job.ts`); one folder per task. In-process `Bun.cron`, UTC, never overlapping. Notable: `heartbeat`, `sync-harness-models` (hourly), `run-cron-jobs` (every minute, fires schedules), `sync-computers`, `sync-mcp-servers`, `prune-bots-queue`, `prune-chatrooms`.
`src/cron/index.ts` registers everything via `registerCronJob` (`src/cron/register-cron-job.ts`); one folder per task. In-process `Bun.cron`, UTC, never overlapping. Notable: `heartbeat`, `sync-harness-models` (hourly), `run-cron-jobs` (every minute, fires schedules), `sync-computers`, `sync-mcp-servers`, `clear-job-steps`, `prune-bots-queue`, `prune-chatrooms`, `prune-logs`.
### Admin CRUD API
@@ -91,13 +91,13 @@ Enables SQLite foreign keys (`enableForeignKeys`), `bunextInit()`, `ensureDocker
- **Sessions:** one thread per user per bot in its own chat plus one per workspace; split into `bot_sessions` (rollover at the app setting `bot_session_max_messages`, default `AppData.BotSessionMaxMessages`, via `grabBotSessionMaxMessages`; titled and summarised by `name-bot-session.ts` from the session's opening (`grabSessionOpening`; `grabBotMessages` reads the latest), summary carried into the next session only when they're linked by `previous_session_id`, which only a carry-over sets; older implicit links are cleared at boot by `clearImplicitSessionLinks`). New session (`startBotSession`) and a conversation with no active session always start fresh. The rollover carries over like the bot does (`rollOverBotSession` -> `continueBotSession`, falling back to the stored summary; kept for the next message while a job is open in the full session or there's nothing to carry), inside send-message, so the user's message lands under the summary. Helpers in `src/functions/backend/db/bot-sessions/`. Job results land in the job's own session, even after it closes, except a reply that called `continue_in_new_session` (`carryOverBotSession`; the menu's and sessions modal's Continue in new session does the same through `continueBotSession`, the model writing title and summary, refused while a job is open in the session): its summary is kept on the closed session and opens the new one as a bot message (`carried_from_session_id`), the job's `session_id` moves so the answer follows, the new session is titled by the bot then (it has no first exchange to be named from), and the old one can never be reopened (`reopenBotSession` refuses; a thread showing it swaps its composer for a link to the new one). Session lists (rail, sessions modal) reopen a session on click, as before; a carried-over one can't be, so it opens to read (`?session_id=`, `conversationHref`). The page tells them which session it shows (`announceShownSession` -> `useShownSession`): they reload, mark it Current (greyed out; without `?session_id=` the active one is), list it even when it's older than the rail's latest, and the active row then leads back. From half the session length the reply prompt suggests moving on (`longSessionNote`). Earlier sessions never enter the prompt (only the last one's summary): the thread can open them above a session (`useOlderMessages.loadPrevious`, a divider per session, quote and reply as usual), and replies get `search_past_sessions`/`read_past_session` (scoped to that user, bot and place, never the current session) when the conversation has any.
- **Coordinator:** exactly one, the super admin's alone, created idempotently by `ensureCoordinatorBot`. Default prompt `src/data/coordinator-system-prompt.ts`, backfilled only when empty.
- **Routes:** `/admin/bots` reopens the last visited bot (cookie `cobble_last_visited_bot_id`, never a workspace conversation) if still reachable, else coordinator (super admin) / latest bot / first workspace / empty. `/admin/bots/[bot_id]` conversation (header, thread and composer at 760px, no right panel; `?session_id=` opens another of the user's sessions there: a closed one is reopened, except a carried-over one, which shows with `SessionMovedBar` instead of the composer, pointing where the conversation went on); `/admin/bots/[bot_id]/models`; `/admin/workspaces/[id]` dashboard; `/admin/workspaces/[id]/chatrooms` that workspace's chatrooms; `/admin/workspaces/[id]/bots/[bot_id]` same conversation scoped to the workspace.
- **Conversation loading:** pages render everything server-side (`grabConversationProps`) **except the history**, which the client loads (`api/admin/bots/conversation-history`, merged with what's already shown, refetched on bfcache restore) and pages older messages on scroll. Rail lists go through `railBots`/`railWorkspaces` (no prompts/memory, previews cut). While a reply is due the page polls `session-messages` (`use-reply-catch-up`) in case a push was missed.
- **Conversation loading:** pages render everything server-side (`grabConversationProps`) **except the history**, which the client loads (`api/admin/bots/conversation-history`, merged with what's already shown, refetched on bfcache restore) and pages older messages on scroll. Rail lists go through `railBots`/`railWorkspaces` (no prompts/memory, previews cut). While a reply is due (read with the live face, never the page's) the page polls `session-messages` (`use-reply-catch-up`, also at once when the page comes back into view or online) in case a push was missed, replaying the messages and the bot's face and task as their pushes.
- **Sending:** `api/admin/bots/send-message.ts` -> `enqueueBotReply` -> `wakeBotsQueue`. **Never import `src/bots-queue/index.ts` from a page or API route** (pulls the bunext runtime into page bundles). Optimistic bubble in `pending_messages` until the saved message replaces it.
- **Composer:** Tiptap editor reading/writing markdown, drafts in `localStorage`, `@` bots / `#` workspaces written as `[bot:ID]`/`[workspace:ID]`. Enter sends (Mod+Enter in lists/code). `@tiptap/react` needs `use-sync-external-store`; the app ships an ESM stand-in in `packages/use-sync-external-store-esm` (file dependency + override; the Dockerfile copies `packages/` before `bun install`).
- **Rendering:** markdown via `react-markdown` + `remark-gfm`, raw HTML never rendered, component map in `[bot_id]/(data)/message-markdown-components.tsx`. Inline refs, all linked by `link-computer-refs.ts` and rendered by chips in `(partials)/`: `[bot:ID]`, `[workspace:ID]`, `[computer:ID]`, `[file:ID]` (a saved file), `[folder:<volume>:<owner>:<path>]` (opens `/admin/files`), `[take-over:ID:mode]`, `[proposal:ID]`, `[hook-proposal:ID]`, `[secret:ID]` (masked env, Reveal/Copy), `[secret-request:ID]` (masked input card), `[graphic:ID]` (a bot graphic, framed inline), `[room:ID]` (a chatroom). Parsers in `src/utils/*-ref.ts`. A ref quoted alone in a code span still links; one inside longer code stays literal (`linkComputerRefs` splits out code first). A file chip opens a file from another bot's folder for a user who reaches that folder (`userReachesFileScope`, the same rule as its bytes), so answers passed on from chatrooms keep working.
- **Copy chat** (header menu): the active session (the one on screen) as one markdown document (`api/admin/bots/conversation-export` -> `conversationMarkdown`, refs as names, message headings demoted). The clipboard write starts on the click with a pending `ClipboardItem` (`copyPendingText`), since Safari drops the gesture after a fetch.
- **Replies/reactions:** `reply_to_message_id` (not an FK); reactions from the fixed `BotMessageReactions`, toggled by `react-message.ts` and the `react_to_message` tool, pushed as `server:bot-reaction`.
- **Attachments:** uploaded on pick (`upload-attachment.ts`, private `media`, text extracted when possible), linked via `bot_message_media`. Replies read files of the last `BotReplyAttachmentMessages` messages. Tasks don't get files yet.
- **Attachments:** uploaded on pick (`upload-attachment.ts`, private `media`, no text kept: a reply reads a document's text from the file), linked via `bot_message_media`. Replies read files of the last `BotReplyAttachmentMessages` messages. Tasks don't get files yet.
- **Thinking row / stop:** `bot-busy-state.ts` decides the row's line and whether Stop shows. Stop (`api/admin/bots/stop.ts`, `stop-bot-jobs`) fails every open job of that bot for the user, aborts the running handler (`job-abort-registry`) and drops late face updates.
- **Voice:** mic -> `api/admin/bots/transcribe.ts`; play -> `GET speak-message` streamed into `<audio>`. Nothing recorded is stored. Both disabled with a note when the harness has no model.
- **Pictures:** `image_media_id` (private media, `bot-image`/`workspace-image`), via `set-profile-image.ts`, the `set_picture` tool or `update_workspace`. Bytes are **copied**, never moved; only picture-paradigm rows are deleted on replace. Format kept by `imageOutputFormat`.
@@ -113,7 +113,7 @@ Enables SQLite foreign keys (`enableForeignKeys`), `bunextInit()`, `ensureDocker
- Kinds (`BotJobKinds`): `reply` (chat answer, no task row) and `task` (long work, usually on a computer). bun-sqlite `isQueue` table. Helpers in `src/functions/backend/db/bots-queue/`: `enqueueBotJob`, `claimBotJobs` (kind-filtered atomic claim), `updateBotJob` (false once lost/cancelled), `completeBotJob`, `failBotJob` (backoff or fatal), `deferBotJob` (all models resting: attempt refunded, face `asleep`), `cancelBotJob`, `stop-bot-jobs`.
- Handler `src/bots-queue/index.ts`: separate reply/task slots, heartbeat, per-attempt deadline (`AppData.BotJobAttemptTimeoutMs`), one task per computer and one reply per session (`createKeyLock`). Work in `run-bot-job.ts` -> `run-bot-reply.ts` / `run-bot-task.ts`. Throw `BotJobFatalError` for failures retrying can't fix.
- Every model turn is a step in `bots_queue_updates` (tool names, never arguments; `describe-job-step.ts`, labels in `JobStepToolLabels`), pushed as `server:bot-job-update`.
- Every model turn is a step in `bots_queue_updates` (tool names, never arguments; `describe-job-step.ts`, labels in `JobStepToolLabels`), pushed as `server:bot-job-update` by the live relay only. Steps feed the live working row alone: the `clear-job-steps` cron deletes them `AppData.BotJobStepsKeepMs` after the job ends, however it ended, keeping their count on `bots_queue.steps`. Never delete them the moment a job ends (the relay would miss the last ones).
- **A bot can't vouch for itself:** a run's tool calls (`tool_uses`, names and ok, never arguments) go to the wrap-up turn when steps run out and onto the result message as `bot_messages.tool_log`; history follows each recorded bot message with an `[App record: ...]` line (`botMessagesToHarness`), and copies of that line in an answer are stripped (`stripAppRecords`).
- **`start_task`/`delegate_task` jobs are runs, not tasks:** their results say `Run #<job id>` and that `list_tasks` doesn't show them (bots sent users to the tracker for them). `start_task` refuses a second open run in its session on the same computer unless `separate` (it would only wait behind it) and says how many runs are ahead on that computer.
- **Replies take in messages sent mid-run** (`incoming`, `IncomingMessagesNote`, redraft); separate requests are acknowledged with `send_message` (interim). A queued reply finishes silently only when a finished reply already read every user message (`hasUnansweredMessage`), never by who wrote last.
@@ -137,7 +137,8 @@ A worker reaches another worker **only through the coordinator**: `ask_crew` (ev
- **Browser profile:** `DATA_DIR/computer-browsers/<id>` mounted at `~/.cobble-browser`, which Chrome is pointed at (`BROWSER_EXTRA_ARGS=--user-data-dir=... --password-store=basic`), so logins, extensions and site permissions survive restarts and recreates. Never use the image's `~/.config/browser`: every boot overwrites its `Default/Preferences` with session-only cookies. Seed prefs (`computer-browser-preferences.ts`) are written only when missing; stale `Singleton*` links are removed before each provision. Bumping `AppData.ComputerWorkspaceLayout` recreates every computer once (`migrateComputerLayout`, skipped while a job runs on it). Default extensions (`ComputerBrowserExtensions`, `normal_installed`: can be turned off, not removed) come from a managed policy written as root into the running container (`syncComputerBrowserPolicy`, on provision and every `sync-computers` run), not a mount, so existing computers get new ones without a recreate; Chrome installs them live. Deleting a computer keeps its directory. Outside docker (dev) containers are reached by IP (`pickReachableAddress`).
- One main computer (`ensureMainComputer`), whose coordinator grant is locked (`isLockedComputerAccess`). With `MAIN_COMPUTER_URL` set (dev) it isn't provisioned. Only super admins create/delete/retry computers.
- A bot's effective computers (`grab-bot-computers`): the workspace's in a workspace; otherwise direct grants plus its workspaces' (coordinator: all); `bots.computer_id` first. Only a `running` (healthy) computer is reached (`grabComputerMcpUrl` `ready`, `computerNotReadyMessage`).
- **Folders (enforced in tools, not a Linux user):** a run works only in `bots/<bot>/users/<user>` (cwd, `HOME`), `bots/<bot>` and `workspaces/<workspace>` (`computerRunDirs`, `withComputerDirs`, `resolveComputerPath`; `sudo` refused).
- **Folders (enforced in tools, not a Linux user):** a run works only in `bots/<bot>/users/<user>` (cwd, `HOME`), `bots/<bot>` and `workspaces/<workspace>` (`computerRunDirs`, `withComputerDirs`, `resolveComputerPath`; the file tools' `sudo` refused; bash runs as `gem`, who has sudo, so packages can be installed, and they're lost on a restart).
- **Bash on a computer:** the sandbox ignores `sandbox_execute_bash`'s `timeout` and runs every call in one shared shell session, so a command that never ends blocked every later one on that computer, from any run, and Stop never killed it. `withCommandTimeout` runs each in its own `bash -c` under coreutils `timeout` (at most `AppData.ComputerCommandMaxSeconds`, below `McpToolTimeoutMs`), stdin from `/dev/null`. Never `exit` in a wrapper (it would kill the session shell).
- **Viewing:** `/computers/<id>/` proxy (`handleComputerProxy`, strips app cookies, bridges websockets). `ComputerView` modes: browser, terminal, code, desktop (noVNC; the only one with Chrome's toolbar). **Take-over:** `ask_user_to_take_over` writes `[take-over:ID:mode]`; "Hand back" sends a chat message; tasks stop and report rather than wait.
### Schedules (routines in the UI)
@@ -176,7 +177,7 @@ Set in Settings > Environment (super admin), not `.env`. `envs` (private; encryp
- Files live on the data volume, not on computers: `DATA_DIR/bots/<id>` and `DATA_DIR/workspaces/<id>`. Catalog `bot_files`, bytes in private `media` (`bot-file`) written by `recordBotFile`. Reach via `grabStorageScopes`; paths via `resolveBotPath` (no absolute, `..`, NUL, symlink escape); quota checked by `assertStorageRoom`.
- Tools in `src/harness/tools/storage/` (every run). `attach` puts a file on the reply. `grabConversationFiles` must list the **whole** folder tree, or nested `[file:ID]` refs render dead. A workspace run saves to the bot's own folder unless it picks the workspace's, so a chip in a workspace conversation looks up both (`api/admin/bots/files?file_id=` with `bot_id` and `workspace_id`).
- `import_file` copies from a computer through the sandbox's streaming `GET /v1/file/download`, **never through its shell** (`/v1/shell/exec` caps output at ~30K chars and silently truncated imports). Size read first, `change_policy=abort`, length checked.
- Ingestion: chunks + embeddings into `bot_file_chunks` / `bot_file_vectors` (vec0), worked by `src/file-ingest/`; without an embedding model search falls back to words and says so. sqlite-vec limits: metadata columns are NOT NULL; side-car columns can't be filtered with `MATCH` (filter in code). **Never pass `--vector` to `bunx bun-sqlite schema` in `entrypoint.sh`** (wipes vectors on boot). Changing `AppData.EmbeddingDimensions` needs a manual `--vector` run and a re-ingest.
- **Only knowledgebases store text.** Bot files and uploads are never indexed and keep no extracted text (`media.text_content` only for knowledgebase documents); bots find files by name or path (`list_files` `search`) and read them with `read_file`, and the Files searches match names only. Text that should be searchable goes in a knowledgebase. `dropStoredFileText` clears what older versions stored, at boot, and vacuums when much of the file is free.
- **Foreign keys are off per connection** unless `enableForeignKeys` ran (only `src/server.ts` does). Scripts never fire `ON DELETE CASCADE`, so **any helper that deletes a parent row must delete its children explicitly** (as `deleteBotFile`, `purgeStorage`, `deleteSkill` do). A leftover child fails the next `bun run db:schema` with a foreign key violation.
### Graphics (`bot_graphics`)
@@ -187,6 +188,8 @@ A bot shows a concept as a visual with `create_graphic` (plus `update_graphic`/`
A fourth extension kind (`ExtensionTables.knowledgebase`): `knowledgebases` given per bot (`bot_knowledgebases`), per workspace (`workspace_knowledgebases`) or every bot (`all_bots`), through the same add/remove/access/default paths as skills (coordinator extension tools leave them out). Documents (`knowledgebase_documents`: an upload in private `media`, paradigm `knowledgebase-document`, or written `content`) are chunked into `knowledgebase_chunks` and embedded into the `knowledgebase_vectors` vec0 table by the file-ingest worker (`file_ingest_queue.document_id`, `ingestKnowledgebaseDocument`), always with the install's embedding model; old passages stay until the new ones are in, `embedding_model` records where vectors came from; whatever can change the install's model (default settings, a provider saved or deleted, the hourly catalog sync) calls `reindexAfterEmbeddingChange`, and Re-index reads a knowledgebase again by hand. Word search runs on `knowledgebase_chunks_fts`, an FTS5 table (`unicode61 remove_diacritics 2`) made by `ensureKnowledgebaseFts` (FTS5 can't be declared in the schema; sync leaves undeclared tables alone), backfilled at boot by `syncKnowledgebaseFts`, ranked by BM25 with a scored `LIKE` fallback for unspaced scripts (`knowledgebaseWordQuery`); every write and delete of `knowledgebase_chunks` updates it. Ingest jobs (`settleFailedIngestJob`) wait without spending an attempt while every embedding model rests, fail at once on an error retrying can't fix, and a document whose model refused keeps its new passages for word search (counted as needing re-index, not failed). Runs get `search_knowledgebase`/`read_knowledgebase_document` (`openBotKnowledgebases`, limited to the run's ones) and replies start with passages found for the unanswered messages (`grabKnowledgebaseContext`); passages are fenced (`knowledgebasePassageBlock`) and `KnowledgebasesNote` says they're never instructions. UI: Settings > Knowledgebases (super admin edits and fills; admins set access), a Knowledgebases group on bot/workspace settings, and the composer's own button beside Tools (`ComposerToolsModal` with `composerKnowledgebasesCopy`). Deleting one deletes its documents' vectors, chunks, media and links explicitly.
sqlite-vec limits: metadata columns are NOT NULL; side-car columns can't be filtered with `MATCH` (filter in code). **Never pass `--vector` to `bunx bun-sqlite schema` in `entrypoint.sh`** (wipes vectors on boot). Changing `AppData.EmbeddingDimensions` needs a manual `--vector` run and a re-index.
### Files browser (`/admin/files`)
Read-only browser of every volume (`src/utils/storage-volumes.ts`: bots, workspaces, computers, media, mcp, db). `resolveStorageSpec` is the only access gate (members see only their bots' and their own user folders inside a computer, `computerPathAllowed`); `resolveStoragePath` applies the path rules. Links built only by `storageSpecHref`. Deletion stays in the chat's files modal and bot tools.
+12 -107
View File
@@ -1106,6 +1106,11 @@ const schema: BUN_SQLITE_DatabaseSchemaType = {
fieldDescription: `Unix timestamp of completion or final failure`,
dataType: "INTEGER",
},
{
fieldName: "steps",
fieldDescription: `How many steps it recorded, written when its \`bots_queue_updates\` are cleared after it ends (null while they're still there)`,
dataType: "INTEGER",
},
],
indexes: [
{
@@ -1116,7 +1121,7 @@ const schema: BUN_SQLITE_DatabaseSchemaType = {
},
{
tableName: "bots_queue_updates",
tableDescription: `What a bot did while working through a queued job, one row per step, oldest first. Live in the working-on panel while the job runs; deleted with the job when it's pruned`,
tableDescription: `What a bot did while working through a queued job, one row per step, oldest first. Live in the working-on panel while the job runs; cleared shortly after the job ends (clearFinishedJobSteps), its count kept on the job`,
fields: [
{
fieldName: "bots_queue_id",
@@ -3527,6 +3532,10 @@ const schema: BUN_SQLITE_DatabaseSchemaType = {
indexName: "idx_logs_user_id",
indexTableFields: ["user_id"],
},
{
indexName: "idx_logs_created_at",
indexTableFields: ["created_at"],
},
],
},
{
@@ -3787,16 +3796,6 @@ const schema: BUN_SQLITE_DatabaseSchemaType = {
options: BotFileSources.map((s) => s.value),
defaultValue: "bot-write",
},
{
fieldName: "ingested_at",
fieldDescription: `When its text was chunked and embedded. Null: not ingested (yet)`,
dataType: "INTEGER",
},
{
fieldName: "ingest_error",
fieldDescription: `Why the last ingestion failed, when it did`,
dataType: "TEXT",
},
],
uniqueConstraints: [
{
@@ -3820,56 +3819,6 @@ const schema: BUN_SQLITE_DatabaseSchemaType = {
},
],
},
{
tableName: "bot_file_chunks",
tableDescription: `A piece of a bot file's text, the unit a search returns. Replaced whole when the file is ingested again`,
fields: [
{
fieldName: "file_id",
dataType: "INTEGER",
foreignKey: {
cascadeDelete: true,
destinationTableName: "bot_files",
destinationTableColumnName: "id",
destinationTableColumnType: "INTEGER",
foreignKeyName: "bot_file_chunks_file_fk",
},
},
{
fieldName: "scope",
dataType: "TEXT",
options: BotFileScopes.map((s) => s.value),
},
{
fieldName: "owner_id",
fieldDescription: `The bot or workspace the file belongs to, so a search filters without a join`,
dataType: "INTEGER",
},
{
fieldName: "chunk_index",
dataType: "INTEGER",
},
{
fieldName: "text",
dataType: "TEXT",
},
{
fieldName: "token_estimate",
fieldDescription: `A rough count (characters / 4), for trimming results`,
dataType: "INTEGER",
},
],
indexes: [
{
indexName: "idx_bot_file_chunks_file_id",
indexTableFields: ["file_id", "chunk_index"],
},
{
indexName: "idx_bot_file_chunks_owner",
indexTableFields: ["scope", "owner_id"],
},
],
},
{
tableName: "token_usages",
tableDescription: `One model call that reported its usage: the tokens it spent, on which model and provider, for whom and for what. A bot message's total is the sum of the calls it took; the usage page sums them by model, provider and period`,
@@ -4018,44 +3967,9 @@ const schema: BUN_SQLITE_DatabaseSchemaType = {
},
],
},
{
tableName: "bot_file_vectors",
tableDescription: `sqlite-vec vectors of a file's chunks, at \`AppData.EmbeddingDimensions\` wide whatever the model returns. A vec0 table is only recreated with \`bunx bun-sqlite schema --vector\`, so the entrypoint never passes it`,
isVector: true,
vectorType: "vec0",
fields: [
{
fieldName: "embedding",
fieldDescription: `Normalized, padded or cut to \`AppData.EmbeddingDimensions\``,
dataType: "BLOB",
isVector: true,
vectorSize: AppData.EmbeddingDimensions,
},
{
fieldName: "chunk_id",
dataType: "INTEGER",
sideCar: true,
},
{
fieldName: "file_id",
dataType: "INTEGER",
sideCar: true,
},
{
fieldName: "scope",
dataType: "TEXT",
sideCar: true,
},
{
fieldName: "owner_id",
dataType: "INTEGER",
sideCar: true,
},
],
},
{
tableName: "file_ingest_queue",
tableDescription: `Files (and knowledgebase documents) waiting to have their text chunked and embedded. A queue, so a restart never strands a file and an upload never waits on a model. A finished job is deleted: \`bot_files.ingested_at\` and \`ingest_error\` keep the outcome`,
tableDescription: `Knowledgebase documents waiting to have their text chunked and embedded (bot files are never indexed). A queue, so a restart never strands a document and an upload never waits on a model. A finished job is deleted: \`knowledgebase_documents.ingested_at\` and \`ingest_error\` keep the outcome`,
isQueue: {
maxAttempts: 3,
lockTimeout: 10 * 60 * 1000,
@@ -4063,22 +3977,13 @@ const schema: BUN_SQLITE_DatabaseSchemaType = {
deleteOnComplete: true,
},
fields: [
{
fieldName: "file_id",
fieldDescription: `The \`bot_files\` row to (re)ingest`,
dataType: "INTEGER",
},
{
fieldName: "document_id",
fieldDescription: `The \`knowledgebase_documents\` row to (re)ingest, instead of a file`,
fieldDescription: `The \`knowledgebase_documents\` row to (re)ingest`,
dataType: "INTEGER",
},
],
indexes: [
{
indexName: "idx_file_ingest_queue_file_id",
indexTableFields: ["file_id"],
},
{
indexName: "idx_file_ingest_queue_document_id",
indexTableFields: ["document_id"],
+8 -68
View File
@@ -61,9 +61,7 @@ export const BunSQLiteTables = [
"push_subscriptions",
"envs",
"bot_files",
"bot_file_chunks",
"token_usages",
"bot_file_vectors",
"file_ingest_queue",
"app_settings",
] as const
@@ -505,11 +503,11 @@ export type BUN_SQLITE_COBBLE_TASKS = {
*/
updated_at?: number | "";
/**
* The bot the task belongs to; null for a workspace's task
* The bot that has the task: its own list's, or with `workspace_id` its task in that workspace; null for a workspace's shared task nobody has yet
*/
bot_id?: number | "";
/**
* The workspace the task belongs to (its bots all see it); null for a bot's own
* The workspace the task is in; null for a bot's own list
*/
workspace_id?: number | "";
title?: string;
@@ -679,6 +677,10 @@ export type BUN_SQLITE_COBBLE_BOTS_QUEUE = {
* Unix timestamp of completion or final failure
*/
finished_at?: number | "";
/**
* How many steps it recorded, written when its `bots_queue_updates` are cleared after it ends (null while they're still there)
*/
steps?: number | "";
/**
* The state of the job. (pending | processing | completed | failed)
*/
@@ -2331,41 +2333,6 @@ export type BUN_SQLITE_COBBLE_BOT_FILES = {
* How it got here: the user attached it to a message, the bot wrote it, or a tool made it
*/
source?: "chat-upload" | "bot-write" | "tool" | "";
/**
* When its text was chunked and embedded. Null: not ingested (yet)
*/
ingested_at?: number | "";
/**
* Why the last ingestion failed, when it did
*/
ingest_error?: string;
}
export type BUN_SQLITE_COBBLE_BOT_FILE_CHUNKS = {
/**
* The unique identifier of the record.
*/
id?: number | "";
/**
* The time when the record was created. (Unix Timestamp)
*/
created_at?: number | "";
/**
* The time when the record was updated. (Unix Timestamp)
*/
updated_at?: number | "";
file_id?: number | "";
scope?: "bot" | "workspace" | "";
/**
* The bot or workspace the file belongs to, so a search filters without a join
*/
owner_id?: number | "";
chunk_index?: number | "";
text?: string;
/**
* A rough count (characters / 4), for trimming results
*/
token_estimate?: number | "";
}
export type BUN_SQLITE_COBBLE_TOKEN_USAGES = {
@@ -2443,29 +2410,6 @@ export type BUN_SQLITE_COBBLE_TOKEN_USAGES = {
settings?: string;
}
export type BUN_SQLITE_COBBLE_BOT_FILE_VECTORS = {
/**
* The unique identifier of the record.
*/
id?: number | "";
/**
* The time when the record was created. (Unix Timestamp)
*/
created_at?: number | "";
/**
* The time when the record was updated. (Unix Timestamp)
*/
updated_at?: number | "";
/**
* Normalized, padded or cut to `AppData.EmbeddingDimensions`
*/
embedding?: Float32Array<ArrayBuffer> | Buffer<ArrayBuffer> | null;
chunk_id?: number | "";
file_id?: number | "";
scope?: string;
owner_id?: number | "";
}
export type BUN_SQLITE_COBBLE_FILE_INGEST_QUEUE = {
/**
* The unique identifier of the record.
@@ -2480,11 +2424,7 @@ export type BUN_SQLITE_COBBLE_FILE_INGEST_QUEUE = {
*/
updated_at?: number | "";
/**
* The `bot_files` row to (re)ingest
*/
file_id?: number | "";
/**
* The `knowledgebase_documents` row to (re)ingest, instead of a file
* The `knowledgebase_documents` row to (re)ingest
*/
document_id?: number | "";
/**
@@ -2530,4 +2470,4 @@ export type BUN_SQLITE_COBBLE_APP_SETTINGS = {
value?: string;
}
export type BUN_SQLITE_COBBLE_ALL_TYPEDEFS = BUN_SQLITE_COBBLE_USERS & BUN_SQLITE_COBBLE_USER_TYPES & BUN_SQLITE_COBBLE_SSO_LOGIN_CODES & BUN_SQLITE_COBBLE_USER_PASSWORDS & BUN_SQLITE_COBBLE_MEDIA & BUN_SQLITE_COBBLE_MEDIA_PARADIGMS & BUN_SQLITE_COBBLE_BOTS & BUN_SQLITE_COBBLE_CHATROOMS & BUN_SQLITE_COBBLE_BOT_SESSIONS & BUN_SQLITE_COBBLE_BOT_MESSAGES & BUN_SQLITE_COBBLE_BOT_MESSAGE_REACTIONS & BUN_SQLITE_COBBLE_BOT_TASKS & BUN_SQLITE_COBBLE_BOT_TASK_STEPS & BUN_SQLITE_COBBLE_TASKS & BUN_SQLITE_COBBLE_TASK_MEDIA & BUN_SQLITE_COBBLE_TASK_NOTES & BUN_SQLITE_COBBLE_BOTS_QUEUE & BUN_SQLITE_COBBLE_BOTS_QUEUE_UPDATES & BUN_SQLITE_COBBLE_CRON_JOBS & BUN_SQLITE_COBBLE_BOT_HOOKS & BUN_SQLITE_COBBLE_BOT_HOOK_EVENTS & BUN_SQLITE_COBBLE_HOOK_PROPOSALS & BUN_SQLITE_COBBLE_SECRET_REQUESTS & BUN_SQLITE_COBBLE_BOT_GRAPHICS & BUN_SQLITE_COBBLE_WORKSPACES & BUN_SQLITE_COBBLE_WORKSPACE_BOTS & BUN_SQLITE_COBBLE_BOT_TYPES & BUN_SQLITE_COBBLE_WORKSPACE_USERS & BUN_SQLITE_COBBLE_BOT_USERS & BUN_SQLITE_COBBLE_BOT_USER_MEMORIES & BUN_SQLITE_COBBLE_BOT_USER_FACES & BUN_SQLITE_COBBLE_BOT_MESSAGE_MEDIA & BUN_SQLITE_COBBLE_CONTAINERS & BUN_SQLITE_COBBLE_COMPUTERS & BUN_SQLITE_COBBLE_BOT_COMPUTERS & BUN_SQLITE_COBBLE_WORKSPACE_COMPUTERS & BUN_SQLITE_COBBLE_HARNESS_PROVIDERS & BUN_SQLITE_COBBLE_HARNESS_MODELS & BUN_SQLITE_COBBLE_HARNESS_SETTINGS & BUN_SQLITE_COBBLE_BOT_HARNESS_SETTINGS & BUN_SQLITE_COBBLE_WORKSPACE_HARNESS_SETTINGS & BUN_SQLITE_COBBLE_MCP_SERVERS & BUN_SQLITE_COBBLE_CUSTOM_TOOLS & BUN_SQLITE_COBBLE_SKILLS & BUN_SQLITE_COBBLE_EXTENSION_PROPOSALS & BUN_SQLITE_COBBLE_BOT_MCP_SERVERS & BUN_SQLITE_COBBLE_WORKSPACE_MCP_SERVERS & BUN_SQLITE_COBBLE_BOT_CUSTOM_TOOLS & BUN_SQLITE_COBBLE_WORKSPACE_CUSTOM_TOOLS & BUN_SQLITE_COBBLE_BOT_SKILLS & BUN_SQLITE_COBBLE_WORKSPACE_SKILLS & BUN_SQLITE_COBBLE_KNOWLEDGEBASES & BUN_SQLITE_COBBLE_KNOWLEDGEBASE_DOCUMENTS & BUN_SQLITE_COBBLE_KNOWLEDGEBASE_CHUNKS & BUN_SQLITE_COBBLE_KNOWLEDGEBASE_VECTORS & BUN_SQLITE_COBBLE_BOT_KNOWLEDGEBASES & BUN_SQLITE_COBBLE_WORKSPACE_KNOWLEDGEBASES & BUN_SQLITE_COBBLE_LOGS & BUN_SQLITE_COBBLE_USER_PRESENCE & BUN_SQLITE_COBBLE_PUSH_SUBSCRIPTIONS & BUN_SQLITE_COBBLE_ENVS & BUN_SQLITE_COBBLE_BOT_FILES & BUN_SQLITE_COBBLE_BOT_FILE_CHUNKS & BUN_SQLITE_COBBLE_TOKEN_USAGES & BUN_SQLITE_COBBLE_BOT_FILE_VECTORS & BUN_SQLITE_COBBLE_FILE_INGEST_QUEUE & BUN_SQLITE_COBBLE_APP_SETTINGS
export type BUN_SQLITE_COBBLE_ALL_TYPEDEFS = BUN_SQLITE_COBBLE_USERS & BUN_SQLITE_COBBLE_USER_TYPES & BUN_SQLITE_COBBLE_SSO_LOGIN_CODES & BUN_SQLITE_COBBLE_USER_PASSWORDS & BUN_SQLITE_COBBLE_MEDIA & BUN_SQLITE_COBBLE_MEDIA_PARADIGMS & BUN_SQLITE_COBBLE_BOTS & BUN_SQLITE_COBBLE_CHATROOMS & BUN_SQLITE_COBBLE_BOT_SESSIONS & BUN_SQLITE_COBBLE_BOT_MESSAGES & BUN_SQLITE_COBBLE_BOT_MESSAGE_REACTIONS & BUN_SQLITE_COBBLE_BOT_TASKS & BUN_SQLITE_COBBLE_BOT_TASK_STEPS & BUN_SQLITE_COBBLE_TASKS & BUN_SQLITE_COBBLE_TASK_MEDIA & BUN_SQLITE_COBBLE_TASK_NOTES & BUN_SQLITE_COBBLE_BOTS_QUEUE & BUN_SQLITE_COBBLE_BOTS_QUEUE_UPDATES & BUN_SQLITE_COBBLE_CRON_JOBS & BUN_SQLITE_COBBLE_BOT_HOOKS & BUN_SQLITE_COBBLE_BOT_HOOK_EVENTS & BUN_SQLITE_COBBLE_HOOK_PROPOSALS & BUN_SQLITE_COBBLE_SECRET_REQUESTS & BUN_SQLITE_COBBLE_BOT_GRAPHICS & BUN_SQLITE_COBBLE_WORKSPACES & BUN_SQLITE_COBBLE_WORKSPACE_BOTS & BUN_SQLITE_COBBLE_BOT_TYPES & BUN_SQLITE_COBBLE_WORKSPACE_USERS & BUN_SQLITE_COBBLE_BOT_USERS & BUN_SQLITE_COBBLE_BOT_USER_MEMORIES & BUN_SQLITE_COBBLE_BOT_USER_FACES & BUN_SQLITE_COBBLE_BOT_MESSAGE_MEDIA & BUN_SQLITE_COBBLE_CONTAINERS & BUN_SQLITE_COBBLE_COMPUTERS & BUN_SQLITE_COBBLE_BOT_COMPUTERS & BUN_SQLITE_COBBLE_WORKSPACE_COMPUTERS & BUN_SQLITE_COBBLE_HARNESS_PROVIDERS & BUN_SQLITE_COBBLE_HARNESS_MODELS & BUN_SQLITE_COBBLE_HARNESS_SETTINGS & BUN_SQLITE_COBBLE_BOT_HARNESS_SETTINGS & BUN_SQLITE_COBBLE_WORKSPACE_HARNESS_SETTINGS & BUN_SQLITE_COBBLE_MCP_SERVERS & BUN_SQLITE_COBBLE_CUSTOM_TOOLS & BUN_SQLITE_COBBLE_SKILLS & BUN_SQLITE_COBBLE_EXTENSION_PROPOSALS & BUN_SQLITE_COBBLE_BOT_MCP_SERVERS & BUN_SQLITE_COBBLE_WORKSPACE_MCP_SERVERS & BUN_SQLITE_COBBLE_BOT_CUSTOM_TOOLS & BUN_SQLITE_COBBLE_WORKSPACE_CUSTOM_TOOLS & BUN_SQLITE_COBBLE_BOT_SKILLS & BUN_SQLITE_COBBLE_WORKSPACE_SKILLS & BUN_SQLITE_COBBLE_KNOWLEDGEBASES & BUN_SQLITE_COBBLE_KNOWLEDGEBASE_DOCUMENTS & BUN_SQLITE_COBBLE_KNOWLEDGEBASE_CHUNKS & BUN_SQLITE_COBBLE_KNOWLEDGEBASE_VECTORS & BUN_SQLITE_COBBLE_BOT_KNOWLEDGEBASES & BUN_SQLITE_COBBLE_WORKSPACE_KNOWLEDGEBASES & BUN_SQLITE_COBBLE_LOGS & BUN_SQLITE_COBBLE_USER_PRESENCE & BUN_SQLITE_COBBLE_PUSH_SUBSCRIPTIONS & BUN_SQLITE_COBBLE_ENVS & BUN_SQLITE_COBBLE_BOT_FILES & BUN_SQLITE_COBBLE_TOKEN_USAGES & BUN_SQLITE_COBBLE_FILE_INGEST_QUEUE & BUN_SQLITE_COBBLE_APP_SETTINGS
+2 -3
View File
@@ -1,6 +1,6 @@
{
"name": "cobble",
"version": "0.6.1",
"version": "0.7.0",
"module": "index.ts",
"type": "module",
"private": true,
@@ -8,12 +8,11 @@
"dev": "bun src/server.ts",
"gen:favicon": "bun src/scripts/generate-brand-assets.ts",
"build": "bunx bunext build",
"db:schema": "bunx bun-sqlite schema -t",
"db:schema": "bunx bun-sqlite schema -t -y",
"db:admin": "bunx bun-sqlite admin",
"db:backup": "bunx bun-sqlite backup",
"seed": "bun src/scripts/seed-super-admin.tsx",
"seed:bots": "bun src/scripts/seed-bots.ts",
"media:prune-text": "bun src/scripts/prune-media-text-content.ts",
"start": "MAIN_SERVER=1 NODE_ENV=production bun src/server.ts",
"typecheck": "tsc --noEmit",
"test": "bun test",
+12
View File
@@ -16,6 +16,18 @@ describe("composeEnvsNote", () => {
expect(note).toContain("Don't ask the user for them");
});
test("says where each env comes from", () => {
const note = composeEnvsNote({
on_computer: [{ key: "DB_PASSWORD", description: "Production database", paradigm: "workspace" }],
tools_only: [{ key: "TOKEN", description: "", paradigm: "user" }],
has_computer: true,
});
expect(note).toContain("- DB_PASSWORD (this workspace's, every bot in it has it): Production database");
expect(note).toContain("- TOKEN (this user's own, for you)");
expect(note).toContain("never needs setting again per bot");
});
test("never mentions values", () => {
const note = composeEnvsNote({ on_computer: [{ key: "K", description: "d" }], tools_only: [], has_computer: true });
+27 -7
View File
@@ -1,4 +1,6 @@
type Env = { key: string; description: string };
import type { EnvParadigm } from "@/src/dict/env-dict";
type Env = { key: string; description: string; paradigm?: EnvParadigm };
type Params = {
/**
@@ -16,21 +18,39 @@ type Params = {
};
/**
* Added to a chat reply so the bot knows which envs it already has (keys
* and descriptions, never values). Without it a reply can't tell, asks the
* user for keys that are set (`request_secret`), or "checks" them with
* `show_secret` or a shell where they aren't loaded.
* Where an env comes from, as the bot is told: who else has it.
*/
const EnvSources: Record<EnvParadigm, string> = {
general: "install-wide, every bot has it",
workspace: "this workspace's, every bot in it has it",
computer: "this computer's, every bot on it has it",
bot: "yours, for every user",
user: "this user's own, for you",
};
/**
* Added to a chat reply so the bot knows which envs it already has (keys,
* descriptions and where each comes from, never values). Without it a
* reply can't tell, asks the user for keys that are set
* (`request_secret`), "checks" them with `show_secret` or a shell where
* they aren't loaded, or tells the user a workspace's keys must be set
* again for every bot.
*/
export default function composeEnvsNote({ on_computer, tools_only, has_computer }: Params) {
const list = (envs: Env[]) =>
envs.map((env) => `- ${env.key}${env.description ? `: ${env.description}` : ""}`).join("\n");
envs
.map(
(env) =>
`- ${env.key}${env.paradigm ? ` (${EnvSources[env.paradigm]})` : ""}${env.description ? `: ${env.description}` : ""}`,
)
.join("\n");
if (!on_computer.length && !tools_only.length) {
return `# Your envs\n\nNo envs are set for you here. If you need a secret, ask for it with request_secret.`;
}
return [
`# Your envs\n\nThese are already set for you here (keys and descriptions; you never see values). Don't ask the user for them, and don't call show_secret or run printenv to check them: this list is the check.`,
`# Your envs\n\nThese are already set for you here (keys and descriptions; you never see values). Don't ask the user for them, and don't call show_secret or run printenv to check them: this list is the check. A workspace, computer or install-wide env reaches every bot there the same way, so it never needs setting again per bot.`,
on_computer.length
? `On your computer, as $KEY in a task's shell and code (start_task; search_computers doesn't load them):\n${list(on_computer)}`
: "",
+13 -5
View File
@@ -5,6 +5,7 @@ import grabMessagesMedia, {
} from "@/src/functions/backend/db/bots/grab-messages-media";
import type { HarnessContentPart } from "@/src/harness/types";
import type { BotMessageWithAttachments } from "@/src/types";
import extractBotFileText from "@/src/functions/backend/db/bot-files/extract-bot-file-text";
import describeAttachment from "./describe-attachment";
type Params = {
@@ -27,10 +28,7 @@ export default async function loadAttachmentParts({ messages }: Params) {
.slice(-AppData.BotReplyAttachmentMessages)
.map((message) => Number(message.id));
const media = await grabMessagesMedia({
message_ids: recent,
with_text_content: true,
});
const media = await grabMessagesMedia({ message_ids: recent });
const parts = new Map<number, HarnessContentPart[]>();
for (const [message_id, files] of media) {
@@ -53,7 +51,17 @@ async function fileToParts({ file }: { file: MessageMediaRow }): Promise<Harness
const label = describeAttachment({ name, id: file.id! });
if (file.media_type == "document") {
const text = file.text_content?.trim();
/**
* Read from the file each time: no text is kept outside
* knowledgebases.
*/
const text = (
await extractBotFileText({
write_path: file.media_write_path,
name: file.title,
mime_type: file.mime_type,
}).catch(() => null)
)?.trim();
if (!text) {
return [{ type: "text", text: `[Attached file ${label}: its text couldn't be read.]` }];
+8
View File
@@ -0,0 +1,8 @@
import clearFinishedJobSteps from "@/src/functions/backend/db/bots-queue/clear-finished-job-steps";
/**
* Clears the steps of jobs that have ended (`clearFinishedJobSteps`).
*/
export default async function cronClearJobSteps() {
return await clearFinishedJobSteps();
}
+15
View File
@@ -1,8 +1,10 @@
import bunext from "@moduletrace/bunext";
import cronHeartbeat from "./heartbeat";
import pruneBotsQueue from "./prune-bots-queue";
import cronClearJobSteps from "./clear-job-steps";
import pruneBotHookEvents from "./prune-bot-hook-events";
import cronPruneChatrooms from "./prune-chatrooms";
import pruneLogs from "./prune-logs";
import syncNotifyWatchers from "./sync-notify-watchers";
import cronSyncHarnessModels from "./sync-harness-models";
import syncComputers from "./sync-computers";
@@ -31,6 +33,13 @@ export default function cron() {
task: pruneBotsQueue,
});
registerCronJob({
title: "clear-job-steps",
schedule: "*/5 * * * *",
task: cronClearJobSteps,
quiet: true,
});
registerCronJob({
title: "prune-bot-hook-events",
schedule: "@daily",
@@ -43,6 +52,12 @@ export default function cron() {
task: cronPruneChatrooms,
});
registerCronJob({
title: "prune-logs",
schedule: "@daily",
task: pruneLogs,
});
registerCronJob({
title: "sync-notify-watchers",
schedule: "*/5 * * * *",
+4 -23
View File
@@ -1,31 +1,12 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import type { BUN_SQLITE_COBBLE_BOT_FILES } from "@/db/types/db";
import enqueueDocumentIngest from "@/src/file-ingest/enqueue-document-ingest";
import enqueueFileIngest from "@/src/file-ingest/enqueue-file-ingest";
/**
* Queues any bot file or knowledgebase document that never got ingested (an upload that happened
* while the worker was down, a restart mid-ingest), so nothing is left
* unsearchable. Runs every ten minutes.
* Queues any knowledgebase document that never got ingested (an upload
* that happened while the worker was down, a restart mid-ingest), so
* nothing is left unsearchable. Runs every ten minutes.
*/
export default async function ingestPendingFiles() {
const res = await BunSQLite.sql<BUN_SQLITE_COBBLE_BOT_FILES>({
sql: `SELECT f.id FROM bot_files f
WHERE f.ingested_at IS NULL
AND f.ingest_error IS NULL
AND f.created_at > ?
AND NOT EXISTS (
SELECT 1 FROM file_ingest_queue q
WHERE q.file_id = f.id AND q.status IN ('pending', 'processing')
)
LIMIT 25`,
values: [Date.now() - 24 * 60 * 60 * 1000],
});
for (const file of res.payload || []) {
if (file.id) await enqueueFileIngest({ file_id: file.id });
}
const documents = await BunSQLite.sql<{ id: number }>({
sql: `SELECT d.id FROM knowledgebase_documents d
WHERE d.ingested_at IS NULL
@@ -41,5 +22,5 @@ export default async function ingestPendingFiles() {
if (document.id) await enqueueDocumentIngest({ document_id: document.id });
}
return (res.payload?.length || 0) + (documents.payload?.length || 0);
return documents.payload?.length || 0;
}
+15
View File
@@ -0,0 +1,15 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import { grabLogsKeepDays } from "@/src/functions/backend/db/app-settings/grab-logs-keep-days";
/**
* Deletes logs older than the install's retention (`grabLogsKeepDays`).
* Nothing points at a log, so no children go with them.
*/
export default async function pruneLogs() {
const cutoff = Date.now() - (await grabLogsKeepDays()) * 24 * 60 * 60 * 1000;
return await BunSQLite.sql({
sql: `DELETE FROM logs WHERE created_at < ? RETURNING id`,
values: [cutoff],
});
}
+37 -4
View File
@@ -278,6 +278,13 @@ export const AppData = {
BotHookMaxEventsPerRun: 20,
BotHookEventsKeepDays: 30,
BotHookEventsKeepDaysMax: 3650,
/**
* Logs (Activity) are deleted after this many days by the daily
* `prune-logs` cron: the default, with the most an install may set
* (`logs_keep_days`, Settings > General).
*/
LogsKeepDays: 90,
LogsKeepDaysMax: 3650,
/**
* How many hook calls a list loads at once, and how much of each call's
* payload and response a list shows (the whole of both opens on one).
@@ -354,6 +361,13 @@ export const AppData = {
* One MCP tool call gives up after this long without progress.
*/
McpToolTimeoutMs: 5 * 60 * 1000,
/**
* A bash command on a computer is killed there after at most this long
* (`withCommandTimeout`), below `McpToolTimeoutMs`: the sandbox ignores
* its own `timeout` and runs every call in one shell session, so a
* command left running would hold up every later one on that computer.
*/
ComputerCommandMaxSeconds: 270,
/**
* MCP servers set up in Settings, under MCPs: a stdio server runs in
* `DATA_DIR/<McpDataDir>/<id>` (its setup installs there, so it
@@ -414,6 +428,12 @@ export const AppData = {
* working-on panel shows.
*/
BotJobUpdatesShown: 12,
/**
* How long a finished job's steps stay before they're cleared
* (`clear-job-steps` cron). Long enough for every process's live relay
* to have pushed the last ones (`LiveRelayPollMs`).
*/
BotJobStepsKeepMs: 60 * 1000,
/**
* How often each server process reads new bot messages, job steps and
* bot changes from the database for its own sockets (`liveRelay`).
@@ -648,8 +668,9 @@ export const AppData = {
*/
BotFileImportMaxBytes: 20 * 1024 * 1024,
/**
* Ingesting a bot file: how much text one chunk holds, how much of the
* previous chunk it repeats, and the largest file it reads at all.
* Ingesting a knowledgebase document: how much text one chunk holds,
* how much of the previous chunk it repeats, and the largest file it
* reads at all (also the largest attachment a reply reads).
*/
ChunkTargetChars: 1200,
ChunkOverlapChars: 200,
@@ -668,12 +689,24 @@ export const AppData = {
EmbeddingBatchSize: 32,
EmbeddingMaxInputChars: 8000,
/**
* How many chunks a file search returns, and how many more it asks
* the vector index for before filtering to the run's scopes.
* How many chunks a knowledgebase search returns, and how many more it
* asks the vector index for before filtering to the run's ones.
*/
RagTopChunks: 8,
RagOverFetch: 4,
RagMaxK: 60,
/**
* How long a search box waits after the last keystroke before it
* searches.
*/
SearchDebounceMs: 250,
/**
* The database is vacuumed at boot once this share of its pages, and
* at least this many bytes, sit free (after a big delete, such as the
* bot file index being dropped).
*/
DbVacuumMinFreeRatio: 0.2,
DbVacuumMinFreeBytes: 4 * 1024 * 1024,
/**
* Knowledgebases: the most one document holds (an upload, or text
* written in), how much of a pasted text a form takes, how many
+6 -5
View File
@@ -60,18 +60,18 @@ export const botFilesNote = ({ bot_id }: { bot_id: number | string }) => `# Your
You have a folder of your own, kept with the app (not on a computer), and it stays between runs and conversations.
- list_files shows what's in it; read_file opens one (by its [file:ID] ref or path); write_file saves a text file, make_pdf a PDF, generate_image an image. Move and delete with move_file and delete_file when something is out of date, so the folder stays useful.
- Text files are chunked and searchable as soon as they're saved. search_files finds the passages that answer a question, by meaning when an embedding model is set and by words otherwise. Search your files before saying you don't know something they may hold, and when the user asks about a document they gave you.
- Uploaded files land in the same folder, under uploads/ (uploads/2026-10/logo.png), so what the user sent you earlier is searchable later too.
- Files are found by name or path, never by what's in them: list_files with "search" matches names and paths, then read_file reads the one you want. Look for a file and read it before saying you don't know something it may hold, and when the user asks about a document they gave you. Searching by content is what knowledgebases are for; your files aren't indexed.
- Uploaded files land in the same folder, under uploads/ (uploads/2026-10/logo.png), so what the user sent you earlier is there to list and read later too.
- A picture can be set from a file: set_picture makes a file your own avatar, and update_workspace sets a workspace's. Both take the [file:ID] ref, so a logo the user sent you is usually ready to go: list_files for it, then set it. An SVG or another kind of file is turned into a PNG first. Its picture is free to change and never removes your file.
- A file and a folder are two different refs, and you never guess which one from the name:
- A file you saved is a [file:ID] ref (from write_file, list_files, search_files or import_file). The user sees a chip that opens the file.
- A file you saved is a [file:ID] ref (from write_file, list_files or import_file). The user sees a chip that opens the file.
- A folder is a folder ref, e.g. ${ownFolderRef({ bot_id })}: the user sees a chip that opens that folder in Files, and nothing is downloaded. Use the volume of the folder you mean ("bot" for your own, "workspace" for a workspace's shared one).
- Never write a path as plain text and expect the user to find it; never write a [file:ID] ref for something that is a folder.
- Write refs as they are, never in backticks or a code block: inside code they show as plain text, not a chip.
- A file you only described is not saved. Save it first, then say it's saved.
- Save what the user will want before the long tail of a run. A step limit ends a run where it stands: write the report first with write_file (or copy it off the computer with import_file), then carry on verifying.
- One copy of a thing is enough. Before you write or import something, list_files (or search_files) for it: if it's already there, update it (write_file over the same path) instead of saving a second copy under a new name. Never split a file into parts unless the user asked for parts.
- Your files don't disappear. If you can't find something, list_files or search_files before saying anything is missing, and never claim the folder is empty without having just listed it.
- One copy of a thing is enough. Before you write or import something, list_files for it: if it's already there, update it (write_file over the same path) instead of saving a second copy under a new name. Never split a file into parts unless the user asked for parts.
- Your files don't disappear. If you can't find something, list_files (with and without "search") before saying anything is missing, and never claim the folder is empty without having just listed it.
- Report what the tools actually returned. Don't invent limits, sizes or caps you haven't seen in a tool result: if an import or a write failed, say what the tool said.`;
/**
@@ -102,6 +102,7 @@ You work only in your own folders on the computer, never elsewhere in it: every
dirs.shared_dir ? `\n- ${dirs.shared_dir}: shared with every bot in this workspace.` : ""
}
- The browser downloads to its own folder: move what you download into your folder straight away.
- A computer can be restarted or rebuilt at any time, and only ${AppData.ComputerWorkspaceDir} and the browser's logins survive it. Programs you install (sudo apt-get install -y, pip, npm -g) may be gone afterwards: check for one first (command -v) and install it again when it's missing.
What you make there (downloads, screenshots, a crawl, raw evidence, logs) is kept with the computer, not in your files, and it is not searchable.
+7 -1
View File
@@ -23,4 +23,10 @@ export const LogCategories = [
title: "Error",
value: "error",
},
] as const;
] as const;
/**
* The app setting (Settings > General) for how many days logs are kept,
* else `AppData.LogsKeepDays`.
*/
export const LogsKeepDaysSettingKey = "logs_keep_days";
-37
View File
@@ -1,37 +0,0 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import type { BUN_SQLITE_COBBLE_FILE_INGEST_QUEUE } from "@/db/types/db";
import wakeFileIngest from "./wake-file-ingest";
type Params = {
file_id: number | string;
};
/**
* Queues a file to be chunked and embedded, unless it's already waiting.
* Never fails the caller: a file that can't be queued is picked up by the
* `ingest-pending-files` cron instead.
*/
export default async function enqueueFileIngest({ file_id }: Params) {
try {
const open = await BunSQLite.sql<BUN_SQLITE_COBBLE_FILE_INGEST_QUEUE>({
sql: `SELECT id FROM file_ingest_queue WHERE file_id = ? AND status IN ('pending', 'processing') LIMIT 1`,
values: [file_id],
});
if (open.payload?.[0]?.id) {
wakeFileIngest();
return;
}
await BunSQLite.queue.add({
table: "file_ingest_queue",
data: [{ file_id: Number(file_id) }],
});
wakeFileIngest();
} catch {
/**
* The cron sweeps it up.
*/
}
}
+3 -3
View File
@@ -14,9 +14,9 @@ let timer: ReturnType<typeof setTimeout> | undefined;
/**
* Starts the file ingestion worker: polls every `AppData.FileIngestPollMs`
* for `file_ingest_queue` jobs and runs up to
* `AppData.FileIngestConcurrency` at once (chunking and embedding a bot's
* file). Gated in production to the `MAIN_SERVER` container, like cron and
* the bots queue.
* `AppData.FileIngestConcurrency` at once (chunking and embedding a
* knowledgebase document). Gated in production to the `MAIN_SERVER`
* container, like cron and the bots queue.
*/
export default async function fileIngest() {
if (process.env.NODE_ENV == "production" && !process.env.MAIN_SERVER) {
+7 -34
View File
@@ -1,6 +1,5 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import type { BUN_SQLITE_COBBLE_FILE_INGEST_QUEUE } from "@/db/types/db";
import ingestBotFile from "@/src/functions/backend/db/bot-files/ingest-bot-file";
import ingestKnowledgebaseDocument from "@/src/functions/backend/db/knowledgebases/ingest-knowledgebase-document";
import { logError } from "@/src/functions/backend/logger";
import settleFailedIngestJob from "./settle-failed-ingest-job";
@@ -10,56 +9,30 @@ type Params = {
};
/**
* Ingests one queued file (or knowledgebase document) and completes (or
* fails) its job. One that was deleted meanwhile is simply done.
* Ingests one queued knowledgebase document and completes (or fails) its
* job. One that was deleted meanwhile is simply done, and so is a job
* without a document (bot files are no longer indexed).
*/
export default async function processIngestJob({ job }: Params) {
if (!job.id) return;
if (job.document_id) return await processDocumentJob({ job_id: job.id, document_id: job.document_id });
if (!job.file_id) {
if (!job.document_id) {
await BunSQLite.queue.complete({ table: "file_ingest_queue", id: job.id }).catch(() => {});
return;
}
try {
const result = await ingestBotFile({ file_id: job.file_id });
await ingestKnowledgebaseDocument({ document_id: job.document_id });
await BunSQLite.queue.complete({ table: "file_ingest_queue", id: job.id });
if (result.status == "ingested") {
BunSQLite.sql({
sql: `UPDATE bot_files SET ingest_error = NULL WHERE id = ?`,
values: [job.file_id],
}).catch(() => {});
}
} catch (error) {
const { deferred } = await settleFailedIngestJob({ job_id: job.id, error });
if (deferred) return;
await logError({
title: `Bot file #${job.file_id} couldn't be ingested`,
title: `Knowledgebase document #${job.document_id} couldn't be ingested`,
error,
metadata: { file_id: job.file_id },
}).catch(() => {});
}
}
async function processDocumentJob({ job_id, document_id }: { job_id: number; document_id: number }) {
try {
await ingestKnowledgebaseDocument({ document_id });
await BunSQLite.queue.complete({ table: "file_ingest_queue", id: job_id });
} catch (error) {
const { deferred } = await settleFailedIngestJob({ job_id, error });
if (deferred) return;
await logError({
title: `Knowledgebase document #${document_id} couldn't be ingested`,
error,
metadata: { document_id },
metadata: { document_id: job.document_id },
}).catch(() => {});
}
}
@@ -5,7 +5,7 @@
* table name as an argument the way a table-valued function does.
*
* Left out: SQLite's own `sqlite_*` tables, and the shadow tables a
* virtual table keeps its internals in (the `bot_file_vectors_*` tables
* virtual table keeps its internals in (the `knowledgebase_vectors_*` tables
* behind the sqlite-vec index), which are storage, not rows anyone
* browses.
*/
@@ -0,0 +1,30 @@
import { AppData } from "@/src/data/app-data";
import { LogsKeepDaysSettingKey } from "@/src/dict/log-categories";
import type { LogRetentionOverview } from "@/src/types";
import readStepLimit from "@/src/utils/read-step-limit";
import grabAppSetting from "./grab-app-setting";
/**
* How many days logs are kept: the install's own number (`logs_keep_days`,
* Settings > General), else `AppData.LogsKeepDays`.
*/
export async function grabLogsKeepDays() {
return readStepLimit({
saved: await grabAppSetting({ key: LogsKeepDaysSettingKey }),
fallback: AppData.LogsKeepDays,
max: AppData.LogsKeepDaysMax,
});
}
/**
* The number with its default and ceiling, for the settings page.
*/
export default async function grabLogRetentionOverview() {
const overview: LogRetentionOverview = {
keep_days: await grabLogsKeepDays(),
default_days: AppData.LogsKeepDays,
max: AppData.LogsKeepDaysMax,
};
return overview;
}
@@ -15,7 +15,7 @@ type Params = {
};
/**
* Deletes a bot file: the catalog row (its chunks go with it), its bytes
* Deletes a bot file: the catalog row, its bytes
* and its `media` row. Refuses anything outside the run's directories.
*/
export default async function deleteBotFile({ scopes, file_id }: Params) {
@@ -32,22 +32,7 @@ export default async function deleteBotFile({ scopes, file_id }: Params) {
rmSync(absolute, { force: true });
}
await BunSQLite.sql({ sql: `DELETE FROM bot_file_vectors WHERE file_id = ?`, values: [file.id] });
/**
* Deleted here rather than left to the schema's cascade: SQLite has
* foreign keys off per connection unless `enableForeignKeys` ran, and
* a script or CLI process that deletes a file would otherwise leave
* its chunks behind, which then fail the next schema sync.
*/
await BunSQLite.sql({ sql: `DELETE FROM bot_file_chunks WHERE file_id = ?`, values: [file.id] });
await BunSQLite.sql({ sql: `DELETE FROM bot_files WHERE id = ?`, values: [file.id] });
/**
* Its pending ingestion would only find nothing left.
*/
await BunSQLite.sql({
sql: `DELETE FROM file_ingest_queue WHERE file_id = ? AND status IN ('pending', 'processing')`,
values: [file.id],
}).catch(() => {});
if (file.media_id) {
const media = await BunSQLite.sql<{ media_write_path?: string; media_thumbnail_write_path?: string }>({
@@ -53,8 +53,6 @@ export default async function grabConversationFiles({
purpose: String(file.purpose || "writing"),
scope: String(file.scope || "bot"),
owner_id: Number(file.owner_id) || 0,
ingested_at: file.ingested_at ? Number(file.ingested_at) : undefined,
ingest_error: file.ingest_error || undefined,
created_at: Number(file.created_at) || 0,
url: media_url || "",
};
@@ -1,329 +0,0 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import type {
BUN_SQLITE_COBBLE_BOT_FILE_CHUNKS,
BUN_SQLITE_COBBLE_BOT_FILES,
BunSQLiteTables,
} from "@/db/types/db";
import { AppData } from "@/src/data/app-data";
import embedText from "@/src/harness/embeddings/embed-text";
import HarnessError from "@/src/harness/harness-error";
import { grabHarnessUsageScope } from "@/src/harness/usage/harness-usage-scope";
import type { HarnessUsageScope } from "@/src/harness/usage/types";
import runWithFallbackScope from "@/src/harness/usage/run-with-fallback-scope";
import grabUsageOwnerUserId from "@/src/functions/backend/db/usage/grab-usage-owner";
import chunkText from "./chunk-text";
import extractBotFileText from "./extract-bot-file-text";
type Params = {
file_id: number | string;
/**
* Read images and audio too (a bot asking for it).
*/
read_media?: boolean;
signal?: AbortSignal;
};
export type IngestResult = {
/**
* `ingested`, `empty` (no text to read), `skipped` (too large) or
* `unavailable` (no embedding model), which keeps the file's text but
* leaves it searchable by words only.
*/
status: "ingested" | "empty" | "skipped" | "unavailable";
chunks: number;
};
/**
* Reads a bot file's text, splits it into chunks and embeds them into
* `bot_file_vectors`, replacing whatever was stored before. The chunks
* stay in `bot_file_chunks` even when there's no embedding model, so a
* search can still match words in them.
*
* Failures are recorded on the file (`ingest_error`) and rethrown, so the
* queue can retry.
*/
export default async function ingestBotFile({
file_id,
read_media,
signal,
}: Params): Promise<IngestResult> {
const file = await BunSQLite.sql<BUN_SQLITE_COBBLE_BOT_FILES>({
sql: `SELECT * FROM bot_files WHERE id = ? LIMIT 1`,
values: [file_id],
}).then((res) => res.payload?.[0]);
if (!file?.id) return { status: "empty", chunks: 0 };
const media = file.media_id
? await BunSQLite.sql<{ media_write_path?: string; text_content?: string }>({
sql: `SELECT media_write_path, text_content FROM media WHERE id = ? LIMIT 1`,
values: [file.media_id],
}).then((res) => res.payload?.[0])
: undefined;
/**
* Decided on the file's size alone. A cached `text_content` is not a
* reason to read an oversized file: it was only ever a slice of it,
* and taking it as the whole document made a file too big to unpack
* look already read.
*/
if (Number(file.size) > AppData.IngestMaxBytes) {
await BunSQLite.sql({
sql: `UPDATE bot_files SET ingest_error = ?, updated_at = ? WHERE id = ?`,
values: [`Too large to read (over ${AppData.IngestMaxBytes / (1024 * 1024)} MB).`, Date.now(), file.id],
});
return { status: "skipped", chunks: 0 };
}
/**
* A bot's own run already carries its user. The ingest queue has no run
* behind it, so everything this file costs - reading an image or an
* audio file, embedding its chunks - is entered under the owner's scope
* instead: it spends real tokens and must show on the usage page.
*/
const owner_scope: HarnessUsageScope | null = grabHarnessUsageScope()?.user_id
? null
: await resolveOwnerScope({ file });
try {
const text = await runWithFallbackScope({
scope: owner_scope,
task: () =>
extractBotFileText({
write_path: media?.media_write_path,
text_content: media?.text_content,
name: file.name,
mime_type: file.mime_type,
read_media,
}),
});
await clearChunks({ file_id: file.id });
if (!text?.trim()) {
await markIngested({ file_id: file.id, chunks: 0 });
return { status: "empty", chunks: 0 };
}
const chunks = chunkText({ text });
if (!chunks.length) {
await markIngested({ file_id: file.id, chunks: 0 });
return { status: "empty", chunks: 0 };
}
const chunk_ids = await insertChunks({ file, chunks });
let embedded = 0;
try {
/**
* A big file's chunks go in batches, so one provider refusal
* doesn't throw away the whole file.
*/
for (let start = 0; start < chunks.length; start += AppData.EmbeddingBatchSize) {
const batch = chunks.slice(start, start + AppData.EmbeddingBatchSize);
const { vectors } = await runEmbed({
scope: owner_scope,
signal,
file,
batch,
});
for (let i = 0; i < vectors.length; i++) {
await insertVector({
vector: vectors[i]!,
chunk_id: chunk_ids[start + i]!,
file_id: file.id,
scope: String(file.scope),
owner_id: Number(file.owner_id),
});
embedded++;
}
}
} catch (error) {
if (error instanceof HarnessError && error.code == "unavailable") {
/**
* No embedding model: the chunks still let a search match
* words, so the file isn't a failure.
*/
await markIngested({
file_id: file.id,
chunks: chunks.length,
error: "No embedding model is set, so this file is searchable by words only.",
});
return { status: "unavailable", chunks: chunks.length };
}
throw error;
}
await markIngested({ file_id: file.id, chunks: chunks.length });
return { status: "ingested", chunks: embedded };
} catch (error) {
/**
* Every model resting means nothing was tried: the job waits for
* them, and the file keeps whatever it said before.
*/
if (error instanceof HarnessError && error.resting) throw error;
const message = error instanceof Error ? error.message : String(error);
await BunSQLite.sql({
sql: `UPDATE bot_files SET ingest_error = ?, updated_at = ? WHERE id = ?`,
values: [message.slice(0, 500), Date.now(), file.id],
}).catch(() => {});
throw error;
}
}
async function clearChunks({ file_id }: { file_id: number }) {
await BunSQLite.sql({ sql: `DELETE FROM bot_file_vectors WHERE file_id = ?`, values: [file_id] });
await BunSQLite.sql({ sql: `DELETE FROM bot_file_chunks WHERE file_id = ?`, values: [file_id] });
}
async function insertChunks({
file,
chunks,
}: {
file: BUN_SQLITE_COBBLE_BOT_FILES;
chunks: string[];
}) {
const ids: number[] = [];
for (let index = 0; index < chunks.length; index++) {
const text = chunks[index]!;
const res = await BunSQLite.insert<
BUN_SQLITE_COBBLE_BOT_FILE_CHUNKS,
(typeof BunSQLiteTables)[number]
>({
table: "bot_file_chunks",
data: [
{
file_id: file.id,
scope: file.scope as any,
owner_id: Number(file.owner_id),
chunk_index: index,
text,
token_estimate: Math.ceil(text.length / 4),
},
],
});
const id = res.postInsertReturn?.insertId;
if (id) ids.push(id);
}
return ids;
}
async function insertVector({
vector,
chunk_id,
file_id,
scope,
owner_id,
}: {
vector: Float32Array;
chunk_id: number;
file_id: number;
scope: string;
owner_id: number;
}) {
/**
* vec0's metadata columns (its id and timestamps) are NOT NULL, so a
* raw insert has to fill them; `BunSQLite.insert` would add them but
* the vector needs `vec_f32`.
*/
const now = Date.now();
const res = await BunSQLite.sql({
sql: `INSERT INTO bot_file_vectors (created_at, updated_at, embedding, chunk_id, file_id, scope, owner_id) VALUES (?, ?, vec_f32(?), ?, ?, ?, ?)`,
values: [now, now, vector as any, chunk_id, file_id, scope, owner_id],
});
if (!res.success) {
throw new Error(`Couldn't store an embedding: ${res.error || res.msg}`);
}
}
async function markIngested({
file_id,
chunks,
error,
}: {
file_id: number;
chunks: number;
error?: string;
}) {
const now = Date.now();
await BunSQLite.sql({
sql: `UPDATE bot_files SET ingested_at = ?, ingest_error = ?, updated_at = ? WHERE id = ?`,
values: [now, error || null, now, file_id],
});
await BunSQLite.sql({
sql: `UPDATE media SET is_rag_ingested = ?, updated_at = ? WHERE id = (SELECT media_id FROM bot_files WHERE id = ?)`,
values: [chunks > 0 ? 1 : 0, now, file_id],
}).catch(() => {});
}
/**
* One batch of a file's chunks, embedded under whatever attribution owns
* it: the run that asked for the ingest, else the file's owner.
*/
async function runEmbed({
scope,
signal,
file,
batch,
}: {
scope: HarnessUsageScope | null;
signal?: AbortSignal;
file: BUN_SQLITE_COBBLE_BOT_FILES;
batch: string[];
}) {
return await runWithFallbackScope({
scope,
task: () =>
embedText({
input: batch,
bot_id: file.bot_id!,
workspace_id: file.scope == "workspace" ? file.owner_id : null,
signal,
}),
});
}
/**
* The attribution a file the queue ingests reports under: its workspace's
* creator when it shares one, else its bot's.
*/
async function resolveOwnerScope({
file,
}: {
file: BUN_SQLITE_COBBLE_BOT_FILES;
}): Promise<HarnessUsageScope | null> {
const workspace_id = file.scope == "workspace" ? file.owner_id : null;
const user_id = await grabUsageOwnerUserId({ bot_id: file.bot_id, workspace_id });
if (!user_id) return null;
return {
user_id,
bot_id: file.scope == "bot" ? file.owner_id : file.bot_id,
workspace_id,
session_id: null,
message_id: null,
job_id: null,
};
}
@@ -54,22 +54,6 @@ export default async function purgeStorage({ scope, owner_id, media_ids }: Param
}).catch(() => {});
}
await BunSQLite.sql({
sql: `DELETE FROM bot_file_vectors WHERE scope = ? AND owner_id = ?`,
values: [scope, owner_id],
}).catch(() => {});
/**
* Deleted here rather than left to the cascade, since SQLite has
* foreign keys off per connection unless `enableForeignKeys` ran (a
* script or CLI process never does), and a chunk left behind fails
* the next schema sync.
*/
await BunSQLite.sql({
sql: `DELETE FROM bot_file_chunks WHERE scope = ? AND owner_id = ?`,
values: [scope, owner_id],
}).catch(() => {});
/**
* Usually gone already, with the bot or workspace (the cascade).
*/
@@ -104,7 +104,7 @@ export default async function recordBotFile({
if (existing?.id) {
await BunSQLite.sql({
sql: `UPDATE bot_files SET media_id = ?, name = ?, folder = ?, mime_type = ?, media_type = ?, size = ?,
purpose = ?, source = ?, ingested_at = NULL, ingest_error = NULL, updated_at = ? WHERE id = ?`,
purpose = ?, source = ?, updated_at = ? WHERE id = ?`,
values: [
media_id,
resolved.name,
@@ -129,8 +129,6 @@ export default async function recordBotFile({
size: bytes.byteLength,
purpose,
source,
ingested_at: undefined,
ingest_error: undefined,
updated_at: now,
};
}
@@ -1,271 +0,0 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import { AppData } from "@/src/data/app-data";
import { embedQuery } from "@/src/harness/embeddings/embed-text";
import HarnessError from "@/src/harness/harness-error";
import type { StorageScope } from "@/src/utils/storage-access";
type Params = {
scopes: StorageScope[];
query: string;
/**
* Only this file's chunks.
*/
file_id?: number | string | null;
limit?: number;
signal?: AbortSignal;
/**
* Whose embedding model to use.
*/
bot_id?: number | string;
workspace_id?: number | string | null;
};
export type BotFileSearchHit = {
file_id: number;
chunk_id: number;
chunk_index: number;
name: string;
path: string;
scope: string;
/**
* Cosine distance when the vector index answered, null for a word
* match.
*/
distance: number | null;
text: string;
};
export type BotFileSearchResult = {
hits: BotFileSearchHit[];
/**
* How the search ran: `vector`, `words` (no embedding model, or nothing
* matched) or `none` (no files at all).
*/
mode: "vector" | "words" | "none";
/**
* Said plainly for the bot's own reading of what it got back.
*/
note: string;
};
/**
* Searches a run's files: by meaning through the embedding vectors when a
* model is set, else (or when nothing matches) by words over the stored
* chunks. Never throws for an unavailable model: the bot always gets
* something it can act on.
*/
export default async function searchBotFiles({
scopes,
query,
file_id,
limit = AppData.RagTopChunks,
signal,
bot_id,
workspace_id,
}: Params): Promise<BotFileSearchResult> {
const text = query.trim();
if (!scopes.length || !text) {
return { hits: [], mode: "none", note: "No files to search." };
}
try {
const vector = await embedQuery({
text,
signal,
bot_id,
workspace_id,
});
const hits = await vectorSearch({
scopes,
query_vector: vector,
file_id,
limit,
seek: Math.min(limit * AppData.RagOverFetch, AppData.RagMaxK),
});
if (hits.length) {
return {
hits,
mode: "vector",
note: `${hits.length} passage(s), found by meaning.`,
};
}
} catch (error) {
if (!(error instanceof HarnessError)) throw error;
}
const hits = await wordSearch({ scopes, query: text, file_id, limit });
return {
hits,
mode: hits.length ? "words" : "none",
note: hits.length
? `${hits.length} passage(s), found by matching words (no embedding model, or nothing matched by meaning).`
: `Nothing in your files matched "${text}".`,
};
}
/**
* The nearest chunks by vector. sqlite-vec refuses a filter on a vec0
* side-car column in the same query as the match, so the index is asked
* for its nearest `seek` and the run's scopes are applied to that list
* here, before the chunk text is read.
*/
async function vectorSearch({
scopes,
query_vector,
file_id,
limit,
seek,
}: {
scopes: StorageScope[];
query_vector: Float32Array;
file_id?: number | string | null;
limit: number;
seek: number;
}): Promise<BotFileSearchHit[]> {
const nearest = await BunSQLite.sql<{
chunk_id: number;
file_id: number;
scope: string;
owner_id: number;
distance: number;
}>({
sql: `SELECT chunk_id, file_id, scope, owner_id, distance FROM bot_file_vectors WHERE embedding MATCH vec_f32(?) AND k = ? ORDER BY distance`,
values: [query_vector as any, seek],
});
/**
* `BunSQLite.sql` resolves with `success: false` instead of throwing, so
* a broken vector query would otherwise read as "nothing matched" and
* silently fall back to words. A failure here is a real error.
*/
if (!nearest.success) {
throw new Error(`The file search index couldn't be read: ${nearest.error || nearest.msg}`);
}
const allowed = nearest.payload?.filter((row) =>
scopes.some((scope) => scope.scope == row.scope && Number(scope.owner_id) == Number(row.owner_id)),
);
const picked = (allowed || [])
.filter((row) => !file_id || Number(row.file_id) == Number(file_id))
.slice(0, limit);
if (!picked.length) return [];
const chunk_ids = picked.map((row) => Number(row.chunk_id));
const placeholders = chunk_ids.map(() => "?").join(", ");
const chunks = await BunSQLite.sql<{
id: number;
text: string;
chunk_index: number;
name: string;
path: string;
}>({
sql: `SELECT c.id, c.text, c.chunk_index, f.name, f.path FROM bot_file_chunks c
JOIN bot_files f ON f.id = c.file_id
WHERE c.id IN (${placeholders})`,
values: chunk_ids,
});
const by_id = new Map((chunks.payload || []).map((row) => [Number(row.id), row]));
return picked.flatMap((row) => {
const chunk = by_id.get(Number(row.chunk_id));
if (!chunk) return [];
return [
toHit({
chunk_id: row.chunk_id,
file_id: row.file_id,
scope: row.scope,
distance: Number(row.distance),
text: chunk.text,
chunk_index: chunk.chunk_index,
name: chunk.name,
path: chunk.path,
}),
];
});
}
/**
* Chunks whose text has the query's words, newest files first. Used when
* there's no embedding model, and as the fallback when the vectors
* answered nothing.
*/
async function wordSearch({
scopes,
query,
file_id,
limit,
}: {
scopes: StorageScope[];
query: string;
file_id?: number | string | null;
limit: number;
}): Promise<BotFileSearchHit[]> {
const words = [
...new Set(
query
.toLowerCase()
.split(/[^a-z0-9]+/)
.filter((word) => word.length > 2),
),
].slice(0, 6);
if (!words.length) return [];
const scope_sql = scopes.map(() => `(c.scope = ? AND c.owner_id = ?)`).join(" OR ");
const scope_values = scopes.flatMap((s) => [s.scope, s.owner_id]);
const word_sql = words.map(() => `lower(c.text) LIKE ?`).join(" OR ");
const word_values = words.map((word) => `%${word}%`);
const file_clause = file_id ? "AND c.file_id = ?" : "";
const res = await BunSQLite.sql<{
chunk_id: number;
file_id: number;
text: string;
chunk_index: number;
name: string;
path: string;
scope: string;
}>({
sql: `SELECT c.id AS chunk_id, c.file_id, c.text, c.chunk_index, c.scope, f.name, f.path
FROM bot_file_chunks c
JOIN bot_files f ON f.id = c.file_id
WHERE (${scope_sql}) ${file_clause} AND (${word_sql})
ORDER BY f.updated_at DESC, c.chunk_index
LIMIT ?`,
values: [...scope_values, ...(file_id ? [file_id] : []), ...word_values, limit],
});
return (res.payload || []).map((row) => toHit({ ...row, distance: null }));
}
function toHit(row: {
chunk_id: number;
file_id: number;
text: string;
chunk_index: number;
name: string;
path: string;
scope: string;
distance: number | null;
}): BotFileSearchHit {
return {
file_id: Number(row.file_id),
chunk_id: Number(row.chunk_id),
chunk_index: Number(row.chunk_index),
name: row.name || "",
path: row.path || "",
scope: row.scope || "bot",
distance: row.distance == null ? null : Number(row.distance),
text: row.text || "",
};
}
@@ -13,10 +13,8 @@ async function source() {
describe("storeChatAttachments", () => {
/**
* The same text used to be copied onto the new `bot-file` media row,
* so a chat upload kept a second copy of its extracted text (and left
* it behind when the upload was pruned). The text belongs only on the
* message's own `bot-attachment` row, where a reply reads it;
* ingestion re-reads the bytes through `extractBotFileText`.
* so a chat upload kept a second copy of its extracted text. Bot files
* keep no text at all: a bot reads the bytes when it needs them.
*/
test("never writes text_content onto the file's media row", async () => {
const code = await source();
@@ -25,10 +23,10 @@ describe("storeChatAttachments", () => {
expect(code).not.toContain("text_content = ?");
});
test("still saves the bytes and queues the ingest", async () => {
test("saves the bytes and indexes nothing", async () => {
const code = await source();
expect(code).toContain("recordBotFile(");
expect(code).toContain("enqueueFileIngest(");
expect(code).not.toContain("enqueueFileIngest(");
});
});
@@ -1,7 +1,6 @@
import mediaFilePath from "@/src/utils/media-file-path";
import type { BUN_SQLITE_COBBLE_MEDIA } from "@/db/types/db";
import { BotUploadFolder } from "@/src/dict/bot-file-dict";
import enqueueFileIngest from "@/src/file-ingest/enqueue-file-ingest";
import { logError } from "@/src/functions/backend/logger";
import recordBotFile from "./record-bot-file";
@@ -14,7 +13,7 @@ type Params = {
/**
* Copies the files the user attached to a message into the conversation's
* folder (`uploads/<yyyy-mm>/<name>`), so they stay searchable after the
* folder (`uploads/<yyyy-mm>/<name>`), so they stay findable after the
* message scrolls away and a later run can find them. Best effort: a
* failure is logged, never shown, and the message still goes out.
*/
@@ -48,8 +47,8 @@ export default async function storeChatAttachments({
* The text stays on the message's own `bot-attachment` row,
* where a reply reads it. Copying it onto the file's row kept
* the same text twice and left the copy behind when the
* upload was pruned; ingestion re-reads the bytes once
* through `extractBotFileText` instead.
* upload was pruned; a bot reads the bytes with `read_file`
* instead.
*/
const saved = await recordBotFile({
bot_id,
@@ -63,8 +62,6 @@ export default async function storeChatAttachments({
mime_type: file.mime_type || undefined,
user_id,
});
await enqueueFileIngest({ file_id: saved.id! });
} catch (error) {
await logError({
title: `A chat attachment couldn't be saved to the bot's files`,
@@ -1,132 +0,0 @@
import { afterAll, describe, expect, test } from "bun:test";
import { Database } from "bun:sqlite";
import * as sqliteVec from "sqlite-vec";
import fitVectorDimensions from "@/src/harness/embeddings/fit-vector-dimensions";
/**
* The exact insert and nearest-neighbour shape `ingestBotFile` and
* `searchBotFiles` use, on a scratch database: vec0's metadata columns are
* NOT NULL, and a side-car filter can't share the match query.
*/
function createDb() {
const db = new Database(":memory:");
sqliteVec.load(db);
db.run(`CREATE VIRTUAL TABLE bot_file_vectors USING vec0(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at INTEGER,
updated_at INTEGER,
embedding float[8],
+chunk_id INTEGER,
+file_id INTEGER,
+scope TEXT,
+owner_id INTEGER
)`);
return db;
}
const vector = (values: number[]) => fitVectorDimensions({ vector: values, dimensions: 8 });
const insert = (db: Database, row: { chunk_id: number; file_id: number; scope: string; owner_id: number; v: number[] }) => {
const now = Date.now();
db.run(
`INSERT INTO bot_file_vectors (created_at, updated_at, embedding, chunk_id, file_id, scope, owner_id) VALUES (?, ?, vec_f32(?), ?, ?, ?, ?)`,
[now, now, vector(row.v), row.chunk_id, row.file_id, row.scope, row.owner_id],
);
};
describe("bot file vectors", () => {
test("stores a vector and matches the nearest one", () => {
const db = createDb();
insert(db, { chunk_id: 1, file_id: 10, scope: "bot", owner_id: 1, v: [1, 0, 0, 0] });
insert(db, { chunk_id: 2, file_id: 11, scope: "bot", owner_id: 1, v: [0, 1, 0, 0] });
const rows = db
.query(
`SELECT chunk_id, file_id, distance FROM bot_file_vectors WHERE embedding MATCH vec_f32(?1) AND k = 2 ORDER BY distance`,
)
.all(vector([0.9, 0.1, 0, 0])) as { chunk_id: number; distance: number }[];
expect(rows.length).toBe(2);
expect(rows[0]!.chunk_id).toBe(1);
expect(rows[0]!.distance).toBeLessThan(rows[1]!.distance);
});
test("filters scopes in the app, not in the match query", () => {
const db = createDb();
insert(db, { chunk_id: 1, file_id: 10, scope: "bot", owner_id: 1, v: [1, 0] });
insert(db, { chunk_id: 2, file_id: 20, scope: "bot", owner_id: 2, v: [1, 0.05] });
insert(db, { chunk_id: 3, file_id: 30, scope: "workspace", owner_id: 3, v: [1, 0.1] });
const nearest = db
.query(`SELECT chunk_id, scope, owner_id, distance FROM bot_file_vectors WHERE embedding MATCH vec_f32(?1) AND k = 10 ORDER BY distance`)
.all(vector([1, 0])) as { chunk_id: number; scope: string; owner_id: number }[];
expect(nearest.length).toBe(3);
const mine = nearest.filter((row) => row.scope == "bot" && row.owner_id == 1);
expect(mine.map((row) => row.chunk_id)).toEqual([1]);
});
test("a side-car filter in the match query is refused", () => {
const db = createDb();
insert(db, { chunk_id: 1, file_id: 10, scope: "bot", owner_id: 1, v: [1, 0] });
expect(() =>
db
.query(
`SELECT chunk_id FROM bot_file_vectors WHERE embedding MATCH vec_f32(?1) AND k = 2 AND scope = 'bot'`,
)
.all(vector([1, 0])),
).toThrow(/illegal WHERE constraint/);
});
test("an insert without vec0's metadata columns is refused", () => {
const db = createDb();
expect(() =>
db.run(
`INSERT INTO bot_file_vectors (embedding, chunk_id, file_id, scope, owner_id) VALUES (vec_f32(?1), 1, 10, 'bot', 1)`,
[vector([1, 0])],
),
).toThrow();
});
test("deletes a file's vectors by its side-car column", () => {
const db = createDb();
insert(db, { chunk_id: 1, file_id: 10, scope: "bot", owner_id: 1, v: [1, 0] });
db.run(`DELETE FROM bot_file_vectors WHERE file_id = ?`, [10]);
const count = db.query(`SELECT COUNT(*) AS n FROM bot_file_vectors`).get() as { n: number };
expect(count.n).toBe(0);
});
});
describe("fitVectorDimensions", () => {
test("pads a short vector and normalizes it", () => {
const out = fitVectorDimensions({ vector: [3, 4], dimensions: 4 });
const norm = Math.sqrt([...out].reduce((sum, value) => sum + value * value, 0));
expect(out.length).toBe(4);
expect(Math.round(norm * 1000) / 1000).toBe(1);
expect(Math.round(out[0]! * 1000) / 1000).toBe(0.6);
});
test("cuts a long vector", () => {
expect(fitVectorDimensions({ vector: [1, 1, 1, 1, 1, 1], dimensions: 2 }).length).toBe(2);
});
test("leaves a zero vector as zeros", () => {
expect([...fitVectorDimensions({ vector: [0, 0], dimensions: 2 })]).toEqual([0, 0]);
});
});
afterAll(() => {});
@@ -0,0 +1,99 @@
import { Database } from "bun:sqlite";
import { describe, expect, test } from "bun:test";
import clearFinishedJobStepsSql from "./clear-finished-job-steps-sql";
/**
* Clearing a finished job's steps must keep their count on the job (the
* message details show it) and never touch a job that can still run: its
* working row reads them.
*/
function setup() {
const db = new Database(":memory:");
db.run(
`CREATE TABLE bots_queue (id INTEGER PRIMARY KEY, status TEXT, finished_at INTEGER, updated_at INTEGER, steps INTEGER)`,
);
db.run(`CREATE TABLE bots_queue_updates (id INTEGER PRIMARY KEY, bots_queue_id INTEGER)`);
db.run(`INSERT INTO bots_queue VALUES
(1, 'completed', 100, 100, NULL),
(2, 'failed', 100, 100, NULL),
(3, 'processing', NULL, 100, NULL),
(4, 'pending', NULL, 100, NULL),
(5, 'completed', 900, 900, NULL),
(6, 'failed', NULL, 100, NULL),
(7, 'completed', 100, 100, NULL)`);
db.run(`INSERT INTO bots_queue_updates (bots_queue_id) VALUES (1), (1), (1), (2), (3), (3), (4), (5), (6)`);
return db;
}
function run({ db, before }: { db: Database; before: number }) {
const { count, clear } = clearFinishedJobStepsSql({ before });
db.query(count.sql).run(...count.values);
db.query(clear.sql).run(...clear.values);
}
function stepsLeft({ db }: { db: Database }) {
return db
.query(`SELECT bots_queue_id AS id FROM bots_queue_updates ORDER BY id`)
.all()
.map((row) => (row as { id: number }).id);
}
function counts({ db }: { db: Database }) {
return Object.fromEntries(
db
.query(`SELECT id, steps FROM bots_queue ORDER BY id`)
.all()
.map((row) => [(row as { id: number }).id, (row as { steps: number | null }).steps]),
);
}
describe("clearFinishedJobStepsSql", () => {
test("clears the steps of jobs that ended before the cutoff, completed or failed", () => {
const db = setup();
run({ db, before: 500 });
expect(stepsLeft({ db })).not.toContain(1);
expect(stepsLeft({ db })).not.toContain(2);
});
test("keeps how many steps each job recorded", () => {
const db = setup();
run({ db, before: 500 });
expect(counts({ db })[1]).toBe(3);
expect(counts({ db })[2]).toBe(1);
});
test("never touches a job that can still run", () => {
const db = setup();
run({ db, before: 500 });
expect(stepsLeft({ db })).toEqual([3, 3, 4, 5]);
expect(counts({ db })[3]).toBeNull();
expect(counts({ db })[4]).toBeNull();
});
test("leaves a job that ended after the cutoff for the next sweep", () => {
const db = setup();
run({ db, before: 500 });
expect(counts({ db })[5]).toBeNull();
run({ db, before: 1000 });
expect(stepsLeft({ db })).not.toContain(5);
expect(counts({ db })[5]).toBe(1);
});
test("a job lost without a finish time is still cleared by when it last changed", () => {
const db = setup();
run({ db, before: 500 });
expect(stepsLeft({ db })).not.toContain(6);
expect(counts({ db })[6]).toBe(1);
});
test("a job with no steps keeps a null count, and a second sweep changes nothing", () => {
const db = setup();
run({ db, before: 500 });
run({ db, before: 500 });
expect(counts({ db })[7]).toBeNull();
expect(counts({ db })[1]).toBe(3);
});
});
@@ -0,0 +1,29 @@
type Params = {
/**
* Jobs that ended before this (unix ms) have their steps cleared.
*/
before: number;
};
/**
* The two statements that clear the steps (`bots_queue_updates`) of jobs
* that ended before `before`: first each job keeps how many it recorded
* (`bots_queue.steps`, for the message details), then the steps go. A job
* that's still open, or one retrying after a failed attempt, keeps its
* steps. Run in this order: the count reads the rows the delete removes.
*/
export default function clearFinishedJobStepsSql({ before }: Params) {
const finished = `SELECT id FROM bots_queue WHERE status IN ('completed', 'failed') AND COALESCE(finished_at, updated_at) < ?`;
return {
count: {
sql: `UPDATE bots_queue SET steps = COALESCE(steps, 0) + (SELECT COUNT(*) FROM bots_queue_updates u WHERE u.bots_queue_id = bots_queue.id)
WHERE id IN (${finished}) AND EXISTS (SELECT 1 FROM bots_queue_updates u WHERE u.bots_queue_id = bots_queue.id)`,
values: [before],
},
clear: {
sql: `DELETE FROM bots_queue_updates WHERE bots_queue_id IN (${finished})`,
values: [before],
},
};
}
@@ -0,0 +1,21 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import { AppData } from "@/src/data/app-data";
import clearFinishedJobStepsSql from "./clear-finished-job-steps-sql";
/**
* Clears the steps of jobs that ended more than `AppData.BotJobStepsKeepMs`
* ago, however they ended (completed, failed, cancelled, stopped, lost).
* Steps only feed the live working row; once the job's message is in they
* have no reader, and the job keeps their count. Not done the moment a job
* ends: steps reach the page only through the live relay, which would
* miss the last ones.
*/
export default async function clearFinishedJobSteps() {
const { count, clear } = clearFinishedJobStepsSql({
before: Date.now() - AppData.BotJobStepsKeepMs,
});
await BunSQLite.sql(count);
return await BunSQLite.sql(clear);
}
@@ -30,18 +30,16 @@ db.run(
db.run(`INSERT INTO bot_message_media (id, message_id, media_id) VALUES (1, 10, 1)`);
/**
* The query `grabMessagesMedia` builds, with the text column included or
* left out. It is the largest column in the table, so only the reply path
* that hands a document to a model asks for it.
* The query `grabMessagesMedia` builds. The text column is never read: a
* reply reads an attachment's text from the file itself.
*/
function read({ with_text_content }: { with_text_content: boolean }) {
function read() {
const columns = [
"m.id",
"m.title",
"m.media_type",
"m.media_write_path",
"m.mime_type",
...(with_text_content ? ["m.text_content"] : []),
].join(", ");
return db
@@ -55,15 +53,11 @@ function read({ with_text_content }: { with_text_content: boolean }) {
describe("grabMessagesMedia", () => {
test("a plain read never drags the extracted text along", () => {
expect(read({ with_text_content: false })).not.toHaveProperty("text_content");
});
test("the reply path can ask for it", () => {
expect(read({ with_text_content: true }).text_content).toBe("Total: 12");
expect(read()).not.toHaveProperty("text_content");
});
test("the row's name and path are always there", () => {
expect(read({ with_text_content: false })).toMatchObject({
expect(read()).toMatchObject({
title: "invoice.pdf",
media_write_path: "media/1.pdf",
message_id: 10,
@@ -4,12 +4,6 @@ import mediaColumns from "@/src/functions/backend/db/media/media-columns";
type Params = {
message_ids: number[];
/**
* Also read `text_content`, the extracted text a reply hands a
* document to the model as. Off by default: it is the largest column
* in the table and nothing else here uses it.
*/
with_text_content?: boolean;
};
export type MessageMediaRow = BUN_SQLITE_COBBLE_MEDIA & { message_id: number };
@@ -20,14 +14,13 @@ export type MessageMediaRow = BUN_SQLITE_COBBLE_MEDIA & { message_id: number };
*/
export default async function grabMessagesMedia({
message_ids,
with_text_content,
}: Params) {
const by_message = new Map<number, MessageMediaRow[]>();
const ids = [...new Set(message_ids.filter(Boolean))];
if (!ids.length) return by_message;
const columns = mediaColumns({ alias: "m", with_text_content });
const columns = mediaColumns({ alias: "m" });
const rows =
(
@@ -5,9 +5,8 @@
* `AppData.BotAttachmentTextStoreMaxChars` of extracted document text, and
* every caller that only needs a file's name, type or path (`SELECT m.*`
* did) pulled it into memory, into page props and into API responses.
* Read it explicitly where it is really used:
* `src/bots-queue/load-attachment-parts.ts` and
* `src/functions/backend/db/bot-files/ingest-bot-file.ts`.
* Only knowledgebase documents keep it now; read it explicitly where it
* is really used.
*/
export default function mediaColumns({
alias = "m",
@@ -31,8 +30,7 @@ export default function mediaColumns({
];
/**
* Only the two paths that really use it ask for it: the reply that
* hands a document to a model, and ingestion.
* Only a caller that really uses it asks for it.
*/
if (with_text_content) columns.push("text_content");
@@ -1,65 +0,0 @@
import { describe, expect, test } from "bun:test";
import { AppData } from "@/src/data/app-data";
import planTextContentPrune from "./plan-text-content-prune";
describe("planTextContentPrune", () => {
test("clears the raw bytes stored for a type the extractor can't read", () => {
const changes = planTextContentPrune({
rows: [
{ id: 1, title: "logo.svg", mime_type: "image/svg+xml", text_content: "<svg><path/></svg>" },
{ id: 2, title: "bundle.zip", mime_type: "application/zip", text_content: "PK\u0003\u0004" },
{ id: 3, title: "sheet.xlsx", mime_type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", text_content: "PK\u0003\u0004" },
],
});
expect(changes.map((change) => change.media_id)).toEqual([1, 2, 3]);
expect(changes.every((change) => change.text_content === null)).toBe(true);
expect(changes.every((change) => change.after_chars === 0)).toBe(true);
});
test("leaves a document's text alone", () => {
const changes = planTextContentPrune({
rows: [
{ id: 4, title: "invoice.pdf", mime_type: "application/pdf", text_content: "Total: 12" },
{ id: 5, title: "notes.md", mime_type: "text/markdown", text_content: "# Hi" },
{ id: 6, title: "rows.csv", mime_type: "text/csv", text_content: "a,b\n1,2" },
],
});
expect(changes).toEqual([]);
});
test("cuts a document over the store ceiling", () => {
const text = "a".repeat(AppData.BotAttachmentTextStoreMaxChars + 100);
const changes = planTextContentPrune({
rows: [{ id: 7, title: "long.txt", mime_type: "text/plain", text_content: text }],
});
expect(changes).toHaveLength(1);
expect(changes[0]!.after_chars).toBeLessThanOrEqual(AppData.BotAttachmentTextStoreMaxChars);
expect(changes[0]!.text_content).toContain("Truncated");
expect(changes[0]!.before_chars).toBe(text.length);
});
test("nothing to say about a row with no text or no id", () => {
expect(
planTextContentPrune({
rows: [
{ id: 8, title: "empty.pdf", text_content: "" },
{ id: 9, title: "blank.pdf", text_content: " \n " },
{ id: 10, title: "none.pdf" },
{ title: "no id.pdf", text_content: "text" },
],
}),
).toEqual([]);
});
test("counting what each change reclaims is left to the caller", () => {
const changes = planTextContentPrune({
rows: [{ id: 11, title: "icon.svg", mime_type: "image/svg+xml", text_content: "12345" }],
});
expect(changes).toHaveLength(1);
expect(changes[0]!.before_chars - changes[0]!.after_chars).toBe(5);
});
});
@@ -1,55 +0,0 @@
import type { BUN_SQLITE_COBBLE_MEDIA } from "@/db/types/db";
import decideTextContent from "@/src/utils/decide-text-content";
export type TextContentChange = {
media_id: number;
name: string;
before_chars: number;
after_chars: number;
/**
* What the row's `text_content` should become: `null` clears it.
*/
text_content: string | null;
};
type Params = {
/**
* The `media` rows that have any `text_content`.
*/
rows: Partial<BUN_SQLITE_COBBLE_MEDIA>[];
};
/**
* What `pruneMediaTextContent` would change, worked out without touching
* the database: one entry per row whose text belongs nowhere or is over
* the store ceiling, and nothing at all for a row that is already right.
*/
export default function planTextContentPrune({ rows }: Params): TextContentChange[] {
const changes: TextContentChange[] = [];
for (const row of rows) {
if (!row.id) continue;
const before = row.text_content || "";
if (!before.trim()) continue;
const text_content = decideTextContent({
name: row.title,
mime_type: row.mime_type,
text: before,
});
if (text_content === before) continue;
changes.push({
media_id: Number(row.id),
name: row.title || "no name",
before_chars: before.length,
after_chars: text_content?.length || 0,
text_content,
});
}
return changes;
}
@@ -1,120 +0,0 @@
import { describe, expect, test } from "bun:test";
import { Database } from "bun:sqlite";
import { AppData } from "@/src/data/app-data";
import pruneMediaTextContent, { type SqlRunner } from "./prune-media-text-content";
type Row = { id: number; title: string; mime_type: string; media_type: string; text_content: string | null; updated_at?: number };
function makeDb({ rows }: { rows: Row[] }) {
const db = new Database(":memory:");
db.run(
`CREATE TABLE media (
id INTEGER PRIMARY KEY,
title TEXT,
mime_type TEXT,
media_type TEXT,
text_content TEXT,
updated_at INTEGER
)`,
);
const insert = db.prepare(
`INSERT INTO media (id, title, mime_type, media_type, text_content) VALUES (?, ?, ?, ?, ?)`,
);
for (const row of rows) insert.run(row.id, row.title, row.mime_type, row.media_type, row.text_content);
const run: SqlRunner = async ({ sql, values }) => {
const statement = db.prepare(sql);
if (sql.trim().toUpperCase().startsWith("SELECT")) {
return { payload: statement.all(...((values || []) as never[])) as unknown[] };
}
statement.run(...((values || []) as never[]));
return { payload: [] };
};
const read = (id: number) =>
(db.query(`SELECT text_content FROM media WHERE id = ?`).get(id) as { text_content: string | null }).text_content;
return { db, run, read };
}
describe("pruneMediaTextContent", () => {
test("clears raw bytes from a type the extractor can't read, and keeps a document's text", async () => {
const { run, read } = makeDb({
rows: [
{ id: 1, title: "logo.svg", mime_type: "image/svg+xml", media_type: "document", text_content: "<svg><path d='M0 0'/></svg>" },
{ id: 2, title: "bundle.zip", mime_type: "application/zip", media_type: "document", text_content: "PK\u0003\u0004binary" },
{ id: 3, title: "invoice.pdf", mime_type: "application/pdf", media_type: "document", text_content: "Total: 12" },
],
});
const result = await pruneMediaTextContent({ run });
expect(result.checked).toBe(3);
expect(result.cleared).toBe(2);
expect(result.truncated).toBe(0);
expect(read(1)).toBeNull();
expect(read(2)).toBeNull();
expect(read(3)).toBe("Total: 12");
});
test("cuts an oversized document to the store ceiling", async () => {
const text = "a".repeat(AppData.BotAttachmentTextStoreMaxChars + 1000);
const { run, read } = makeDb({
rows: [{ id: 4, title: "huge.txt", mime_type: "text/plain", media_type: "document", text_content: text }],
});
const result = await pruneMediaTextContent({ run });
const after = read(4)!;
expect(result.truncated).toBe(1);
expect(result.cleared).toBe(0);
expect(after.length).toBeLessThanOrEqual(AppData.BotAttachmentTextStoreMaxChars);
expect(after).toContain("Truncated");
expect(result.reclaimed_bytes).toBeGreaterThan(0);
});
test("is safe to rerun: a second sweep changes nothing", async () => {
const { run, read } = makeDb({
rows: [{ id: 5, title: "icon.svg", mime_type: "image/svg+xml", media_type: "document", text_content: "raw" }],
});
await pruneMediaTextContent({ run });
const second = await pruneMediaTextContent({ run });
expect(second.checked).toBe(0);
expect(second.changes).toEqual([]);
expect(second.reclaimed_bytes).toBe(0);
expect(read(5)).toBeNull();
});
test("stamps updated_at on the rows it rewrites", async () => {
const now = 1_700_000_000_000;
const { run, db } = makeDb({
rows: [{ id: 6, title: "x.zip", mime_type: "application/zip", media_type: "document", text_content: "raw" }],
});
await pruneMediaTextContent({ run, now });
expect(
(db.query(`SELECT updated_at FROM media WHERE id = 6`).get() as { updated_at: number }).updated_at,
).toBe(now);
});
test("reports every change with its row, name and sizes", async () => {
const { run } = makeDb({
rows: [{ id: 7, title: "sheet.xlsx", mime_type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", media_type: "document", text_content: "PK\u0003\u0004" }],
});
const result = await pruneMediaTextContent({ run });
expect(result.changes).toEqual([
{ media_id: 7, name: "sheet.xlsx", before_chars: 4, after_chars: 0, text_content: null },
]);
});
});
@@ -1,75 +0,0 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import type { BUN_SQLITE_COBBLE_MEDIA } from "@/db/types/db";
import planTextContentPrune, { type TextContentChange } from "./plan-text-content-prune";
/**
* One statement against the database. `BunSQLite.sql` is the real one; a
* test passes its own, so the whole sweep can be run without the app's
* database.
*/
export type SqlRunner = (params: {
sql: string;
values?: (string | number | null)[];
}) => Promise<{ payload?: unknown[] | null }>;
export type PruneMediaTextResult = {
checked: number;
cleared: number;
truncated: number;
reclaimed_bytes: number;
changes: TextContentChange[];
};
type Params = {
run?: SqlRunner;
/**
* When the sweep runs, for the rows it writes.
*/
now?: number;
};
/**
* Brings every `media.text_content` in line with what may be stored there
* (`planTextContentPrune`): no text on a type the extractor can't read,
* and no text longer than the store ceiling.
*
* Run once per install after the allowlist landed; safe to rerun, since a
* row that is already right is not written.
*/
export default async function pruneMediaTextContent({
run,
now = Date.now(),
}: Params = {}): Promise<PruneMediaTextResult> {
const sql: SqlRunner =
run || (({ sql, values }) => BunSQLite.sql<BUN_SQLITE_COBBLE_MEDIA>({ sql, values }));
const res = await sql({
sql: `SELECT id, title, mime_type, media_type, text_content
FROM media
WHERE text_content IS NOT NULL AND TRIM(text_content) != ''`,
});
const rows = (res.payload || []) as BUN_SQLITE_COBBLE_MEDIA[];
const changes = planTextContentPrune({ rows });
const result: PruneMediaTextResult = {
checked: rows.length,
cleared: 0,
truncated: 0,
reclaimed_bytes: 0,
changes,
};
for (const change of changes) {
await sql({
sql: `UPDATE media SET text_content = ?, updated_at = ? WHERE id = ?`,
values: [change.text_content, now, change.media_id],
});
result.reclaimed_bytes += change.before_chars - change.after_chars;
if (change.text_content === null) result.cleared += 1;
else result.truncated += 1;
}
return result;
}
@@ -0,0 +1,65 @@
import { Database } from "bun:sqlite";
import { describe, expect, test } from "bun:test";
import dropStoredFileTextSql from "./drop-stored-file-text-sql";
function scratch() {
const db = new Database(":memory:");
db.run(`CREATE TABLE media (id INTEGER PRIMARY KEY, text_content TEXT)`);
db.run(`CREATE TABLE knowledgebase_documents (id INTEGER PRIMARY KEY, media_id INTEGER)`);
db.run(`CREATE TABLE file_ingest_queue (id INTEGER PRIMARY KEY, document_id INTEGER)`);
db.run(`CREATE TABLE bot_file_chunks (id INTEGER PRIMARY KEY, text TEXT)`);
db.run(`INSERT INTO media (id, text_content) VALUES (1, 'an attachment'), (2, 'a knowledgebase upload'), (3, NULL)`);
db.run(`INSERT INTO knowledgebase_documents (id, media_id) VALUES (1, 2), (2, NULL)`);
db.run(`INSERT INTO file_ingest_queue (id, document_id) VALUES (1, NULL), (2, 1)`);
db.run(`INSERT INTO bot_file_chunks (id, text) VALUES (1, 'old passage')`);
return db;
}
function run(db: Database) {
for (const sql of dropStoredFileTextSql()) db.run(sql);
}
describe("dropStoredFileTextSql", () => {
test("drops the bot file index", () => {
const db = scratch();
run(db);
const tables = db.query(`SELECT name FROM sqlite_master WHERE name LIKE 'bot_file_%'`).all();
expect(tables).toEqual([]);
});
test("clears upload text but keeps a knowledgebase document's", () => {
const db = scratch();
run(db);
const rows = db.query(`SELECT id, text_content FROM media ORDER BY id`).all();
expect(rows).toEqual([
{ id: 1, text_content: null },
{ id: 2, text_content: "a knowledgebase upload" },
{ id: 3, text_content: null },
]);
});
test("keeps only document jobs on the ingest queue", () => {
const db = scratch();
run(db);
expect(db.query(`SELECT id FROM file_ingest_queue`).all()).toEqual([{ id: 2 }]);
});
test("runs again on a clean database without failing", () => {
const db = scratch();
run(db);
expect(() => run(db)).not.toThrow();
});
});
@@ -0,0 +1,17 @@
/**
* The statements that remove text stored outside knowledgebases: the old
* bot file index (chunks and their vectors; the vec0 drop takes its shadow
* tables with it), file jobs left on the ingest queue, and the extracted
* text uploads used to keep. A knowledgebase document's media keeps its
* text. Every statement is a no-op once it has run.
*/
export default function dropStoredFileTextSql(): string[] {
return [
`DROP TABLE IF EXISTS bot_file_vectors`,
`DROP TABLE IF EXISTS bot_file_chunks`,
`DELETE FROM file_ingest_queue WHERE document_id IS NULL`,
`UPDATE media SET text_content = NULL
WHERE text_content IS NOT NULL
AND id NOT IN (SELECT media_id FROM knowledgebase_documents WHERE media_id IS NOT NULL)`,
];
}
@@ -0,0 +1,46 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import logEvent, { logError } from "@/src/functions/backend/logger";
import dropStoredFileTextSql from "./drop-stored-file-text-sql";
import shouldVacuum from "./should-vacuum";
/**
* Knowledgebases are the only place text is kept for search. Removes what
* bot files and uploads stored before that (`dropStoredFileTextSql`), then
* vacuums once if the database has a lot of free pages, so the space comes
* back on disk. Safe on every start: once clean, it only reads two pragmas.
*/
export default async function dropStoredFileText() {
for (const sql of dropStoredFileTextSql()) {
const res = await BunSQLite.sql({ sql });
if (!res.success) throw new Error(`${sql.split("\n")[0]}: ${res.error || res.msg}`);
}
const [page_count, freelist_count, page_size] = await Promise.all(
["page_count", "freelist_count", "page_size"].map((pragma) =>
BunSQLite.sql<Record<string, number>>({ sql: `PRAGMA ${pragma}` }).then((res) =>
Number(res.payload?.[0]?.[pragma]) || 0,
),
),
);
if (!shouldVacuum({ page_count: page_count!, freelist_count: freelist_count!, page_size: page_size! })) return;
const res = await BunSQLite.sql({ sql: `VACUUM` });
if (!res.success) {
await logError({
title: "Could not vacuum the database",
error: new Error(`${res.error || res.msg}. It needs about as much free disk as the database takes.`),
metadata: { free_bytes: freelist_count! * page_size! },
}).catch(() => {});
return;
}
await logEvent({
title: "Vacuumed the database",
category: "system",
plain_text: `Gave back ${Math.round((freelist_count! * page_size!) / (1024 * 1024))} MB of free pages to the disk.`,
metadata: { freed_bytes: freelist_count! * page_size! },
}).catch(() => {});
}
@@ -0,0 +1,24 @@
import { describe, expect, test } from "bun:test";
import { AppData } from "@/src/data/app-data";
import shouldVacuum from "./should-vacuum";
const page_size = 4096;
const min_pages = Math.ceil(AppData.DbVacuumMinFreeBytes / page_size);
describe("shouldVacuum", () => {
test("vacuums when a large share of a big file is free", () => {
expect(shouldVacuum({ page_count: min_pages * 2, freelist_count: min_pages, page_size })).toBe(true);
});
test("leaves a database with little free space alone", () => {
expect(shouldVacuum({ page_count: min_pages * 100, freelist_count: min_pages, page_size })).toBe(false);
});
test("leaves a small database alone even when mostly free", () => {
expect(shouldVacuum({ page_count: 20, freelist_count: 15, page_size })).toBe(false);
});
test("never vacuums on missing numbers", () => {
expect(shouldVacuum({ page_count: 0, freelist_count: 0, page_size })).toBe(false);
});
});
@@ -0,0 +1,22 @@
import { AppData } from "@/src/data/app-data";
type Params = {
page_count: number;
freelist_count: number;
page_size: number;
};
/**
* Whether enough of the database file sits free to be worth a `VACUUM`
* (`AppData.DbVacuumMinFreeRatio` of its pages and at least
* `AppData.DbVacuumMinFreeBytes`). Deleted rows only free pages inside the
* file; it shrinks on disk only when vacuumed.
*/
export default function shouldVacuum({ page_count, freelist_count, page_size }: Params) {
if (!(page_count > 0) || !(freelist_count > 0)) return false;
return (
freelist_count / page_count >= AppData.DbVacuumMinFreeRatio &&
freelist_count * page_size >= AppData.DbVacuumMinFreeBytes
);
}
@@ -105,7 +105,7 @@ async function grabJob({ job_id }: { job_id: number }): Promise<MessageDetailsJo
const row = (
await BunSQLite.sql<JobRow>({
sql: `SELECT q.id, q.kind, q.title, q.status, q.attempts, q.started_at, q.finished_at,
(SELECT COUNT(*) FROM bots_queue_updates u WHERE u.bots_queue_id = q.id) AS steps
COALESCE(q.steps, 0) + (SELECT COUNT(*) FROM bots_queue_updates u WHERE u.bots_queue_id = q.id) AS steps
FROM bots_queue q WHERE q.id = ? LIMIT 1`,
values: [job_id],
})
@@ -4,7 +4,7 @@ type Params = {
vector: number[];
/**
* Defaults to `AppData.EmbeddingDimensions`, the width of the fixed
* `bot_file_vectors` table.
* `knowledgebase_vectors` table.
*/
dimensions?: number;
};
@@ -64,7 +64,7 @@ export default function listTasksTool({ bot_id, user_id, is_coordinator, admin_w
if (!tasks.length) {
return args.open_only
? `You have no open tasks${args.status ? ` with that status` : ""}.`
? `No open tasks${args.status ? ` with that status` : ""} here. Finished and cancelled ones aren't listed with open_only: list without it before saying a task is gone.`
: `No tasks${args.status ? ` with that status` : ""}.`;
}
@@ -90,7 +90,9 @@ export default function updateTaskTool({ user_id, bot_id, is_coordinator, admin_
updated_by_bot_id: bot_id,
});
if (args.status) said.push(`status is now ${args.status}`);
if (args.status) {
said.push(task.status == args.status ? `status was already ${args.status}` : `status is now ${args.status}`);
}
}
if (args.note?.trim()) {
@@ -3,7 +3,6 @@ import { tmpdir } from "node:os";
import path from "node:path";
import { z } from "zod";
import { AppData } from "@/src/data/app-data";
import enqueueFileIngest from "@/src/file-ingest/enqueue-file-ingest";
import recordBotFile, { grabBotFileRow } from "@/src/functions/backend/db/bot-files/record-bot-file";
import grabComputerMcpUrl from "@/src/functions/backend/db/computers/grab-computer-mcp-url";
import computerNotReadyMessage from "@/src/utils/computer-not-ready-message";
@@ -49,7 +48,7 @@ type Args = {
* Copies what a bot made on its computer into its own files: one file, or
* a whole folder (taken as a gzipped tar and unpacked into one row per
* file). The copies are kept with the app, stay after the computer is
* rebuilt, and are searchable at once; the originals stay on the computer,
* rebuilt, and are readable at once; the originals stay on the computer,
* where the message's folder ref points at the folder holding them.
*
* The bytes come back over the sandbox's streaming download endpoint
@@ -72,7 +71,7 @@ export default function importFileTool({
name: "import_file",
description: `Copy a file or folder from this computer into your own files, so it is kept with the app and stays after the computer is rebuilt. Give its path on the computer, relative to your folder there (${dirs.cwd}), e.g. "seo/coderank/raw/crawl.json" or "seo/coderank/raw"; a folder comes in as it is, in one go. Up to ${max_mb} MB. Check with list_files and the computer's own tools what you already imported: importing the same thing twice just writes over the copy, so do it once.${
own?.scope == "workspace" ? " The copies land in the workspace's shared files." : ""
} Imports are searchable at once. Name a copy with its [file:ID] ref. Import the report you wrote before the last long steps of a run, so it is never lost to the step limit.`,
} Imports are readable at once. Name a copy with its [file:ID] ref. Import the report you wrote before the last long steps of a run, so it is never lost to the step limit.`,
schema: z.object({
path: z
.string()
@@ -190,8 +189,6 @@ async function importOneFile({
user_id: target.user_id,
});
if (saved.id) await enqueueFileIngest({ file_id: saved.id });
if (attach && saved.media_id) {
onAttach?.({ media_id: Number(saved.media_id), name: saved.name || name });
}
@@ -427,8 +424,6 @@ async function unpackArchive({
if (!row?.id) continue;
await enqueueFileIngest({ file_id: row.id }).catch(() => {});
saved.push({ id: Number(row.id), path: row.path || `${folder}/${relative}` });
}
} finally {
@@ -12,6 +12,7 @@ import type { HarnessTool } from "../../types";
import computerDirsShell from "./computer-dirs-shell";
import captureSecretTool from "./capture-secret-tool";
import runComputerCommand from "./run-computer-command";
import withCommandTimeout from "./with-command-timeout";
import withComputerDirs from "./with-computer-dirs";
import withComputerEnv from "./with-computer-env";
import writeComputerEnv from "./write-computer-env";
@@ -65,7 +66,8 @@ export type OpenComputer = {
* shell, files, code and browser. Its envs (`grabComputerEnvs`) are written
* to the computer and loaded by the shell and code tools; secret values are
* redacted from everything the tools return. Every tool is kept in the
* run's folders (`withComputerDirs`), which are created first. Screenshots
* run's folders (`withComputerDirs`), which are created first, and every
* bash command is stopped on the computer (`withCommandTimeout`). Screenshots
* come back as readings from the media reading harness. Call `close` when the run ends.
* Throws when the computer can't be reached, which the queue retries.
*/
@@ -125,7 +127,7 @@ export default async function openComputerTools({
return {
name: target.name,
tools: [
...tools.map((tool) => withComputerDirs({ tool: withComputerEnv({ tool, ...paths }), dirs })),
...tools.map((tool) => withComputerDirs({ tool: withComputerEnv({ tool: withCommandTimeout({ tool }), ...paths }), dirs })),
...(user_id && bot_id && onSecretRef
? [
captureSecretTool({
@@ -0,0 +1,44 @@
import { describe, expect, test } from "bun:test";
import { AppData } from "@/src/data/app-data";
import type { HarnessTool } from "../../types";
import withCommandTimeout from "./with-command-timeout";
const echoTool = (name: string): HarnessTool => ({
name,
description: "Run a command.",
parameters: {},
run: async ({ args }) => JSON.stringify(args),
});
const signal = new AbortController().signal;
const call = async (args: Record<string, unknown>) =>
JSON.parse(await withCommandTimeout({ tool: echoTool("sandbox_execute_bash") }).run({ args, signal }));
const max = AppData.ComputerCommandMaxSeconds;
describe("withCommandTimeout", () => {
test("runs the command under timeout, reading no input, in its own bash", async () => {
const sent = await call({ cmd: "echo 'hi'", timeout: 20 });
expect(sent.cmd).toStartWith(`timeout -k 5 20 bash -c 'echo '\\''hi'\\''' < /dev/null; `);
expect(sent.cmd).toContain("[Stopped after 20s");
expect(sent.cmd).toEndWith("(exit $_cobble_status)");
expect(sent.cmd).not.toMatch(/(^|; )exit /);
});
test("keeps the timeout between 1 second and the cap, the cap without one", async () => {
expect((await call({ cmd: "ls" })).cmd).toStartWith(`timeout -k 5 ${max} `);
expect((await call({ cmd: "ls", timeout: 0 })).cmd).toStartWith(`timeout -k 5 ${max} `);
expect((await call({ cmd: "ls", timeout: 100000 })).cmd).toStartWith(`timeout -k 5 ${max} `);
expect((await call({ cmd: "ls", timeout: 2.5 })).cmd).toStartWith(`timeout -k 5 3 `);
expect(max * 1000).toBeLessThan(AppData.McpToolTimeoutMs);
});
test("tells the model how commands run", () => {
expect(withCommandTimeout({ tool: echoTool("sandbox_execute_bash") }).description).toContain("reads no input");
});
test("leaves other tools alone", () => {
const tool = echoTool("sandbox_execute_code");
expect(withCommandTimeout({ tool })).toBe(tool);
});
});
@@ -0,0 +1,64 @@
import { AppData } from "@/src/data/app-data";
import type { HarnessTool } from "../../types";
import { shellQuote } from "./download-computer-file";
type Params = {
tool: HarnessTool;
};
/**
* Makes the computer's bash tool stop its commands on the computer. The
* sandbox ignores the tool's `timeout` and runs every call in one shared
* shell session, so a command that never ends (a network read without a
* timeout, a password prompt) used to keep running after the call gave up,
* and every later command on that computer, from any run, waited behind
* it. Each command now runs under `timeout` (its own, at most
* `ComputerCommandMaxSeconds`), reading no input, in its own bash, and
* says so when it was stopped. Other tools pass through.
*/
export default function withCommandTimeout({ tool }: Params): HarnessTool {
if (tool.name != "sandbox_execute_bash") return tool;
const max = AppData.ComputerCommandMaxSeconds;
return {
...tool,
description: `${tool.description}\n\nEvery command runs in a fresh shell (cd and export don't carry over to the next call), reads no input (anything waiting for a prompt fails at once: pass -y, a password through its env, and so on) and is stopped after timeout seconds (at most ${max}, ${max} when not given). Start longer work in the background (nohup ... > log 2>&1 &) and check its log in later calls.`,
run: async ({ args, signal }) => {
if (typeof args?.cmd != "string") return tool.run({ args, signal });
const seconds = commandSeconds({ timeout: args.timeout });
return tool.run({
args: { ...args, timeout: seconds + 10, cmd: timedCommand({ cmd: args.cmd, seconds }) },
signal,
});
},
};
}
/**
* The command's own timeout in whole seconds, kept to 1..`ComputerCommandMaxSeconds`.
*/
function commandSeconds({ timeout }: { timeout?: unknown }) {
const max = AppData.ComputerCommandMaxSeconds;
const asked = typeof timeout == "number" ? timeout : Number(timeout);
if (!Number.isFinite(asked) || asked <= 0) return max;
return Math.min(max, Math.max(1, Math.ceil(asked)));
}
/**
* `cmd` under `timeout` (TERM, then KILL 5 seconds later, to its whole
* process group), with its exit code kept. Never `exit`s: the sandbox's
* session shell would go with it.
*/
function timedCommand({ cmd, seconds }: { cmd: string; seconds: number }) {
return [
`timeout -k 5 ${seconds} bash -c ${shellQuote({ text: cmd })} < /dev/null`,
`_cobble_status=$?`,
`if [ $_cobble_status -eq 124 ] || [ $_cobble_status -eq 137 ]; then echo "[Stopped after ${seconds}s: the command timed out. Run long work in the background and check its log.]"; fi`,
`(exit $_cobble_status)`,
].join("; ");
}
@@ -1,5 +1,4 @@
import { z } from "zod";
import enqueueFileIngest from "@/src/file-ingest/enqueue-file-ingest";
import recordBotFile from "@/src/functions/backend/db/bot-files/record-bot-file";
import generateImage from "@/src/harness/image/generate-image";
import type { HarnessTool } from "../../types";
@@ -98,8 +97,6 @@ export default function generateImageTool(context: StorageToolContext): HarnessT
user_id: context.user_id,
});
await enqueueFileIngest({ file_id: file.id! });
if (args.attach && file.media_id) {
context.onAttach?.({ media_id: Number(file.media_id), name: file.name || "" });
}
@@ -1,11 +1,9 @@
import type { HarnessTool } from "../../types";
import copyFileTool from "./copy-file-tool";
import deleteFileTool from "./delete-file-tool";
import ingestFileTool from "./ingest-file-tool";
import listFilesTool from "./list-files-tool";
import moveFileTool from "./move-file-tool";
import readFileTool from "./read-file-tool";
import searchFilesTool from "./search-files-tool";
import type { StorageToolContext } from "./storage-tool-context";
import writeFileTool from "./write-file-tool";
@@ -19,10 +17,8 @@ export default function grabStorageTools(context: StorageToolContext): HarnessTo
listFilesTool(context),
readFileTool(context),
writeFileTool(context),
searchFilesTool(context),
moveFileTool(context),
deleteFileTool(context),
ingestFileTool(context),
...(context.scopes.length > 1 ? [copyFileTool(context)] : []),
];
}
@@ -1,44 +0,0 @@
import { z } from "zod";
import { AppData } from "@/src/data/app-data";
import ingestBotFile from "@/src/functions/backend/db/bot-files/ingest-bot-file";
import type { HarnessTool } from "../../types";
import type { StorageToolContext } from "./storage-tool-context";
type Args = {
file_id: number;
read_media?: boolean;
};
/**
* Ingests a file again: reads its text, chunks it and embeds it, so a
* changed or previously failed file is searchable. Images and audio are
* only read when asked for, since that costs a model call.
*/
export default function ingestFileTool(_context: StorageToolContext): HarnessTool<Args> {
return {
name: "ingest_file",
description: `Make one of your files searchable again (a course correction when search misses something you know is in it), by its ref id. Images and scans are only read when "read_media" is true.`,
schema: z.object({
file_id: z.number().int().positive(),
read_media: z.boolean().optional(),
}),
run: async ({ args, signal }) => {
const result = await ingestBotFile({
file_id: args.file_id,
read_media: args.read_media,
signal,
});
switch (result.status) {
case "ingested":
return `Ingested: ${result.chunks} passage(s), searchable now.`;
case "unavailable":
return `Its text is stored and searchable by words, but no embedding model is set, so meaning search is off.`;
case "skipped":
return `It's too large to read (over ${Math.round(AppData.IngestMaxBytes / (1024 * 1024))} MB).`;
default:
return `It has no text to read.`;
}
},
};
}
+1 -2
View File
@@ -50,9 +50,8 @@ export default function listFilesTool(context: StorageToolContext): HarnessTool<
const lines = files.map((file) => {
const where = file.scope == "workspace" ? ` (workspace ${file.owner_id})` : "";
const ingested = file.ingested_at ? "" : " - not searchable yet";
return `- ${file.path} - ${formatBytes({ bytes: Number(file.size) || 0 })}${where}${ingested} ${fileRef({ file_id: file.id! })}`;
return `- ${file.path} - ${formatBytes({ bytes: Number(file.size) || 0 })}${where} ${fileRef({ file_id: file.id! })}`;
});
const more =
@@ -1,6 +1,5 @@
import { z } from "zod";
import { PDFDocument, StandardFonts, rgb } from "pdf-lib";
import enqueueFileIngest from "@/src/file-ingest/enqueue-file-ingest";
import recordBotFile from "@/src/functions/backend/db/bot-files/record-bot-file";
import type { HarnessTool } from "../../types";
import {
@@ -69,8 +68,6 @@ export default function makePdfTool(context: StorageToolContext): HarnessTool<Ar
user_id: context.user_id,
});
await enqueueFileIngest({ file_id: file.id! });
if (args.attach && file.media_id) {
context.onAttach?.({ media_id: Number(file.media_id), name: file.name || "" });
}
@@ -1,49 +0,0 @@
import { z } from "zod";
import { AppData } from "@/src/data/app-data";
import searchBotFiles from "@/src/functions/backend/db/bot-files/search-bot-files";
import type { HarnessTool } from "../../types";
import { fileRef, type StorageToolContext } from "./storage-tool-context";
type Args = {
query: string;
file_id?: number;
limit?: number;
};
/**
* Searches the bot's saved files by meaning (through the embedding
* vectors), falling back to words, and returns the passages with their
* file's ref so the bot can read more.
*/
export default function searchFilesTool(context: StorageToolContext): HarnessTool<Args> {
return {
name: "search_files",
description: `Search the files you have saved (documents the user gave you and what you wrote) and get the passages that answer "query", with their file's ref. Search before saying you don't know something that may be in a file, and before writing a document from memory.`,
schema: z.object({
query: z.string().min(1).max(500),
file_id: z.number().int().positive().optional().describe(`Only this file's passages`),
limit: z.number().int().min(1).max(20).optional(),
}),
run: async ({ args, signal }) => {
const result = await searchBotFiles({
scopes: context.scopes,
query: args.query,
file_id: args.file_id,
limit: args.limit || AppData.RagTopChunks,
signal,
bot_id: context.bot_id,
workspace_id: context.workspace_id,
});
if (!result.hits.length) return result.note;
const passages = result.hits.map((hit, index) => {
const where = hit.scope == "workspace" ? ` (workspace ${hit.scope})` : "";
return `## ${index + 1}. ${hit.name}${where} ${fileRef({ file_id: hit.file_id })}\n${hit.text.trim()}`;
});
return `${result.note}\n\n${passages.join("\n\n")}`;
},
};
}
+2 -5
View File
@@ -1,7 +1,6 @@
import { z } from "zod";
import { AppData } from "@/src/data/app-data";
import type { BotFilePurpose } from "@/src/dict/bot-file-dict";
import enqueueFileIngest from "@/src/file-ingest/enqueue-file-ingest";
import recordBotFile from "@/src/functions/backend/db/bot-files/record-bot-file";
import type { HarnessTool } from "../../types";
import {
@@ -23,7 +22,7 @@ type Args = {
/**
* Writes a text file (markdown, CSV, JSON, code, plain text) into one of
* the bot's directories, making folders as needed. The file stays for
* later runs and becomes searchable.
* later runs.
*/
export default function writeFileTool(context: StorageToolContext): HarnessTool<Args> {
const own = context.scopes[0];
@@ -32,7 +31,7 @@ export default function writeFileTool(context: StorageToolContext): HarnessTool<
name: "write_file",
description: `Save a text file for later: markdown, CSV, JSON, code or plain text. Give it a path like "reports/q3.md"; folders are made as needed. Files belong to ${
own?.scope == "workspace" ? "the workspace you're working in" : "you"
} and stay between runs: write a file before telling the user something is saved, and search_files to find one you wrote before. It is readable and searchable at once.${
} and stay between runs: write a file before telling the user something is saved, and list_files (by name or path) to find one you wrote before. It is readable at once.${
context.scopes.length > 1
? ` "scope" picks a directory: ${context.scopes.map((s) => `${s.scope}:${s.owner_id}`).join(", ")}.`
: ""
@@ -74,8 +73,6 @@ export default function writeFileTool(context: StorageToolContext): HarnessTool<
user_id: context.user_id,
});
await enqueueFileIngest({ file_id: file.id! });
if (args.attach && file.media_id) {
context.onAttach?.({ media_id: Number(file.media_id), name: file.name || "" });
}
+30
View File
@@ -0,0 +1,30 @@
import { useEffect, useState } from "react";
import { AppData } from "@/src/data/app-data";
type Params<T> = {
value: T;
delay_ms?: number;
};
/**
* `value` once it has stopped changing for `delay_ms`
* (`AppData.SearchDebounceMs` by default), so a search box searches as the
* user types without a request per keystroke. An emptied value comes
* through at once.
*/
export default function useDebouncedValue<T>({ value, delay_ms = AppData.SearchDebounceMs }: Params<T>) {
const [debounced, setDebounced] = useState(value);
useEffect(() => {
if (!value) {
setDebounced(value);
return;
}
const timer = setTimeout(() => setDebounced(value), delay_ms);
return () => clearTimeout(timer);
}, [value, delay_ms]);
return debounced;
}
@@ -1,6 +1,6 @@
import fetchApi from "@moduletrace/twui/utils/fetch/fetchApi";
import type { APIResponseObject } from "@moduletrace/bunext/types";
import { useCallback, useContext, useEffect, useState } from "react";
import { useCallback, useContext, useEffect, useRef, useState } from "react";
import { AppContext } from "@/src/pages/__root";
import type { BotFileScopesPayload } from "@/src/types";
@@ -12,22 +12,30 @@ type Params = {
* the first page of files).
*/
enabled?: boolean;
/**
* Only files whose name or path has this.
*/
search?: string;
};
/**
* A conversation's files: the page's own copy first, then the folder and
* search the modal asks for. Saves and deletes refresh it.
* search the modal asks for (reloaded when the search changes; an answer
* that a newer request overtook is dropped). Saves and deletes refresh it.
*/
export default function useBotFiles({ bot_id, workspace_id, enabled = true }: Params) {
export default function useBotFiles({ bot_id, workspace_id, enabled = true, search }: Params) {
const { pageProps } = useContext(AppContext);
const [data, setData] = useState<BotFileScopesPayload | undefined>(
pageProps?.bot_files || undefined,
);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string>();
const latest = useRef(0);
const load = useCallback(
async (params?: { folder?: string; search?: string }) => {
const request = ++latest.current;
setLoading(true);
setError(undefined);
@@ -44,6 +52,8 @@ export default function useBotFiles({ bot_id, workspace_id, enabled = true }: Pa
{ method: "GET" },
).catch(() => null);
if (request != latest.current) return;
setLoading(false);
if (!res?.success || !res.singleRes) {
@@ -57,8 +67,8 @@ export default function useBotFiles({ bot_id, workspace_id, enabled = true }: Pa
);
useEffect(() => {
if (enabled) load().catch(() => {});
}, [enabled, load]);
if (enabled) load({ search }).catch(() => {});
}, [enabled, load, search]);
return { data, loading, error, reload: load, setData };
}
@@ -1,6 +1,10 @@
import { useEffect, useRef } from "react";
import { AppData } from "@/src/data/app-data";
import type { BotMessageWithAttachments, WebSocketDataType } from "@/src/types";
import type {
BotMessageWithAttachments,
SessionMessagesResponse,
WebSocketDataType,
} from "@/src/types";
import postSessionApi from "@/src/functions/frontend/post-session-api";
type Params = {
@@ -19,9 +23,13 @@ type Params = {
* While a reply is due, checks the session's latest messages every
* `AppData.BotReplyCatchUpMs`. Replies arrive live; this only catches one
* whose push was missed (the socket was down at the time), so the
* thinking row can't stay up until a reload. A missed message is replayed
* thinking row can't stay up until a reload. It also checks at once when
* the page comes back into view or online. A missed message is replayed
* as the `server:bot-message` it would have been, so the thread, the rail
* and the unread flag all take it the usual way.
* and the unread flag all take it the usual way. The bot's face, status
* line and active task are replayed too, as `server:bot-updated`, so a
* reply that ended without a final message (its face push missed) can't
* leave the bot working on screen.
*/
export default function useReplyCatchUp({ session_id, awaiting_reply, last_message_id }: Params) {
const last_message = useRef(Number(last_message_id) || 0);
@@ -33,28 +41,52 @@ export default function useReplyCatchUp({ session_id, awaiting_reply, last_messa
let stale = false;
const timer = setInterval(async () => {
const res = await postSessionApi<BotMessageWithAttachments>({
const replay = ({ data }: { data: WebSocketDataType }) =>
window.dispatchEvent(new CustomEvent("wsDataEvent", { detail: { data, message: data } }));
const check = async () => {
const res = (await postSessionApi<BotMessageWithAttachments>({
path: "session-messages",
body: { session_id },
}).catch(() => null);
}).catch(() => null)) as SessionMessagesResponse | null;
if (stale || !res?.success || !Array.isArray(res.payload)) return;
const missed = (res.payload as BotMessageWithAttachments[]).filter(
const missed = res.payload.filter(
(message) => Number(message.id) > last_message.current,
);
for (const message of missed) {
const data: WebSocketDataType = { event: "server:bot-message", data: message };
window.dispatchEvent(new CustomEvent("wsDataEvent", { detail: { data, message: data } }));
replay({ data: { event: "server:bot-message", data: message } });
}
}, AppData.BotReplyCatchUpMs);
/**
* After the messages, so a reply that ended silently (its last
* word an interim one) drops the row once its face moves on.
*/
if (res.bot?.id) replay({ data: { event: "server:bot-updated", data: res.bot } });
};
/**
* Coming back to the app (a notification tapped, the PWA brought
* forward) checks at once: the socket was likely down meanwhile.
*/
const onVisible = () => {
if (document.visibilityState == "visible") check();
};
const timer = setInterval(check, AppData.BotReplyCatchUpMs);
window.addEventListener("online", check);
window.addEventListener("pageshow", check);
document.addEventListener("visibilitychange", onVisible);
return () => {
stale = true;
clearInterval(timer);
window.removeEventListener("online", check);
window.removeEventListener("pageshow", check);
document.removeEventListener("visibilitychange", onVisible);
};
}, [awaiting_reply, session_id]);
}
@@ -12,6 +12,7 @@ import type { BUN_SQLITE_COBBLE_WORKSPACES } from "@/db/types/db";
import type { BotWithLastMessage } from "@/src/types";
import HeaderIconButton from "@/src/components/general/header-icon-button";
import { AppContext } from "@/src/pages/__root";
import useDebouncedValue from "@/src/hooks/use-debounced-value";
import canManageCrew from "@/src/utils/rbac/can-manage-crew";
import useBotFiles from "../(hooks)/use-bot-files";
import FileRow from "./file-row";
@@ -29,21 +30,23 @@ type Props = {
/**
* The files a conversation works with: a bot's own folder, or the
* workspace's shared one. Each can be opened, read again so it can be
* searched, or deleted (a crew manager only). The list loads when the
* modal opens, and search goes through the same endpoint, so the page
* props stay small.
* workspace's shared one. Each can be opened or deleted (a crew manager
* only). The list loads when the modal opens, and search (by name or path,
* as the user types) goes through the same endpoint, so the page props
* stay small.
*/
export default function BotFilesModal({ bot, workspace, menu, on_open }: Props) {
const { pageProps } = useContext(AppContext);
const [open, setOpen] = useState(false);
const [search, setSearch] = useState("");
const query = useDebouncedValue({ value: search.trim() });
const [busy_id, setBusyId] = useState<number>();
const [status, setStatus] = useState<string>();
const { data, loading, error, reload } = useBotFiles({
bot_id: Number(bot.id),
workspace_id: workspace?.id ? Number(workspace.id) : null,
enabled: open,
search: query,
});
const can_manage = canManageCrew({ user_types: pageProps?.user_types });
@@ -76,7 +79,7 @@ export default function BotFilesModal({ bot, workspace, menu, on_open }: Props)
setBusyId(undefined);
setStatus(res?.success ? done : res?.msg || `That didn't work.`);
if (res?.success) await reload({ search });
if (res?.success) await reload({ search: query });
};
const files = data?.files || [];
@@ -105,7 +108,7 @@ export default function BotFilesModal({ bot, workspace, menu, on_open }: Props)
<H2 className="text-lg! leading-6! tracking-normal! font-bold text-ink">{title}</H2>
<Span className="text-[13px] leading-[18px] text-ink-secondary">
{workspace?.id
? `Files every bot here can read and search. They're kept with the app, not on a computer.`
? `Files every bot here can find and read. They're kept with the app, not on a computer.`
: `What ${name} has saved: documents you sent, files it wrote and images it made. They're kept with the app, not on a computer.${can_manage ? " Deleting one removes it for good." : ""}`}
</Span>
{usage ? (
@@ -128,13 +131,10 @@ export default function BotFilesModal({ bot, workspace, menu, on_open }: Props)
<Stack className="w-full mt-4 gap-2! items-stretch">
<Input
placeholder="Search files"
placeholder="Search by name or path"
value={search}
onChange={(event) => setSearch(event.target.value)}
onKeyDown={(event) => {
if (event.key == "Enter") reload({ search }).catch(() => {});
}}
clearInputProps={{ onClick: () => reload({}).catch(() => {}) }}
clearInputProps={{ onClick: () => setSearch("") }}
/>
</Stack>
@@ -146,14 +146,6 @@ export default function BotFilesModal({ bot, workspace, menu, on_open }: Props)
file={file}
can_manage={can_manage}
busy={busy_id == file.id}
onIngest={() =>
run({
url: `/api/admin/bots/ingest-file`,
payload: { file_id: file.id },
file_id: file.id,
done: `${file.name} will be read again shortly.`,
})
}
onDelete={() =>
run({
url: `/api/admin/bots/delete-file`,
@@ -168,7 +160,7 @@ export default function BotFilesModal({ bot, workspace, menu, on_open }: Props)
<Span className="py-6 text-[13px] leading-[18px] text-ink-secondary">
{loading
? "Loading files..."
: error || (search ? `Nothing matches "${search}".` : "No files yet.")}
: error || (query ? `Nothing matches "${query}".` : "No files yet.")}
</Span>
)}
</Stack>
@@ -3,7 +3,7 @@ import Row from "@moduletrace/twui/layout/Row";
import Span from "@moduletrace/twui/layout/Span";
import Stack from "@moduletrace/twui/layout/Stack";
import Button from "@moduletrace/twui/layout/Button";
import { RefreshCw, Trash2 } from "lucide-react";
import { Trash2 } from "lucide-react";
import type { MediaType, PublicBotFile } from "@/src/types";
import relativeTime from "@/src/utils/relative-time";
import AttachmentIcon from "./attachment-icon";
@@ -11,19 +11,18 @@ import AttachmentIcon from "./attachment-icon";
type Props = {
file: PublicBotFile;
/**
* A crew manager may delete and re-ingest.
* A crew manager may delete it.
*/
can_manage: boolean;
busy?: boolean;
onIngest: () => void;
onDelete: () => void;
};
/**
* One file in the files modal: its type, path, size and age, and what can
* be done with it. A file that isn't searchable says why.
* be done with it.
*/
export default function FileRow({ file, can_manage, busy, onIngest, onDelete }: Props) {
export default function FileRow({ file, can_manage, busy, onDelete }: Props) {
return (
<Row className="w-full gap-3! flex-nowrap items-start py-2 border-0 border-b border-solid border-hairline last:border-b-0">
<Row className="mt-0.5 text-ink-secondary">
@@ -44,11 +43,6 @@ export default function FileRow({ file, can_manage, busy, onIngest, onDelete }:
file.scope == "workspace" ? "Workspace" : "Bot files",
`${Math.max(1, Math.round(file.size / 1024))} KB`,
file.created_at ? relativeTime({ at: file.created_at }) : "",
file.ingest_error
? file.ingest_error
: file.ingested_at
? "searchable"
: "not searchable yet",
]
.filter(Boolean)
.join(" · ")}
@@ -56,19 +50,6 @@ export default function FileRow({ file, can_manage, busy, onIngest, onDelete }:
</Stack>
{can_manage ? (
<Row className="shrink-0 gap-0!">
<Button
title="Read it again so it can be searched"
aria-label="Reingest"
type="button"
variant="ghost"
color="gray"
size="small"
disabled={busy}
onClick={onIngest}
className="p-1.5!"
>
<RefreshCw size={14} aria-hidden />
</Button>
<Button
title="Delete it"
aria-label="Delete"
@@ -197,11 +197,13 @@ export default function Conversation() {
const last_thread_message = thread_messages[thread_messages.length - 1];
/**
* A session moved on from gets no reply here: the bot answered in the
* new one.
* new one. The face is the live one, as the thinking row reads it: the
* page's is from when it rendered, so after an interim message the
* catch-up would stop while the row stays up.
*/
const awaiting_reply =
!moved_to_session_id &&
isAwaitingReply({ last_message: last_thread_message, face: bot?.face });
isAwaitingReply({ last_message: last_thread_message, face: live_bot?.face });
const reply_progress = useReplyProgress({
bot_id: bot?.id || 0,
@@ -2,26 +2,20 @@ import Input from "@moduletrace/twui/form/Input";
type Props = {
value: string;
loading?: boolean;
onChange: (search: string) => void;
onSubmit: (search: string) => void;
};
/**
* Narrows the open folder by name or path. Enter runs the search; clearing
* the field runs it again empty.
* Narrows the open folder by name or path as the user types (the pane
* debounces the search).
*/
export default function FilesSearchInput({ value, loading, onChange, onSubmit }: Props) {
export default function FilesSearchInput({ value, onChange }: Props) {
return (
<Input
placeholder="Search this folder"
placeholder="Search this folder by name"
value={value}
disabled={loading}
onChange={(event) => onChange(event.target.value)}
onKeyDown={(event) => {
if (event.key == "Enter") onSubmit(event.currentTarget.value);
}}
clearInputProps={{ onClick: () => onSubmit("") }}
clearInputProps={{ onClick: () => onChange("") }}
wrapperWrapperProps={{ className: "w-full mt-3" }}
/>
);
@@ -21,7 +21,6 @@ type Props = {
loading?: boolean;
error?: string;
onSearch: (search: string) => void;
onSubmitSearch: () => void;
onOpenFolder: (path: string) => void;
onOpenVolume: (option: StorageSpecOption) => void;
};
@@ -40,7 +39,6 @@ export default function FilesBrowser({
loading,
error,
onSearch,
onSubmitSearch,
onOpenFolder,
onOpenVolume,
}: Props) {
@@ -89,12 +87,7 @@ export default function FilesBrowser({
crumbs={folder?.breadcrumbs || []}
onOpenFolder={onOpenFolder}
/>
<FilesSearchInput
value={search}
loading={loading}
onChange={onSearch}
onSubmit={onSubmitSearch}
/>
<FilesSearchInput value={search} onChange={onSearch} />
<Stack className="w-full mt-2 gap-0! items-stretch">
{folder?.entries.length ? (
folder.entries.map((entry) => (
@@ -1,4 +1,5 @@
import { useContext, useState } from "react";
import useDebouncedValue from "@/src/hooks/use-debounced-value";
import { AppContext } from "@/src/pages/__root";
import type { StorageSpecOption } from "@/src/types";
import { parseStorageSpec, type StorageSpec } from "@/src/utils/storage-volumes";
@@ -20,7 +21,7 @@ export default function FilesPane() {
);
const [path, set_path] = useState(page_folder?.path || "");
const [search, set_search] = useState("");
const [query, set_query] = useState("");
const query = useDebouncedValue({ value: search.trim() });
const { folder, loading, error } = useStorageFolder({
spec,
path,
@@ -38,7 +39,6 @@ export default function FilesPane() {
set_spec(next_spec);
set_path(next_path);
set_search("");
set_query("");
const url = new URL(window.location.href);
@@ -63,7 +63,6 @@ export default function FilesPane() {
loading={loading}
error={error}
onSearch={set_search}
onSubmitSearch={() => set_query(search)}
onOpenFolder={(next_path: string) => navigate({ next_spec: spec, next_path })}
onOpenVolume={(option: StorageSpecOption) =>
navigate({ next_spec: option.spec, next_path: "" })
@@ -8,6 +8,7 @@ import type { BunextPageServerFn } from "@moduletrace/bunext/types";
import grabRunLimitsOverview from "@/src/functions/backend/db/app-settings/grab-run-limits";
import grabSkillLimitsOverview from "@/src/functions/backend/db/app-settings/grab-skill-limits";
import grabHookCallRetentionOverview from "@/src/functions/backend/db/app-settings/grab-hook-events-keep-days";
import grabLogRetentionOverview from "@/src/functions/backend/db/app-settings/grab-logs-keep-days";
import grabChatroomRetentionOverview from "@/src/functions/backend/db/app-settings/grab-chatroom-retention";
import grabSessionLengthOverview from "@/src/functions/backend/db/app-settings/grab-bot-session-max-messages";
import grabStorageQuotaOverview from "@/src/functions/backend/db/app-settings/grab-storage-quota-overview";
@@ -53,6 +54,7 @@ export default async function grabSettingsServerProps({ req }: Params) {
run_limits: await grabRunLimitsOverview().catch(() => null),
skill_limits: await grabSkillLimitsOverview().catch(() => null),
hook_call_retention: await grabHookCallRetentionOverview().catch(() => null),
log_retention: await grabLogRetentionOverview().catch(() => null),
chatroom_retention: await grabChatroomRetentionOverview().catch(() => null),
session_length: await grabSessionLengthOverview().catch(() => null),
running_version: runningVersion({ env: process.env }),
@@ -4,7 +4,7 @@ import type { DatabaseTableSummary } from "@/src/types";
import DatabaseTableRow from "./database-table-row";
const table: DatabaseTableSummary = {
name: "bot_file_vectors",
name: "knowledgebase_vectors",
type: "virtual",
rows: 12,
size_bytes: 2048,
@@ -16,7 +16,7 @@ describe("DatabaseTableRow", () => {
test("names the table, its record count and its size", () => {
const html = renderToString(<DatabaseTableRow table={table} selected={false} onOpen={() => {}} />);
expect(html).toContain("bot_file_vectors");
expect(html).toContain("knowledgebase_vectors");
expect(html).toContain("12");
expect(html).toContain("2.0 KB");
});
@@ -24,7 +24,7 @@ describe("DatabaseTableRow", () => {
test("links to the table's own address", () => {
const html = renderToString(<DatabaseTableRow table={table} selected={false} onOpen={() => {}} />);
expect(html).toContain(`href="/admin/settings/database?table=bot_file_vectors"`);
expect(html).toContain(`href="/admin/settings/database?table=knowledgebase_vectors"`);
});
test("a table with no count yet shows a dash", () => {
@@ -0,0 +1,109 @@
import { useState } from "react";
import fetchApi from "@moduletrace/twui/utils/fetch/fetchApi";
import type { APIResponseObject } from "@moduletrace/bunext/types";
import Form from "@moduletrace/twui/form/Form";
import Input from "@moduletrace/twui/form/Input";
import type { LogRetentionOverview } from "@/src/types";
import SettingsSaveBar from "../../(partials)/settings-save-bar";
type Props = {
retention?: LogRetentionOverview | null;
};
/**
* How many days logs are kept, saved on its own without a reload.
* Zero puts the code's default back.
*/
export default function LogRetentionForm({ retention }: Props) {
const [overview, setOverview] = useState(retention || undefined);
const [dirty, setDirty] = useState(false);
const [keep_days, setKeepDays] = useState(String(retention?.keep_days ?? ""));
/**
* The input only reads `defaultValue` when it mounts, so discarding or
* saving remounts it with the saved number.
*/
const [form_key, setFormKey] = useState(0);
const [loading, setLoading] = useState(false);
const [status, setStatus] = useState<{
msg?: string;
error?: boolean;
success?: boolean;
}>();
const save = async () => {
if (!window.confirm("Save how long logs are kept? Entries older than that are deleted at the next daily prune.")) return;
setLoading(true);
setStatus(undefined);
const res = await fetchApi<{}, APIResponseObject<LogRetentionOverview>>(
"/api/admin/settings/log-retention",
{
method: "POST",
body: { keep_days: Number(keep_days) || 0 },
},
).catch(() => null);
setLoading(false);
if (!res?.success || !res.singleRes) {
setStatus({
error: true,
msg: res?.msg || `Couldn't save it.`,
});
return;
}
setOverview(res.singleRes);
setKeepDays(String(res.singleRes.keep_days));
setFormKey((key) => key + 1);
setDirty(false);
setStatus({
success: true,
msg: `Saved: logs are kept for ${res.singleRes.keep_days} days.`,
});
};
const discard = () => {
setKeepDays(String(overview?.keep_days ?? ""));
setFormKey((key) => key + 1);
setDirty(false);
setStatus(undefined);
};
return (
<Form
onSubmit={(event) => {
event.preventDefault();
save().catch(() => {});
}}
className="w-full gap-3! items-stretch"
>
<Input
key={`keep-${form_key}`}
label="Keep activity for"
type="number"
min={0}
max={overview?.max}
step={1}
numberText
rawNumber
defaultValue={keep_days}
onChange={(event) => {
setKeepDays(event.target.value);
setDirty(true);
setStatus(undefined);
}}
suffix={`days`}
info={`Every cron run writes an entry, so a long time grows the database. 0 uses the default (${overview?.default_days ?? ""}).`}
/>
<SettingsSaveBar
dirty={dirty}
loading={loading}
status={status}
save_label="Save"
onDiscard={discard}
/>
</Form>
);
}
@@ -0,0 +1,25 @@
import { useContext } from "react";
import { AppContext } from "@/src/pages/__root";
import isPassThroughUser from "@/src/utils/rbac/is-pass-through-user";
import SettingsGroup from "../../(partials)/settings-group";
import LogRetentionForm from "../(partials)/log-retention-form";
/**
* How long logs (Activity) are kept. Only the super admin sees it, since
* only the super admin reads them.
*/
export default function LogRetentionSection() {
const { pageProps } = useContext(AppContext);
if (!isPassThroughUser({ user_types: pageProps?.user_types })) return null;
return (
<SettingsGroup
title="Activity log"
description="How long sign-ins, routine runs, errors and other events in Activity are kept. Older entries are deleted once a day."
id="activity-log"
>
<LogRetentionForm retention={pageProps?.log_retention} />
</SettingsGroup>
);
}
@@ -3,6 +3,7 @@ import ChatroomRetentionSection from "./(sections)/chatroom-retention-section";
import HookCallRetentionSection from "./(sections)/hook-call-retention-section";
import InstallIconSection from "./(sections)/install-icon-section";
import InstallNameSection from "./(sections)/install-name-section";
import LogRetentionSection from "./(sections)/log-retention-section";
import NotificationsSection from "./(sections)/notifications-section";
import PromptSection from "./(sections)/prompt-section";
import RunLimitsSection from "./(sections)/run-limits-section";
@@ -29,6 +30,7 @@ export default function AdminSettingsGeneralPage() {
<SkillLimitsSection />
<HookCallRetentionSection />
<ChatroomRetentionSection />
<LogRetentionSection />
<StorageSection />
<VersionSection />
</SettingsShell>
-4
View File
@@ -132,8 +132,6 @@ function toPublicFile({ file }: { file: BotFileWithMediaLite }) {
purpose: file.purpose || "writing",
scope: file.scope || "bot",
owner_id: Number(file.owner_id) || 0,
ingested_at: file.ingested_at ? Number(file.ingested_at) : undefined,
ingest_error: file.ingest_error || undefined,
created_at: Number(file.created_at) || 0,
url: media_url || "",
};
@@ -149,8 +147,6 @@ type BotFileWithMediaLite = {
purpose?: string;
scope?: string;
owner_id?: number | "";
ingested_at?: number | "";
ingest_error?: string;
created_at?: number | "";
media?: Record<string, unknown> | null;
};
-82
View File
@@ -1,82 +0,0 @@
import userAuth from "@/src/functions/backend/auth/user-auth";
import grabBotFile from "@/src/functions/backend/db/bot-files/grab-bot-file";
import grabUserBot from "@/src/functions/backend/db/bots/grab-user-bot";
import grabUserWorkspace from "@/src/functions/backend/db/workspaces/grab-user-workspace";
import { IngestBotFileRequestSchema } from "@/src/schemas/bot-file-schema";
import enqueueFileIngest from "@/src/file-ingest/enqueue-file-ingest";
import AppError from "@/src/utils/app-error";
import parseWithSchema from "@/src/utils/parse-with-schema";
import canManageCrew from "@/src/utils/rbac/can-manage-crew";
import isReadOnlyUser from "@/src/utils/rbac/is-read-only-user";
import safeErrorMessage from "@/src/utils/safe-error-message";
import type { StorageScope } from "@/src/utils/storage-access";
import type { APIResponseObject, BunextAPIRouteHandler } from "@moduletrace/bunext/types";
/**
* Queues a conversation's file to be read again and re-embedded, for when
* a search misses something the user knows is in it.
*/
export const handler: BunextAPIRouteHandler<APIResponseObject> = async ({ req, body }) => {
try {
if (req.method != "POST") throw new AppError(`Method not allowed`);
const { user, user_types } = await userAuth({ req });
if (!user?.id) return { success: false, msg: `Unauthorized`, logoutUser: true };
if (isReadOnlyUser({ user_types: user_types || [] })) {
throw new AppError(`This user can only read records.`);
}
if (!canManageCrew({ user_types: user_types || [] })) {
throw new AppError(`Only a crew manager can re-read files.`);
}
const { file_id, read_media } = parseWithSchema({
schema: IngestBotFileRequestSchema,
data: body,
});
const scopes = await reachableScopes({ user_id: user.id, file_id });
const file = await grabBotFile({ scopes, file_id });
if (!file?.id) throw new AppError(`That file isn't here.`);
await enqueueFileIngest({ file_id: file.id });
return { success: true, msg: `${file.name || "The file"} will be read again shortly.` };
} catch (error: unknown) {
return { success: false, msg: safeErrorMessage({ error }) };
}
};
/**
* The directories the user reaches for this file.
*/
async function reachableScopes({
user_id,
file_id,
}: {
user_id: number;
file_id: number;
}): Promise<StorageScope[]> {
const { default: BunSQLite } = await import("@moduletrace/bun-sqlite");
const res = await BunSQLite.sql<{ scope: string; owner_id: number }>({
sql: `SELECT scope, owner_id FROM bot_files WHERE id = ? LIMIT 1`,
values: [file_id],
});
const file = res.payload?.[0];
if (!file?.owner_id) return [];
if (file.scope == "workspace") {
const workspace = await grabUserWorkspace({ workspace_id: file.owner_id, user_id });
return workspace?.id ? [{ scope: "workspace", owner_id: Number(file.owner_id) }] : [];
}
const bot = await grabUserBot({ bot_id: file.owner_id, user_id });
return bot?.id ? [{ scope: "bot", owner_id: Number(file.owner_id) }] : [];
}
+16 -10
View File
@@ -1,24 +1,23 @@
import userAuth from "@/src/functions/backend/auth/user-auth";
import grabUserBotSession from "@/src/functions/backend/db/bot-sessions/grab-user-bot-session";
import grabBotMessages from "@/src/functions/backend/db/bots/grab-bot-messages";
import grabBotFace from "@/src/functions/backend/db/bots/grab-bot-face";
import grabActiveBotTask from "@/src/functions/backend/db/bots/grab-active-bot-task";
import { AppData } from "@/src/data/app-data";
import { BotSessionIdRequestSchema } from "@/src/schemas/bot-schema";
import type { BotMessageWithAttachments } from "@/src/types";
import type { SessionMessagesResponse } from "@/src/types";
import AppError from "@/src/utils/app-error";
import parseWithSchema from "@/src/utils/parse-with-schema";
import safeErrorMessage from "@/src/utils/safe-error-message";
import type {
APIResponseObject,
BunextAPIRouteHandler,
} from "@moduletrace/bunext/types";
import type { BunextAPIRouteHandler } from "@moduletrace/bunext/types";
/**
* The latest messages of one of the session user's own sessions, so the
* open conversation can catch up on a reply whose live push it missed.
* The latest messages of one of the session user's own sessions, and the
* bot's face, status line and active task for that user (as
* `server:bot-updated` carries them), so the open conversation can catch
* up on a reply or a face change whose live push it missed.
*/
export const handler: BunextAPIRouteHandler<
APIResponseObject<BotMessageWithAttachments>
> = async ({ req, body }) => {
export const handler: BunextAPIRouteHandler<SessionMessagesResponse> = async ({ req, body }) => {
try {
if (req.method != "POST") {
throw new AppError(`Method not allowed`);
@@ -38,6 +37,8 @@ export const handler: BunextAPIRouteHandler<
throw new AppError(`Session not found.`);
}
const bot_id = Number(session.bot_id);
return {
success: true,
payload: await grabBotMessages({
@@ -45,6 +46,11 @@ export const handler: BunextAPIRouteHandler<
limit: AppData.BotReplyCatchUpMessages,
user_id: user.id,
}),
bot: {
id: bot_id,
...(await grabBotFace({ bot_id, user_id: user.id })),
task: await grabActiveBotTask({ bot_id, user_id: user.id }),
},
};
} catch (error: unknown) {
return { success: false, msg: safeErrorMessage({ error }) };
@@ -8,7 +8,6 @@ import type { BotMessageAttachment } from "@/src/types";
import AppError from "@/src/utils/app-error";
import attachmentFileKind from "@/src/utils/attachment-file-kind";
import bufferFromBase64 from "@/src/utils/buffer-from-base-64";
import canExtractText from "@/src/utils/can-extract-text";
import parseWithSchema from "@/src/utils/parse-with-schema";
import isReadOnlyUser from "@/src/utils/rbac/is-read-only-user";
import safeErrorMessage from "@/src/utils/safe-error-message";
@@ -19,8 +18,8 @@ import type {
/**
* Stores one file for a chat message, private to the session user, before
* the message is sent with its id. Images are resized; documents have
* their text pulled out now, so bots can read them later.
* the message is sent with its id. Images are resized; a document keeps
* no extracted text (a reply reads it from the file).
*/
export const handler: BunextAPIRouteHandler<
APIResponseObject<BotMessageAttachment>
@@ -65,7 +64,6 @@ export const handler: BunextAPIRouteHandler<
media_mime_type: ext,
media_paradigms: ["bot-attachment"],
media: { title: name },
extract_media_text_content: canExtractText({ name, mime_type: mime }),
user: { id: Number(user.id) },
}).catch(async (error) => {
await logError({ title: `Attachment ${name} couldn't be saved`, error }).catch(() => {});
@@ -0,0 +1,45 @@
import grabSuperAdminUser from "@/src/functions/backend/backups/grab-super-admin-user";
import grabLogRetentionOverview from "@/src/functions/backend/db/app-settings/grab-logs-keep-days";
import saveAppSetting from "@/src/functions/backend/db/app-settings/save-app-setting";
import logEvent, { grabRequestIP } from "@/src/functions/backend/logger";
import { LogsKeepDaysSettingKey } from "@/src/dict/log-categories";
import { SaveLogRetentionRequestSchema } from "@/src/schemas/log-schema";
import type { LogRetentionOverview } from "@/src/types";
import AppError from "@/src/utils/app-error";
import parseWithSchema from "@/src/utils/parse-with-schema";
import safeErrorMessage from "@/src/utils/safe-error-message";
import type { APIResponseObject, BunextAPIRouteHandler } from "@moduletrace/bunext/types";
/**
* Super admin only: how many days logs are kept before the daily prune
* deletes them. Zero puts the code's default back.
*/
export const handler: BunextAPIRouteHandler<APIResponseObject<LogRetentionOverview>> = async ({ req, body }) => {
try {
if (req.method != "POST") throw new AppError(`Method not allowed`);
const user = await grabSuperAdminUser({ req });
if (!user?.id) throw new AppError(`Only a super admin can change how long logs are kept.`);
const { keep_days } = parseWithSchema({ schema: SaveLogRetentionRequestSchema, data: body });
await saveAppSetting({
key: LogsKeepDaysSettingKey,
value: keep_days > 0 ? String(keep_days) : null,
});
await logEvent({
title: "Log retention changed",
category: "system",
user_id: user.id,
plain_text: `User #${user.id} set logs to be kept for ${keep_days || "the default number of"} days.`,
metadata: { keep_days },
ip_address: grabRequestIP(req),
});
return { success: true, singleRes: await grabLogRetentionOverview() };
} catch (error: unknown) {
return { success: false, msg: safeErrorMessage({ error }) };
}
};
-2
View File
@@ -9,7 +9,6 @@ import type { TaskDetail } from "@/src/types";
import AppError from "@/src/utils/app-error";
import attachmentFileKind from "@/src/utils/attachment-file-kind";
import bufferFromBase64 from "@/src/utils/buffer-from-base-64";
import canExtractText from "@/src/utils/can-extract-text";
import parseWithSchema from "@/src/utils/parse-with-schema";
import isReadOnlyUser from "@/src/utils/rbac/is-read-only-user";
import safeErrorMessage from "@/src/utils/safe-error-message";
@@ -66,7 +65,6 @@ export const handler: BunextAPIRouteHandler<APIResponseObject<TaskDetail>> = asy
media_mime_type: ext,
media_paradigms: ["task-media"],
media: { title: name },
extract_media_text_content: canExtractText({ name, mime_type: mime }),
user: { id: Number(user.id) },
});
-2
View File
@@ -44,7 +44,6 @@ export const handler: BunextAPIRouteHandler<APIResponseObject> = async (
is_media_primary,
update_on_duplicate,
reauth,
return_media_text_content,
media,
media_name,
} = body;
@@ -100,7 +99,6 @@ export const handler: BunextAPIRouteHandler<APIResponseObject> = async (
media_type,
is_primary: is_media_primary,
update_on_duplicate,
extract_media_text_content: return_media_text_content,
media,
media_mime_type: ext,
user: { id: target_user_id },
-8
View File
@@ -7,14 +7,6 @@ export const DeleteBotFileRequestSchema = z.object({
file_id: z.coerce.number().int().positive(),
});
/**
* Read one file again so it can be searched.
*/
export const IngestBotFileRequestSchema = z.object({
file_id: z.coerce.number().int().positive(),
read_media: z.boolean().optional(),
});
/**
* The General settings' storage quota, in MB.
*/
+9
View File
@@ -1,4 +1,5 @@
import { z } from "zod";
import { AppData } from "@/src/data/app-data";
import { LogCategories } from "@/src/dict/log-categories";
const LogCategoryValues = LogCategories.map((lc) => lc.value) as [
@@ -15,3 +16,11 @@ export const ClearLogsRequestSchema = z.object({
});
export type ClearLogsRequest = z.infer<typeof ClearLogsRequestSchema>;
/**
* How many days logs are kept (Settings > General). Zero puts the code's
* default back.
*/
export const SaveLogRetentionRequestSchema = z.object({
keep_days: z.coerce.number().int().min(0).max(AppData.LogsKeepDaysMax),
});
-40
View File
@@ -1,40 +0,0 @@
import { AppData } from "@/src/data/app-data";
import pruneMediaTextContent from "@/src/functions/backend/db/media/prune-media-text-content";
/**
* Cleans the `media.text_content` that should never have been stored.
*
* Before the extractor had an allowlist, every non-image/audio/video file
* was decoded as UTF-8 and its bytes were written to `text_content`: an
* SVG, a zip, a spreadsheet and an unknown binary all left raw (garbled)
* content in the database. Nothing capped the length either, so a large
* document could leave megabytes of text behind.
*
* Safe to rerun: it only writes a row whose text has to change.
*
* `bun src/scripts/prune-media-text-content.ts`
*/
async function main() {
const result = await pruneMediaTextContent();
for (const change of result.changes) {
console.log(
change.after_chars === 0
? `Media ${change.media_id} (${change.name}): dropped ${change.before_chars} characters.`
: `Media ${change.media_id} (${change.name}): cut ${change.before_chars} characters to ${change.after_chars}.`,
);
}
console.log(
`Done: ${result.cleared} row(s) cleared, ${result.truncated} truncated, about ${Math.round(result.reclaimed_bytes / 1024)} KB reclaimed from ${result.checked} row(s) with text.`,
);
if (result.reclaimed_bytes > 0) {
console.log(
`The freed space stays in the database file until it is vacuumed: run "bunx bun-sqlite vacuum" (or restore from a fresh backup) to reclaim it on disk. Kept text is capped at AppData.BotAttachmentTextStoreMaxChars (${AppData.BotAttachmentTextStoreMaxChars}).`,
);
}
}
await main();
process.exit(0);
+4
View File
@@ -11,6 +11,7 @@ import liveRelay from "./live-relay";
import enableForeignKeys from "./functions/backend/db/enable-foreign-keys";
import migrateBotTypes from "./functions/backend/db/bot-types/migrate-bot-types";
import clearImplicitSessionLinks from "./functions/backend/db/bot-sessions/clear-implicit-session-links";
import dropStoredFileText from "./functions/backend/db/stored-text/drop-stored-file-text";
import syncKnowledgebaseFts from "./functions/backend/db/knowledgebases/fts/sync-knowledgebase-fts";
import handleComputerProxy from "./functions/server/computers/handle-computer-proxy";
import handleGraphicsRequest from "./functions/server/graphics/handle-graphics-request";
@@ -40,6 +41,9 @@ await migrateBotTypes().catch((error) =>
await clearImplicitSessionLinks().catch((error) =>
logError({ title: "Could not clear implicit session links", error }).catch(() => {}),
);
await dropStoredFileText().catch((error) =>
logError({ title: "Could not drop the stored file text", error }).catch(() => {}),
);
await syncKnowledgebaseFts().catch((error) =>
logError({ title: "Could not sync the knowledgebase search index", error }).catch(() => {}),
);
+25 -5
View File
@@ -28,7 +28,7 @@ import type { WebsocketRooms } from "../dict/websocket-rooms";
import type { LucideIcon } from "lucide-react";
import type { ServerQueryParam } from "@moduletrace/bun-sqlite";
import type { MediaParadigms, MediaTypes } from "../dict/media-dict";
import type { BunextPageModuleServerReturnURLObject } from "@moduletrace/bunext/types";
import type { APIResponseObject, BunextPageModuleServerReturnURLObject } from "@moduletrace/bunext/types";
import type { BUN_SQLITE_COBBLE_USERS_JOIN } from "./sql-joins";
import type { ImageInputToBase64FunctionReturn } from "@moduletrace/twui/utils/form/imageInputToBase64";
import type { HarnessKind } from "../dict/harness-dict";
@@ -477,6 +477,15 @@ export type BotUpdatedData = {
admin_workspace_ids?: number[];
};
/**
* `api/admin/bots/session-messages`: the session's latest messages, and
* the bot as the session's user sees it now (face, status line, active
* task), so a conversation that missed pushes catches up on both.
*/
export type SessionMessagesResponse = APIResponseObject<BotMessageWithAttachments> & {
bot?: BotUpdatedData | null;
};
/**
* `server:workspace-updated` data: a workspace's picture changed, so the
* rail and its header show the new one.
@@ -1108,6 +1117,15 @@ export type HookCallRetentionOverview = {
max: number;
};
/**
* How long logs are kept (Settings > General, `grab-logs-keep-days.ts`).
*/
export type LogRetentionOverview = {
keep_days: number;
default_days: number;
max: number;
};
/**
* The install's skill limits (Settings > General, `grab-skill-limits.ts`).
*/
@@ -1268,8 +1286,6 @@ export type PublicBotFile = {
purpose: string;
scope: string;
owner_id: number;
ingested_at?: number;
ingest_error?: string;
created_at: number;
/**
* Where the bytes are served (`/media/<media id>`, private).
@@ -1581,7 +1597,7 @@ export type MessageDetailsJob = {
started_at: number | null;
finished_at: number | null;
/**
* How many model turns and notes it recorded (`bots_queue_updates`).
* How many model turns and notes it recorded (`bots_queue.steps` once cleared, plus any `bots_queue_updates` left).
*/
steps: number;
};
@@ -1904,6 +1920,11 @@ export type PagePropsType = {
* General settings page).
*/
hook_call_retention?: HookCallRetentionOverview | null;
/**
* How many days logs are kept, with the code's default (the General
* settings page).
*/
log_retention?: LogRetentionOverview | null;
/**
* How long closed chatrooms are kept and how many one place keeps, with
* the code's defaults (the General settings page).
@@ -2348,7 +2369,6 @@ export type ApiReqParams<
search_term_user_type?: string | string[];
text?: string;
return_media_text_content?: boolean;
media?: BUN_SQLITE_COBBLE_MEDIA;
backup_name?: string;
};