Public: anyone can read this; the crew can change it.
A Talk turn that reaches its limit says so and offers one tap to go on #17981
Merged 17h 36m ago, live in production
scout/forge:scout/17977-a-talk-turn-that-reaches-its-limit-says → main 0 commits fork
3 files
changed +29
−4
src/contract/openapi/routes/getApiV1ConversationTurns.ts +2 −2
| @@ -5,10 +5,10 @@ export const route: Route = { | ||
| 5 | 5 | method: "get", |
| 6 | 6 | path: "/api/v1/conversation/turns", |
| 7 | 7 | summary: "The signed-in person's conversation with the forge's agent", |
| 8 | detail: "The signed-in person's conversation with the forge's agent: the latest turns, oldest first, and whether an answer is being written now", | |
| 8 | detail: "The signed-in person's conversation with the forge's agent: the latest turns, oldest first, and whether an answer is being written now; stoppedAt is \"limit\" when the latest turn ended because it reached its turn or token limit (cut off, not finished; send a short message to go on), else null", | |
| 9 | 9 | auth: true, |
| 10 | 10 | role: "member", |
| 11 | 11 | query: { limit: limitTo(MAX_PAGE), before: "string", conversation: "string" }, |
| 12 | reply: { running: "boolean", turns: "array" }, | |
| 12 | reply: { running: "boolean", stoppedAt: "string|null", turns: "array" }, | |
| 13 | 13 | refuses: [401], |
| 14 | 14 | }; |
src/http/api/conversation_talk.ts +9 −2
| @@ -27,6 +27,7 @@ import { | ||
| 27 | 27 | openConversation, |
| 28 | 28 | renameConversation, |
| 29 | 29 | TITLE_MAX, |
| 30 | type Turn, | |
| 30 | 31 | } from "../../core/conversations.ts"; |
| 31 | 32 | import { FILE_WORDS, fileBytes, fileOwnedBy, FILES_PER_MESSAGE, IMAGE_MAX, pendingAmong, removePending, storeFile } from "../../core/conversation_files.ts"; |
| 32 | 33 | import { choiceFrom, modelList, setChoice, switchModel } from "../../core/model_choice.ts"; |
| @@ -76,6 +77,12 @@ const uploadLimiter = (app: App): RateLimiter => { | ||
| 76 | 77 | return l; |
| 77 | 78 | }; |
| 78 | 79 | |
| 80 | /** "limit" when the newest turn of the conversation is a reply cut off by its limit; else null. */ | |
| 81 | const stoppedAt = (turns: readonly Turn[], older: boolean): "limit" | null => { | |
| 82 | const last = turns.at(-1); | |
| 83 | return !older && last !== undefined && last.role === "agent" && !last.replaced && last.limited === true ? "limit" : null; | |
| 84 | }; | |
| 85 | ||
| 79 | 86 | export function conversationTalkRoutes(router: Router<Handler>): void { |
| 80 | 87 | router.add("POST", "/api/v1/conversation/messages", async (ctx) => { |
| 81 | 88 | const user = ctx.auth.user; |
| @@ -229,10 +236,10 @@ export function conversationTalkRoutes(router: Router<Handler>): void { | ||
| 229 | 236 | // parser every door uses (`core/talk_voice.ts`); a reply that places none is its own text. |
| 230 | 237 | const kept = cardsPlaced(ctx.app.db, user.id, conversation.id, all); |
| 231 | 238 | const plainOf = (text: string) => ((p) => (p === text ? {} : { plain: p }))(plainVoice(text, kept)); |
| 232 | const shown = all.map((t) => (t.role === "agent" ? { ...t, work: works.get(t.id) ?? null, costUsd: costs.get(t.id) ?? null, ...plainOf(t.text) } : t)); | |
| 239 | const shown = all.map((t) => (t.role === "agent" ? { ...t, ...(t.limited === true ? { stoppedAt: "limit" } : {}), work: works.get(t.id) ?? null, costUsd: costs.get(t.id) ?? null, ...plainOf(t.text) } : t)); | |
| 233 | 240 | // The tag is of the exact bytes sent (so `before` and `limit` give their own), made after the |
| 234 | 241 | // person's own access was settled above. |
| 235 | const text = JSON.stringify({ running: turnRunning(ctx.app, user.id, conversation.id), conversation: conversation.id, totalUsd: conversationTotal(ctx.app.db, conversation.id) ?? null, queued: queuedOf(ctx.app, user.id, conversation.id).map((q) => ({ id: q.id, text: q.text })), turns: shown }); | |
| 242 | const text = JSON.stringify({ running: turnRunning(ctx.app, user.id, conversation.id), stoppedAt: stoppedAt(all, ctx.url.searchParams.has("before")), conversation: conversation.id, totalUsd: conversationTotal(ctx.app.db, conversation.id) ?? null, queued: queuedOf(ctx.app, user.id, conversation.id).map((q) => ({ id: q.id, text: q.text })), turns: shown }); | |
| 236 | 243 | const etag = weakEtag(text); |
| 237 | 244 | const headers = { etag, "cache-control": "private", vary: "authorization, cookie" }; |
| 238 | 245 | if (holds(ctx.request, etag)) return new Response(null, { status: 304, headers }); |
test/talk-limit.test.ts +18
| @@ -153,3 +153,21 @@ describe("through a real turn", () => { | ||
| 153 | 153 | expect(await page(c.id)).not.toContain('class="talk-cost '); |
| 154 | 154 | }); |
| 155 | 155 | }); |
| 156 | ||
| 157 | describe("the JSON carries the limit", () => { | |
| 158 | const turnsOf = async (id: string): Promise<{ stoppedAt: string | null; turns: { stoppedAt?: string }[] }> => | |
| 159 | (await fetch(`${servers.url}/api/v1/conversation/turns?conversation=${id}`, { headers: { cookie: `forge_session=${createSession(app.db, alice.id).token}` } })).json() as Promise<{ stoppedAt: string | null; turns: { stoppedAt?: string }[] }>; | |
| 160 | ||
| 161 | test("stoppedAt is limit after a cut-off reply, null after a normal one", async () => { | |
| 162 | const cut = newConversation(app.db, alice.id); | |
| 163 | appendTurn(app.db, alice.id, cut.id, { role: "person", text: "go" }); | |
| 164 | appendTurn(app.db, alice.id, cut.id, { role: "agent", text: LIMIT_LINE, failed: true, limited: true }); | |
| 165 | const body = await turnsOf(cut.id); | |
| 166 | expect(body.stoppedAt).toBe("limit"); | |
| 167 | expect(body.turns.at(-1)?.stoppedAt).toBe("limit"); | |
| 168 | ||
| 169 | appendTurn(app.db, alice.id, cut.id, { role: "person", text: "Go on." }); | |
| 170 | appendTurn(app.db, alice.id, cut.id, { role: "agent", text: "done" }); | |
| 171 | expect((await turnsOf(cut.id)).stoppedAt).toBeNull(); | |
| 172 | }); | |
| 173 | }); |
new above
offline