AI
host.ai: one non-streamed completion from the user's default connected model, with optional tool definitions, for extensions that run their own model loop.
host.ai calls the user's default connected model. One completion per call, no streaming. Pass tool definitions
and the model may ask for tool calls; running them (for example with host.tools.call) and continuing
the conversation is up to you.
interface SpacemixHost {
ai?: AiApi; // present with `ai:invoke`
}
interface AiApi {
complete(input: { messages: AiMessage[]; tools?: ToolDefinition[] }): Promise<AiCompletion>;
available(): Promise<boolean>;
}
type AiMessage =
| { role: "system" | "user"; content: string }
| { role: "assistant"; content: string; toolCalls?: AiToolCall[] }
| { role: "tool"; toolCallId: string; content: string };
interface AiToolCall {
id: string;
name: string;
arguments: Record<string, unknown> | string;
}
interface AiCompletion {
content: string;
toolCalls: AiToolCall[];
model: string;
}
Permission: ai:invoke. Without it host.ai is undefined.
The call runs as the signed-in person, on their default model and their provider account. The person doesn't see the conversation in their chats.
When the default model is a SpaceMIX Hub model, the call runs through SpaceMIX Hub and is
billed to the server's SpaceMIX Hub credit, which the whole team shares. It works only for people with the
server:hubModels permission, and stops when the credit is used up or the server's monthly cap is reached.
host.ai.complete
complete(input: { messages: AiMessage[]; tools?: ToolDefinition[] }): Promise<AiCompletion>;
Bridge method: ai.complete. Frame timeout: 10 minutes.
messages: at least one.contentis converted to a string. Anassistantmessage'stoolCallsare sent back to the model as the calls it made; atoolmessage answers one bytoolCallId.tools:ToolDefinitions (name,description,inputSchema), such ashost.tools.list()returns. Form-only hints (options) are stripped from the schemas before the model sees them.- The result:
content(may be empty when the model only calls tools),toolCallswithargumentsparsed from the model's JSON (the raw string when it isn't a JSON object;{}when empty), andmodel, the model's id (for a SpaceMIX Hub model, the Hub model's id).
Errors:
| When | Message |
|---|---|
| No messages | A completion needs at least one message. |
| No model connected | No model connected. Connect one in Settings, then try again. |
| The default model is a CLI agent | The default model is a CLI agent; connect an API model for this. |
| Unknown role | Unknown message role "<role>". |
| A tool call or tool message without its id or name | A tool call's id must be a string. (and similar) |
| The default model is a SpaceMIX Hub model and the server's Hub is off | The default model is a SpaceMIX Hub model, and this server has SpaceMIX Hub turned off. Connect another one in Settings. |
The default model is a SpaceMIX Hub model and the person lacks server:hubModels |
You don't have permission to use SpaceMIX Hub models on this server. Ask someone who manages members. |
SpaceMIX Hub model, the server's credit is used up (Hub 402 credit_exhausted) |
This server's SpaceMIX Hub credit is used up. Someone who manages settings can top it up in Settings → Models. |
SpaceMIX Hub model, the server's monthly cap is reached (Hub 402 monthly_cap_reached) |
This server has reached its monthly spending cap for SpaceMIX Hub models. Someone who manages settings can raise it in Settings → Models. |
| The provider fails | the provider's error (for a SpaceMIX Hub model, the server's sentence; see Errors) |
host.ai.available
available(): Promise<boolean>;
Whether complete can work right now: a default model is connected, it isn't a CLI agent, and, when it is an
SpaceMIX Hub model, the server's Hub isn't off and the person has server:hubModels. It doesn't check the server's credit:
complete can still fail with the credit or cap errors above. Bridge method: ai.available.
Example
import { host, type AiMessage } from "@spacemix/sdk";
export async function summarize(text: string): Promise<string> {
const ai = host.ai;
if (!ai || !(await ai.available())) return text.slice(0, 200);
const messages: AiMessage[] = [
{ role: "system", content: "Summarize the user's text in one sentence." },
{ role: "user", content: text },
];
return (await ai.complete({ messages })).content;
}
Stability
Experimental: one completion per call with no streaming and no model choice; both may be added.