Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.
import {
isLearnedSkillName,
isLearningSharingMode,
type PersonalLearningSharing,
} from "@openbot/shared/learning";
import { Hono, type MiddlewareHandler } from "hono";
import { bodyLimit } from "hono/body-limit";
import { type AuditStore, recordAuditEvent } from "../audit";
import type { AppVariables } from "../auth/guards";
import type { LearningSharingStore } from "./sharing";
export type PersonalLearningRouteDeps = {
store: LearningSharingStore;
/** The Skills in this person's own space, read the way a run reads them (learning/runtime.ts `personalSkills`). */
skillsOf: (
userId: string,
) => Promise<
| { skills: { name: string; description: string }[] }
| { unavailable: "off" | "unavailable" }
>;
/** Tells this replica's open-Skill index to read again; other replicas see the change within its TTL. */
invalidate: () => void;
/**
* Whether an administrator lets people open their Skills to every Bot (Admin, Enterprise controls, Skill sharing),
* read fresh for every change. Off, opening is refused and closing is not: a person can always take their Skills
* back to their own Bots.
*/
sharingAllowed: () => Promise<boolean>;
auditStore?: AuditStore;
};
/** What a person reads when they try to open their Skills while an administrator has not allowed it. */
const SHARING_OFF =
"An admin hasn't turned on sharing Skills with every Bot, so your Skills stay with your own Bots.";
const smallBody = bodyLimit({
maxSize: 2 * 1024,
onError: (context) =>
context.json({ error: "That request is too large." }, 413),
});
/**
* Settings, Learning: who else's Bots use the Skills learned from a person's own recordings. Theirs alone, scoped to the
* signed-in person and never to anything in the path, like their standing instructions: there is no route for reading
* or changing somebody else's.
*/
export function createPersonalLearningRoutes(
deps: PersonalLearningRouteDeps,
requireUser: MiddlewareHandler<{ Variables: AppVariables }>,
) {
const routes = new Hono<{ Variables: AppVariables }>();
routes.use("*", requireUser);
const audit = async (event: Parameters<typeof recordAuditEvent>[1]) => {
if (!deps.auditStore) return;
await recordAuditEvent(deps.auditStore, event);
};
routes.get("/", async (context) => {
const userId = context.var.actor.id;
const [sharing, read, sharingAllowed] = await Promise.all([
deps.store.read(userId),
deps.skillsOf(userId),
deps.sharingAllowed(),
]);
const opened = new Set(sharing.openSkills);
const answer: PersonalLearningSharing =
"skills" in read
? {
mode: sharing.mode,
sharingAllowed,
skills: read.skills.map((skill) => ({
...skill,
open: opened.has(skill.name),
})),
}
: {
mode: sharing.mode,
sharingAllowed,
skills: null,
unavailable: read.unavailable,
};
return context.json(answer);
});
routes.put("/mode", smallBody, async (context) => {
const body = (await context.req.json().catch(() => null)) as {
mode?: unknown;
} | null;
if (!isLearningSharingMode(body?.mode)) {
return context.json(
{ error: "Choose closed, open or choose for your Skills." },
400,
);
}
// Closing is always allowed; opening to every Bot waits for an administrator.
if (body.mode !== "closed" && !(await deps.sharingAllowed())) {
return context.json({ error: SHARING_OFF }, 403);
}
const userId = context.var.actor.id;
const { from, to } = await deps.store.setMode(userId, body.mode);
deps.invalidate();
if (from !== to) {
await audit({
eventType: "learning.sharing_changed",
targetType: "learning_sharing",
targetId: userId,
actorUserId: userId,
payload: { from, to },
});
}
return context.json({ mode: to });
});
routes.put("/skills/:name", smallBody, async (context) => {
const name = context.req.param("name");
const body = (await context.req.json().catch(() => null)) as {
open?: unknown;
} | null;
if (!isLearnedSkillName(name) || typeof body?.open !== "boolean") {
return context.json(
{ error: "Name a Skill and say whether it is open to everyone." },
400,
);
}
if (body.open && !(await deps.sharingAllowed())) {
return context.json({ error: SHARING_OFF }, 403);
}
const userId = context.var.actor.id;
/*
* Only a Skill the person has. Opening is checked against the Skills published in their own space, read the way a
* run reads them, so nobody can open a name in advance for a Skill they do not have yet. Closing also accepts a
* name they opened earlier that has since left their space, so an entry can always be taken back.
*/
const [read, sharing] = await Promise.all([
deps.skillsOf(userId),
body.open ? undefined : deps.store.read(userId),
]);
const theirs =
("skills" in read && read.skills.some((skill) => skill.name === name)) ||
(sharing?.openSkills.includes(name) ?? false);
if (!theirs) {
if ("skills" in read)
return context.json({ error: "You have no Skill by that name." }, 404);
return read.unavailable === "off"
? context.json(
{
error:
"An admin has turned off Automatic Learning, so you have no Skills to change.",
},
409,
)
: context.json(
{
error:
"Couldn't check your Skills with Intelligence just now, so nothing changed. Try again.",
},
503,
);
}
const changed = await deps.store.setSkillOpen(userId, name, body.open);
deps.invalidate();
if (changed) {
await audit({
eventType: body.open
? "learning.skill_opened"
: "learning.skill_closed",
targetType: "learned_skill",
targetId: name,
actorUserId: userId,
payload: { skill: name },
});
}
return context.json({ name, open: body.open });
});
return routes;
}