Public: anyone can read this; the crew can change it.
GET the models Talk offers me, and `model` on POST /api/v1/conversations and /conversation/messages #17916
Merged 23h 43m ago, live in production
torque/forge:torque/17912-get-the-models-talk-offers-me-and-model → main 0 commits fork 1 thread · 1 unresolved
14 files
changed +474
−166
- docs/features/talk.md
- src/contract/openapi/routes/getApiV1Models.ts
- src/contract/openapi/routes/index.ts
- src/contract/openapi/routes/postApiV1ConversationMessages.ts
- src/contract/openapi/routes/postApiV1Conversations.ts
- src/contract/openapi/schemas/conversation.ts
- src/contract/tags.ts
- src/core/model_choice.ts
- src/generated/marks.ts
- src/http/api/conversation_talk.ts
- src/http/lazy_lists.ts
- src/http/marks/api-v1-models.json
- test/api-models.test.ts
- test/route-timing-coverage.test.ts
docs/features/talk.md +5
| @@ -362,3 +362,8 @@ The meter says how full the model that answered really is: its window comes from | ||
| 362 | 362 | ## When an answer fails |
| 363 | 363 | |
| 364 | 364 | A turn that could not answer says which call failed (the model and the account, `the default model` when none was chosen) and why (timed out, refused, rate limit, or an error class), then offers Retry. Each fact is said once: where the turn's own words already give the cause (a rest, a spent credit, a timeout) only the call is added; where they name the call but not the cause (a chosen model that did not answer) only the cause is. The words come from the same outcome the `talk model call` log line carries (`src/crew/call_line.ts`). They never show a key, the provider's message or a status body. The page and the terminal show the same stored turn. |
| 365 | ||
| 366 | ## Choosing a model over the API | |
| 367 | ||
| 368 | - **API:** `GET /api/v1/models` lists `{ models: [{ id, name, connection, default }] }` in the order Talk's picker has them: the forge's own first (id empty), then each connection's models. `default` marks the one a new conversation starts on. `model` takes an `id` from it on `POST /api/v1/conversations` (start on it) and on `POST /api/v1/conversation/messages` (switch for this and later turns, the same choice the page's picker makes). A model not offered to you now is 422 `model_not_offered` and nothing is made or sent. A message the forge then refuses (rate limit, queue, agent off) leaves the switch undone: the conversation, your last choice and any conversation it would have made stay as they were. A kept message counts as accepted. | |
| 369 | - **Terminal:** the same list is the `/model` picker (`pickList`). |
src/contract/openapi/routes/getApiV1Models.ts added +12
| @@ -0,0 +1,12 @@ | ||
| 1 | import type { Route } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "get", | |
| 5 | path: "/api/v1/models", | |
| 6 | summary: "The models Talk offers the signed-in person, in Talk's order", | |
| 7 | detail: "The models Talk offers the signed-in person, in the order its picker has them: { models: [{ id, name, connection, default }] }. `id` is what `model` takes on POST /api/v1/conversations, /api/v1/conversation/messages and /api/v1/builds (empty for the forge's own); `default` marks the one a new conversation starts on (the person's last choice while offered)", | |
| 8 | auth: true, | |
| 9 | role: "member", | |
| 10 | reply: { models: "array" }, | |
| 11 | refuses: [401], | |
| 12 | }; |
src/contract/openapi/routes/index.ts +1
| @@ -140,6 +140,7 @@ export { route as getApiV1MaintenanceRuns } from "./getApiV1MaintenanceRuns.ts"; | ||
| 140 | 140 | export { route as getApiV1Map } from "./getApiV1Map.ts"; |
| 141 | 141 | export { route as getApiV1MigrateJob } from "./getApiV1MigrateJob.ts"; |
| 142 | 142 | export { route as getApiV1MigrateJobStream } from "./getApiV1MigrateJobStream.ts"; |
| 143 | export { route as getApiV1Models } from "./getApiV1Models.ts"; | |
| 143 | 144 | export { route as getApiV1OffersReceipt } from "./getApiV1OffersReceipt.ts"; |
| 144 | 145 | export { route as getApiV1OpenapiJson } from "./getApiV1OpenapiJson.ts"; |
| 145 | 146 | export { route as getApiV1OrgsOrg } from "./getApiV1OrgsOrg.ts"; |
src/contract/openapi/routes/postApiV1ConversationMessages.ts +4 −2
| @@ -5,14 +5,16 @@ export const route: Route = { | ||
| 5 | 5 | method: "post", |
| 6 | 6 | path: "/api/v1/conversation/messages", |
| 7 | 7 | summary: "Say something to the forge's own agent, as the signed-in person", |
| 8 | detail: "Say something to the forge's own agent, as the signed-in person: it answers through the forge's MCP with that person's permissions, resuming their conversation; a message sent while it answers is handed to the turn, or with `during` (optional) set to `queue` waits for it (at most 5), or set to `steer` stops it and goes at once; answers 202 at once, read the answer from the turns; at most 12 a minute per person. A message that cannot start now (the account resting or out of credit, the forge busy, no model account, the agent not running) is kept with a line saying why and answers 202 too, as { how: \"kept\", why, message, turn, until? } (until: when the account wakes, where known; with no account the body also carries says and ask); a refusal that keeps nothing (an empty or overlong message, no such conversation, too many a minute) stays an error", | |
| 8 | detail: "Say something to the forge's own agent, as the signed-in person: it answers through the forge's MCP with that person's permissions, resuming their conversation; a message sent while it answers is handed to the turn, or with `during` (optional) set to `queue` waits for it (at most 5), or set to `steer` stops it and goes at once; `model` (optional, an id from GET /api/v1/models; empty for the forge's own) switches the conversation to it for this and later turns; answers 202 at once, read the answer from the turns; at most 12 a minute per person. A message that cannot start now (the account resting or out of credit, the forge busy, no model account, the agent not running) is kept with a line saying why and answers 202 too, as { how: \"kept\", why, message, turn, until? } (until: when the account wakes, where known; with no account the body also carries says and ask); a refusal that keeps nothing (an empty or overlong message, no such conversation, too many a minute) stays an error", | |
| 9 | 9 | auth: true, |
| 10 | 10 | role: "member", |
| 11 | 11 | status: 202, |
| 12 | body: { text: "string", conversation: "string" }, | |
| 12 | body: { text: "string", conversation: "string", model: "string" }, | |
| 13 | 13 | refusals: { |
| 14 | 14 | not_found: "conversation names none of the person's (404)", |
| 15 | 15 | invalid_text: "text is not a string of 1 to 20000 characters", |
| 16 | invalid_model: "model is not a string of up to 400 characters (422)", | |
| 17 | model_not_offered: "the model is not one Talk offers you now, so nothing was sent (422)", | |
| 16 | 18 | invalid_during: "during is not queue or steer", |
| 17 | 19 | file_taken: "an attached file already waits in a queued message (409)", |
| 18 | 20 | queue_full: "five messages already wait (429)", |
src/contract/openapi/routes/postApiV1Conversations.ts +9 −4
| @@ -5,13 +5,18 @@ export const route: Route = { | ||
| 5 | 5 | method: "post", |
| 6 | 6 | path: "/api/v1/conversations", |
| 7 | 7 | summary: "Start a new, empty conversation with the forge's agent", |
| 8 | detail: "Start a new, empty conversation with the forge's agent; answers it, and it opens at /talk/<id>", | |
| 8 | detail: "Start a new, empty conversation with the forge's agent; `model` (optional) is an id from GET /api/v1/models and the conversation starts on it; answers it, and it opens at /talk/<id>", | |
| 9 | 9 | auth: true, |
| 10 | 10 | role: "member", |
| 11 | 11 | status: 201, |
| 12 | body: { title: "string" }, | |
| 13 | refusals: { invalid_title: "title is over 80 characters or has a line break (422)", forbidden: "the request is cross-origin, or the token is read-only" }, | |
| 12 | body: { title: "string", model: "string" }, | |
| 13 | refusals: { | |
| 14 | invalid_model: "model is not a string of up to 400 characters (422)", | |
| 15 | model_not_offered: "the model is not one Talk offers you now (422)", | |
| 16 | invalid_title: "title is over 80 characters or has a line break (422)", | |
| 17 | forbidden: "the request is cross-origin, or the token is read-only", | |
| 18 | }, | |
| 14 | 19 | response: ref("ConversationEnvelope"), |
| 15 | request: obj({ title: str }, ["title"]), | |
| 20 | request: obj({ title: str, model: str }, ["title", "model"]), | |
| 16 | 21 | refuses: [401], |
| 17 | 22 | }; |
src/contract/openapi/schemas/conversation.ts +2 −2
| @@ -6,8 +6,8 @@ | ||
| 6 | 6 | import { bool, int, list, obj, orNull, ref, str, words, type Schema } from "../schema_kit.ts"; |
| 7 | 7 | |
| 8 | 8 | const MESSAGE_REQUEST: Schema = obj( |
| 9 | { text: str, conversation: str, files: list(str), during: words("queue", "steer"), about: str }, | |
| 10 | ["conversation", "files", "during", "about"], | |
| 9 | { text: str, conversation: str, files: list(str), during: words("queue", "steer"), about: str, model: str }, | |
| 10 | ["conversation", "files", "during", "about", "model"], | |
| 11 | 11 | ); |
| 12 | 12 | |
| 13 | 13 | /** |
src/contract/tags.ts +1
src/core/model_choice.ts +64
| @@ -15,6 +15,7 @@ import { | ||
| 15 | 15 | listConnections, |
| 16 | 16 | type Connection, |
| 17 | 17 | } from "./connections.ts"; |
| 18 | import { dropConversation, openConversation } from "./conversations.ts"; | |
| 18 | 19 | import { getUserSetting, setUserSetting } from "./settings.ts"; |
| 19 | 20 | import { anyTalkConnection, EVERYONE } from "./subscriptions.ts"; |
| 20 | 21 | import { getUserById } from "./users.ts"; |
| @@ -187,6 +188,35 @@ export const pickList = (db: Database, user: Person): { id: string; label: strin | ||
| 187 | 188 | ...offers(db, user).flatMap((o) => o.models.map((m) => ({ id: choiceValue({ connectionId: o.connection.id, model: m }), label: choiceLabel(o.connection, m) }))), |
| 188 | 189 | ]; |
| 189 | 190 | |
| 191 | /** | |
| 192 | * One model as the API lists it: `id` is the picker's value (what `model` takes), "" for the | |
| 193 | * forge's own. | |
| 194 | */ | |
| 195 | export interface ModelEntry { | |
| 196 | readonly id: string; | |
| 197 | readonly name: string; | |
| 198 | readonly connection: string; | |
| 199 | readonly default: boolean; | |
| 200 | } | |
| 201 | ||
| 202 | /** | |
| 203 | * Every model this person may use, in the order Talk offers them (the forge's own first, then each | |
| 204 | * connection's, `pickList`), and which one a new conversation starts on (`startingChoice`). | |
| 205 | */ | |
| 206 | export function modelList(db: Database, user: Person): ModelEntry[] { | |
| 207 | const start = startingChoice(db, user); | |
| 208 | const now = start === undefined ? "" : choiceValue(start); | |
| 209 | return [ | |
| 210 | { id: "", name: FORGE_MODEL, connection: "", default: now === "" }, | |
| 211 | ...offers(db, user).flatMap((o) => | |
| 212 | o.models.map((m) => { | |
| 213 | const id = choiceValue({ connectionId: o.connection.id, model: m }); | |
| 214 | return { id, name: plainName(m, o.connection.name), connection: o.connection.name, default: id === now }; | |
| 215 | }), | |
| 216 | ), | |
| 217 | ]; | |
| 218 | } | |
| 219 | ||
| 190 | 220 | /** |
| 191 | 221 | * Set a conversation's model from a picker's value: "" for the forge's own, else one the picker |
| 192 | 222 | * offers this person now. Anything else is refused and nothing changes. The choice is also kept as |
| @@ -215,6 +245,40 @@ export function setChoice( | ||
| 215 | 245 | return choice; |
| 216 | 246 | } |
| 217 | 247 | |
| 248 | /** | |
| 249 | * Switch the conversation a message goes to (the one named, else the open one, made when there is | |
| 250 | * none) before its turn runs, which reads the choice. `undo` puts everything back when the message | |
| 251 | * is then refused: the conversation's own choice, the person's last choice, and a conversation this | |
| 252 | * made, so a refused message changes nothing. | |
| 253 | */ | |
| 254 | export function switchModel( | |
| 255 | db: Database, | |
| 256 | user: Person, | |
| 257 | named: string | undefined, | |
| 258 | value: string, | |
| 259 | ): Result<{ readonly undo: () => void }, "not_offered" | "not_found"> { | |
| 260 | if (!choiceFrom(db, user, value).ok) return err("not_offered"); | |
| 261 | const made = named === undefined && db.query<{ n: number }, [string]>("SELECT COUNT(*) AS n FROM conversation WHERE user_id = ?1 AND archived_at IS NULL").get(user.id)!.n === 0; | |
| 262 | const target = named ?? openConversation(db, user.id).id; | |
| 263 | const row = db.query<{ connection_id: string; model: string }, [string, string]>("SELECT connection_id, model FROM conversation WHERE id = ?1 AND user_id = ?2").get(target, user.id); | |
| 264 | const last = getUserSetting(db, user.id, LAST_KEY) ?? ""; | |
| 265 | const set = setChoice(db, user, target, value); | |
| 266 | if (!set.ok) { | |
| 267 | if (made) dropConversation(db, user.id, target); | |
| 268 | return set; | |
| 269 | } | |
| 270 | return { | |
| 271 | ok: true, | |
| 272 | value: { | |
| 273 | undo: () => { | |
| 274 | if (made) dropConversation(db, user.id, target); | |
| 275 | else if (row !== null) db.query("UPDATE conversation SET connection_id = ?2, model = ?3 WHERE id = ?1").run(target, row.connection_id, row.model); | |
| 276 | setUserSetting(db, user.id, LAST_KEY, last); | |
| 277 | }, | |
| 278 | }, | |
| 279 | }; | |
| 280 | } | |
| 281 | ||
| 218 | 282 | /** |
| 219 | 283 | * What a turn on a choice runs on: the connection, when the person may still use it; undefined |
| 220 | 284 | * when it was removed or is no longer theirs to use (the turn then says so and runs nothing). |
src/generated/marks.ts +160 −158
| @@ -51,164 +51,165 @@ import k46 from "../http/marks/api-v1-mail-key.json"; | ||
| 51 | 51 | import k47 from "../http/marks/api-v1-maintenance.json"; |
| 52 | 52 | import k48 from "../http/marks/api-v1-map.json"; |
| 53 | 53 | import k49 from "../http/marks/api-v1-migrate.json"; |
| 54 | import k50 from "../http/marks/api-v1-offers.json"; | |
| 55 | import k51 from "../http/marks/api-v1-openapi.json.json"; | |
| 56 | import k52 from "../http/marks/api-v1-orgs.json"; | |
| 57 | import k53 from "../http/marks/api-v1-packs.json"; | |
| 58 | import k54 from "../http/marks/api-v1-passport.json"; | |
| 59 | import k55 from "../http/marks/api-v1-places.json"; | |
| 60 | import k56 from "../http/marks/api-v1-plans.json"; | |
| 61 | import k57 from "../http/marks/api-v1-proof.json"; | |
| 62 | import k58 from "../http/marks/api-v1-reach.json"; | |
| 63 | import k59 from "../http/marks/api-v1-release.json"; | |
| 64 | import k60 from "../http/marks/api-v1-releases.json"; | |
| 65 | import k61 from "../http/marks/api-v1-repos.json"; | |
| 66 | import k62 from "../http/marks/api-v1-returns.json"; | |
| 67 | import k63 from "../http/marks/api-v1-runner.json"; | |
| 68 | import k64 from "../http/marks/api-v1-search.json"; | |
| 69 | import k65 from "../http/marks/api-v1-secrets.json"; | |
| 70 | import k66 from "../http/marks/api-v1-sessions.json"; | |
| 71 | import k67 from "../http/marks/api-v1-share.json"; | |
| 72 | import k68 from "../http/marks/api-v1-slash.json"; | |
| 73 | import k69 from "../http/marks/api-v1-start.json"; | |
| 74 | import k70 from "../http/marks/api-v1-status.json"; | |
| 75 | import k71 from "../http/marks/api-v1-telemetry.json"; | |
| 76 | import k72 from "../http/marks/api-v1-templates.json"; | |
| 77 | import k73 from "../http/marks/api-v1-thoughts.json"; | |
| 78 | import k74 from "../http/marks/api-v1-timers.json"; | |
| 79 | import k75 from "../http/marks/api-v1-tokens.json"; | |
| 80 | import k76 from "../http/marks/api-v1-usage.json"; | |
| 81 | import k77 from "../http/marks/api-v1-user.json"; | |
| 82 | import k78 from "../http/marks/api-v1-users.json"; | |
| 83 | import k79 from "../http/marks/api-v1-version.json"; | |
| 84 | import k80 from "../http/marks/api-v1.json"; | |
| 85 | import k81 from "../http/marks/api.json"; | |
| 86 | import k82 from "../http/marks/apple-touch-icon-precomposed.png.json"; | |
| 87 | import k83 from "../http/marks/apple-touch-icon.png.json"; | |
| 88 | import k84 from "../http/marks/apps.json"; | |
| 89 | import k85 from "../http/marks/archive.json"; | |
| 90 | import k86 from "../http/marks/asked.json"; | |
| 91 | import k87 from "../http/marks/asks.json"; | |
| 92 | import k88 from "../http/marks/assets.json"; | |
| 93 | import k89 from "../http/marks/audit.csv.json"; | |
| 94 | import k90 from "../http/marks/audit.json"; | |
| 95 | import k91 from "../http/marks/big.json"; | |
| 96 | import k92 from "../http/marks/board.json"; | |
| 97 | import k93 from "../http/marks/boards.json"; | |
| 98 | import k94 from "../http/marks/bring.json"; | |
| 99 | import k95 from "../http/marks/broker.json"; | |
| 100 | import k96 from "../http/marks/changelog.atom.json"; | |
| 101 | import k97 from "../http/marks/changelog.json"; | |
| 102 | import k98 from "../http/marks/claim.json"; | |
| 103 | import k99 from "../http/marks/client-clouds.json"; | |
| 104 | import k100 from "../http/marks/cloud.json"; | |
| 105 | import k101 from "../http/marks/cockpit.json"; | |
| 106 | import k102 from "../http/marks/contract.json"; | |
| 107 | import k103 from "../http/marks/contribute.json"; | |
| 108 | import k104 from "../http/marks/csp-report.json"; | |
| 109 | import k105 from "../http/marks/dash.json"; | |
| 110 | import k106 from "../http/marks/data.json"; | |
| 111 | import k107 from "../http/marks/dataset.json"; | |
| 112 | import k108 from "../http/marks/deploy-to.json"; | |
| 113 | import k109 from "../http/marks/design.json"; | |
| 114 | import k110 from "../http/marks/desk.json"; | |
| 115 | import k111 from "../http/marks/displays.json"; | |
| 116 | import k112 from "../http/marks/docs.json"; | |
| 117 | import k113 from "../http/marks/edge.json"; | |
| 118 | import k114 from "../http/marks/edition.json"; | |
| 119 | import k115 from "../http/marks/events.json"; | |
| 120 | import k116 from "../http/marks/explore.atom.json"; | |
| 121 | import k117 from "../http/marks/explore.json"; | |
| 122 | import k118 from "../http/marks/explore.json.json"; | |
| 123 | import k119 from "../http/marks/family.json"; | |
| 124 | import k120 from "../http/marks/favicon.ico.json"; | |
| 125 | import k121 from "../http/marks/feed.json"; | |
| 126 | import k122 from "../http/marks/feed.json.json"; | |
| 127 | import k123 from "../http/marks/first-admin.json"; | |
| 128 | import k124 from "../http/marks/fleet.json"; | |
| 129 | import k125 from "../http/marks/flow.json"; | |
| 130 | import k126 from "../http/marks/forge.json"; | |
| 131 | import k127 from "../http/marks/forges.json"; | |
| 132 | import k128 from "../http/marks/forks.json"; | |
| 133 | import k129 from "../http/marks/front.json"; | |
| 134 | import k130 from "../http/marks/funnel.json"; | |
| 135 | import k131 from "../http/marks/games.json"; | |
| 136 | import k132 from "../http/marks/go.json"; | |
| 137 | import k133 from "../http/marks/health.json"; | |
| 138 | import k134 from "../http/marks/home.json"; | |
| 139 | import k135 from "../http/marks/hooks.json"; | |
| 140 | import k136 from "../http/marks/icons.json"; | |
| 141 | import k137 from "../http/marks/infra.json"; | |
| 142 | import k138 from "../http/marks/join.json"; | |
| 143 | import k139 from "../http/marks/live.json"; | |
| 144 | import k140 from "../http/marks/llms-full.txt.json"; | |
| 145 | import k141 from "../http/marks/llms.txt.json"; | |
| 146 | import k142 from "../http/marks/login.json"; | |
| 147 | import k143 from "../http/marks/logout.json"; | |
| 148 | import k144 from "../http/marks/maintenance-json.json"; | |
| 149 | import k145 from "../http/marks/maintenance.json"; | |
| 150 | import k146 from "../http/marks/manifest.webmanifest.json"; | |
| 151 | import k147 from "../http/marks/map.json"; | |
| 152 | import k148 from "../http/marks/mcp.json"; | |
| 153 | import k149 from "../http/marks/measure.json"; | |
| 154 | import k150 from "../http/marks/metrics.json"; | |
| 155 | import k151 from "../http/marks/migrate.json"; | |
| 156 | import k152 from "../http/marks/new.json"; | |
| 157 | import k153 from "../http/marks/next.json"; | |
| 158 | import k154 from "../http/marks/notes.json"; | |
| 159 | import k155 from "../http/marks/now.json"; | |
| 160 | import k156 from "../http/marks/oauth.json"; | |
| 161 | import k157 from "../http/marks/oembed.json"; | |
| 162 | import k158 from "../http/marks/offers.json"; | |
| 163 | import k159 from "../http/marks/offline.json"; | |
| 164 | import k160 from "../http/marks/og.json"; | |
| 165 | import k161 from "../http/marks/openapi.json.json"; | |
| 166 | import k162 from "../http/marks/opensearch.xml.json"; | |
| 167 | import k163 from "../http/marks/organizations.json"; | |
| 168 | import k164 from "../http/marks/origins.json"; | |
| 169 | import k165 from "../http/marks/owner.atom.json"; | |
| 170 | import k166 from "../http/marks/owner.json"; | |
| 171 | import k167 from "../http/marks/packs.json"; | |
| 172 | import k168 from "../http/marks/peek.json"; | |
| 173 | import k169 from "../http/marks/pinned.json"; | |
| 174 | import k170 from "../http/marks/pipeline.json"; | |
| 175 | import k171 from "../http/marks/plugins.json"; | |
| 176 | import k172 from "../http/marks/pricing.json"; | |
| 177 | import k173 from "../http/marks/privacy.json"; | |
| 178 | import k174 from "../http/marks/r.json"; | |
| 179 | import k175 from "../http/marks/recover.json"; | |
| 180 | import k176 from "../http/marks/releases.json"; | |
| 181 | import k177 from "../http/marks/releases.json.json"; | |
| 182 | import k178 from "../http/marks/repos.json"; | |
| 183 | import k179 from "../http/marks/resilience.json"; | |
| 184 | import k180 from "../http/marks/review.json"; | |
| 185 | import k181 from "../http/marks/robots.txt.json"; | |
| 186 | import k182 from "../http/marks/root.json"; | |
| 187 | import k183 from "../http/marks/runs.json"; | |
| 188 | import k184 from "../http/marks/script-error.json"; | |
| 189 | import k185 from "../http/marks/search.json"; | |
| 190 | import k186 from "../http/marks/security.json"; | |
| 191 | import k187 from "../http/marks/settings.json"; | |
| 192 | import k188 from "../http/marks/sign-in.json"; | |
| 193 | import k189 from "../http/marks/signin.json"; | |
| 194 | import k190 from "../http/marks/signup.json"; | |
| 195 | import k191 from "../http/marks/sitemap.xml.json"; | |
| 196 | import k192 from "../http/marks/status.json"; | |
| 197 | import k193 from "../http/marks/sw.js.json"; | |
| 198 | import k194 from "../http/marks/system.json"; | |
| 199 | import k195 from "../http/marks/tab.json"; | |
| 200 | import k196 from "../http/marks/tabforge.json"; | |
| 201 | import k197 from "../http/marks/talk.json"; | |
| 202 | import k198 from "../http/marks/tell.json"; | |
| 203 | import k199 from "../http/marks/templates.json"; | |
| 204 | import k200 from "../http/marks/templates.json.json"; | |
| 205 | import k201 from "../http/marks/terms.json"; | |
| 206 | import k202 from "../http/marks/theme.json"; | |
| 207 | import k203 from "../http/marks/toolbar-lab.js.json"; | |
| 208 | import k204 from "../http/marks/toolbar-lab.json"; | |
| 209 | import k205 from "../http/marks/verify.json"; | |
| 210 | import k206 from "../http/marks/welcome.json"; | |
| 211 | import k207 from "../http/marks/well-known.json"; | |
| 54 | import k50 from "../http/marks/api-v1-models.json"; | |
| 55 | import k51 from "../http/marks/api-v1-offers.json"; | |
| 56 | import k52 from "../http/marks/api-v1-openapi.json.json"; | |
| 57 | import k53 from "../http/marks/api-v1-orgs.json"; | |
| 58 | import k54 from "../http/marks/api-v1-packs.json"; | |
| 59 | import k55 from "../http/marks/api-v1-passport.json"; | |
| 60 | import k56 from "../http/marks/api-v1-places.json"; | |
| 61 | import k57 from "../http/marks/api-v1-plans.json"; | |
| 62 | import k58 from "../http/marks/api-v1-proof.json"; | |
| 63 | import k59 from "../http/marks/api-v1-reach.json"; | |
| 64 | import k60 from "../http/marks/api-v1-release.json"; | |
| 65 | import k61 from "../http/marks/api-v1-releases.json"; | |
| 66 | import k62 from "../http/marks/api-v1-repos.json"; | |
| 67 | import k63 from "../http/marks/api-v1-returns.json"; | |
| 68 | import k64 from "../http/marks/api-v1-runner.json"; | |
| 69 | import k65 from "../http/marks/api-v1-search.json"; | |
| 70 | import k66 from "../http/marks/api-v1-secrets.json"; | |
| 71 | import k67 from "../http/marks/api-v1-sessions.json"; | |
| 72 | import k68 from "../http/marks/api-v1-share.json"; | |
| 73 | import k69 from "../http/marks/api-v1-slash.json"; | |
| 74 | import k70 from "../http/marks/api-v1-start.json"; | |
| 75 | import k71 from "../http/marks/api-v1-status.json"; | |
| 76 | import k72 from "../http/marks/api-v1-telemetry.json"; | |
| 77 | import k73 from "../http/marks/api-v1-templates.json"; | |
| 78 | import k74 from "../http/marks/api-v1-thoughts.json"; | |
| 79 | import k75 from "../http/marks/api-v1-timers.json"; | |
| 80 | import k76 from "../http/marks/api-v1-tokens.json"; | |
| 81 | import k77 from "../http/marks/api-v1-usage.json"; | |
| 82 | import k78 from "../http/marks/api-v1-user.json"; | |
| 83 | import k79 from "../http/marks/api-v1-users.json"; | |
| 84 | import k80 from "../http/marks/api-v1-version.json"; | |
| 85 | import k81 from "../http/marks/api-v1.json"; | |
| 86 | import k82 from "../http/marks/api.json"; | |
| 87 | import k83 from "../http/marks/apple-touch-icon-precomposed.png.json"; | |
| 88 | import k84 from "../http/marks/apple-touch-icon.png.json"; | |
| 89 | import k85 from "../http/marks/apps.json"; | |
| 90 | import k86 from "../http/marks/archive.json"; | |
| 91 | import k87 from "../http/marks/asked.json"; | |
| 92 | import k88 from "../http/marks/asks.json"; | |
| 93 | import k89 from "../http/marks/assets.json"; | |
| 94 | import k90 from "../http/marks/audit.csv.json"; | |
| 95 | import k91 from "../http/marks/audit.json"; | |
| 96 | import k92 from "../http/marks/big.json"; | |
| 97 | import k93 from "../http/marks/board.json"; | |
| 98 | import k94 from "../http/marks/boards.json"; | |
| 99 | import k95 from "../http/marks/bring.json"; | |
| 100 | import k96 from "../http/marks/broker.json"; | |
| 101 | import k97 from "../http/marks/changelog.atom.json"; | |
| 102 | import k98 from "../http/marks/changelog.json"; | |
| 103 | import k99 from "../http/marks/claim.json"; | |
| 104 | import k100 from "../http/marks/client-clouds.json"; | |
| 105 | import k101 from "../http/marks/cloud.json"; | |
| 106 | import k102 from "../http/marks/cockpit.json"; | |
| 107 | import k103 from "../http/marks/contract.json"; | |
| 108 | import k104 from "../http/marks/contribute.json"; | |
| 109 | import k105 from "../http/marks/csp-report.json"; | |
| 110 | import k106 from "../http/marks/dash.json"; | |
| 111 | import k107 from "../http/marks/data.json"; | |
| 112 | import k108 from "../http/marks/dataset.json"; | |
| 113 | import k109 from "../http/marks/deploy-to.json"; | |
| 114 | import k110 from "../http/marks/design.json"; | |
| 115 | import k111 from "../http/marks/desk.json"; | |
| 116 | import k112 from "../http/marks/displays.json"; | |
| 117 | import k113 from "../http/marks/docs.json"; | |
| 118 | import k114 from "../http/marks/edge.json"; | |
| 119 | import k115 from "../http/marks/edition.json"; | |
| 120 | import k116 from "../http/marks/events.json"; | |
| 121 | import k117 from "../http/marks/explore.atom.json"; | |
| 122 | import k118 from "../http/marks/explore.json"; | |
| 123 | import k119 from "../http/marks/explore.json.json"; | |
| 124 | import k120 from "../http/marks/family.json"; | |
| 125 | import k121 from "../http/marks/favicon.ico.json"; | |
| 126 | import k122 from "../http/marks/feed.json"; | |
| 127 | import k123 from "../http/marks/feed.json.json"; | |
| 128 | import k124 from "../http/marks/first-admin.json"; | |
| 129 | import k125 from "../http/marks/fleet.json"; | |
| 130 | import k126 from "../http/marks/flow.json"; | |
| 131 | import k127 from "../http/marks/forge.json"; | |
| 132 | import k128 from "../http/marks/forges.json"; | |
| 133 | import k129 from "../http/marks/forks.json"; | |
| 134 | import k130 from "../http/marks/front.json"; | |
| 135 | import k131 from "../http/marks/funnel.json"; | |
| 136 | import k132 from "../http/marks/games.json"; | |
| 137 | import k133 from "../http/marks/go.json"; | |
| 138 | import k134 from "../http/marks/health.json"; | |
| 139 | import k135 from "../http/marks/home.json"; | |
| 140 | import k136 from "../http/marks/hooks.json"; | |
| 141 | import k137 from "../http/marks/icons.json"; | |
| 142 | import k138 from "../http/marks/infra.json"; | |
| 143 | import k139 from "../http/marks/join.json"; | |
| 144 | import k140 from "../http/marks/live.json"; | |
| 145 | import k141 from "../http/marks/llms-full.txt.json"; | |
| 146 | import k142 from "../http/marks/llms.txt.json"; | |
| 147 | import k143 from "../http/marks/login.json"; | |
| 148 | import k144 from "../http/marks/logout.json"; | |
| 149 | import k145 from "../http/marks/maintenance-json.json"; | |
| 150 | import k146 from "../http/marks/maintenance.json"; | |
| 151 | import k147 from "../http/marks/manifest.webmanifest.json"; | |
| 152 | import k148 from "../http/marks/map.json"; | |
| 153 | import k149 from "../http/marks/mcp.json"; | |
| 154 | import k150 from "../http/marks/measure.json"; | |
| 155 | import k151 from "../http/marks/metrics.json"; | |
| 156 | import k152 from "../http/marks/migrate.json"; | |
| 157 | import k153 from "../http/marks/new.json"; | |
| 158 | import k154 from "../http/marks/next.json"; | |
| 159 | import k155 from "../http/marks/notes.json"; | |
| 160 | import k156 from "../http/marks/now.json"; | |
| 161 | import k157 from "../http/marks/oauth.json"; | |
| 162 | import k158 from "../http/marks/oembed.json"; | |
| 163 | import k159 from "../http/marks/offers.json"; | |
| 164 | import k160 from "../http/marks/offline.json"; | |
| 165 | import k161 from "../http/marks/og.json"; | |
| 166 | import k162 from "../http/marks/openapi.json.json"; | |
| 167 | import k163 from "../http/marks/opensearch.xml.json"; | |
| 168 | import k164 from "../http/marks/organizations.json"; | |
| 169 | import k165 from "../http/marks/origins.json"; | |
| 170 | import k166 from "../http/marks/owner.atom.json"; | |
| 171 | import k167 from "../http/marks/owner.json"; | |
| 172 | import k168 from "../http/marks/packs.json"; | |
| 173 | import k169 from "../http/marks/peek.json"; | |
| 174 | import k170 from "../http/marks/pinned.json"; | |
| 175 | import k171 from "../http/marks/pipeline.json"; | |
| 176 | import k172 from "../http/marks/plugins.json"; | |
| 177 | import k173 from "../http/marks/pricing.json"; | |
| 178 | import k174 from "../http/marks/privacy.json"; | |
| 179 | import k175 from "../http/marks/r.json"; | |
| 180 | import k176 from "../http/marks/recover.json"; | |
| 181 | import k177 from "../http/marks/releases.json"; | |
| 182 | import k178 from "../http/marks/releases.json.json"; | |
| 183 | import k179 from "../http/marks/repos.json"; | |
| 184 | import k180 from "../http/marks/resilience.json"; | |
| 185 | import k181 from "../http/marks/review.json"; | |
| 186 | import k182 from "../http/marks/robots.txt.json"; | |
| 187 | import k183 from "../http/marks/root.json"; | |
| 188 | import k184 from "../http/marks/runs.json"; | |
| 189 | import k185 from "../http/marks/script-error.json"; | |
| 190 | import k186 from "../http/marks/search.json"; | |
| 191 | import k187 from "../http/marks/security.json"; | |
| 192 | import k188 from "../http/marks/settings.json"; | |
| 193 | import k189 from "../http/marks/sign-in.json"; | |
| 194 | import k190 from "../http/marks/signin.json"; | |
| 195 | import k191 from "../http/marks/signup.json"; | |
| 196 | import k192 from "../http/marks/sitemap.xml.json"; | |
| 197 | import k193 from "../http/marks/status.json"; | |
| 198 | import k194 from "../http/marks/sw.js.json"; | |
| 199 | import k195 from "../http/marks/system.json"; | |
| 200 | import k196 from "../http/marks/tab.json"; | |
| 201 | import k197 from "../http/marks/tabforge.json"; | |
| 202 | import k198 from "../http/marks/talk.json"; | |
| 203 | import k199 from "../http/marks/tell.json"; | |
| 204 | import k200 from "../http/marks/templates.json"; | |
| 205 | import k201 from "../http/marks/templates.json.json"; | |
| 206 | import k202 from "../http/marks/terms.json"; | |
| 207 | import k203 from "../http/marks/theme.json"; | |
| 208 | import k204 from "../http/marks/toolbar-lab.js.json"; | |
| 209 | import k205 from "../http/marks/toolbar-lab.json"; | |
| 210 | import k206 from "../http/marks/verify.json"; | |
| 211 | import k207 from "../http/marks/welcome.json"; | |
| 212 | import k208 from "../http/marks/well-known.json"; | |
| 212 | 213 | |
| 213 | 214 | /** Each file of `http/marks/`, parsed, in name order. */ |
| 214 | 215 | export const MARK_FILES: readonly unknown[] = [ |
| @@ -420,4 +421,5 @@ export const MARK_FILES: readonly unknown[] = [ | ||
| 420 | 421 | k205, |
| 421 | 422 | k206, |
| 422 | 423 | k207, |
| 424 | k208, | |
| 423 | 425 | ]; |
src/http/api/conversation_talk.ts +27
| @@ -29,6 +29,8 @@ import { | ||
| 29 | 29 | TITLE_MAX, |
| 30 | 30 | } from "../../core/conversations.ts"; |
| 31 | 31 | import { FILE_WORDS, fileBytes, fileOwnedBy, FILES_PER_MESSAGE, IMAGE_MAX, pendingAmong, removePending, storeFile } from "../../core/conversation_files.ts"; |
| 32 | import { choiceFrom, modelList, setChoice, switchModel } from "../../core/model_choice.ts"; | |
| 33 | import { modelRefusal } from "../../actions/build_input.ts"; | |
| 32 | 34 | import { WORDS_MAX } from "../../core/fence.ts"; |
| 33 | 35 | import { notePhase, takeSessionTime } from "../../core/route_timing.ts"; |
| 34 | 36 | import { scopeAllows } from "../../core/tokens.ts"; |
| @@ -97,8 +99,20 @@ export function conversationTalkRoutes(router: Router<Handler>): void { | ||
| 97 | 99 | // stops it and sends these at once. |
| 98 | 100 | const during = input["during"]; |
| 99 | 101 | if (during !== undefined && during !== "queue" && during !== "steer") return apiError("invalid_during", 422, "during is queue or steer"); |
| 102 | // `model` switches the conversation for this and later turns, only to one Talk offers now. | |
| 103 | const model = input["model"]; | |
| 104 | if (modelRefusal(model) !== undefined) return apiError("invalid_model", 422, modelRefusal(model)!); | |
| 105 | // The turn reads the choice as it starts, so the switch comes first and is taken back when | |
| 106 | // the message is refused (rate limit, queue, files): a refused message changes nothing. | |
| 107 | let undo: (() => void) | undefined; | |
| 108 | if (typeof model === "string") { | |
| 109 | const switched = switchModel(ctx.app.db, user, named, model); | |
| 110 | if (!switched.ok) return switched.error === "not_offered" ? apiError("model_not_offered", 422, MODEL_WORDS) : apiError("not_found", 404, "no such conversation"); | |
| 111 | undo = switched.value.undo; | |
| 112 | } | |
| 100 | 113 | const said = talk(ctx.app, user, text, files.value, during === "queue" ? "queue" : during === "steer" ? "interrupt" : "inject", named, typeof input["about"] === "string" ? input["about"] : null); |
| 101 | 114 | if (!said.ok) { |
| 115 | undo?.(); | |
| 102 | 116 | const [status, words] = TALK_WORDS[said.error]; |
| 103 | 117 | return status === 429 || status === 503 ? tryAgain(said.error, status, words, talkWait(said.error)) : json({ error: said.error, message: words }, status); |
| 104 | 118 | } |
| @@ -237,6 +251,13 @@ export function conversationTalkRoutes(router: Router<Handler>): void { | ||
| 237 | 251 | return new Response(text, { status: 200, headers: { "content-type": "application/json", ...headers } }); |
| 238 | 252 | }); |
| 239 | 253 | |
| 254 | // What `model` takes, in Talk's order, and which one a new conversation starts on. | |
| 255 | router.add("GET", "/api/v1/models", (ctx) => { | |
| 256 | const user = ctx.auth.user; | |
| 257 | if (user === undefined) return unauthorized(); | |
| 258 | return new Response(JSON.stringify({ models: modelList(ctx.app.db, user) }), { headers: { "content-type": "application/json", "cache-control": "private, no-cache", vary: "authorization, cookie" } }); | |
| 259 | }); | |
| 260 | ||
| 240 | 261 | router.add("POST", "/api/v1/conversations", async (ctx) => { |
| 241 | 262 | const user = mayChange(ctx); |
| 242 | 263 | if (user instanceof Response) return user; |
| @@ -244,7 +265,12 @@ export function conversationTalkRoutes(router: Router<Handler>): void { | ||
| 244 | 265 | if (input === "too_large") return apiError("too_large", 413, "at most 4000 bytes"); |
| 245 | 266 | const title = input["title"]; |
| 246 | 267 | if (title !== undefined && typeof title !== "string") return apiError("invalid_title", 422, TITLE_WORDS); |
| 268 | const model = input["model"]; | |
| 269 | if (modelRefusal(model) !== undefined) return apiError("invalid_model", 422, modelRefusal(model)!); | |
| 270 | if (typeof model === "string" && !choiceFrom(ctx.app.db, user, model).ok) return apiError("model_not_offered", 422, MODEL_WORDS); | |
| 247 | 271 | const started = createConversation(ctx.app.db, user.id); |
| 272 | // Offered a moment ago and the conversation is new and theirs: setChoice cannot refuse here. | |
| 273 | if (typeof model === "string") setChoice(ctx.app.db, user, started.value.id, model); | |
| 248 | 274 | const made = started.value; |
| 249 | 275 | if (title === undefined || title.trim() === "") return json({ conversation: made }, 201); |
| 250 | 276 | const named = renameConversation(ctx.app.db, user.id, made.id, title); |
| @@ -268,6 +294,7 @@ export function conversationTalkRoutes(router: Router<Handler>): void { | ||
| 268 | 294 | }); |
| 269 | 295 | } |
| 270 | 296 | |
| 297 | const MODEL_WORDS = "that model is not offered to you now; GET /api/v1/models lists the ones that are"; | |
| 271 | 298 | const ID = /^[0-9a-z]{1,40}$/; |
| 272 | 299 | const TITLE_WORDS = `title is text of at most ${TITLE_MAX} characters, on one line`; |
| 273 | 300 |
src/http/lazy_lists.ts +1
| @@ -1468,6 +1468,7 @@ export const API_ROUTES = [ | ||
| 1468 | 1468 | ["DELETE", "/api/v1/conversation/files/:id"], |
| 1469 | 1469 | ["GET", "/api/v1/conversation/turns"], |
| 1470 | 1470 | ["GET", "/api/v1/conversations"], |
| 1471 | ["GET", "/api/v1/models"], | |
| 1471 | 1472 | ["POST", "/api/v1/conversations"], |
| 1472 | 1473 | ["PATCH", "/api/v1/conversations/:id"], |
| 1473 | 1474 | ["POST", "/api/v1/builds"], |
src/http/marks/api-v1-models.json added +4
test/api-models.test.ts added +183
| @@ -0,0 +1,183 @@ | ||
| 1 | /** | |
| 2 | * The models Talk offers, over the API: `GET /api/v1/models` lists them in the picker's order with | |
| 3 | * the starting one marked; `model` on `POST /api/v1/conversations` and `/conversation/messages` | |
| 4 | * chooses one, and refuses one not offered with 422 `model_not_offered`, as builds do. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { listConnections, saveConnection } from "../src/core/connections.ts"; | |
| 12 | import { choiceOf, pickList } from "../src/core/model_choice.ts"; | |
| 13 | import { EVERYONE } from "../src/core/subscriptions.ts"; | |
| 14 | import { createToken } from "../src/core/tokens.ts"; | |
| 15 | import { createUser, type User } from "../src/core/users.ts"; | |
| 16 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 17 | import { quietLogs } from "./helpers/globals.ts"; | |
| 18 | import { promote } from "./helpers/members.ts"; | |
| 19 | ||
| 20 | quietLogs(); | |
| 21 | ||
| 22 | let dir: string; | |
| 23 | let app: App; | |
| 24 | let servers: Servers; | |
| 25 | let keeper: User; | |
| 26 | let bob: User; | |
| 27 | let keeperToken: string; | |
| 28 | let bobToken: string; | |
| 29 | let sparkId: string; | |
| 30 | ||
| 31 | const call = async (token: string, method: string, path: string, body?: unknown) => { | |
| 32 | const res = await fetch(`${servers.url}${path}`, { | |
| 33 | method, | |
| 34 | headers: { "content-type": "application/json", authorization: `Bearer ${token}` }, | |
| 35 | ...(body === undefined ? {} : { body: JSON.stringify(body) }), | |
| 36 | }); | |
| 37 | return { status: res.status, body: (await res.json()) as Record<string, any> }; | |
| 38 | }; | |
| 39 | ||
| 40 | beforeAll(async () => { | |
| 41 | dir = await mkdtemp(join(tmpdir(), "forge-api-models-")); | |
| 42 | app = createApp({ dataDir: dir, port: 0 }); | |
| 43 | servers = await serve(app); | |
| 44 | const person = async (username: string, first = false) => { | |
| 45 | const made = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: first }); | |
| 46 | if (!made.ok) throw new Error(made.error); | |
| 47 | promote(app.db, made.value); | |
| 48 | return made.value; | |
| 49 | }; | |
| 50 | keeper = await person("keeper", true); | |
| 51 | bob = await person("bob"); | |
| 52 | const spark = saveConnection(app.db, app.config.dataDir, { | |
| 53 | name: "Spark", | |
| 54 | protocol: "openai", | |
| 55 | base: "http://127.0.0.1:9/v1", | |
| 56 | key: "sk-spark", | |
| 57 | scope: "forge", | |
| 58 | userId: keeper.id, | |
| 59 | models: [{ id: "canned/small" }, { id: "canned/large" }], | |
| 60 | said: "2 models listed", | |
| 61 | }); | |
| 62 | if (!spark.ok) throw new Error(spark.error); | |
| 63 | sparkId = listConnections(app.db, keeper.id).find((c) => c.name === "Spark")!.id; | |
| 64 | const mint = (u: User) => createToken(app.db, u.id, "t", "rw").token; | |
| 65 | keeperToken = mint(keeper); | |
| 66 | bobToken = mint(bob); | |
| 67 | }); | |
| 68 | afterAll(async () => { | |
| 69 | servers.stop(); | |
| 70 | app.close(); | |
| 71 | await rm(dir, { recursive: true, force: true }); | |
| 72 | }); | |
| 73 | ||
| 74 | describe("GET /api/v1/models", () => { | |
| 75 | test("lists the picker's models in its order, the forge's own first and the default marked", async () => { | |
| 76 | const got = await call(keeperToken, "GET", "/api/v1/models"); | |
| 77 | expect(got.status).toBe(200); | |
| 78 | expect(got.body["models"].map((m: { id: string }) => m.id)).toEqual(pickList(app.db, keeper).map((p) => p.id)); | |
| 79 | expect(got.body["models"]).toEqual([ | |
| 80 | { id: "", name: "The forge's model", connection: "", default: true }, | |
| 81 | { id: `${sparkId} canned/small`, name: "Small", connection: "Spark", default: false }, | |
| 82 | { id: `${sparkId} canned/large`, name: "Large", connection: "Spark", default: false }, | |
| 83 | ]); | |
| 84 | }); | |
| 85 | ||
| 86 | test("offers a person only what they may use, and needs a sign-in", async () => { | |
| 87 | expect((await call(bobToken, "GET", "/api/v1/models")).body["models"]).toEqual([{ id: "", name: "The forge's model", connection: "", default: true }]); | |
| 88 | expect((await fetch(`${servers.url}/api/v1/models`)).status).toBe(401); | |
| 89 | }); | |
| 90 | }); | |
| 91 | ||
| 92 | describe("model on POST /api/v1/conversations", () => { | |
| 93 | test("starts the conversation on an offered model", async () => { | |
| 94 | const made = await call(keeperToken, "POST", "/api/v1/conversations", { model: `${sparkId} canned/large` }); | |
| 95 | expect(made.status).toBe(201); | |
| 96 | expect(choiceOf(app.db, keeper.id, made.body["conversation"].id)).toEqual({ connectionId: sparkId, model: "canned/large" }); | |
| 97 | }); | |
| 98 | ||
| 99 | test("refuses a model not offered with 422 model_not_offered, and makes nothing", async () => { | |
| 100 | const before = (await call(bobToken, "GET", "/api/v1/conversations")).body["conversations"].length; | |
| 101 | const refused = await call(bobToken, "POST", "/api/v1/conversations", { model: `${sparkId} canned/small` }); | |
| 102 | expect(refused.status).toBe(422); | |
| 103 | expect(refused.body["error"]).toBe("model_not_offered"); | |
| 104 | expect((await call(bobToken, "GET", "/api/v1/conversations")).body["conversations"].length).toBe(before); | |
| 105 | expect((await call(keeperToken, "POST", "/api/v1/conversations", { model: 7 })).body["error"]).toBe("invalid_model"); | |
| 106 | }); | |
| 107 | }); | |
| 108 | ||
| 109 | describe("model on POST /api/v1/conversation/messages", () => { | |
| 110 | test("refuses a model not offered with 422 and sends nothing", async () => { | |
| 111 | const made = await call(bobToken, "POST", "/api/v1/conversations", {}); | |
| 112 | const id = made.body["conversation"].id as string; | |
| 113 | const refused = await call(bobToken, "POST", "/api/v1/conversation/messages", { text: "hi", conversation: id, model: `${sparkId} canned/small` }); | |
| 114 | expect(refused.status).toBe(422); | |
| 115 | expect(refused.body["error"]).toBe("model_not_offered"); | |
| 116 | expect(choiceOf(app.db, bob.id, id)).toBeUndefined(); | |
| 117 | expect(app.db.query<{ n: number }, [string]>("SELECT COUNT(*) AS n FROM conversation_turn WHERE conversation_id = ?1").get(id)!.n).toBe(0); | |
| 118 | }); | |
| 119 | ||
| 120 | test("switches the conversation to an offered model before the turn", async () => { | |
| 121 | const made = await call(keeperToken, "POST", "/api/v1/conversations", {}); | |
| 122 | const id = made.body["conversation"].id as string; | |
| 123 | const said = await call(keeperToken, "POST", "/api/v1/conversation/messages", { text: "hi", conversation: id, model: `${sparkId} canned/small` }); | |
| 124 | expect(said.status).toBe(202); | |
| 125 | expect(choiceOf(app.db, keeper.id, id)).toEqual({ connectionId: sparkId, model: "canned/small" }); | |
| 126 | }); | |
| 127 | ||
| 128 | test("refuses a model that is not text, or too long, with 422 invalid_model", async () => { | |
| 129 | for (const model of [7, "x".repeat(401)]) { | |
| 130 | const refused = await call(keeperToken, "POST", "/api/v1/conversation/messages", { text: "hi", model }); | |
| 131 | expect(refused.status).toBe(422); | |
| 132 | expect(refused.body["error"]).toBe("invalid_model"); | |
| 133 | } | |
| 134 | }); | |
| 135 | ||
| 136 | test("model \"\" switches back to the forge's own, here and on a new conversation", async () => { | |
| 137 | const made = await call(keeperToken, "POST", "/api/v1/conversations", { model: `${sparkId} canned/large` }); | |
| 138 | const id = made.body["conversation"].id as string; | |
| 139 | expect(choiceOf(app.db, keeper.id, id)).toBeDefined(); | |
| 140 | expect((await call(keeperToken, "POST", "/api/v1/conversation/messages", { text: "hi", conversation: id, model: "" })).status).toBe(202); | |
| 141 | expect(choiceOf(app.db, keeper.id, id)).toBeUndefined(); | |
| 142 | const fresh = await call(keeperToken, "POST", "/api/v1/conversations", { model: "" }); | |
| 143 | expect(fresh.status).toBe(201); | |
| 144 | expect(choiceOf(app.db, keeper.id, fresh.body["conversation"].id)).toBeUndefined(); | |
| 145 | }); | |
| 146 | ||
| 147 | test("a message refused for another reason leaves the choice, and the person's last, as they were", async () => { | |
| 148 | const made = await call(keeperToken, "POST", "/api/v1/conversations", { model: "" }); | |
| 149 | const id = made.body["conversation"].id as string; | |
| 150 | const before = (await call(keeperToken, "GET", "/api/v1/models")).body["models"]; | |
| 151 | const refused = await call(keeperToken, "POST", "/api/v1/conversation/messages", { text: "hi", conversation: id, model: `${sparkId} canned/small`, files: ["nosuchfile"] }); | |
| 152 | expect(refused.status).toBe(404); | |
| 153 | expect(choiceOf(app.db, keeper.id, id)).toBeUndefined(); | |
| 154 | expect((await call(keeperToken, "GET", "/api/v1/models")).body["models"]).toEqual(before); | |
| 155 | }); | |
| 156 | ||
| 157 | test("a message refused by the rate limit is taken back too, and makes no conversation", async () => { | |
| 158 | const solo = await createUser(app.db, { origin: "seed", trusted: true, username: "solo", email: "[email protected]", password: "a-good-passphrase" }); | |
| 159 | if (!solo.ok) throw new Error(solo.error); | |
| 160 | promote(app.db, solo.value); | |
| 161 | const token = createToken(app.db, solo.value.id, "t", "rw").token; | |
| 162 | const count = () => app.db.query<{ n: number }, [string]>("SELECT COUNT(*) AS n FROM conversation WHERE user_id = ?1").get(solo.value.id)!.n; | |
| 163 | let refused = { status: 0, body: {} as Record<string, any> }; | |
| 164 | for (let i = 0; i < 14 && refused.status !== 429; i++) refused = await call(token, "POST", "/api/v1/conversation/messages", { text: "hi" }); | |
| 165 | expect(refused.status).toBe(429); | |
| 166 | const n = count(); | |
| 167 | // Granted to everyone, so this person is offered the model; the limit refuses the message. | |
| 168 | app.db.query("INSERT INTO subscription_grant (scope_id, agent_id, subscription_id) VALUES (?1, ?2, ?3)").run(solo.value.id, EVERYONE, sparkId); | |
| 169 | const again = await call(token, "POST", "/api/v1/conversation/messages", { text: "hi", model: `${sparkId} canned/small` }); | |
| 170 | expect(again.status).toBe(429); | |
| 171 | expect(count()).toBe(n); | |
| 172 | expect(app.db.query("SELECT COUNT(*) AS n FROM conversation WHERE user_id = ?1 AND connection_id != ''").get(solo.value.id)).toEqual({ n: 0 }); | |
| 173 | expect(app.db.query("SELECT value FROM user_setting WHERE user_id = ?1 AND key = 'talk.model'").get(solo.value.id) ?? { value: "" }).toEqual({ value: "" }); | |
| 174 | }); | |
| 175 | }); | |
| 176 | ||
| 177 | describe("default on GET /api/v1/models", () => { | |
| 178 | test("follows the person's last choice", async () => { | |
| 179 | await call(keeperToken, "POST", "/api/v1/conversations", { model: `${sparkId} canned/large` }); | |
| 180 | const got = (await call(keeperToken, "GET", "/api/v1/models")).body["models"] as { id: string; default: boolean }[]; | |
| 181 | expect(got.filter((m) => m.default).map((m) => m.id)).toEqual([`${sparkId} canned/large`]); | |
| 182 | }); | |
| 183 | }); |
test/route-timing-coverage.test.ts +1
| @@ -21,6 +21,7 @@ import { seedAdmin } from "./helpers/admin.ts"; | ||
| 21 | 21 | |
| 22 | 22 | /** Routes that answer 200 without a `session` phase → why. */ |
| 23 | 23 | const UNTIMED: Record<string, string> = { |
| 24 | "/api/v1/models": "the models offered to a person, two indexed reads; no page to phase", | |
| 24 | 25 | "/api/v1/conversations": "a person's conversation list, one indexed query; no page to phase", |
| 25 | 26 | "/api/v1/admin/machines/:machine/disks": "an owner's per-machine disk summary, one indexed read; no page to phase", |
| 26 | 27 | "/api/v1/repos/:owner/:name/issues/:number/estimates": "an issue's base rate from one indexed read of its history; no page to phase", |
new above
offline
Switching here, before
talk()runs, persists the choice even when the message is then refused (rate limit, files, queue). MovesetChoiceaftertalk()succeeds, and avoidopenConversationcreating a conversation for a refused request.