Charliehub · by A.N.D.Why

Building block 7 · Going further

Live state

A tool list tells Charlie what it can do, never what already exists. Live state fills that gap: Charlie starts each conversation knowing the latest, and your extension's windows and sidebar can save and load without going through the chat.

What you'd use it for

In your manifest

From the checklist extension. Each field points to a file in the extension folder.

checklist / manifest.json (trimmed)
{
  "name": "checklist",
  "boot": { "module": "./handlers/boot.js" },
  "channels": { "module": "./handlers/channels.js" },
  "sidebar": {
    "label": "Checklists",
    "component": "./build/ChecklistSidebar.js"
  }
}

The boot line

The boot module returns one short piece of text, or null when there's nothing worth saying. Checklist's version lists what exists:

checklist / handlers/boot.js (trimmed)
module.exports = async function boot(ctx) {
  await store.sync(ctx)
  const checklists = await store.list(ctx)
  // Null, not "there are no checklists"
  if (checklists.length === 0) return null

  const lines = checklists.map(
    (c) => `  - "${c.title}" — slug: ${c.slug}`)
  return [
    `${checklists.length} checklists exist:`,
    ...lines,
    'Read one with checklist__read, using its slug.'
  ].join('\n')
}

Channels

The channels module exports named functions. Your window or sidebar calls them by name with charlie.invoke. Each one gets the payload and a ctx, and whatever it returns goes back to the window.

checklist / handlers/channels.js (trimmed)
async function load(payload, ctx) {
  return store.load(ctx, payload.slug)
}

async function save(payload, ctx) {
  const saved = await store.save(ctx, payload.checklist, {
    expectedRevision: payload.expectedRevision,
    writtenBy: 'operator'
  })
  return { ok: true, revision: saved.revision }
}

module.exports = { load, save, list: listChecklists }
checklist / src/ChecklistWindow.jsx (one line)
const result = await charlie.invoke('save', {
  checklist: next, expectedRevision: next.revision
})

What each part means

FieldWhat it does
boot.moduleA file whose default export returns one line of state (or null). Runs on the first turn of every new thread.
channels.moduleA file of named functions your own window, sidebar or top-bar view can call. The export name is the channel name.

Things to know

See it in

Every field in detail: boot and channels in the reference.