AI, email, bot and support

@exo/kit/ai#

Exports: createAiClient. No dependencies.

A product asks the exo AI gateway for a tier, fast, capable or agent, never for a model. The gateway chooses the model, falls back when one fails and applies daily ceilings.

import { createAiClient } from '@exo/kit/ai';

export const ai = createAiClient({
  baseUrl: env.EXO_AI_URL,
  key: env.EXO_AI_KEY, // names the product in the gateway's ledger
  reportError,
  logWarn,
});

const out = await ai.complete({
  tier: 'capable',
  messages,
  subject: `myapp:user:${userId}`,
  timeoutMs: 90_000,
});

if (out.ok) return out.content; // also out.model, out.pool, out.rung
if (out.error === 'budget_exhausted') return handOver();
return failSafe();

complete() never throws. Results:

ResultMeaningWhat to do
budget_exhaustedA daily ceiling closed. The service is healthy.Hand over to a person.
all_rungs_failedEvery model was tried and none answered.Fail safe.
unknown_tier, unauthorized, bad_requestAn integration mistake.Fail safe and fix the wiring.
timeout, unavailableThe service did not answer.Fail safe.
not_configuredNo address or key. Nothing was sent.Behave as without AI.

timeoutMs bounds the whole call, 60 seconds by default. There are no client retries, because the service already retries and a second attempt would be charged twice. subject identifies the end customer for per-customer limits and may be at most 200 characters. ai.usage() returns what the product has been charged today.

@exo/kit/mailer#

Exports: createMailer, MailSendError, senderAddress, isOwnSender. No dependencies.

import { createMailer } from '@exo/kit/mailer';

export const { mailerEnabled, sendEmail, isOwnSender } = createMailer({
  apiKey: env.resendApiKey,
  from: env.emailFrom,
  reportError: (err, ctx) => logError('email_send_failed', err, ctx.fields),
});

await sendEmail({ to: 'person@example.com', subject: 'Hello', text: 'Plain text body.' });

Without a key or a sender, mailerEnabled() is false and sendEmail returns false without a network call. Any failure returns false and is reported once. The strict send throws MailSendError with a reason of not_configured, refused or transport.

@exo/kit/bot#

Exports: createBotRoute, fromExoUsers, botpark. Uses node:crypto.

The exo bot service owns everything about Telegram: menus, buttons, linking and escaping. Your product answers one internal route with plain JSON screens.

import { createBotRoute, fromExoUsers, botpark } from '@exo/kit/bot';
import { fromPg } from '@exo/kit/signin';

const bot = createBotRoute({
  key: env.BOT_KEY,
  resolve: fromExoUsers(fromPg(pool)),
  home: ({ user }) => ({ summary: ['3 pending'] }),
  actions: {
    done: async () => ({ toast: 'Done', buttons: [[{ label: 'Back', action: 'home' }]] }),
  },
});

app.post('/internal/bot', async (req, reply) => {
  const r = await bot(req.headers.authorization, req.body);
  return reply.code(r.status).send(r.json);
});

await botpark({ url: env.BOT_URL, key: env.BOT_KEY, bot: env.BOT_ID }).notify(sub, { title: 'Due today' });

Route answers: 503 bot_not_configured without a key, 401 unauthorized for a wrong bearer, 400 bad_request for a malformed body, and 200 with not_found, no_account or unavailable for the matching cases. notify returns true when accepted and false otherwise, and never throws.

@exo/kit/connector-sdk#

Exports: createConnectorHandler, widgetRef, parseWidgetRef, signWidgetToken, widgetAnswer.

Mount one route so that the exo support desk can read facts about your product. Requests are signed with an HMAC secret, and an unsigned request gets 503 and no data.

import { createConnectorHandler } from '@exo/kit/connector-sdk';

const handler = createConnectorHandler({
  secret: process.env.SUPPORT_CONNECTOR_SECRET!,
  facts: async () => ({ plans: ['free', 'pro'] }),
  resolveIdentity: async (query) => findCustomer(query),
  tools: {
    plan: {
      description: 'The plan of this account',
      input: { detail: { type: 'string', enum: ['short', 'full'] } },
      level: 'member',
      subject: 'user',
      run: ({ userId, input }) => plans.of(parseWidgetRef(userId!)),
    },
  },
});
export const GET = handler;
export const POST = handler;

Tools are lookups only. The SDK rejects unknown tools, undeclared or mistyped input, a missing user for a user tool, and a caller whose level is below the tool's.