From 9ab3f9fb5cfe2f48d1626a4455216e782474098e Mon Sep 17 00:00:00 2001 From: Omar Zunic Date: Wed, 11 Nov 2026 20:55:22 +0100 Subject: [PATCH 01/16] feat(web): draw the assistant loader as a sprout The loader was a 3x3 pixel grid with no tie to the brand. It is now the wheat from the Farmer mark: the stalk draws, the grains spring out of it, then they drop. The grains come from the MARK path itself, so the loader and the logo cannot drift apart. --- .../src/components/assistant/primitives.tsx | 63 ++++++++------ apps/web/src/components/farmer-mark.tsx | 2 +- apps/web/src/styles.css | 82 +++++++++++++++++-- 3 files changed, 113 insertions(+), 34 deletions(-) diff --git a/apps/web/src/components/assistant/primitives.tsx b/apps/web/src/components/assistant/primitives.tsx index b2b87bf3..b923f827 100644 --- a/apps/web/src/components/assistant/primitives.tsx +++ b/apps/web/src/components/assistant/primitives.tsx @@ -11,9 +11,17 @@ // Sources: LoadingState.tsx, ThinkingState.tsx, ApprovalCard.tsx. import { useQuery } from "@tanstack/react-query"; -import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"; +import { + type CSSProperties, + useEffect, + useLayoutEffect, + useMemo, + useRef, + useState, +} from "react"; import { z } from "zod"; +import { MARK } from "#/components/farmer-mark.tsx"; import { PersonAvatar } from "#/components/person-avatar.tsx"; import { useOrgSlug } from "#/hooks/use-org-slug.ts"; import { cn } from "#/lib/utils.ts"; @@ -26,35 +34,42 @@ import { MONTHS, printDayKey } from "./answer.tsx"; import type { PendingApproval, ToolTrace } from "./assistant-runs.tsx"; import { toolLabel } from "./tool-labels.ts"; -/* ── the pixel-grid loader (LoadingState) ─────────────────────────────────── */ +/* ── the sprout loader (LoadingState) ─────────────────────────────────────── */ -// A chevron wavefront: cell delay grows with the column and with distance from -// the middle row, so the front reads as driving rightwards. The 650ms cycle is -// shorter than the sweep across the grid, so two fronts are always in flight. -const CHEVRON = Array.from({ length: 9 }, (_, i) => { - const row = Math.floor(i / 3); - const column = i % 3; - return (column + Math.abs(row - 1)) * 90; -}); +// The Farmer wheat. The stalk draws, then each grain grows out of it and drops. +// The first piece of the mark is the tile, so we drop it. +const [, ...GRAINS] = MARK.split(/(?<=Z)(?=M)/); +// Step along the stalk for each grain: leaf, then the pairs from base to tip. +const STEP = [0, 1, 2, 1, 3, 2, 3]; +// The edge of each grain that touches the stalk. +const KIND = ["leaf", "v", "v", "h", "v", "h", "h"]; -function LoaderGrid() { +function SproutMark() { return ( - - {CHEVRON.map((delay, index) => ( - + {GRAINS.map((d, n) => ( + ))} - + ); } @@ -92,7 +107,7 @@ export function AssistantLoader({ label = "Thinking" }: { label?: string }) { // be maintained by hand. return ( - + {label} {elapsed} diff --git a/apps/web/src/components/farmer-mark.tsx b/apps/web/src/components/farmer-mark.tsx index 21a43e2f..bb837410 100644 --- a/apps/web/src/components/farmer-mark.tsx +++ b/apps/web/src/components/farmer-mark.tsx @@ -46,5 +46,5 @@ export function FarmerMark({ ); } -const MARK = +export const MARK = "M65 0C69.1843 0 73.0148 1.51248 75.9766 4.01953C75.5585 4.0764 75.1544 4.26368 74.833 4.58496L67.3594 12.0576L64.2422 8.93945L64.2393 8.9375C61.9212 6.62861 58.7825 5.33205 55.5107 5.33203C52.239 5.33206 49.1003 6.62866 46.7822 8.9375L46.7793 8.93945L42.2471 13.4727C41.4663 14.2537 41.4662 15.5198 42.2471 16.3008L43.1396 17.1934C40.0574 17.3234 37.1272 18.6012 34.9326 20.7871L34.9297 20.7891L30.3975 25.3223C29.6166 26.1033 29.6166 27.3694 30.3975 28.1504L31.29 29.043C28.2079 29.173 25.2776 30.4508 23.083 32.6367L23.0801 32.6387L18.5479 37.1719C18.173 37.5469 17.962 38.0557 17.9619 38.5859C17.962 39.1162 18.173 39.625 18.5479 40L23.083 44.5352L23.3027 44.749C23.8079 45.2279 24.3516 45.6578 24.9229 46.041C22.7267 46.6905 20.7053 47.8806 19.0566 49.5293C16.4594 52.1266 15 55.6492 15 59.3223V65.2471C15.0001 66.3515 15.8955 67.247 17 67.2471H22.9248C26.5979 67.247 30.1205 65.7877 32.7178 63.1904C34.3663 61.5419 35.5546 59.5201 36.2041 57.3242C36.6437 57.9796 37.1479 58.5968 37.7129 59.1641L37.7148 59.167L42.248 63.6992C42.6231 64.074 43.1319 64.2852 43.6621 64.2852C44.1261 64.285 44.5739 64.1236 44.9297 63.832L45.0762 63.6992L49.6113 59.1641C51.7972 56.9695 53.074 54.0392 53.2041 50.957L54.0967 51.8496C54.8287 52.5815 55.987 52.6274 56.7725 51.9873L56.9248 51.8496L61.46 47.3145C63.6459 45.1199 64.9237 42.1896 65.0537 39.1074L65.9463 40C66.6783 40.7319 67.8366 40.7779 68.6221 40.1377L68.7744 40L73.3096 35.4648C75.6185 33.1468 76.915 30.0081 76.915 26.7363C76.915 23.4645 75.6185 20.3259 73.3096 18.0078L70.1885 14.8867L77.6611 7.41406C77.9715 7.10364 78.1561 6.7164 78.2197 6.31348C80.5831 9.23341 82 12.9509 82 17V65C82 74.3888 74.3888 82 65 82H17C7.61116 82 0 74.3888 0 65V17C0 7.61116 7.61116 0 17 0H65ZM32.7744 53.3975C32.7744 56.0097 31.7367 58.5152 29.8896 60.3623C28.0425 62.2094 25.537 63.247 22.9248 63.2471H19V59.3223C19 56.71 20.0376 54.2045 21.8848 52.3574C23.7319 50.5103 26.2374 49.4727 28.8496 49.4727H32.7744V53.3975ZM46.7803 44.5332C48.3403 46.1012 49.2168 48.2235 49.2168 50.4355C49.2167 52.649 48.3403 54.7735 46.7783 56.3418L43.6621 59.457L40.5469 56.3418V56.3408C38.9848 54.7725 38.1075 52.6491 38.1074 50.4355C38.1075 48.2232 38.9835 46.1013 40.5439 44.5332L43.6621 41.415L46.7803 44.5332ZM58.6289 32.6836C60.1892 34.2517 61.0654 36.3737 61.0654 38.5859C61.0654 40.7994 60.189 42.9239 58.627 44.4922L55.5107 47.6074L52.3955 44.4922V44.4912C50.8334 42.9229 49.9561 40.7995 49.9561 38.5859C49.9561 36.3737 50.8323 34.2517 52.3926 32.6836L55.5107 29.5654L58.6289 32.6836ZM31.8115 33.0312C34.0251 33.0313 36.1494 33.9086 37.7178 35.4707L40.8311 38.584L37.7002 41.7158C36.1334 43.2682 34.0178 44.1406 31.8115 44.1406C29.7363 44.1405 27.7397 43.3699 26.2061 41.9863L25.9062 41.7012L22.791 38.5859L25.9062 35.4707C27.4746 33.9085 29.598 33.0314 31.8115 33.0312ZM70.4785 20.834C72.0388 22.4021 72.915 24.5241 72.915 26.7363C72.915 28.9499 72.0386 31.0742 70.4766 32.6426L67.3604 35.7578L64.2451 32.6426V32.6416C62.683 31.0733 61.8057 28.9499 61.8057 26.7363C61.8057 24.5241 62.6819 22.4021 64.2422 20.834L67.3604 17.7158L70.4785 20.834ZM43.6611 21.1816C45.8748 21.1816 47.999 22.0589 49.5674 23.6211L52.6816 26.7354L49.5518 29.8643C47.9847 31.4179 45.8684 32.291 43.6611 32.291C41.5858 32.291 39.5894 31.5204 38.0557 30.1367L37.7559 29.8516L34.6406 26.7363L37.7559 23.6211C39.3242 22.059 41.4475 21.1817 43.6611 21.1816ZM55.5107 9.33203C57.7244 9.33205 59.8486 10.2093 61.417 11.7715L64.5312 14.8857L61.416 18.002C59.8476 19.5641 57.7243 20.4414 55.5107 20.4414C53.4354 20.4414 51.439 19.6708 49.9053 18.2871L49.6055 18.002L46.4902 14.8867L49.6055 11.7715C51.1738 10.2094 53.2972 9.33206 55.5107 9.33203Z"; diff --git a/apps/web/src/styles.css b/apps/web/src/styles.css index b356a404..91df4b2c 100644 --- a/apps/web/src/styles.css +++ b/apps/web/src/styles.css @@ -2521,14 +2521,79 @@ html { animation: shimmer-text 1.4s linear infinite; } - /* The pixel-grid loader's cell: a brief lift out of its resting dimness. */ - @keyframes pixel-on { - 0%, + /* The assistant loader: the Farmer wheat. The stalk draws, the grains grow + out of it, then they drop. --i staggers the grains from base to tip. */ + .sprout { + overflow: visible; + } + .sprout-stalk { + fill: none; + stroke: var(--ink); + stroke-width: 4; + stroke-linecap: round; + animation: sprout-draw 2600ms cubic-bezier(0.3, 0, 0.2, 1) infinite both; + } + .sprout-grain { + fill: var(--ink); + transform-box: fill-box; + animation: sprout-grain 2600ms infinite both; + animation-delay: calc(var(--i) * 150ms + 150ms); + } + /* Each grain grows from the point where it touches the stalk. */ + .sprout-v { + transform-origin: 50% 0; + } + .sprout-h { + transform-origin: 100% 50%; + } + .sprout-leaf { + transform-origin: 100% 0; + } + @keyframes sprout-draw { + 0% { + stroke-dashoffset: 1; + } + 15%, + 78% { + stroke-dashoffset: 0; + opacity: 1; + } + 92%, 100% { - opacity: 0.15; + stroke-dashoffset: 0; + opacity: 0; + } + } + @keyframes sprout-grain { + 0% { + scale: 0; + opacity: 1; + translate: 0 0; + rotate: 0deg; + } + 9% { + scale: 1.22; } - 35% { - opacity: 0.85; + 14% { + scale: 0.92; + } + 18%, + 62% { + scale: 1; + translate: 0 0; + rotate: 0deg; + opacity: 1; + } + 76% { + translate: 0 12px; + rotate: 25deg; + opacity: 0; + animation-timing-function: steps(1); + } + 77%, + 100% { + scale: 0; + opacity: 0; } } @@ -2580,9 +2645,8 @@ html { transition: none; } /* The assistant's entrances and the loader's shimmer are narration: with - reduced motion the content simply arrives. The loader grid stops at its - dim resting state, which still reads as "working" because the elapsed - timer beside it keeps counting. */ + reduced motion the content simply arrives. The sprout loader shows the + full grown wheat, and the elapsed timer beside it keeps counting. */ .assistant-motion, .assistant-motion * { animation: none !important; From 090eb213c6cc4f7a47fea6939eded6aeb6f02afd Mon Sep 17 00:00:00 2001 From: Omar Zunic Date: Wed, 11 Nov 2026 20:59:10 +0100 Subject: [PATCH 02/16] feat(web): export answer tables and charts A person who takes data out of an answer had to copy markdown or read values off a bar. Add actions to export the data. A table gets "Download CSV" and "Copy as table" (TSV). A chart gets "Download PNG" and "Download CSV". The actions show on hover or focus and only on a finished answer, so a half table is never exported. The files are built from the parsed blocks, not from the DOM. Inline markdown is removed from each cell. The CSV follows RFC 4180, has a BOM for Excel, and guards a cell that starts with a formula sign unless the cell is a plain number. The chart is HTML and CSS, not SVG. The PNG is drawn again on a canvas at 2x from the chart spec, with the colors of the current theme. --- .../assistant/answer-export-actions.tsx | 233 ++++++++++++++++++ .../assistant/answer-export.test.ts | 87 +++++++ .../src/components/assistant/answer-export.ts | 65 +++++ apps/web/src/components/assistant/answer.tsx | 33 ++- 4 files changed, 414 insertions(+), 4 deletions(-) create mode 100644 apps/web/src/components/assistant/answer-export-actions.tsx create mode 100644 apps/web/src/components/assistant/answer-export.test.ts create mode 100644 apps/web/src/components/assistant/answer-export.ts diff --git a/apps/web/src/components/assistant/answer-export-actions.tsx b/apps/web/src/components/assistant/answer-export-actions.tsx new file mode 100644 index 00000000..fd613b49 --- /dev/null +++ b/apps/web/src/components/assistant/answer-export-actions.tsx @@ -0,0 +1,233 @@ +import { CopyIcon, DownloadSimpleIcon, ImageIcon } from "@phosphor-icons/react"; +import { toast } from "sonner"; + +import type { ChartSpec } from "./answer-chart.tsx"; +import { + chartRows, + exportFileName, + tableRows, + toCsv, + toTsv, +} from "./answer-export.ts"; + +const buttonClass = + "quiet flex items-center gap-1.5 rounded-control px-1.5 py-1 text-[12px] transition-colors duration-(--dur-fast) hover:bg-hover"; + +// Shown on hover or focus of the block. Always shown where there is no hover. +const barClass = + "flex items-center gap-1 opacity-0 transition-opacity duration-(--dur-base) group-hover/export:opacity-100 focus-within:opacity-100 max-md:opacity-100"; + +const saveBlob = (blob: Blob, name: string) => { + const url = URL.createObjectURL(blob); + const link = document.createElement("a"); + link.href = url; + link.download = name; + link.click(); + URL.revokeObjectURL(url); +}; + +const saveCsv = (rows: string[][], name: string) => + saveBlob( + new Blob([toCsv(rows)], { type: "text/csv;charset=utf-8" }), + `${name}.csv`, + ); + +/** The computed color of a CSS variable, as a string a canvas accepts. */ +const cssColor = (name: string) => { + const probe = document.createElement("span"); + probe.style.color = `var(${name})`; + document.body.append(probe); + const color = getComputedStyle(probe).color; + probe.remove(); + return color; +}; + +type Palette = { + bg: string; + ink: string; + ink2: string; + track: string; + bar: string; +}; + +const readPalette = (): Palette => ({ + bg: cssColor("--bg"), + ink: cssColor("--ink"), + ink2: cssColor("--ink-2"), + track: cssColor("--inset"), + bar: cssColor("--chart-1"), +}); + +const printValue = (value: number, unit?: string) => + `${Math.round(value * 100) / 100}${unit ?? ""}`; + +const SCALE = 2; +const PAD = 16; +const TITLE_H = 28; + +type Draw = { + ctx: CanvasRenderingContext2D; + spec: ChartSpec; + palette: Palette; + width: number; + top: number; + peak: number; +}; + +const drawBars = ({ ctx, spec, palette, width, top, peak }: Draw) => { + const labelW = 160; + const valueW = 64; + const trackW = width - PAD * 2 - labelW - valueW; + spec.data.forEach((point, index) => { + const y = top + index * 24; + ctx.fillStyle = palette.ink2; + ctx.textAlign = "left"; + ctx.fillText(point.label.slice(0, 24), PAD, y + 12); + ctx.fillStyle = palette.track; + ctx.fillRect(PAD + labelW, y + 6, trackW, 10); + ctx.fillStyle = palette.bar; + const share = peak === 0 ? 0 : Math.max(0, point.value / peak); + ctx.fillRect(PAD + labelW, y + 6, Math.max(trackW * share, 1), 10); + ctx.fillStyle = palette.ink; + ctx.textAlign = "right"; + ctx.fillText(printValue(point.value, spec.unit), width - PAD, y + 12); + }); +}; + +const drawColumns = ({ ctx, spec, palette, width, top, peak }: Draw) => { + const slot = (width - PAD * 2) / spec.data.length; + const trackH = 120; + spec.data.forEach((point, index) => { + const x = PAD + index * slot; + const mid = x + slot / 2; + ctx.textAlign = "center"; + ctx.fillStyle = palette.ink2; + ctx.fillText(printValue(point.value, spec.unit), mid, top + 12); + ctx.fillStyle = palette.track; + ctx.fillRect(x + 2, top + 20, slot - 4, trackH); + ctx.fillStyle = palette.bar; + const share = peak === 0 ? 0 : Math.max(0, point.value / peak); + const barH = Math.max(trackH * share, 2); + ctx.fillRect(x + 2, top + 20 + trackH - barH, slot - 4, barH); + ctx.fillStyle = palette.ink2; + ctx.fillText(point.label.slice(0, 8), mid, top + trackH + 38); + }); +}; + +/** The chart is HTML and CSS, not SVG, so it is drawn again on a canvas + * from its spec with the colors of the current theme. */ +const renderChart = (spec: ChartSpec) => { + const trend = spec.type === "trend"; + const width = trend ? Math.max(640, spec.data.length * 44 + PAD * 2) : 640; + const top = PAD + (spec.title ? TITLE_H : 0); + const body = trend ? 170 : spec.data.length * 24; + const height = top + body + PAD; + const canvas = document.createElement("canvas"); + canvas.width = width * SCALE; + canvas.height = height * SCALE; + const ctx = canvas.getContext("2d"); + if (!ctx) return null; + ctx.scale(SCALE, SCALE); + const palette = readPalette(); + ctx.fillStyle = palette.bg; + ctx.fillRect(0, 0, width, height); + const font = getComputedStyle(document.body).fontFamily; + if (spec.title) { + ctx.font = `500 13px ${font}`; + ctx.fillStyle = palette.ink; + ctx.textAlign = "left"; + ctx.fillText(spec.title, PAD, PAD + 12); + } + ctx.font = `12px ${font}`; + const peak = Math.max(...spec.data.map((point) => Math.abs(point.value)), 0); + const draw = { ctx, spec, palette, width, top, peak }; + if (trend) drawColumns(draw); + else drawBars(draw); + return canvas; +}; + +const toPng = (canvas: HTMLCanvasElement) => + new Promise((resolve) => canvas.toBlob(resolve, "image/png")); + +const downloadPng = async (spec: ChartSpec, name: string) => { + const canvas = renderChart(spec); + const blob = canvas ? await toPng(canvas) : null; + if (!blob) { + toast.error("Could not make the image"); + return; + } + saveBlob(blob, `${name}.png`); +}; + +export function TableExport({ + head, + rows, + title, +}: { + head: string[]; + rows: string[][]; + title?: string; +}) { + const all = tableRows(head, rows); + const name = exportFileName(title, "farmer-table"); + const copy = async () => { + try { + await navigator.clipboard.writeText(toTsv(all)); + toast.success("Table copied"); + } catch { + toast.error("Could not copy the table"); + } + }; + return ( +
+ + +
+ ); +} + +export function ChartExport({ spec }: { spec: ChartSpec }) { + const name = exportFileName(spec.title, "farmer-chart"); + return ( +
+ + +
+ ); +} diff --git a/apps/web/src/components/assistant/answer-export.test.ts b/apps/web/src/components/assistant/answer-export.test.ts new file mode 100644 index 00000000..079aeaa4 --- /dev/null +++ b/apps/web/src/components/assistant/answer-export.test.ts @@ -0,0 +1,87 @@ +/// +import { describe, expect, test } from "bun:test"; + +import { + chartRows, + exportFileName, + plainCell, + tableRows, + toCsv, + toTsv, +} from "./answer-export.ts"; + +const body = (csv: string) => csv.slice(1); + +describe("plainCell", () => { + test("strips bold, italic, code and links", () => { + expect(plainCell("**7.5h** and *x* `y`")).toBe("7.5h and x y"); + expect(plainCell("[Docs](https://example.com/a)")).toBe("Docs"); + }); + + test("turns a day link into the date", () => { + expect(plainCell("[[2026-08-03]]")).toBe("2026-08-03"); + }); +}); + +describe("toCsv", () => { + test("starts with a BOM and uses CRLF", () => { + const csv = toCsv([ + ["a", "b"], + ["1", "2"], + ]); + expect(csv.startsWith("")).toBe(true); + expect(body(csv)).toBe("a,b\r\n1,2\r\n"); + }); + + test("quotes commas, quotes and newlines", () => { + const csv = toCsv([["a,b", 'say "hi"', "x\ny"]]); + expect(body(csv)).toBe('"a,b","say ""hi""","x\ny"\r\n'); + }); + + test("guards a formula but not a negative number", () => { + const csv = toCsv([["=1+1", "+x", "@a", "-2.5", "-x", "+3"]]); + expect(body(csv)).toBe("'=1+1,'+x,'@a,-2.5,'-x,+3\r\n"); + }); + + test("writes plain values for markdown cells", () => { + const csv = toCsv([["[[2026-08-03]]", "**5**"]]); + expect(body(csv)).toBe("2026-08-03,5\r\n"); + }); +}); + +describe("toTsv", () => { + test("joins with tabs and flattens inner tabs and newlines", () => { + expect( + toTsv([ + ["a", "b\tc"], + ["1", "x\ny"], + ]), + ).toBe("a\tb c\n1\tx y"); + }); +}); + +describe("rows and names", () => { + test("tableRows puts the header first", () => { + expect(tableRows(["h"], [["r"]])).toEqual([["h"], ["r"]]); + }); + + test("chartRows names the unit", () => { + const rows = chartRows({ + type: "bar", + unit: "h", + data: [{ label: "A", value: 3.5 }], + }); + expect(rows).toEqual([ + ["Label", "Value (h)"], + ["A", "3.5"], + ]); + }); + + test("exportFileName slugs a heading and falls back", () => { + expect(exportFileName("**Hours** by project!", "x")).toBe( + "hours-by-project", + ); + expect(exportFileName(undefined, "farmer-table")).toBe("farmer-table"); + expect(exportFileName("???", "farmer-table")).toBe("farmer-table"); + }); +}); diff --git a/apps/web/src/components/assistant/answer-export.ts b/apps/web/src/components/assistant/answer-export.ts new file mode 100644 index 00000000..97976e8d --- /dev/null +++ b/apps/web/src/components/assistant/answer-export.ts @@ -0,0 +1,65 @@ +// Pure helpers that turn a parsed table or chart into CSV or TSV text. +// The input is the output of `parseBlocks`, never the DOM. + +import type { ChartSpec } from "./answer-chart.tsx"; + +const BOM = ""; + +/** The text of a cell without its inline markdown. A `[[date]]` becomes the + * date, a link becomes its label. */ +export const plainCell = (cell: string) => + cell + .replace(/\[\[(\d{4}-\d{2}-\d{2})\]\]/g, "$1") + .replace(/\[([^\]\n]+)\]\([^)\s]+\)/g, "$1") + .replace(/\*\*([^*]+)\*\*/g, "$1") + .replace(/\*([^*\n]+)\*/g, "$1") + .replace(/`([^`]+)`/g, "$1") + .trim(); + +const isPlainNumber = (value: string) => /^[-+]?[\d.,]*\d%?$/.test(value); + +/** A spreadsheet runs a cell that starts with one of these as a formula. A + * plain number, such as `-2.5`, stays as it is. */ +const guardFormula = (value: string) => + /^[=+\-@]/.test(value) && !isPlainNumber(value) ? `'${value}` : value; + +const cleanRow = (row: string[]) => + row.map((cell) => guardFormula(plainCell(cell))); + +const quote = (field: string) => + /[",\r\n]/.test(field) ? `"${field.replaceAll('"', '""')}"` : field; + +/** RFC 4180: CRLF line ends, and a BOM so Excel reads UTF-8. */ +export const toCsv = (rows: string[][]) => + BOM + + rows.map((row) => cleanRow(row).map(quote).join(",")).join("\r\n") + + "\r\n"; + +/** Tab-separated text for a paste into a spreadsheet. */ +export const toTsv = (rows: string[][]) => + rows + .map((row) => + cleanRow(row) + .map((field) => field.replace(/[\t\r\n]+/g, " ")) + .join("\t"), + ) + .join("\n"); + +/** The header and the body of a table as one list of rows. */ +export const tableRows = (head: string[], rows: string[][]) => [head, ...rows]; + +/** The rows of a chart: a label column and a value column. */ +export const chartRows = (spec: ChartSpec) => [ + ["Label", spec.unit ? `Value (${spec.unit})` : "Value"], + ...spec.data.map((point) => [point.label, String(point.value)]), +]; + +/** A safe file name from a heading, or the fallback. */ +export const exportFileName = (title: string | undefined, fallback: string) => { + const slug = plainCell(title ?? "") + .toLowerCase() + .replace(/[^\p{L}\p{N}]+/gu, "-") + .replace(/^-+|-+$/g, "") + .slice(0, 60); + return slug || fallback; +}; diff --git a/apps/web/src/components/assistant/answer.tsx b/apps/web/src/components/assistant/answer.tsx index 2eef81af..f745de54 100644 --- a/apps/web/src/components/assistant/answer.tsx +++ b/apps/web/src/components/assistant/answer.tsx @@ -9,7 +9,13 @@ import { useToday } from "#/hooks/use-today.ts"; import { dateSearch } from "#/lib/time-search.ts"; import { AnswerChart, ChartPlaceholder } from "./answer-chart.tsx"; -import { type Align, parseBlocks, safeHref } from "./answer-parse.ts"; +import { ChartExport, TableExport } from "./answer-export-actions.tsx"; +import { + type Align, + type Block, + parseBlocks, + safeHref, +} from "./answer-parse.ts"; import { useTypeset } from "./typeset.ts"; // A small markdown reader, so the browser does not get a markdown dependency. @@ -414,6 +420,15 @@ export function AnswerActions({ text }: { text: string }) { ); } +/** For each block, the text of the heading above it, if any. Names an export. */ +const headingBefore = (blocks: Block[]) => { + let last: string | undefined; + return blocks.map((block) => { + if (block.kind === "h") last = block.text; + return last; + }); +}; + export function Answer({ text, onFollowUp, @@ -441,9 +456,11 @@ export function Answer({ const blockIn = reveal ? { animation: "fade-up var(--dur-slow) var(--ease-out) both" } : undefined; + const blocks = parseBlocks(text, streaming); + const headings = headingBefore(blocks); return (
- {parseBlocks(text, streaming).map((block, index) => { + {blocks.map((block, index) => { const key = `${block.kind}-${index}`; if (block.kind === "code") { return ( @@ -458,12 +475,19 @@ export function Answer({ } if (block.kind === "table") { return ( -
+
+ {!streaming && ( + + )}
); } @@ -479,7 +503,7 @@ export function Answer({ // block's position and does not change when the fence closes — that // is what makes this a swap rather than a remount (see `Block` in `answer-parse.ts`). return ( -
+
{block.spec ? ( @@ -487,6 +511,7 @@ export function Answer({ )} + {!streaming && block.spec && }
); } From ffd37a8d134c528866575de7d918aa837b8cfd43 Mon Sep 17 00:00:00 2001 From: Omar Zunic Date: Wed, 11 Nov 2026 21:02:49 +0100 Subject: [PATCH 03/16] feat(assistant): trim old tool results and show a context meter Every turn sends the whole thread again, and old tool results are most of it. The wire copy now replaces a tool result over 400 characters with a short stub when it is older than the last 4 user messages. Stored rows, call ids and the current turn do not change. The thread payload (web and api/v1) now carries promptTokens of the last answer and the contextLength of the thread's model. The web composer and the mobile chat show a note when the chat uses 70% or more of the model's memory, with a New chat action. --- apps/mobile/src/assistant/chat.tsx | 6 ++ apps/mobile/src/assistant/context-note.tsx | 63 ++++++++++++++++ .../components/assistant/context-meter.tsx | 74 +++++++++++++++++++ apps/web/src/lib/openapi.ts | 10 +++ apps/web/src/routes/$orgSlug/assistant.tsx | 6 ++ .../$orgSlug/assistant/threads/$id/index.ts | 4 +- docs/domains/assistant.md | 1 + packages/api-client/openapi.json | 30 +++++++- packages/api-client/src/openapi.gen.ts | 4 + packages/api-client/src/schemas-assistant.ts | 4 + packages/core/src/api/assistant.ts | 4 + .../core/src/services/assistant-wire.test.ts | 71 ++++++++++++++++++ packages/core/src/services/assistant-wire.ts | 38 ++++++++++ .../core/src/services/assistant.service.ts | 40 +++++++++- 14 files changed, 351 insertions(+), 4 deletions(-) create mode 100644 apps/mobile/src/assistant/context-note.tsx create mode 100644 apps/web/src/components/assistant/context-meter.tsx create mode 100644 packages/core/src/services/assistant-wire.test.ts create mode 100644 packages/core/src/services/assistant-wire.ts diff --git a/apps/mobile/src/assistant/chat.tsx b/apps/mobile/src/assistant/chat.tsx index 6d7b0309..5e4d824d 100644 --- a/apps/mobile/src/assistant/chat.tsx +++ b/apps/mobile/src/assistant/chat.tsx @@ -18,6 +18,7 @@ import { SPACE } from "../ui/type"; import { Answer } from "./answer"; import { ApprovalCard } from "./approval-card"; import { Composer } from "./composer"; +import { ContextNote } from "./context-note"; import { useRun, useRunActions } from "./runs"; import { ToolTraceList } from "./tool-trace"; import { visibleMessages, visiblePending } from "./turn"; @@ -304,6 +305,11 @@ export function Chat({ id: routeId }: { id: string | null }) { {run.error ? : null} {draftError ? : null} + router.replace("/assistant/new")} + /> runId !== "" && actions.stop(runId)} diff --git a/apps/mobile/src/assistant/context-note.tsx b/apps/mobile/src/assistant/context-note.tsx new file mode 100644 index 00000000..5acd50a9 --- /dev/null +++ b/apps/mobile/src/assistant/context-note.tsx @@ -0,0 +1,63 @@ +import { Platform, Pressable, View } from "react-native"; + +import { Text } from "../ui/components"; +import { usePalette } from "../ui/theme"; +import { SPACE } from "../ui/type"; + +/** The note shows from this share of the model's context. */ +const NOTE_FROM = 0.7; + +/** Tells the person that a long chat fills the model's memory. */ +export function ContextNote({ + used, + limit, + onNewChat, +}: { + used: number | null | undefined; + limit: number | null | undefined; + onNewChat: () => void; +}) { + const palette = usePalette(); + if (!used || !limit || used / limit < NOTE_FROM) return null; + const percent = Math.min(100, Math.round((used / limit) * 100)); + const android = Platform.OS === "android"; + return ( + + + + This chat uses {percent}% of the model's memory. Start a new chat for + best results. + + + + + New chat + + + + ); +} diff --git a/apps/web/src/components/assistant/context-meter.tsx b/apps/web/src/components/assistant/context-meter.tsx new file mode 100644 index 00000000..4d79093f --- /dev/null +++ b/apps/web/src/components/assistant/context-meter.tsx @@ -0,0 +1,74 @@ +import { + Tooltip, + TooltipContent, + TooltipProvider, + TooltipTrigger, +} from "#/components/ui/tooltip.tsx"; + +/** The meter shows from this share of the model's context. */ +export const CONTEXT_METER_FROM = 0.7; + +const kTokens = (n: number) => + n >= 1000 ? `${Math.round(n / 1000)}k` : String(n); + +/** How full the chat is, as a share from 0 to 1. Null when it is unknown. */ +export const contextShare = ( + used: number | null | undefined, + limit: number | null | undefined, +) => (used && limit && limit > 0 ? used / limit : null); + +export function ContextMeter({ + used, + limit, + onNewChat, +}: { + used: number | null | undefined; + limit: number | null | undefined; + onNewChat: () => void; +}) { + const share = contextShare(used, limit); + if (share === null || !used || !limit || share < CONTEXT_METER_FROM) { + return null; + } + const percent = Math.min(100, Math.round(share * 100)); + return ( +
+ + + + {/* The native element carries the value. The ring is drawn + with a conic gradient under a mask. */} + + + + + This chat uses {percent}% of the model's memory. Start a new chat + for best results. +
+ {kTokens(used)} of {kTokens(limit)} tokens +
+
+
+
+ +
+ ); +} diff --git a/apps/web/src/lib/openapi.ts b/apps/web/src/lib/openapi.ts index 79ad764c..4e7200a5 100644 --- a/apps/web/src/lib/openapi.ts +++ b/apps/web/src/lib/openapi.ts @@ -1561,6 +1561,16 @@ const apiAssistantThreadDetail = named( z.object({ thread: apiAssistantThread, messages: z.array(apiAssistantMessage).describe("Oldest first."), + promptTokens: z + .number() + .int() + .nullable() + .describe("Prompt tokens of the last model call."), + contextLength: z + .number() + .int() + .nullable() + .describe("Context size of the thread's model."), }), ), ); diff --git a/apps/web/src/routes/$orgSlug/assistant.tsx b/apps/web/src/routes/$orgSlug/assistant.tsx index 03c74163..476242b1 100644 --- a/apps/web/src/routes/$orgSlug/assistant.tsx +++ b/apps/web/src/routes/$orgSlug/assistant.tsx @@ -15,6 +15,7 @@ import { useAssistantRun, } from "#/components/assistant/assistant-runs.tsx"; import { MobileChatBar } from "#/components/assistant/chat-rail.tsx"; +import { ContextMeter } from "#/components/assistant/context-meter.tsx"; import { ApprovalCard, AssistantLoader, @@ -503,6 +504,11 @@ function AssistantPage() { past a card as "no" to it (see `abandonPendingCalls`), which keeps the thread well-formed — but that is a fallback for a stale tab, not a way anybody should answer. The card is. */} + void startChat.mutate()} + /> { diff --git a/apps/web/src/routes/api/v1/orgs/$orgSlug/assistant/threads/$id/index.ts b/apps/web/src/routes/api/v1/orgs/$orgSlug/assistant/threads/$id/index.ts index cda4e161..dc6bd277 100644 --- a/apps/web/src/routes/api/v1/orgs/$orgSlug/assistant/threads/$id/index.ts +++ b/apps/web/src/routes/api/v1/orgs/$orgSlug/assistant/threads/$id/index.ts @@ -47,11 +47,13 @@ export const Route = createFileRoute( ): Promise => { await assertAssistantAvailable(); const { assistantService } = await import("@farmer/core/services"); - const { conversation, messages } = + const { conversation, messages, promptTokens, contextLength } = await assistantService.conversations.get(ctx, conversationId); return { thread: toApiAssistantThread(conversation), messages: messages.map(toApiAssistantMessage), + promptTokens, + contextLength, }; }, }), diff --git a/docs/domains/assistant.md b/docs/domains/assistant.md index 3e53f5f5..5689ce06 100644 --- a/docs/domains/assistant.md +++ b/docs/domains/assistant.md @@ -4,6 +4,7 @@ An OpenRouter-backed chat page (`$orgSlug/assistant`) that answers questions abo - **The step ceiling ends in an ANSWER, not an error.** `MAX_STEPS` (8) is a ceiling on model round-trips, and the number is in the system prompt so most turns never reach it. A turn that does gets one more call with the tools taken away (`OUT_OF_STEPS`) — the model cannot ask for a ninth step, so it terminates, and the person gets what the eight steps found plus what is missing from it. The nudge is not stored: it is the loop talking to the model about its own ceiling, not something anyone said. Only a closing answer that is empty falls back to an error (`NO_ANSWER`). - **A step's independent reads run together.** `runClaimedBatch` claims the leading run of `read` tools in ONE transaction and awaits them with `Promise.all`, so four lists cost one round trip and not four. Writes stay strictly in order. `AssistantTool.serial` opts a read out — `integration_status` sets it, because it can refresh an OAuth token and store it. `list_time_entries` does not: it calls `listRange` with `{ persist: false }`, which writes nothing, so two overlapping ranges cannot race. **A new read that reaches a service which writes must set `serial`**; nothing detects it, and the failure is a race. +- **Old tool results are stubbed in the wire copy only.** `toWireMessages` calls `elideOldToolResults` (`assistant-wire.ts`): a `tool` result over `ELIDE_MIN_CHARS` (400) from before the last `KEEP_TOOL_RESULT_TURNS` (4) user messages becomes a short stub. Every `tool` row and call id stays, so the pairing rule holds. Stored rows never change, and the current turn is never stubbed. The window is a fixed count, so the prompt prefix stays the same until a new user message moves it. - **A capped tool result is cut by ROW where the value is a list** (`MAX_TOOL_RESULT_CHARS`), never mid-object, and the note says how many rows were dropped and tells the model to narrow the call ITSELF rather than send the person back to re-ask. - **A tool that throws tells the model only what a person may read.** The tool result goes to the model provider, so `executeTool` passes a failure's message only when `classifyServiceError` (`services/error-message.ts`) says so — a validation failure, or a class that extends `UserFacingError` (`lib/user-facing-error.ts`: an `AccessError`, a full day, a GitHub rate limit or dead installation, a refused upload, the assistant's own errors), or a bare `Error` sentence a service threw on purpose. Exposure is a property of the class, so a new error a person should read extends the base; one that can carry an address, a URL or a provider's body (`WebhookUrlRefusedError`, `OpenRouterError`, `ObjectStoreError`) does not. A database error, which carries its SQL and parameters, is logged and becomes a generic line. `executeTool` calls `agentErrorMessage`, which adds a recovery hint (`list_tasks`, `list_projects`) to a refused task or project id, as the MCP server does. diff --git a/packages/api-client/openapi.json b/packages/api-client/openapi.json index 35e9b65d..00c5f1ea 100644 --- a/packages/api-client/openapi.json +++ b/packages/api-client/openapi.json @@ -17086,11 +17086,39 @@ "$ref": "#/components/schemas/AssistantMessage" }, "description": "Oldest first." + }, + "promptTokens": { + "anyOf": [ + { + "type": "integer", + "minimum": -9007199254740991, + "maximum": 9007199254740991 + }, + { + "type": "null" + } + ], + "description": "Prompt tokens of the last model call." + }, + "contextLength": { + "anyOf": [ + { + "type": "integer", + "minimum": -9007199254740991, + "maximum": 9007199254740991 + }, + { + "type": "null" + } + ], + "description": "Context size of the thread's model." } }, "required": [ "thread", - "messages" + "messages", + "promptTokens", + "contextLength" ] }, "AssistantEvent": { diff --git a/packages/api-client/src/openapi.gen.ts b/packages/api-client/src/openapi.gen.ts index a31014a9..2f1a63a8 100644 --- a/packages/api-client/src/openapi.gen.ts +++ b/packages/api-client/src/openapi.gen.ts @@ -3789,6 +3789,10 @@ export interface components { thread: components["schemas"]["AssistantThread"]; /** @description Oldest first. */ messages: components["schemas"]["AssistantMessage"][]; + /** @description Prompt tokens of the last model call. */ + promptTokens: number | null; + /** @description Context size of the thread's model. */ + contextLength: number | null; }; /** @description One Server-Sent Event: each `data:` line is one of these, as JSON. The turn ends after `done` or `error`. */ AssistantEvent: { diff --git a/packages/api-client/src/schemas-assistant.ts b/packages/api-client/src/schemas-assistant.ts index 15f4816f..b5ffc4f4 100644 --- a/packages/api-client/src/schemas-assistant.ts +++ b/packages/api-client/src/schemas-assistant.ts @@ -97,6 +97,10 @@ export const assistantThreadDetailSchema = z.object({ thread: assistantThreadSchema, /** Oldest first. */ messages: z.array(assistantMessageSchema), + /** Prompt tokens of the last model call. Null before the first answer. */ + promptTokens: z.number().nullable(), + /** Context size of the thread's model. Null when unknown. */ + contextLength: z.number().nullable(), }); export type AssistantThreadDetail = z.output< typeof assistantThreadDetailSchema diff --git a/packages/core/src/api/assistant.ts b/packages/core/src/api/assistant.ts index 603af676..8c3d20d5 100644 --- a/packages/core/src/api/assistant.ts +++ b/packages/core/src/api/assistant.ts @@ -193,6 +193,10 @@ export const toApiAssistantMessage = (row: { export type ApiAssistantThreadDetail = { thread: ApiAssistantThread; messages: ApiAssistantMessage[]; + /** Prompt tokens of the last model call. Null before the first answer. */ + promptTokens: number | null; + /** Context size of the thread's model. Null when unknown. */ + contextLength: number | null; }; /** diff --git a/packages/core/src/services/assistant-wire.test.ts b/packages/core/src/services/assistant-wire.test.ts new file mode 100644 index 00000000..522b7766 --- /dev/null +++ b/packages/core/src/services/assistant-wire.test.ts @@ -0,0 +1,71 @@ +import { describe, expect, test } from "bun:test"; + +import type { ChatMessage } from "../lib/openrouter-client"; +import { at } from "../test/harness"; +import { + ELIDE_MIN_CHARS, + ELIDED_RESULT, + elideOldToolResults, + KEEP_TOOL_RESULT_TURNS, +} from "./assistant-wire"; + +const big = "x".repeat(ELIDE_MIN_CHARS + 1); + +/** One turn: user, assistant with a call, and the tool result. */ +const turn = (n: number, result: string): ChatMessage[] => [ + { role: "user", content: `q${n}` }, + { + role: "assistant", + content: "", + tool_calls: [ + { + id: `c${n}`, + type: "function", + function: { name: "t", arguments: "{}" }, + }, + ], + }, + { role: "tool", content: result, tool_call_id: `c${n}` }, +]; + +const toolRows = (wire: ChatMessage[]) => + wire.flatMap((m) => (m.role === "tool" ? [m] : [])); + +describe("elideOldToolResults", () => { + test("stubs big results older than the window", () => { + const turns = KEEP_TOOL_RESULT_TURNS + 2; + const wire = Array.from({ length: turns }, (_, n) => turn(n, big)).flat(); + const out = toolRows(elideOldToolResults(wire)); + expect(out).toHaveLength(turns); + out.forEach((row, n) => { + const old = n < 2; + expect(row.tool_call_id).toBe(`c${n}`); + expect(row.content).toBe(old ? ELIDED_RESULT : big); + }); + }); + + test("keeps small results and the current turn", () => { + const wire = [ + ...turn(0, "tiny"), + ...turn(1, big), + ...turn(2, big), + ...turn(3, big), + ...turn(4, big), + ...turn(5, big), + ]; + const out = toolRows(elideOldToolResults(wire)); + expect(at(out, 0).content).toBe("tiny"); + expect(at(out, 1).content).toBe(ELIDED_RESULT); + expect(at(out, 5).content).toBe(big); + }); + + test("leaves a short thread and the input untouched", () => { + const wire = turn(0, big); + expect(elideOldToolResults(wire)).toBe(wire); + expect(at(wire, 2)).toEqual({ + role: "tool", + content: big, + tool_call_id: "c0", + }); + }); +}); diff --git a/packages/core/src/services/assistant-wire.ts b/packages/core/src/services/assistant-wire.ts new file mode 100644 index 00000000..1fdfd5fb --- /dev/null +++ b/packages/core/src/services/assistant-wire.ts @@ -0,0 +1,38 @@ +import type { ChatMessage } from "../lib/openrouter-client"; + +/** Turns (user messages) whose tool results stay whole. Older ones are + * stubbed. A fixed count keeps the prompt prefix the same between turns. */ +export const KEEP_TOOL_RESULT_TURNS = 4; + +/** A shorter result costs nothing, so it stays. */ +export const ELIDE_MIN_CHARS = 400; + +export const ELIDED_RESULT = JSON.stringify({ + elided: true, + note: "Older result removed to save space. Call the tool again if you need it.", +}); + +/** + * Replace big tool results of old turns with a stub. Wire copy only. + * Every tool row and its `tool_call_id` stays: one result per call id. + */ +export const elideOldToolResults = (wire: ChatMessage[]): ChatMessage[] => { + let seen = 0; + let boundary = -1; + for (let i = wire.length - 1; i >= 0; i--) { + if (wire[i]?.role !== "user") continue; + seen += 1; + if (seen === KEEP_TOOL_RESULT_TURNS) { + boundary = i; + break; + } + } + if (boundary === -1) return wire; + return wire.map((message, i) => + i < boundary && + message.role === "tool" && + message.content.length > ELIDE_MIN_CHARS + ? { ...message, content: ELIDED_RESULT } + : message, + ); +}; diff --git a/packages/core/src/services/assistant.service.ts b/packages/core/src/services/assistant.service.ts index 84706f20..62a26b8a 100644 --- a/packages/core/src/services/assistant.service.ts +++ b/packages/core/src/services/assistant.service.ts @@ -42,6 +42,7 @@ import { systemPrompt, toolDefinitions, } from "./assistant-tools"; +import { elideOldToolResults } from "./assistant-wire"; import { diffFields, recordAudit } from "./audit"; import { ctxAccess, ctxTimezone, requireAdmin } from "./context"; import { agentErrorMessage } from "./error-message"; @@ -126,6 +127,7 @@ export const chatModel = (): ChatModelPort => portOverride ?? realPort; /** Test-only: substitute a scripted model. Pass null to restore. */ export const setChatModelForTests = (port: ChatModelPort | null): void => { portOverride = port; + catalogue = null; }; // ── events ─────────────────────────────────────────────────────────────────── @@ -481,6 +483,32 @@ const requireConversation = async (ctx: Ctx, conversationId: string) => { return row; }; +const MODEL_CATALOGUE_TTL_MS = 60 * 60 * 1000; +let catalogue: { at: number; lengths: Map } | null = + null; + +/** The context size of a model, for the meter. Any member may see it, so it + * skips the admin gate of `listModels`. A failed lookup gives null. */ +const contextLengthOf = async ( + ctx: Ctx, + modelId: string, +): Promise => { + try { + if (!catalogue || Date.now() - catalogue.at > MODEL_CATALOGUE_TTL_MS) { + const row = await loadSettings(ctx); + if (!row?.apiKey) return null; + const models = await chatModel().listModels(row.apiKey); + catalogue = { + at: Date.now(), + lengths: new Map(models.map((m) => [m.id, m.contextLength])), + }; + } + return catalogue.lengths.get(modelId) ?? null; + } catch { + return null; + } +}; + const conversations = { list: async (ctx: Ctx) => db @@ -524,7 +552,15 @@ const conversations = { where: eq(assistantMessage.conversationId, conversation.id), orderBy: asc(assistantMessage.createdAt), }); - return { conversation, messages }; + const promptTokens = + messages + .filter((row) => row.role === "assistant" && row.promptTokens) + .at(-1)?.promptTokens ?? null; + const contextLength = + promptTokens === null + ? null + : await contextLengthOf(ctx, conversation.model); + return { conversation, messages, promptTokens, contextLength }; }, rename: async (ctx: Ctx, input: RenameConversationInput) => { @@ -627,7 +663,7 @@ const toWireMessages = (rows: StoredMessage[]): ChatMessage[] => { }); } }); - return wire; + return elideOldToolResults(wire); }; const loadHistory = async (conversationId: string, client: DbClient = db) => From 63052b08a69aba1611dadc2663d401294900180b Mon Sep 17 00:00:00 2001 From: Omar Zunic Date: Wed, 11 Nov 2026 21:04:36 +0100 Subject: [PATCH 04/16] feat(assistant): add the AI usage ledger and Settings AI An admin could not see what the assistant cost. PostHog held the figures, but the product showed none of them. Add the assistant_usage table. generate() writes one row for each billed call (turn step, retry and title), beside the telemetry event. The cost is null when a stalled attempt has no usage row, so the view counts it as unknown and not as zero. A failed insert is logged and never fails the turn. userId has no foreign key and conversationId is set null, so a removed person or a deleted thread keeps its cost. Add assistantUsageService.summary (admin only). It sums in SQL for the organization, each person and each model over a period. Add Settings -> AI. It holds the assistant settings, moved from Workspace -> Integrations, and a usage panel with a period picker. Update every text that named the old place. --- apps/mobile/app/workspace/assistant.tsx | 4 +- .../src/workspace/assistant-settings.tsx | 4 +- apps/web/CLAUDE.md | 2 +- apps/web/src/components/settings-nav.tsx | 14 +- apps/web/src/lib/nav-items.ts | 1 + apps/web/src/queries/assistant.ts | 8 + apps/web/src/routeTree.gen.ts | 31 +- apps/web/src/routes/$orgSlug/assistant.tsx | 8 +- apps/web/src/routes/$orgSlug/settings/ai.tsx | 624 ++++++++++++++++++ .../settings/workspace-integrations.tsx | 416 +----------- apps/web/src/server/assistant.ts | 9 + docs/domains/assistant.md | 3 +- docs/domains/integrations.md | 2 +- packages/core/drizzle/0084_married_solo.sql | 19 + ...{0083_snapshot.json => 0084_snapshot.json} | 167 ++++- packages/core/drizzle/meta/_journal.json | 7 + packages/core/src/db/schema/assistant.ts | 35 + packages/core/src/schema/assistant.ts | 2 +- packages/core/src/schema/reports.ts | 4 + .../src/services/assistant-usage.service.ts | 136 ++++ .../core/src/services/assistant-usage.test.ts | 149 +++++ .../core/src/services/assistant.service.ts | 24 + packages/core/src/services/assistant.test.ts | 14 +- packages/core/src/services/index.ts | 1 + 24 files changed, 1271 insertions(+), 413 deletions(-) create mode 100644 apps/web/src/routes/$orgSlug/settings/ai.tsx create mode 100644 packages/core/drizzle/0084_married_solo.sql rename packages/core/drizzle/meta/{0083_snapshot.json => 0084_snapshot.json} (97%) create mode 100644 packages/core/src/services/assistant-usage.service.ts create mode 100644 packages/core/src/services/assistant-usage.test.ts diff --git a/apps/mobile/app/workspace/assistant.tsx b/apps/mobile/app/workspace/assistant.tsx index 11a4f4e0..3fb71540 100644 --- a/apps/mobile/app/workspace/assistant.tsx +++ b/apps/mobile/app/workspace/assistant.tsx @@ -1,7 +1,7 @@ import { Redirect } from "expo-router"; -/** The assistant's settings are a part of the workspace integrations, as on - * the web. This route stays for a link that points here. */ +/** The assistant's settings are a part of the workspace integrations in the + * app. This route stays for a link that points here. */ export default function AssistantSettingsScreen() { return ; } diff --git a/apps/mobile/src/workspace/assistant-settings.tsx b/apps/mobile/src/workspace/assistant-settings.tsx index 3242ce4f..f30cc4e4 100644 --- a/apps/mobile/src/workspace/assistant-settings.tsx +++ b/apps/mobile/src/workspace/assistant-settings.tsx @@ -40,7 +40,7 @@ const connectedOn = (iso: string | null) => : "-"; /** The model of a first connection, as the web's connect form sends it - * (`settings/workspace-integrations.tsx`). The admin changes it after the + * (`settings/ai.tsx`). The admin changes it after the * connection, because the server lists the models only with a key. */ const FIRST_MODEL = "openai/gpt-4o-mini"; @@ -155,7 +155,7 @@ function HouseRules({ settings }: { settings: AssistantSettings }) { /** * The assistant's settings for the workspace, as the web shows them in the - * workspace integrations. An admin connects the OpenRouter key + * AI settings page. An admin connects the OpenRouter key * (write-only), chooses the model, pauses the assistant, writes house rules * and disconnects it. A member does not see them, as on the web. */ diff --git a/apps/web/CLAUDE.md b/apps/web/CLAUDE.md index df58b86d..8d8f9811 100644 --- a/apps/web/CLAUDE.md +++ b/apps/web/CLAUDE.md @@ -13,7 +13,7 @@ bun run generate-routes # tsr generate — regenerates routeTree.gen.ts - `src/routes/` — TanStack Router file-based routing. - `$orgSlug/route.tsx` is the org-scoped layout: its `beforeLoad` resolves the session, the organization and the caller's role in ONE server call (`enterOrganization`) alongside `getPublicAppConfig`, and redirects out if any check fails — every nested route inherits that guard. `enterOrganization` repoints the session only when the active org actually differs. - - Under it: `index.tsx` (home: today, plus week and month targets), `time/` (a `route.tsx` layout with the targets strip and the unfiled banner, then `index`/`week`/`month`), `projects/` (the list, and `$projectId/` with `index`/`team`/`settings`), `reports.tsx`, `audit-log.tsx` (admins only, a sidebar row under Reports; see `docs/domains/audit-log.md`), `assistant.tsx`, and `settings/` (an `index.tsx` hub plus organization, members, my-schedule, clients, tasks, recurring-blocks, holidays, hour-fund, reminders, import, integrations, preferences, `webhooks/` — the endpoint list plus one endpoint's delivery log — and the Infrastructure group, shown only to a platform admin: platform-admins plus `instance/` (sign-in, email, storage, integrations, features — one page per section of `INSTANCE_SETTINGS`, all drawn by `components/instance-settings.tsx`; see ADR 0004)). + - Under it: `index.tsx` (home: today, plus week and month targets), `time/` (a `route.tsx` layout with the targets strip and the unfiled banner, then `index`/`week`/`month`), `projects/` (the list, and `$projectId/` with `index`/`team`/`settings`), `reports.tsx`, `audit-log.tsx` (admins only, a sidebar row under Reports; see `docs/domains/audit-log.md`), `assistant.tsx`, and `settings/` (an `index.tsx` hub plus organization, members, my-schedule, clients, tasks, recurring-blocks, holidays, hour-fund, reminders, import, integrations, ai (the assistant's settings and its usage), preferences, `webhooks/` — the endpoint list plus one endpoint's delivery log — and the Infrastructure group, shown only to a platform admin: platform-admins plus `instance/` (sign-in, email, storage, integrations, features — one page per section of `INSTANCE_SETTINGS`, all drawn by `components/instance-settings.tsx`; see ADR 0004)). - Outside it: `index.tsx` (a redirect into the workspace; the public pages live in `apps/site`, see `docs/domains/deployment.md`), `login.tsx`, `choose-organization.tsx` (the workspace chooser for a link such as `/:org/settings`; see `docs/domains/authentication.md`), `accept-invite/$invitationId.tsx`, `no-access.tsx`, `device.tsx` (approves a CLI login) and `cli/` (the install scripts and binary downloads). - Server routes: `api/auth/$.ts` (better-auth), `api/login.ts`, `api/mcp-login.ts`, `api/github/setup.ts`, `api/slack/callback.ts`, `api/files/$.ts`, `api/assistant/stream.ts`, `api/mcp/$orgSlug.ts`, `[.]well-known/oauth-*.ts`, `api/v1/projects` + `api/v1/time-entries` (cookie-authed REST reads), and `api/v1/{meta,me,session}`, `api/v1/me/devices` (the mobile app's phone, see `docs/domains/mobile.md`) + `api/v1/orgs/$orgSlug/*` (the CLI's API). `api/v1/openapi[.]json.ts` serves the OpenAPI document that `lib/openapi.ts` builds, and `api-docs.tsx` renders it with Scalar. **A new `api/v1` route needs an entry in `OPERATIONS`** — `lib/openapi.test.ts` fails until the two lists match, and each response schema there is checked against its `@farmer/core/api` type at compile time. That API takes two bearer credentials — a device-flow session token and a personal API token (`fmr_…`, Preferences → API tokens, `components/api-tokens-settings.tsx`); `docs/domains/api-tokens.md` has the rules, including the read-only 403 in `serve` and the `session` scheme that lets `/api-docs` send requests as the signed-in reader (`?org=` on the OpenAPI route; never add CORS headers to `api/v1`). All of that auth lives in `server/api-auth.ts` — deliberately NOT `server/auth.ts`, see the client-bundle rule below. - `reports.tsx` is the shell. Its four tabs — Time, Overtime, Tracking (`compliance`) and Absence — are declared in `components/reports/search.ts` and live in `components/reports/`. `periodSearch` there is shared with the project page. diff --git a/apps/web/src/components/settings-nav.tsx b/apps/web/src/components/settings-nav.tsx index ccae9dc9..69e343e9 100644 --- a/apps/web/src/components/settings-nav.tsx +++ b/apps/web/src/components/settings-nav.tsx @@ -12,6 +12,7 @@ import { PuzzlePieceIcon, ShieldCheckIcon, SignInIcon, + SparkleIcon, TagIcon, ToggleRightIcon, UploadSimpleIcon, @@ -156,7 +157,18 @@ export const SETTINGS_ENTRIES: SettingsEntry[] = [ }, group: "workspace", description: - "What the workspace is connected to: GitHub accounts for repositories, Slack for reminders, and the assistant's model provider.", + "What the workspace is connected to: GitHub accounts for repositories and Slack for reminders.", + show: (audience) => audience.isAdmin, + }, + { + item: { + label: "AI", + to: "/$orgSlug/settings/ai", + icon: SparkleIcon, + }, + group: "workspace", + description: + "The assistant's key, model and house rules, and what its answers cost.", show: (audience) => audience.isAdmin, }, { diff --git a/apps/web/src/lib/nav-items.ts b/apps/web/src/lib/nav-items.ts index 9af0a97b..e075beee 100644 --- a/apps/web/src/lib/nav-items.ts +++ b/apps/web/src/lib/nav-items.ts @@ -32,6 +32,7 @@ export type OrgRoutePath = | "/$orgSlug/reports" | "/$orgSlug/audit-log" | "/$orgSlug/settings" + | "/$orgSlug/settings/ai" | "/$orgSlug/settings/clients" | "/$orgSlug/settings/my-schedule" | "/$orgSlug/settings/recurring-blocks" diff --git a/apps/web/src/queries/assistant.ts b/apps/web/src/queries/assistant.ts index c9576084..e4675e5a 100644 --- a/apps/web/src/queries/assistant.ts +++ b/apps/web/src/queries/assistant.ts @@ -1,7 +1,9 @@ +import type { AssistantUsageInput } from "@farmer/core/schema"; import { queryOptions } from "@tanstack/react-query"; import { getAssistantSettings, + getAssistantUsage, getConversation, listAssistantModels, listConversations, @@ -25,6 +27,12 @@ export const assistantModelsQuery = (org: string) => staleTime: 30 * 60_000, }); +export const assistantUsageQuery = (org: string, input: AssistantUsageInput) => + queryOptions({ + queryKey: [...queryKeys(org).assistant, "usage", input], + queryFn: () => getAssistantUsage({ data: input }), + }); + export const conversationsQuery = (org: string) => queryOptions({ queryKey: [...queryKeys(org).assistant, "conversations"], diff --git a/apps/web/src/routeTree.gen.ts b/apps/web/src/routeTree.gen.ts index 77fec23d..0b04fcfd 100644 --- a/apps/web/src/routeTree.gen.ts +++ b/apps/web/src/routeTree.gen.ts @@ -33,6 +33,7 @@ import { Route as CliInstallDotps1RouteImport } from './routes/cli/install[.]ps1 import { Route as CliInstallDotshRouteImport } from './routes/cli/install[.]sh' import { Route as OrgSlugProjectsIndexRouteImport } from './routes/$orgSlug/projects/index' import { Route as OrgSlugSettingsIndexRouteImport } from './routes/$orgSlug/settings/index' +import { Route as OrgSlugSettingsAiRouteImport } from './routes/$orgSlug/settings/ai' import { Route as OrgSlugSettingsClientsRouteImport } from './routes/$orgSlug/settings/clients' import { Route as OrgSlugSettingsHolidaysRouteImport } from './routes/$orgSlug/settings/holidays' import { Route as OrgSlugSettingsHourFundRouteImport } from './routes/$orgSlug/settings/hour-fund' @@ -325,6 +326,11 @@ const OrgSlugSettingsIndexRoute = OrgSlugSettingsIndexRouteImport.update({ path: '/settings/', getParentRoute: () => OrgSlugRouteRoute, } as any) +const OrgSlugSettingsAiRoute = OrgSlugSettingsAiRouteImport.update({ + id: '/settings/ai', + path: '/settings/ai', + getParentRoute: () => OrgSlugRouteRoute, +} as any) const OrgSlugSettingsClientsRoute = OrgSlugSettingsClientsRouteImport.update({ id: '/settings/clients', path: '/settings/clients', @@ -1326,6 +1332,7 @@ export interface FileRoutesByFullPath { '/cli/install.sh': typeof CliInstallDotshRoute '/$orgSlug/': typeof OrgSlugIndexRoute '/cli/': typeof CliIndexRoute + '/$orgSlug/settings/ai': typeof OrgSlugSettingsAiRoute '/$orgSlug/settings/clients': typeof OrgSlugSettingsClientsRoute '/$orgSlug/settings/holidays': typeof OrgSlugSettingsHolidaysRoute '/$orgSlug/settings/hour-fund': typeof OrgSlugSettingsHourFundRoute @@ -1518,6 +1525,7 @@ export interface FileRoutesByTo { '/cli/install.sh': typeof CliInstallDotshRoute '/$orgSlug': typeof OrgSlugIndexRoute '/cli': typeof CliIndexRoute + '/$orgSlug/settings/ai': typeof OrgSlugSettingsAiRoute '/$orgSlug/settings/clients': typeof OrgSlugSettingsClientsRoute '/$orgSlug/settings/holidays': typeof OrgSlugSettingsHolidaysRoute '/$orgSlug/settings/hour-fund': typeof OrgSlugSettingsHourFundRoute @@ -1713,6 +1721,7 @@ export interface FileRoutesById { '/cli/install.sh': typeof CliInstallDotshRoute '/$orgSlug/': typeof OrgSlugIndexRoute '/cli/': typeof CliIndexRoute + '/$orgSlug/settings/ai': typeof OrgSlugSettingsAiRoute '/$orgSlug/settings/clients': typeof OrgSlugSettingsClientsRoute '/$orgSlug/settings/holidays': typeof OrgSlugSettingsHolidaysRoute '/$orgSlug/settings/hour-fund': typeof OrgSlugSettingsHourFundRoute @@ -1909,6 +1918,7 @@ export interface FileRouteTypes { | '/cli/install.sh' | '/$orgSlug/' | '/cli/' + | '/$orgSlug/settings/ai' | '/$orgSlug/settings/clients' | '/$orgSlug/settings/holidays' | '/$orgSlug/settings/hour-fund' @@ -2101,6 +2111,7 @@ export interface FileRouteTypes { | '/cli/install.sh' | '/$orgSlug' | '/cli' + | '/$orgSlug/settings/ai' | '/$orgSlug/settings/clients' | '/$orgSlug/settings/holidays' | '/$orgSlug/settings/hour-fund' @@ -2295,6 +2306,7 @@ export interface FileRouteTypes { | '/cli/install.sh' | '/$orgSlug/' | '/cli/' + | '/$orgSlug/settings/ai' | '/$orgSlug/settings/clients' | '/$orgSlug/settings/holidays' | '/$orgSlug/settings/hour-fund' @@ -2779,6 +2791,13 @@ declare module '@tanstack/react-router' { preLoaderRoute: typeof OrgSlugSettingsIndexRouteImport parentRoute: typeof OrgSlugRouteRoute } + '/$orgSlug/settings/ai': { + id: '/$orgSlug/settings/ai' + path: '/settings/ai' + fullPath: '/$orgSlug/settings/ai' + preLoaderRoute: typeof OrgSlugSettingsAiRouteImport + parentRoute: typeof OrgSlugRouteRoute + } '/$orgSlug/settings/clients': { id: '/$orgSlug/settings/clients' path: '/settings/clients' @@ -3980,6 +3999,7 @@ interface OrgSlugRouteRouteChildren { OrgSlugReportsRoute: typeof OrgSlugReportsRoute OrgSlugRewindRoute: typeof OrgSlugRewindRoute OrgSlugIndexRoute: typeof OrgSlugIndexRoute + OrgSlugSettingsAiRoute: typeof OrgSlugSettingsAiRoute OrgSlugSettingsClientsRoute: typeof OrgSlugSettingsClientsRoute OrgSlugSettingsHolidaysRoute: typeof OrgSlugSettingsHolidaysRoute OrgSlugSettingsHourFundRoute: typeof OrgSlugSettingsHourFundRoute @@ -4015,6 +4035,7 @@ const OrgSlugRouteRouteChildren: OrgSlugRouteRouteChildren = { OrgSlugReportsRoute: OrgSlugReportsRoute, OrgSlugRewindRoute: OrgSlugRewindRoute, OrgSlugIndexRoute: OrgSlugIndexRoute, + OrgSlugSettingsAiRoute: OrgSlugSettingsAiRoute, OrgSlugSettingsClientsRoute: OrgSlugSettingsClientsRoute, OrgSlugSettingsHolidaysRoute: OrgSlugSettingsHolidaysRoute, OrgSlugSettingsHourFundRoute: OrgSlugSettingsHourFundRoute, @@ -4324,13 +4345,3 @@ const rootRouteChildren: RootRouteChildren = { export const routeTree = rootRouteImport ._addFileChildren(rootRouteChildren) ._addFileTypes() - -import type { getRouter } from './router.tsx' -import type { startInstance } from './start.ts' -declare module '@tanstack/react-start' { - interface Register { - ssr: true - router: Awaited> - config: Awaited> - } -} diff --git a/apps/web/src/routes/$orgSlug/assistant.tsx b/apps/web/src/routes/$orgSlug/assistant.tsx index 03c74163..3f4ba5a9 100644 --- a/apps/web/src/routes/$orgSlug/assistant.tsx +++ b/apps/web/src/routes/$orgSlug/assistant.tsx @@ -318,8 +318,8 @@ function AssistantPage() {

The assistant needs to be connected again

The stored OpenRouter key can no longer be read. An admin can paste - it again under , - and the assistant comes back with every conversation intact. + it again under , and the + assistant comes back with every conversation intact.

@@ -334,8 +334,8 @@ function AssistantPage() {

The assistant isn't connected

An admin can connect this workspace's OpenRouter account under - . Until then - there's nothing to ask. + . Until then there's nothing + to ask.

diff --git a/apps/web/src/routes/$orgSlug/settings/ai.tsx b/apps/web/src/routes/$orgSlug/settings/ai.tsx new file mode 100644 index 00000000..b20ba230 --- /dev/null +++ b/apps/web/src/routes/$orgSlug/settings/ai.tsx @@ -0,0 +1,624 @@ +import { resolveReportPeriod } from "@farmer/core/lib/report-period"; +import { matchesSearch } from "@farmer/core/lib/search"; +import { MAX_SYSTEM_PROMPT_CHARS } from "@farmer/core/schema"; +import type { + AssistantUsageModel, + AssistantUsagePerson, +} from "@farmer/core/services"; +import { SparkleIcon } from "@phosphor-icons/react"; +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { createFileRoute, Link, useNavigate } from "@tanstack/react-router"; +import { useMemo, useState } from "react"; +import { toast } from "sonner"; +import { z } from "zod"; + +import { PersonAvatar } from "#/components/person-avatar.tsx"; +import { periodSearch } from "#/components/reports/search.ts"; +import { PeriodPicker } from "#/components/reports/shared.tsx"; +import { SettingsEyebrow } from "#/components/settings-nav.tsx"; +import { + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, + AlertDialogTrigger, +} from "#/components/ui/alert-dialog.tsx"; +import { Badge } from "#/components/ui/badge.tsx"; +import { Button } from "#/components/ui/button.tsx"; +import { + Combobox, + ComboboxContent, + ComboboxEmpty, + ComboboxItem, + ComboboxList, + ComboboxTrigger, + ComboboxValue, +} from "#/components/ui/combobox.tsx"; +import { Input } from "#/components/ui/input.tsx"; +import { + Page, + PageHeader, + PageSection, + SettingList, + SettingRow, +} from "#/components/ui/page-header.tsx"; +import { StatTile, StatTiles } from "#/components/ui/stat-tile.tsx"; +import { Switch } from "#/components/ui/switch.tsx"; +import { + Table, + TableBody, + TableCell, + TableHead, + TableHeader, + TableRow, +} from "#/components/ui/table.tsx"; +import { Textarea } from "#/components/ui/textarea.tsx"; +import { useOrgSlug } from "#/hooks/use-org-slug.ts"; +import { useToday } from "#/hooks/use-today.ts"; +import { useWeekStartsOn } from "#/hooks/use-week-starts-on.ts"; +import { + assistantModelsQuery, + assistantSettingsQuery, + assistantUsageQuery, +} from "#/queries/assistant.ts"; +import { queryKeys } from "#/queries/keys.ts"; +import { + disconnectAssistant, + setAssistantSettings, + updateAssistantSettings, +} from "#/server/assistant.ts"; +import { requireOrgAdmin } from "#/server/auth.ts"; + +// The assistant's settings and what its answers cost. One OpenRouter account +// per organization, set by an admin, so the page is admin only. + +export const Route = createFileRoute("/$orgSlug/settings/ai")({ + validateSearch: z.object(periodSearch), + beforeLoad: async () => { + await requireOrgAdmin(); + }, + component: AiSettingsPage, +}); + +function AiSettingsPage() { + const { assistantEnabled } = Route.useRouteContext(); + + return ( + + } title="AI" /> + {/* Behind the deployment's `ASSISTANT_ENABLED` switch: the services + refuse to store a key while the feature is off. */} + {assistantEnabled ? ( + <> + + + + ) : ( + +

+ The Assistant is turned off for this deployment. +

+
+ )} +
+ ); +} + +/** + * House rules appended to the assistant's system prompt. + * + * Its own row rather than a field in the list above because it is a textarea + * with a dirty state and an explicit save — the switches beside it write on + * change, which is wrong for prose somebody is still in the middle of typing. + */ +function HouseRulesRow({ stored }: { stored: string }) { + const orgSlug = useOrgSlug(); + const queryClient = useQueryClient(); + const [draft, setDraft] = useState(stored); + // Re-seed when the server's copy changes underneath (another admin, another + // tab) — but only when the box is clean, so it can't eat what is being typed. + const [baseline, setBaseline] = useState(stored); + if (stored !== baseline && draft === baseline) { + setBaseline(stored); + setDraft(stored); + } + const dirty = draft !== stored; + + const save = useMutation({ + mutationFn: () => + updateAssistantSettings({ + // Empty box clears the rules: null is "forget them", where undefined + // would mean "leave them alone". + data: { systemPrompt: draft.trim() ? draft.trim() : null }, + }), + onSuccess: async () => { + await queryClient.invalidateQueries({ + queryKey: queryKeys(orgSlug).assistant, + }); + toast.success("House rules saved"); + }, + onError: (error) => toast.error(error.message ?? "Could not save"), + }); + + return ( +
+
+

House rules

+

+ Added to the built-in instructions. The built-in ones win a conflict. +

+
+