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

opened this

Merged 17h 36m ago, live in production

scout/forge:scout/17977-a-talk-turn-that-reaches-its-limit-says → main 0 commits fork

  • Merged. scout/17977-a-talk-turn-that-reaches-its-limit-says was merged into main as 7ada34c.

Review

Sign in to review this change.
3 files changed +29 −4
src/contract/openapi/routes/getApiV1ConversationTurns.ts +2 −2
@@ -5,10 +5,10 @@ export const route: Route = {
55 method: "get",
66 path: "/api/v1/conversation/turns",
77 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",
99 auth: true,
1010 role: "member",
1111 query: { limit: limitTo(MAX_PAGE), before: "string", conversation: "string" },
12 reply: { running: "boolean", turns: "array" },
12 reply: { running: "boolean", stoppedAt: "string|null", turns: "array" },
1313 refuses: [401],
1414 };
src/http/api/conversation_talk.ts +9 −2
@@ -27,6 +27,7 @@ import {
2727 openConversation,
2828 renameConversation,
2929 TITLE_MAX,
30 type Turn,
3031 } from "../../core/conversations.ts";
3132 import { FILE_WORDS, fileBytes, fileOwnedBy, FILES_PER_MESSAGE, IMAGE_MAX, pendingAmong, removePending, storeFile } from "../../core/conversation_files.ts";
3233 import { choiceFrom, modelList, setChoice, switchModel } from "../../core/model_choice.ts";
@@ -76,6 +77,12 @@ const uploadLimiter = (app: App): RateLimiter => {
7677 return l;
7778 };
7879
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
7986 export function conversationTalkRoutes(router: Router<Handler>): void {
8087 router.add("POST", "/api/v1/conversation/messages", async (ctx) => {
8188 const user = ctx.auth.user;
@@ -229,10 +236,10 @@ export function conversationTalkRoutes(router: Router<Handler>): void {
229236 // parser every door uses (`core/talk_voice.ts`); a reply that places none is its own text.
230237 const kept = cardsPlaced(ctx.app.db, user.id, conversation.id, all);
231238 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));
233240 // The tag is of the exact bytes sent (so `before` and `limit` give their own), made after the
234241 // 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 });
236243 const etag = weakEtag(text);
237244 const headers = { etag, "cache-control": "private", vary: "authorization, cookie" };
238245 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", () => {
153153 expect(await page(c.id)).not.toContain('class="talk-cost ');
154154 });
155155 });
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 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs