Writing programs for Codesey
A program is a folder with a manifest.json. There are two ways it reaches a desktop:
- As software (the normal way): zip the folder and have an administrator upload it in the
admin panel (codesey.com → Admin → Add software) or run
php artisan codesey:import-software folder-or-zip --publish. Users then install it from the catalog on the website or from the Software program on their desktop. - As a built-in: drop the folder in the app's
programs/directory. It appears on every desktop, immediately. (Files, Photos, Settings and Software are built-ins. Use this for first-party programs.)
programs/
└── hello/
├── manifest.json required
├── index.html the entry point (configurable)
└── icon.svg optional (icon.svg, icon.png or icon.webp are auto-detected)
For built-ins, folders whose names start with _ or ., contain spaces/capitals, or lack a valid
manifest are ignored (and logged), so programs/_template/ is a safe place to park things.
Packaging for upload: zip the program folder (a single wrapper folder inside the zip is fine). The
program's id is the manifest "id" if present, otherwise a slug of its name; it must be lowercase letters,
digits and hyphens (max 40) and can't be the id of a built-in. Uploads are rejected if they contain
../absolute paths, script or executable files (.php, .sh, .exe, ...), more than 500 files, or more than
20 MB once unpacked. Hidden files (.git, .DS_Store) are dropped.
manifest.json #
{
"name": "Hello",
"version": "1.0.0",
"description": "Says hello",
"author": "You",
"icon": "icon.svg",
"entry": "index.html",
"window": {
"width": 640, "height": 420,
"minWidth": 320, "minHeight": 200,
"resizable": true, "maximizable": true
}
}
| Field | Required | Notes |
|---|---|---|
name |
yes | Shown in Start menu, title bar, taskbar (max 60 chars). |
id |
no | If present it must equal the folder name. The folder name is the id: lowercase letters, digits, -. |
entry |
no | Default index.html. Relative path inside the folder. |
icon |
no | Relative path. White glyph on a transparent background: the desktop puts it on a blue tile. Without one, a letter tile is shown. |
window.* |
no | Sizes are clamped to the screen; on screens narrower than 640 px programs open maximized. |
version, description, author |
no | Shown in Start and Settings → About. |
A minimal program #
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="/css/codesey-ui.css"> <!-- optional: matches the desktop theme -->
<script src="/js/codesey-sdk.js"></script>
</head>
<body>
<h1>Hello</h1>
<button class="cs-btn cs-btn--primary" onclick="codesey.notify('Hi from Hello!')">Say hi</button>
</body>
</html>
The SDK (window.codesey) #
Everything returns a Promise.
| Call | What it does |
|---|---|
await codesey.ready |
Resolves to { app, os, theme, trusted } once the desktop has answered. |
codesey.window.setTitle(text) |
Change the title bar / taskbar tooltip. |
codesey.window.minimize() / .toggleMaximize() / .close() |
Window control. |
codesey.notify(message) |
Toast on the desktop, titled with your program's name. |
codesey.storage.set(key, value) |
Persist any JSON value (≤256 KB per value, 5 MB per program). Synced to the account, so it follows the person to other devices. Pass { local: true } as a last argument to keep it on this device only. |
codesey.storage.get(key) / .remove(key) / .keys() / .usage() |
Read, delete, list, and { bytes, quota }. Missing keys give null. |
codesey.db.collection(name) |
Collections of JSON records (see below). |
codesey.on('theme', fn) |
Called with { mode, accent, accentContrast } when the user changes theme/accent. |
Storage is private to the signed-in account and to your program, within the browser it runs in.
Available to every program:
| Call | What it does |
|---|---|
await codesey.dialog.prompt({ title, label, value, confirmText }) |
The desktop's own text-entry dialog. Resolves to the text, or null if cancelled. It is labelled with your program's name. |
await codesey.dialog.confirm({ title, message, confirmText, danger }) |
Yes/no dialog. Resolves to true/false. |
codesey.args |
What the program was launched with, e.g. { open: { fileId, folderId } } (or null). |
codesey.on('open', fn) |
Called with new launch arguments if the program is launched again while it is already open. |
Dialogs wait for the person (no timeout); other calls time out after 10 s, and file/software calls after 5 minutes.
Trusted built-ins only (these reject with "requires a trusted program" for store software): codesey.files.*
(list, read, url, write, createText, createFolder, createShortcut, rename, move, remove, upload, download), codesey.os.programs(),
codesey.os.launch(id, args), codesey.os.windows() / closeWindow(id) / focusWindow(id) / stats() (the System Monitor), codesey.layout.reset(), and the 'files' event, which fires when anything in the account's files changes.
File contents arrive as an ArrayBuffer from files.read(id): a sandboxed program can't send the login cookie, so it can't fetch private files by URL.
For video (too big to load into memory), files.url(id) returns { url, name, mime, size }: a temporary signed link for that one file that supports seeking, so you can set it as a <video src>.
Links expire (CODESEY_STREAM_TTL_MINUTES, default 6 hours); renew one before it runs out, and also if a video stalls (an expired link does not raise a media error event in Chrome). Trusted built-ins are also granted autoplay.
Manifest flag "autoplay": true: lets a built-in program start sound by itself without being trusted (the Clock uses it for alarm beeps). It is honoured only for programs in the app's programs/ folder; a store package that sets it is ignored.
files.write(id, text, expected) saves a text file (UTF-8, up to CODESEY_EDIT_MAX_KB); pass the updated value you last saw as expected and the server answers 409 instead of overwriting a newer save. files.createText(parent, name, text) makes a new text file.
The SDK also sets data-theme="light|dark" on <html> and the CSS variables --cs-accent
and --cs-accent-contrast for you, before first paint. codesey-ui.css builds on
those (--cs-bg, --cs-fg, --cs-surface, --cs-border, --cs-muted, and the
.cs-btn, .cs-btn--primary, .cs-input classes).
Developing outside Codesey: open your index.html straight in a browser. storage falls
back to localStorage and notify to console.log, so you can build most of a program
without the desktop. (codesey.embedded tells you which mode you're in.)
Saved data: codesey.storage and codesey.db #
Both are stored on the server for the signed-in account and this program only (another program, or another account, can't see them, even with the same names). Programs never create tables. The desktop keeps a local copy, so calls are fast and keep working when the server can't be reached; writes are queued and sent when it is back. Writes resolve once the server accepts them (or after ~3 s as "queued" when it can't be reached), and reject with a clear message if refused (quota, validation).
const tasks = codesey.db.collection('tasks'); // names: letters, digits, - _ (up to 64)
const t = await tasks.insert({ title: 'Buy milk', done: false }); // → { id, title, done, _created, _updated, _version }
await tasks.get(t.id); // → the record, or null
await tasks.update(t.id, { done: true }); // merges fields
await tasks.replace(t.id, { title: 'New' }); // swaps the whole record
await tasks.remove(t.id);
await tasks.find({ where: { done: false }, sort: ['-starred', '-_created'], limit: 50, offset: 0 });
await tasks.count({ done: true });
codesey.on('db', ({ collections }) => render()); // another device changed something
codesey.on('storage', ({ keys }) => reload());
- A record is a JSON object up to 64 KB. Field names
idand anything starting with_are reserved. Up to 5,000 records per collection, 50 collections per program. - where: equality
{ done: false };$eq $ne $gt $gte $lt $lte(numbers with numbers, text with text);$in $nin;$contains(case-insensitive text, or array member);$startsWith;$exists;$or $and $not; dotted paths{ 'meta.owner': 'kim' }.nullmatches a missing field. Mistakes (unknown operator) are an error, not an empty result. - sort:
'title','-created'or an array; text sorts case-insensitively; missing values sort last. limit/offset page the result. - Conflicts are last-write-wins. To be told instead:
tasks.update(id, patch, { expected: record._version })rejects with "changed somewhere else" if someone else saved first. - Limits: 256 KB per storage value, 500 keys, 5 MB per program and 50 MB per account in total (
codesey.storage.usage()shows yours). - Anything specific to one device (a volume, a layout) should use
{ local: true }so it doesn't sync. - Outside the desktop (opening your program's
index.htmlon its own)codesey.storagefalls back to the browser's localStorage andcodesey.dbisn't available.
Server services: codesey.http and codesey.web #
Programs can't talk to most websites from the browser, and can't hold secrets. For that, declare what you need in manifest.json and call a service:
{ "permissions": ["http", "web.read"],
"http": { "endpoints": [ { "host": "api.example.com", "methods": ["GET"], "auth": { "in": "header", "name": "X-Api-Key", "secret": "EXAMPLE_KEY" } } ] } }
const r = await codesey.http.get('https://api.example.com/v1/things'); // { status, ok, json, body, headers }
const data = await codesey.http.getJson(url); // throws unless 2xx
const page = await codesey.web.read('https://example.com/article'); // { title, html, text, images, ... } (only if the administrator enabled the reader)
A failed call rejects with an Error whose message is safe to show and whose status is the HTTP status (for example 403 not allowed, 415 not a web page, 429 too many requests, 504 timed out). Only declared hosts can be called, the server adds API keys (the administrator pastes them under Admin → Services), and private addresses are never reachable. Full details, limits and error codes: Server services.
Uploads report progress. codesey.files.upload(parent, files, { onProgress, signal }) (trusted programs only): onProgress({ phase: 'reading' | 'uploading', loaded, total }) while the files are read and then sent, and an AbortSignal cancels it (the promise rejects with an AbortError).
Dialogs return focus. When a desktop dialog (codesey.dialog.*) closes, keyboard focus goes back into the program's window, so its shortcuts keep working.
The sandbox (read this before writing a backend-y program) #
Every program runs in an <iframe sandbox> without allow-same-origin, and Laravel also
sends a Content-Security-Policy: sandbox header for every program file. In practice:
- A program cannot touch the desktop page, its DOM, or other programs.
- A program has no cookies,
localStorage,sessionStorageorindexedDBof its own. Usecodesey.storage. - A program can run its own JS/CSS, load its own files (including
<script type="module">andfetch()to its own folder: CORS is enabled for/apps/*), use forms, modals, popups and downloads, andfetch()any external API that allows cross-origin requests. - A program cannot call Codesey's own
/api/*routes (they rely on cookies/CSRF). That's intentional; the SDK is the supported door.
Trusted programs #
settings.get/update, os.info, os.launch and software.list/install/uninstall are privileged.
Only built-in programs may call them: Files, Photos, Settings and Software always can, and
CODESEY_TRUSTED_PROGRAMS (comma separated ids) can add more built-ins; everyone else gets a
"requires a trusted program" error. A manifest can't promote itself, and uploaded software is
never trusted, even if it reuses a trusted id (uploads can't take a built-in's id at all).
Trust is still sandboxed: it only widens what the bridge will answer.
How it works (for contributors) #
ProgramRegistry knows the built-ins (programs/*/manifest.json) plus an account's installed software
(ProgramCatalog::forUser). ProgramController::asset serves /apps/{id}/{path} on the desktop domain with
containment headers and traversal protection. Program files are public on purpose: the sandboxed iframe's
sub-requests carry no cookie, and everything served is already in the public catalog. The desktop opens
each program in an iframe (windows.js); bridge.js answers postMessage requests from
exactly the window that sent them (matched by event.source), and codesey-sdk.js is the
other half.