Waleed 11fe8481d0 fix(knowledge): list knowledge bases on the same authority that creates them (#6770)
* fix(knowledge): list a workspace's bases on the same authority that creates them

GET /api/knowledge authorizes the session against the canonical workspace and
then re-derives access inside the row query from a `permissions` join. Those two
no longer agree: workspace `admin` can come from an organization role alone, with
no workspace permission row behind it. Such a caller passes authorization, creates
a knowledge base, and then sees an empty list forever — the row is filtered out by
the join. Tables and files carry no equivalent join, which is why only knowledge
is affected.

Read the workspace's own rows through `getWorkspaceKnowledgeBases` once the
operation is authorized. The caller-scoped query stays only on the path with no
workspace to authorize against.

* refactor(knowledge): stop re-deriving workspace access inside the list query

The module resolves workspace authority one way everywhere — an explicit
permission row OR an organization admin role — except in the listing query,
which joined `permissions` and required a row. Both list surfaces authorized the
caller and then contradicted that authorization: an org admin could create a
knowledge base through /api/knowledge or /api/v1/knowledge and never see it
listed. Tables and files carry no such join.

Replace the caller-scoped query with `getLegacyPersonalKnowledgeBases`, which
answers only for workspace-less bases whose creator IS their only authority, and
have both surfaces read the workspace's own rows through
`getWorkspaceKnowledgeBases` after authorizing. The legacy rows keep riding along
so they stay reachable. The permissions join now appears nowhere in the module,
and the duplicated connector projection collapses onto the shared helper that
enforces the row cap.

* refactor(knowledge): clean up the module's client layer and fix two state bugs

Cleanup pass over the knowledge module — effects, state, memo, callback, React
Query, url-state, emcn, and comments — keeping the fixes that change behavior for
the better and leaving the ones that would change how the UI feels.

Bugs found and fixed:
- Opening a document flashed "Document not ready" for a frame. The chunk-row
  builder rendered the loading state as a status claim: with no document loaded
  yet it fell through to the branch that reports a missing processing status.
- A partial upload failure skipped every cache invalidation, because the throw
  jumped past them, so the list stayed missing rows the server had already
  created. Admission failures create nothing and still skip the refetch.
- The document and chunk context menus captured the row they opened on, so the
  Enable/Disable label went stale under the list's own polling. They hold an id
  and resolve against live data now.
- The action bar's "Select all"/"Clear" links were painted with `--brand-primary`,
  which is defined nowhere: the links fell back to `currentColor` and were
  indistinguishable from the text beside them.

Consistency and weight:
- Mutations no longer invalidate `detail` non-exactly for writes that touch one
  document: that key is the parent of every documents page, chunk page, tag
  definition, and connector row cached for the base.
- Dead hook surface removed (five exports with no consumer, a query instantiated
  only to reach a cache helper, a `goToPage` that only range-checked), unused
  parameters dropped, `getErrorMessage` replacing hand-rolled instanceof checks.
- `page` joins the document list's param group, so a search resets pagination in
  the same debounced write instead of writing the URL on every keystroke.
- Icons import from `@sim/emcn/icons`, the action bar composes
  `chipFilledFillTokens` instead of restating it three times, chunk cells use the
  canonical content-label chrome, and the icon-only buttons have accessible names.

* refactor(knowledge): one row reader, one visible-list composition

Follow-up from the quality pass. The two list queries had grown into near-copies
of each other — same 14-column projection, same document join, same cap check,
same row mapping — and the workspace-plus-legacy composition was pasted into both
the internal use case and the v1 route, one of which is a surface adapter that
should not be composing domain reads at all.

Both queries now read through one private projection, so a column added to one
list cannot go missing from the other half of the same rendered list, and
`listWorkspaceAndLegacyKnowledgeBases` owns the composition both surfaces call.
That merge also projects connector types once over the merged set instead of once
per source, and skips the copy-and-sort entirely when there are no legacy rows —
the common case.

Also from the review: the chunk-row memo depends on the two primitives it reads
rather than the whole polled document object, the selected chunk resolves in one
scan instead of two, an aborted chunk-search pagination throws instead of caching
a truncated result as complete, upload cache reconciliation no longer delays the
rejected promise, the key-hierarchy rule is stated once on the key factory rather
than six times at its call sites, and `TagDefinition` has one declaration.

* fix(knowledge): refresh the document list pages after a document write

Review caught a regression in the invalidation narrowing: `documents` (the list
pages) and `document` (one row) are SIBLINGS under `detail`, not parent and
child, so scoping a write to the row key left every list rendering the filename,
status, tags, `tokenCount`, and `chunkCount` it had just changed.

The key factory now exposes a `documentLists` prefix and all six document-scoped
mutations invalidate it alongside the row — the chunk mutations included, since
every chunk write moves the parent document's `tokenCount`. `detail` stays
`exact: true` where only the base's own totals move.

Also repoints the shared list-convention test at `getWorkspaceKnowledgeBases`;
it exercised the caller-scoped query this branch removed.
2026-08-16 22:21:20 -07:00

Sim.ai Documentation Slack X

Ask DeepWiki Set Up with Cursor

Sim — Integrate, Context, Build, and Monitor AI agents

A workspace to build, deploy and manage AI agents and workflows.

Quickstart

Cloud-hosted: sim.ai

Open sim.ai

Self-hosted

git clone https://github.com/simstudioai/sim.git && cd sim
bun install
bun run setup

Open http://localhost:3000

The Sim platform — chat on the left, the visual workflow builder on the right

Capabilities

  • Connect 1,000+ integrations and every major LLM
  • Add Slack, Notion, HubSpot, Salesforce, databases, and more
  • Build agents visually, conversationally, or with code
  • Ingest files, knowledge bases, and structured table data
  • Monitor runs, logs, schedules, and workflow activity

One workspace, every surface

Chat and workflows are just the start — tables, files, and knowledge all live in the same workspace.

Tables in Sim — structured data your agents can query

Tables — a database, built in

Files in Sim — documents for your team and every agent

Files — one store for your team and every agent

Knowledge bases in Sim — synced docs your agents can search

Knowledge — your agents' memory

Self-hosting

Requirements: Bun and Docker.

bun run setup is an interactive wizard: it provisions the database, generates secrets, writes your .env files, connects a Chat API key, and starts Sim the way you choose:

  • Local dev — run from source to contribute or hack on Sim
  • Docker Compose — a self-contained instance for testing self-hosting
  • Kubernetes (Helm) — deploy to a local cluster

When it finishes, open http://localhost:3000.

Reconfigure an optional capability without rerunning the full wizard:

bun run setup status
bun run setup email
bun run setup storage
bun run setup sandbox
bun run setup jobs
bun run setup cache
bun run setup knowledge
bun run setup llm
bun run setup integration slack

bun run setup status detects the effective local-dev, Docker Compose, or current-context Helm configuration and reports configured, missing, or invalid capabilities and OAuth integrations without printing credential values. This is separate from bun run sim status, which reports whether installed services are running and healthy.

Manage your install with bun run sim:

bun run sim start | stop | restart   # bring your install up / down / cycle
bun run sim update                   # pull/rebuild and apply Compose images
bun run sim status                    # what's installed and healthy
bun run sim logs                      # follow logs
bun run sim doctor                    # diagnose configuration problems
bun run sim down                      # remove containers (data kept)
bun run sim reset                     # archive .env and wipe managed data

sim detects how you're running (Docker Compose, local dev, or Kubernetes) and acts accordingly.

Prefer a bare sim? Run bun link once — but note sim lands in ~/.bun/bin, which Homebrew's bun doesn't add to your PATH, so you may need export PATH="$HOME/.bun/bin:$PATH" in your shell profile.

Sim also supports local models via Ollama and vLLM. See the self-hosting docs for details.

Chat API Keys

Chat is a Sim-managed service. bun run setup connects a Chat API key for you — sign in when it opens your browser and the key is stored automatically. To view, create, or revoke keys later, go to sim.ai/selfhost/settings/chat-keys.

Environment Variables

See the environment variables reference for the full list, or apps/sim/.env.example for defaults.

Tech Stack

Next.js · Bun · PostgreSQL · Drizzle · Better Auth · Tailwind — and the rest of the stack

Contributing

We welcome contributions! Please see our Contributing Guide for details.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Built by the Sim team in San Francisco

Languages
TypeScript 77%
MDX 20.8%
JavaScript 1.9%
CSS 0.1%