diff --git a/ui-packages/web/src/core/byok/runtime.ts b/ui-packages/web/src/core/byok/runtime.ts index 6f06484..000393e 100644 --- a/ui-packages/web/src/core/byok/runtime.ts +++ b/ui-packages/web/src/core/byok/runtime.ts @@ -50,7 +50,8 @@ const customModel = (id: string, provider: string, baseUrl: string): Model // table and the reader names the model that handles them. input: ['text', 'image'], cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, - contextWindow: 128_000, + // Unknown. Pi reads 0 as no known window, rather than capping replies against a guessed one. + contextWindow: 0, maxTokens: 4_096, }) diff --git a/ui-packages/web/src/features/agent/image-tools.ts b/ui-packages/web/src/features/agent/image-tools.ts index 2e55439..0376d80 100644 --- a/ui-packages/web/src/features/agent/image-tools.ts +++ b/ui-packages/web/src/features/agent/image-tools.ts @@ -1,7 +1,7 @@ import { Type } from '@earendil-works/pi-ai' import { prepareImage, readerImagePixels } from '../../core/image-input' import type { getStoredFile } from '../../data/file-store' -import { bind } from './tool' +import { bind, Metered } from './tool' import { LocalToolError } from './tool-types' import type { VisionAnalyzer } from './vision' @@ -25,12 +25,15 @@ export const createImageTools = (loadFile: typeof getStoredFile, analyze: Vision readerImagePixels, signal, ) - const analysis = await analyze( + const { analysis, usage } = await analyze( image, input.question?.trim() || 'Describe this image and transcribe any important visible text.', signal, ) - return { fileId: stored.metadata.id, path: stored.metadata.path, analysis } + return new Metered( + { fileId: stored.metadata.id, path: stored.metadata.path, analysis }, + usage, + ) }, ), ] diff --git a/ui-packages/web/src/features/agent/pdf/tools.ts b/ui-packages/web/src/features/agent/pdf/tools.ts index 4e6988d..430dcf8 100644 --- a/ui-packages/web/src/features/agent/pdf/tools.ts +++ b/ui-packages/web/src/features/agent/pdf/tools.ts @@ -1,6 +1,6 @@ import { Type } from '@earendil-works/pi-ai' import { documentPagePixels, prepareImage } from '../../../core/image-input' -import { bind } from '../tool' +import { bind, Metered } from '../tool' import { LocalToolError } from '../tool-types' import type { VisionAnalyzer } from '../vision' import type { PdfRuntime } from './runtime' @@ -32,13 +32,18 @@ export const createPdfTools = (pdf: PdfRuntime, analyze?: VisionAnalyzer) => { pageNumber: Type.Integer({ minimum: 1 }), question: Type.String({ minLength: 1, maxLength: 2000 }), }), - async (input, signal): Promise => { + async (input, signal) => { if (!input.question.trim()) throw new LocalToolError('Provide a focused question for the PDF page.') const blob = await pdf.renderPage(input.fileId, input.pageNumber, signal) const image = await prepareImage(blob, blob.type, documentPagePixels, signal) - const analysis = await analyze(image, input.question, signal) - return { fileId: input.fileId, pageNumber: input.pageNumber, analysis } + const { analysis, usage } = await analyze(image, input.question, signal) + const result: AnalyzePdfPageResult = { + fileId: input.fileId, + pageNumber: input.pageNumber, + analysis, + } + return new Metered(result, usage) }, ), ] diff --git a/ui-packages/web/src/features/agent/tool.ts b/ui-packages/web/src/features/agent/tool.ts index eb4b6d1..e446038 100644 --- a/ui-packages/web/src/features/agent/tool.ts +++ b/ui-packages/web/src/features/agent/tool.ts @@ -1,5 +1,15 @@ import type { AgentTool } from '@earendil-works/pi-agent-core' -import type { Static, TSchema } from '@earendil-works/pi-ai' +import type { Static, TSchema, Usage } from '@earendil-works/pi-ai' + +// A tool that calls a model itself returns that call's usage beside its result. Pi keeps it on the +// tool result, apart from the context the model sees. +export class Metered { + constructor( + readonly result: unknown, + readonly usage: Usage, + ) {} +} + export const bind =

( name: string, description: string, @@ -13,8 +23,13 @@ export const bind =

( executionMode: 'sequential', execute: async (_id, input, signal) => { signal?.throwIfAborted() - const result = await execute(input, signal) + const output = await execute(input, signal) signal?.throwIfAborted() - return { content: [{ type: 'text', text: JSON.stringify(result) }], details: undefined } + const result = output instanceof Metered ? output.result : output + return { + content: [{ type: 'text', text: JSON.stringify(result) }], + details: undefined, + ...(output instanceof Metered && { usage: output.usage }), + } }, }) diff --git a/ui-packages/web/src/features/agent/vision.ts b/ui-packages/web/src/features/agent/vision.ts index e8c616e..3eaee2b 100644 --- a/ui-packages/web/src/features/agent/vision.ts +++ b/ui-packages/web/src/features/agent/vision.ts @@ -1,4 +1,4 @@ -import { type Api, clampThinkingLevel, type Model } from '@earendil-works/pi-ai' +import { type Api, clampThinkingLevel, type Model, type Usage } from '@earendil-works/pi-ai' import type { PreparedImage } from '../../core/image-input' import { apiKeyFor, models, proxyRequestOptions } from './model-runtime' @@ -8,7 +8,7 @@ export type VisionAnalyzer = ( image: PreparedImage, question: string, signal?: AbortSignal, -) => Promise +) => Promise<{ analysis: string; usage: Usage }> export const createVisionAnalyzer = (model: Model): VisionAnalyzer => { const thinkingLevel = clampThinkingLevel(model, 'off') @@ -43,6 +43,6 @@ export const createVisionAnalyzer = (model: Model): VisionAnalyzer => { .join('') .trim() if (!analysis) throw new Error('The vision model returned no image analysis.') - return analysis + return { analysis, usage: response.usage } } } diff --git a/ui-packages/web/src/features/conversation/composer/index.tsx b/ui-packages/web/src/features/conversation/composer/index.tsx index 2e3a5ec..4ae7d8f 100644 --- a/ui-packages/web/src/features/conversation/composer/index.tsx +++ b/ui-packages/web/src/features/conversation/composer/index.tsx @@ -11,6 +11,7 @@ import { import type { ConversationDraft } from '../../../core/conversations' import { DraftAttachmentTray } from '../conversation-attachments' import { ConversationModelControl, type ModelControlProps } from '../model-control' +import type { ConversationTokenUsage } from '../token-usage' import type { ConversationPhase } from '../use-conversation' import type { DraftAttachment } from '../use-draft-attachments' import { QueuedMessages } from './queued-messages' @@ -22,12 +23,14 @@ import { SlashCommandMenu, } from './slash-command-menu' import styles from './style.module.scss' +import { TokenUsageStatus } from './token-usage-status' type ComposerProps = { modelConfiguration: Pick | null onModelChange: ModelControlProps['onModelChange'] onEffortChange: ModelControlProps['onEffortChange'] onConfigureModels: ModelControlProps['onConfigure'] + tokenUsage: ConversationTokenUsage | null inputRef: RefObject draft: string phase: ConversationPhase @@ -58,6 +61,7 @@ export const ConversationComposer = ({ onModelChange, onEffortChange, onConfigureModels, + tokenUsage, draft, phase, queued, @@ -179,52 +183,81 @@ export const ConversationComposer = ({ }} onDrop={addDroppedFiles} > - - - {menuOpen && ( - - )} -