Skip to content

Write a helper function for the org’s library

POST
/ai/helper
curl --request POST \
--url 'https://api.sloose.com/ai/helper?connector=con_8f2a' \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{ "question": "example", "namespace": "example", "sampleValues": [ "example" ], "draft": { "manifest": { "namespace": "example", "version": "example", "scope": "global", "description": "example", "functions": [ { "name": "example", "description": "example", "params": [ { "name": "example", "type": "example", "optional": true, "description": "example" } ], "returns": "example", "examples": [ { "call": "example", "result": "example" } ], "tags": [ "example" ] } ] }, "source": "example" } }'

Writes ONE helper function from a description: its manifest entry (name, parameters, return, examples), its JavaScript, and the files to put in your configuration repository under libraries/<namespace>/.

The function is loaded into the sandbox beside your org’s own libraries and every example call the model wrote is run there before it is returned — so what comes back has executed, in the same Worker with no bindings and no network that your real libraries run in. A function that will not load, or an example that throws, is fed back to the model once; valid says whether the final answer loaded and ran clean, and checks shows each example’s result.

Nothing is saved. A library reaches the org only when it is published — from its page, or pushed from a configuration repository with sloose push — and every publish is a version. For a namespace the org already has, extends is true and the manifest is that library’s with the function added; snippet is the line to add under its functions. The namespace core, and the name of any of Sloose’s own helpers (trim, date…), are refused before anything is spent, as a publish refuses them: a library by such a name would replace that helper.

sampleValues follow the org’s AI data setting. The model is shown them as they are under allow, as shapes under masked, and not at all under deny; under the last two notes says so, and the checks are the model’s own examples.

A helper belongs to the org, not to any one connection. Unlike this router’s other generation routes — the ones that validate against a connection’s fields, modules or match rules — ?connector= is not required here: a library is written once and called from every connection’s expressions alike. It is ignored if sent.

Into a library’s draft (#559 PR 4): with draft — the library as its page holds it, unpublished — the function is written INTO it, a new one or one of the same name in its place, and the answer carries the whole new draft as draft: every other line of the source as it was sent, the manifest’s entry where the old one was, its version label left as the author’s. What is loaded and run in the sandbox is that whole draft, so valid speaks for the library the person would publish. Nothing is saved; the page shows the change and the person takes it or not. A draft is refused before anything is spent wherever its publish would be refused before its compile — one of global scope (422 GLOBAL_LIBRARY), of a library the configuration repository publishes (409 LIBRARY_MANAGED), or on a plan that cannot publish libraries (403 FEATURE_HELPER_LIBRARIES) — and so is one with no functions object to put a function into (422 DRAFT_UNREADABLE).

connector

Ignored. A helper library belongs to the org, not to any one connection, so nothing here reads it — sent or not makes no difference.

string
Example
con_8f2a

Ignored. A helper library belongs to the org, not to any one connection, so nothing here reads it — sent or not makes no difference.

x-sloose-org

The org you believe this session is in. When it is not the org the session is in NOW — POST /auth/session/org moves a session without issuing a new token — the request is refused with 409 ORG_CHANGED rather than served against the other org’s schema. Omit it and nothing changes.

string

The org you believe this session is in. When it is not the org the session is in NOW — POST /auth/session/org moves a session without issuing a new token — the request is refused with 409 ORG_CHANGED rather than served against the other org’s schema. Omit it and nothing changes.

Media typeapplication/json
object
question
required

What the helper should do, in your own words.

string
>= 1 characters <= 4000 characters
namespace

The org library the helper belongs to. An existing one is extended; a new name starts one. Defaults to the org’s first library, else org. core, and the name of any of Sloose’s own helpers (trim, date…), are refused, as a publish refuses them.

string
/^[a-z][a-z0-9_]*$/
sampleValues

Values to try the helper on, so the examples it is checked against are yours.

Array<string>
<= 20 items
draft

The library as its page holds it, unpublished (#559 PR 4): its manifest and its source in the SDK’s one shape, at most 256 KB as a publish takes it. Given, the helper is written INTO it — a new function, or one of the same name in its place — and the answer carries the whole new draft (draft), every other line of the source as it was sent. Its namespace is the library’s; namespace, if sent too, must be the same. A library the configuration repository publishes is refused (409 LIBRARY_MANAGED), and a source whose functions cannot be found is refused (422 DRAFT_UNREADABLE), both before anything is spent. Nothing is saved.

object
manifest
required
object
namespace
required
string
/^[a-z][a-z0-9_]*$/
version
required
string
/^\d+\.\d+\.\d+$/
scope
required
string
Allowed values: global org
description
string
functions
required
Array<object>
object
name
required
string
/^[A-Za-z_$][\w$]*$/
description
required
string
params
required
Array<object>
object
name
required
string
type
required
string
optional
boolean
description
string
returns
required
string
examples
Array<object>
object
call
required
string
result
string
tags
Array<string>
source
required
string
>= 1 characters <= 262144 characters

The function, checked in the sandbox, with what it cost.

Media typeapplication/json

The helper: manifest entry, implementation, the files for the repository, and what its examples produced.

object
requestId
required

This call’s id. It is also the ledger’s idempotency key.

string
credits
required

Credits settled for this call.

number
usage
required

Token counts, including cache reads and writes.

object
key
additional properties
model
required

The model that answered.

string
key
additional properties
Examplegenerated
{
"requestId": "example",
"credits": 1,
"usage": {
"additionalProperty": "example"
},
"model": "example"
}

The body did not match the schema (BAD_INPUT, with issues), the namespace is one no org library may take — core, or one of Sloose’s own helpers (RESERVED_NAMESPACE) — or namespace names another library than the draft sent beside it (NAMESPACE_MISMATCH).

Media typeapplication/json
object
error
required
string
code
required
string
key
additional properties
Examplegenerated
{
"error": "example",
"code": "example"
}

No bearer, or one that is expired, revoked or no longer resolves to a member.

Media typeapplication/json

The error envelope every non-2xx answer uses.

object
error
required

Human-readable explanation.

string
code

Machine-readable reason. Absent on a few legacy 400s.

string
key
additional properties
Examplegenerated
{
"error": "example",
"code": "example"
}

Not enough AI credits (CREDITS_EXHAUSTED), or the org is suspended (SUSPENDED). balance shows what is left, needed what this asked for, planIncludesCredits whether the plan includes any each period, and renewsAt, where a subscription renews them, when they come back.

Media typeapplication/json
object
error
required
string
code
required
string
balance
required

AI credits. Included credits reset each period; purchased ones do not.

object
included
required
number
purchased
required
number
reserved
required
number
available
required

Included + purchased − reserved

number
periodStart
required
string
periodEnd
required
string
needed
required

The least the action would hold: its price, or for an action priced by how much it reads, the cheapest band, since such an action holds what the balance covers of its dearest band. The balance fell short of it — which is not the same as nothing left.

number
renewsAt

When the credits ran short (CREDITS_EXHAUSTED) under a subscription that renews them — active, not set to cancel, and renewing on a plan with an allowance, whether charged to a card or billed by invoice (whose credits come as each invoice is issued, #1337): when that allowance comes back, the end of the current period. Absent on a plan that includes none, on a trial, under a subscription that is cancelled, set to cancel or past due, when the plan it moves to at the period’s end includes none, for a suspension, and when the plan could not be read.

string
planIncludesCredits

When the credits ran short (CREDITS_EXHAUSTED): whether the org’s plan, as the catalogue has it, includes AI credits each period — a staff override changes this period’s limits, not that. Absent for a suspension, and when the plan could not be read — which says nothing either way.

boolean
key
additional properties
Examplegenerated
{
"error": "example",
"code": "example",
"balance": {
"included": 1,
"purchased": 1,
"reserved": 1,
"available": 1,
"periodStart": "example",
"periodEnd": "example"
},
"needed": 1,
"renewsAt": "example",
"planIncludesCredits": true
}

A draft was sent and this plan cannot publish helper libraries, so the draft could not be published (FEATURE_HELPER_LIBRARIES; without a draft the helper is written on every plan); or the session is a read-only staff view of the org, which asks nothing that spends (IMPERSONATION_READ_ONLY). Neither is charged.

Media typeapplication/json
object
error
required
string
code
required
string
Allowed values: FEATURE_HELPER_LIBRARIES IMPERSONATION_READ_ONLY
key
additional properties
Example
{
"code": "FEATURE_HELPER_LIBRARIES"
}

No such org.

Media typeapplication/json
object
error
required
string
code
required
string
Allowed values: ORG_NOT_FOUND
key
additional properties
Example
{
"code": "ORG_NOT_FOUND"
}

You sent X-Sloose-Org and this session is in a different org now, which is what stops an answer being applied to the org you were in when you asked (ORG_CHANGED); or the draft sent is of a library the configuration repository publishes, whose page is read only (LIBRARY_MANAGED). Nothing was spent.

Media typeapplication/json
Any of:
object
error
required
string
code
required
string
Allowed values: ORG_CHANGED
key
additional properties
Example
{
"code": "ORG_CHANGED"
}

The model declined (AI_REFUSED; category says why); the draft’s source has no functions object written in it to put a helper into — it does not parse, its default export is not defineLibrary({ manifest, functions }), or its functions is not an object in its code — said in the words of what was not found (DRAFT_UNREADABLE); or the draft’s manifest has global scope, which no publish of the org’s takes (GLOBAL_LIBRARY). None is charged.

Media typeapplication/json
Any of:
object
error
required
string
code
required
string
Allowed values: AI_REFUSED
category
required
string
key
additional properties
Example
{
"code": "AI_REFUSED"
}

The model’s answer could not be used.

Media typeapplication/json
object
error
required
string
code
required
string
Allowed values: AI_BAD_OUTPUT
key
additional properties
Example
{
"code": "AI_BAD_OUTPUT"
}

AI is not configured on this deployment (AI_NOT_CONFIGURED): unconfigured paths say so rather than failing. Or the model could not be asked just now (AI_UNAVAILABLE) — the gateway out of credits, rate limited or failing, or the provider unavailable: nothing was changed and nothing was charged, so the same request can be sent again shortly.

Media typeapplication/json
object
error
required
string
code
required
string
Allowed values: AI_NOT_CONFIGURED AI_UNAVAILABLE
key
additional properties
Example
{
"code": "AI_NOT_CONFIGURED"
}

Report a problem with this page