Write a helper function for the org’s library
const url = 'https://api.sloose.com/ai/helper?connector=con_8f2a';const options = { method: 'POST', headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'}, body: '{"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"}}'};
try { const response = await fetch(url, options); const data = await response.json(); console.log(data);} catch (error) { console.error(error);}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).
Authorizations
Section titled “Authorizations”Parameters
Section titled “Parameters”Query Parameters
Section titled “Query Parameters”Ignored. A helper library belongs to the org, not to any one connection, so nothing here reads it — sent or not makes no difference.
Example
con_8f2aIgnored. A helper library belongs to the org, not to any one connection, so nothing here reads it — sent or not makes no difference.
Header Parameters
Section titled “Header Parameters”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.
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.
Request Bodyrequired
Section titled “Request Bodyrequired”object
What the helper should do, in your own words.
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.
Values to try the helper on, so the examples it is checked against are yours.
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
object
object
object
object
Responses
Section titled “Responses”The function, checked in the sandbox, with what it cost.
The helper: manifest entry, implementation, the files for the repository, and what its examples produced.
object
This call’s id. It is also the ledger’s idempotency key.
Credits settled for this call.
Token counts, including cache reads and writes.
object
The model that answered.
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).
object
Examplegenerated
{ "error": "example", "code": "example"}No bearer, or one that is expired, revoked or no longer resolves to a member.
The error envelope every non-2xx answer uses.
object
Human-readable explanation.
Machine-readable reason. Absent on a few legacy 400s.
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.
object
AI credits. Included credits reset each period; purchased ones do not.
object
Included + purchased − reserved
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.
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.
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.
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.
object
Example
{ "code": "FEATURE_HELPER_LIBRARIES"}No such org.
object
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.
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.
The model’s answer could not be used.
object
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.
object
Example
{ "code": "AI_NOT_CONFIGURED"}