SpaceMIX v0.1.0

Writing for the model

for bothsince protocol 1

How to write names, descriptions, schemas, errors and results the assistant can use correctly, often on a small local model.

The assistant only knows your extension through the names, descriptions and schemas you write. Write them for a reader who has never seen your UI. Often that reader is a small local model, and the schema is also used for constrained decoding.

Describe everything

  • Describe every collection and every field. Say what it means, not just its type: "Sort order; lower is further left. Omit to add at the right end."
  • Say where ids come from. "Id of the column it's in, from tasks_columns_list." The model can't guess them. Collection id arguments get a description like this on their own ("The task's id, from tasks_items_list."); your own id fields need one.
  • Use enum whenever a field has a fixed set of values. The host enforces it.
  • Say what happens when a field is left out, and use default when there's a sensible one. default is filled in on create.
  • Write action titles as imperatives ("Move task", "Send email") and descriptions as one or two plain sentences about what changes.
  • Make the manifest's first sentence say what the extension is for, in at most about 100 characters counting name: . The model doesn't see your tools until it loads them: a chat lists each extension as one line, its namespace, name and description, at most 120 characters. When the whole is longer only the first sentence is kept, so a second sentence ("Read-only.") is lost, and a first sentence still too long is cut off with …. Name the kinds of things and the verbs, without jargon, and put "Read-only" inside the first sentence if it applies. name: "Tasks" with description: "A team board of tasks in columns." gives - tasks (com.acme.tasks) — Tasks: A team board of tasks in columns. (7 tools), which tells the model when to load it. See The catalogue.

Clear but lean

Once a chat loads your group, every tool's name, description and schema is sent with every model call for the rest of the chat, and the person pays for those tokens each time (see tools_load). A word repeated on 30 tools is paid 30 times per call. Say each thing once, where the model will look for it:

  • A tool description says what it does, what comes back and which sibling to use for what. One to three sentences, about 60 tokens; up to about 150 for a list tool that describes its return shape (key fields, paging).
  • Say a group-wide convention once: money units, date format, list shape, routes, which account. Put it on the tool the model calls first or the main list tool, and refer to it elsewhere ("Same list shape as crm_deals."). Never append a shared constant to every description.
  • A parameter many tools take gets at most 15 words naming its source ("Repo id from git_repos."). The long explanation goes once, on the tool that lists those values.
  • Don't repeat the schema in prose. No "Optional" (that's required), no "Default X." when default is set, no list of the enum values; explain only the values that aren't obvious.
  • Update tools refer to create ("Same fields as crm_deals_create.") instead of copying long field descriptions.
  • A passthrough tool (*_get, *_api_*) doesn't list its allowed paths. Its error does, so the model learns them only when it needs them.
  • Lean never means vague: every field still has a description and every id still says where it comes from (Describe everything).

Before, on each of 20 tools:

account: { type: "string", description: "Optional. The id of the CRM account to use. Get the ids by calling crm_accounts. If you leave it out, the default account is used. Default: the default account." },

After, on those 20 tools, with the full explanation once in crm_accounts' description:

account: { type: "string", description: "From crm_accounts; omit for the default account." },

Budgets. These are targets, not limits the host enforces. The main one: about 150 tokens per tool on average, and no single tool over about 400 except one power tool per group. A group should land near 3,000 tokens; a large integration with many tools may go past that as long as the per-tool average holds (47 tools at about 8,000 tokens is fine, 10 tools at 3,000 is not). Measure: a tool reaches the model as { type: "function", function: { name, description, parameters } }, with parameters your input schema after modelSchema (from @spacemix/sdk); the JSON length of all your tools divided by 4 is close to the group's token cost.

When a field's values come from the user's data instead of a fixed list, add options to its schema:

column: {
  type: "string",
  title: "Column",
  description: "Id of the destination column, from tasks_columns_list.",
  options: { tool: "columns_list", args: { limit: 200 }, value: "id", label: "title" },
},
  • tool names the read-only tool that lists the values. A short name (columns_list, conversations) is one of your own collection or action tools; the host prefixes your namespace. A full name (chat_conversations) is used as is.
  • value is the field of each listed item that becomes the argument (default "id"); label is what the user sees (default "name", then "title", then the value); group optionally groups the choices; args are passed to the tool.
  • The host's own forms (Automation's "Run a tool", for one) then show a dropdown instead of a text box, labelled with the field's title.
  • The hint isn't sent to the model. modelSchema strips options before the schema reaches it, so the description still has to say where ids come from.
  • Collection id arguments get options on their own when the collection offers list.

Errors that explain the fix

Arguments are validated against the schema before your code runs, and any error goes back to the model as { error } so it can try again. Make your own errors just as useful:

throw new Error(`No column with id "${String(column)}". Use tasks_columns_list to find valid ids.`);

That gets a correct second attempt. "Not found" doesn't.

Results

  • Return the truth. If an action only queued something, say so. Mail's send, with no account connected yet, returns status: "queued" and a note. Otherwise the assistant will tell the user it was done.
  • Keep results small. Return the record or a short summary, not everything the extension knows. Results are JSON; a function, a DOM node or a class instance can't cross the frame boundary. A result over 12,000 characters reaches the model as a structured view, and in later turns one over 1,500 as a digest: each table keeps its row count, its columns and a few key fields of every row (id-like ones first, so name id fields id or <thing>Id), and the model reads the rest with tool_result_read. Return tables as arrays of objects, and lists of records rather than records keyed by id. See What the model sees of tool results.

Attribute to the caller

Action handlers get a third argument, ActionContext. Its via (ActionVia: extension, extensionName, optional label) is set when another extension ran the action through host.tools.call; Automation passes the automation's name as label. A call through the SpaceMIX API sets it too, with extension: "ai.spacemix.api", extensionName: "SpaceMIX API" and the API key's name as label. It is absent when the assistant in the SpaceMIX app acts for the user. An action whose effect carries a name should use it: Chat's send posts as SpaceMIX with that name instead of as the user.

import type { ActionHandler } from "@spacemix/sdk";

export const actions: Record<string, ActionHandler> = {
  log: async ({ text }, host, ctx) => {
    const by = ctx.via ? (ctx.via.label ?? ctx.via.extensionName) : host.env.me.name;
    return host.collection<{ text: string; by: string }>("entries").create({ text: String(text), by });
  },
};

README

The store shows your README.md. List the assistant tools the extension adds, what each one does, the routes your view understands, the notification kinds and what their targets mean, and the events you emit.

This page as markdown · All pages as one file