--- title: sharedspace load: requirelib("sharedspace"); order: 240 --- A **shared space** is an area where multiple different users can share texts, images, files and collaboratively edited documents together - the collaboration backbone behind chat-style apps, document collaboration and MeetRoom meetings. Every space carries: - an **access mode**: `"open"` (default - the random space ID acts as the capability, anyone who knows it can read and post), `"public"` (discoverable via `listPublicSpaces`, any logged-in user can self-join) or `"private"` (members only, invited by the owner or a space admin); - a **member list** with roles `owner` / `admin` / `member`; - a **metadata** key-value store (managers only, capped); - a **persistent** flag: persistent spaces (and their items, files and documents) survive server restarts; ephemeral spaces (the default, and what MeetRoom uses) are gone after a restart. Web clients get the same feature set over `/system/sharedspace/*` and a realtime WebSocket channel (`/system/sharedspace/ws`), so items and document patches posted from AGI appear live in connected clients. MeetRoom binds one space to every meeting room (see the meetroom API), so this library is also how scripts read a live meeting's chat and post messages or files into it. ## `sharedspace.createSpace(name)` → object Create a new space owned by the calling user. Returns `{ spaceid, name, owner, items, createdat }`. ```javascript requirelib("sharedspace"); var space = sharedspace.createSpace("Design sync"); sendJSONResp(space); ``` ## `sharedspace.listMySpaces()` → array List the spaces owned by the calling user (same fields as `createSpace`). ## `sharedspace.getSpaceInfo(spaceid)` → object Describe a space by ID. Returns `{ exists: false }` for unknown IDs. ## `sharedspace.addText(spaceid, text)` → string Post a text snippet (clipped to 4000 characters) into the space. Returns the new item ID; throws a `SharedSpaceError` on failure. ## `sharedspace.addFile(spaceid, vpath)` → string Copy a file from the calling user's storage into the space. Files with a raster-image extension (png / jpg / jpeg / gif / webp / bmp) are stored as `image` items, everything else as `file`. Returns the new item ID. ```javascript requirelib("sharedspace"); var itemid = sharedspace.addFile(spaceid, "user:/Photo/cat.png"); ``` ## `sharedspace.notifyMembers(spaceid, title, message, priority, usernames)` → number Raise a notification to fellow members of a space through the ArozOS notification system. Membership-scoped: only a member (or space manager) may notify, and only current members can be reached, so this cannot be used to spam arbitrary users (no admin permission required, unlike `notification.sendToUser`). Members currently connected to the space's realtime channel are skipped, since they already receive the live message. Delivery per recipient follows that user's own notification preferences (desktop, Telegram, email, webhook). `message`, `priority` (`"low"` / `"medium"` / `"high"`, default `"medium"`) and `usernames` are optional. When `usernames` (an array) is omitted every other member is notified; when given, the recipients are narrowed to that set intersected with the current members. Returns the number of members actually notified; throws a `SharedSpaceError` on error. ```javascript requirelib("sharedspace"); //Notify everyone else in the space sharedspace.notifyMembers(spaceid, "New message", "Alice: are we still on?"); //Notify just two members, at high priority sharedspace.notifyMembers(spaceid, "Alice in #plan", "@bob @carol ping", "high", ["bob", "carol"]); ``` ## `sharedspace.listItems(spaceid)` → array Chronological list of items: `{ itemid, type, name, text, size, uploader, origin, time }`. `type` is `"text"`, `"image"` or `"file"`; `text` is only filled for text items. ## `sharedspace.getText(spaceid, itemid)` → string Read the content of a text item. ## `sharedspace.saveFileTo(spaceid, itemid, destVpath)` → bool Copy an image / file item into the calling user's storage. ## `sharedspace.removeItem(spaceid, itemid)` → bool Remove an item. Only the item's uploader, a space admin or the space owner may remove it. ## `sharedspace.deleteSpace(spaceid)` → bool Delete a space and all its items. Space owner or a space admin only. ## `sharedspace.createSpaceAdvanced(name, options)` → object Create a space with options: `{ access, persistent, metadata }`. Returns the space descriptor (`{ spaceid, name, owner, access, persistent, items, docs, members, metadata, createdat }` - the same shape every listing below uses), or throws a `SharedSpaceError` when e.g. persistence is disabled by the administrator. ```javascript requirelib("sharedspace"); var space = sharedspace.createSpaceAdvanced("Team chat", { access: "private", persistent: true, metadata: { purpose: "chat" } }); ``` ## `sharedspace.listPublicSpaces()` → array The public space directory (descriptors of every `"public"` space). ## `sharedspace.listJoinedSpaces()` → array Descriptors of every space the calling user belongs to (owner, admin or member). `listMySpaces()` remains the owned-only subset. ## `sharedspace.joinSpace(spaceid)` / `sharedspace.leaveSpace(spaceid)` → bool Self-join a public (or open) space as a member / leave a space. Private spaces are invite-only, so `joinSpace` fails on them. ## `sharedspace.setAccess(spaceid, access)` → bool Change the access mode (`"open"`, `"public"` or `"private"`). Managers only. ## `sharedspace.setMeta(spaceid, key, value)` / `sharedspace.getMeta(spaceid)` → bool / object Set (empty value deletes) or read the space metadata. Writing requires manager rights; reading requires read access. ## `sharedspace.addMember(spaceid, username, role)` → bool Invite a user with role `"admin"` or `"member"` (default). Managers only. ## `sharedspace.removeMember(spaceid, username)` → bool Remove a member (managers) or leave (self). The owner cannot be removed. ## `sharedspace.listMembers(spaceid)` → object Member map `{ username: role, ... }`. Members and managers only. ## `sharedspace.createDoc(spaceid, name)` → object Create a collaborative document. Returns the document snapshot `{ docid, name, creator, revision, content, createdat, updatedat, updatedby }`. ## `sharedspace.listDocs(spaceid)` → array Document snapshots without their `content`. ## `sharedspace.getDoc(spaceid, docid)` → object One document with its full content - also the recovery path after an update conflict. ## `sharedspace.updateDoc(spaceid, docid, baserev, content)` → object Compare-and-swap update: the new `content` replaces the document body only when `baserev` matches the document's current revision. Returns `{ ok: true, revision }` on success or `{ ok: false, conflict: true, revision }` when someone else updated the document first - re-fetch with `getDoc`, merge your change and retry. Debounce saves (~500ms): every accepted revision is one database write, and live WebSocket clients receive each revision as a patch frame. ```javascript requirelib("sharedspace"); var doc = sharedspace.getDoc(spaceid, docid); var result = sharedspace.updateDoc(spaceid, docid, doc.revision, doc.content + "\nAppended by AGI"); if (result.conflict) { // someone got there first: re-fetch, rebase, retry } ``` ## `sharedspace.deleteDoc(spaceid, docid)` → bool Delete a document. Creator or space managers only.