MossInference is a drop-in for OpenAI’s client: its chat.completions.create takes OpenAI’s
arguments plus indexes and returns the same shapes. Each call goes to
Moss Inference, which searches those indexes
and adds what it finds to the prompt.
Requires
@moss-js/moss 1.21.0 or later, installed as in Install.Create a client
MOSS_PROJECT_ID and MOSS_PROJECT_KEY set,
import { inference } from '@moss-js/moss' gives the same inference.chat.completions.create
with no client to build.
Get a reply
completion.requestId is the response’s X-Moss-Request-Id, and so is stream.requestId on a
stream. Include it when you report a problem. Both are undefined when the response has none.
Stream a reply
stream.controller.abort(): either closes the connection,
which stops the model, and neither throws.
Only the last chunk carries usage, and it has no choices, which is why the loop reads choices[0]?..
Errors
Every failure other than an abort throwsInferenceError, from the for await loop once a stream
has started. status is the HTTP status of a refused request, type names the failure, and
message describes it. errorCode is the error code Moss Inference sent, such as
"rate_limit_exceeded", requestId is the response’s X-Moss-Request-Id, and retryAfter is
the wait in milliseconds that a Retry-After header asks for. A field the failure lacks is
undefined. The error’s Moss code lets isMossError accept it.
The SDK sets these types itself, with status undefined:
Every other error reports what Moss Inference sent. See
Errors, and
Limits for a
429.
Tool calls
tools, tool_choice and parallel_tool_calls take OpenAI’s shapes. Stream the reply, then
stream.finalChatCompletion() joins it into one completion. Push its message onto messages, add
one tool message per call, and call again until the reply has no tool calls, as in
Tool calls:
checkStock stands for your own code. Push the message unchanged, so each tool call keeps every
field the model sent. finalChatCompletion() also works without the for await loop. Without
stream: true, create returns the joined completion directly.
MCP servers
AnMcpServer entry in tools names an MCP server whose tools Moss Inference calls for the model
during the turn. The stream carries each tool step, then the answer:
chunk.moss_tool_step and completion.moss_tool_steps, and MCP failures throw
InferenceError with status, type, errorCode and retryAfter, as in
MCP servers.
Options
new MossInference(projectId, projectKey, options?):
chat.completions.create(params, options?) takes OpenAI’s fields and the Moss fields in params,
and settings for this call only in options: