|
| 1 | +import { readWorkbotModel, withWorkbotModel, workbotSettingsInputSchema, workbotSettingsSchema } from "@openwork/types/den/workbot-settings" |
| 2 | +import type { Hono } from "hono" |
| 3 | +import { describeRoute } from "hono-openapi" |
| 4 | +import { z } from "zod" |
| 5 | +import { requireFeature } from "../../features.js" |
| 6 | +import { jsonValidator, orgMemberRoute, requireOrgPermission } from "../../middleware/index.js" |
| 7 | +import { forbiddenSchema, invalidRequestSchema, jsonResponse, unauthorizedSchema } from "../../openapi.js" |
| 8 | +import { updateOrganizationMetadata } from "../../organization-metadata.js" |
| 9 | +import { |
| 10 | + checkWorkbotModelChoice, |
| 11 | + forgetHeadlessModelCatalog, |
| 12 | + headlessModelCatalog, |
| 13 | + readOrganizationDefaultModel, |
| 14 | + workbotSettingsView, |
| 15 | +} from "../../workbot/model.js" |
| 16 | +import type { OrgRouteVariables } from "./shared.js" |
| 17 | + |
| 18 | +const workbotSettingsDocumentSchema = workbotSettingsSchema.meta({ ref: "WorkbotSettings" }) |
| 19 | +const workbotSettingsInputDocumentSchema = workbotSettingsInputSchema.meta({ ref: "WorkbotSettingsInput" }) |
| 20 | +const featureDisabledSchema = z.object({ error: z.literal("feature_disabled"), feature: z.string() }) |
| 21 | +const unknownModelSchema = z.object({ error: z.literal("unknown_model"), message: z.string() }).meta({ ref: "WorkbotUnknownModelError" }) |
| 22 | +const runnerUnavailableSchema = z |
| 23 | + .object({ error: z.literal("workbot_runner_unavailable"), message: z.string() }) |
| 24 | + .meta({ ref: "WorkbotRunnerUnavailableError" }) |
| 25 | + |
| 26 | +export const UNKNOWN_MODEL_MESSAGE = "Workbot can't use that model. Choose one from the list." |
| 27 | +export const RUNNER_UNAVAILABLE_MESSAGE = "Workbot's models can't be checked right now. Try again in a minute." |
| 28 | + |
| 29 | +const notFound = jsonResponse("The organization was not found, or Workbot or its workbotDefaultModel feature is off.", featureDisabledSchema) |
| 30 | + |
| 31 | +/** |
| 32 | + * Manage › Workbot: the organization's default model for its cloud agents (Workbot, the Slack assistant, and cloud |
| 33 | + * Automations set to the cloud default). Admins choose it from the models the headless runner serves. |
| 34 | + */ |
| 35 | +export function registerOrgWorkbotSettingsRoutes<T extends { Variables: OrgRouteVariables }>(app: Hono<T>) { |
| 36 | + app.get( |
| 37 | + "/v1/org/workbot-settings", |
| 38 | + describeRoute({ |
| 39 | + tags: ["Organizations"], |
| 40 | + summary: "Get Workbot settings", |
| 41 | + description: |
| 42 | + "Returns the organization's default model for Workbot, the Slack assistant and cloud Automations set to the cloud default (null means the headless runner's default), and the models the runner can serve. Requires the View OpenWork Models settings permission, Workbot and the workbotDefaultModel feature.", |
| 43 | + responses: { |
| 44 | + 200: jsonResponse("Workbot settings returned.", workbotSettingsDocumentSchema), |
| 45 | + 401: jsonResponse("Authentication required.", unauthorizedSchema), |
| 46 | + 403: jsonResponse("The caller lacks the View OpenWork Models settings permission.", forbiddenSchema), |
| 47 | + 404: notFound, |
| 48 | + }, |
| 49 | + }), |
| 50 | + orgMemberRoute(), |
| 51 | + requireFeature("workbot"), |
| 52 | + requireFeature("workbotDefaultModel"), |
| 53 | + requireOrgPermission("inference.view"), |
| 54 | + async (c) => { |
| 55 | + c.header("Cache-Control", "no-store") |
| 56 | + const organizationId = c.get("organizationContext").organization.id |
| 57 | + const chosen = await readOrganizationDefaultModel(organizationId) |
| 58 | + return c.json(workbotSettingsView(chosen, await headlessModelCatalog())) |
| 59 | + }, |
| 60 | + ) |
| 61 | + |
| 62 | + app.put( |
| 63 | + "/v1/org/workbot-settings", |
| 64 | + describeRoute({ |
| 65 | + tags: ["Organizations"], |
| 66 | + summary: "Set Workbot's default model", |
| 67 | + description: |
| 68 | + "Sets the organization's default model for Workbot, the Slack assistant and cloud Automations set to the cloud default, from their next message or run; null uses the headless runner's default. Automations with their own model keep it. Only models the runner serves are accepted. Requires the Manage OpenWork Models permission, a recent sign-in, Workbot and the workbotDefaultModel feature.", |
| 69 | + responses: { |
| 70 | + 200: jsonResponse("Workbot settings saved.", workbotSettingsDocumentSchema), |
| 71 | + 400: jsonResponse("The model is not one the runner serves, or the request was invalid.", z.union([unknownModelSchema, invalidRequestSchema])), |
| 72 | + 401: jsonResponse("Authentication required.", unauthorizedSchema), |
| 73 | + 403: jsonResponse("The caller lacks the Manage OpenWork Models permission or needs to sign in again.", forbiddenSchema), |
| 74 | + 404: notFound, |
| 75 | + 503: jsonResponse("The headless runner could not be reached to check the model.", runnerUnavailableSchema), |
| 76 | + }, |
| 77 | + }), |
| 78 | + orgMemberRoute(), |
| 79 | + requireFeature("workbot"), |
| 80 | + requireFeature("workbotDefaultModel"), |
| 81 | + requireOrgPermission("inference.manage"), |
| 82 | + jsonValidator(workbotSettingsInputDocumentSchema), |
| 83 | + async (c) => { |
| 84 | + c.header("Cache-Control", "no-store") |
| 85 | + const organizationId = c.get("organizationContext").organization.id |
| 86 | + const { model } = c.req.valid("json") |
| 87 | + // Checked against a fresh catalog, so a model added to the runner a moment ago can be chosen. |
| 88 | + if (model !== null) forgetHeadlessModelCatalog() |
| 89 | + const catalog = await headlessModelCatalog() |
| 90 | + const choice = checkWorkbotModelChoice(model, catalog) |
| 91 | + if (choice === "runner_unavailable") return c.json({ error: "workbot_runner_unavailable" as const, message: RUNNER_UNAVAILABLE_MESSAGE }, 503) |
| 92 | + if (choice === "unknown_model") return c.json({ error: "unknown_model" as const, message: UNKNOWN_MODEL_MESSAGE }, 400) |
| 93 | + const metadata = await updateOrganizationMetadata(organizationId, (current) => withWorkbotModel(current, model)) |
| 94 | + return c.json(workbotSettingsView(readWorkbotModel(metadata), catalog)) |
| 95 | + }, |
| 96 | + ) |
| 97 | +} |
0 commit comments