Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

image-tool-repro.ts

54 lines3.3 KB

1import { toolDefinition } from '@tanstack/ai'
2import { z } from 'zod'
3import type { ContentPart } from '@tanstack/ai'
4​
5/**
6 * Repro for https://github.com/TanStack/ai/issues/363
7 *
8 * `getReproImage` returns a multimodal tool result — an array of `ContentPart`
9 * (a text part + an image part). With multimodal tool results supported, the
10 * OpenAI Responses adapter sends the image as a structured `input_image` in
11 * `function_call_output.output`, so the model can actually see it. Before the
12 * fix every tool result was `JSON.stringify`'d and the model never received the
13 * image.
14 *
15 * The image is a PNG showing a fixed 3-digit secret number ({@link REPRO_SECRET})
16 * rendered as black pixels on white. The number appears nowhere in any text the
17 * tool returns, so a model that can genuinely see the image can read it back and
18 * a model that only received stringified JSON cannot — a clean pass/fail signal
19 * that a blind model cannot fake (1-in-1000 to guess).
20 *
21 * Generated by `scripts/make-repro-image.mjs` (also written to
22 * `public/repro-secret.png` for human comparison). Keep REPRO_SECRET in sync
23 * with the SECRET constant in that script.
24 */
25export const REPRO_SECRET = '473'
26​
27const REPRO_IMAGE_BASE64 =
28 'iVBORw0KGgoAAAANSUhEUgAAAOAAAACACAIAAACdu/LsAAAEIElEQVR4nO2SgWkAMRDDfv+l2wlSaJA5JbEG8FmHv59SxHzTBUr5iw60qOlAi5oOtKjpQIuaDrSo6UCLmg60qOlAi5oOtKjpQIuaDrSo6UCLmg60qOlAi5oOtKjpQIuaDrSo6UCLmg60qOlAi5oOtKjpQIsabKDfPzk9P81r/ZdeWFBYwJaf5rX+Sy8sKCxgy0/zWv+lFxYUFrDlp3mt/9ILCwoL2PLTvNZ/6YUFhQVs+Wle67/0woLCArb8NK/1X3phQWEBW36a1/ovvbCgsIAtP81r/ZdeWFBYwJaf5rX+Sy8sKCxgy0/zWv+lFxYUFrDlp3mt/9ILCwoL2PLTvNZ/6YUFhQVs+Wle67/0woLCArb8NK/1X3phQWEBW36a1/ovvbCgsMDUgyjaf/MuFhQWmHoQRftv3sWCwgJTD6Jo/827WFBYYOpBFO2/eRcLCgtMPYii/TfvYkFhgakHUbT/5l0sKCww9SCK9t+8iwWFBaYeRNH+m3exoLDA1IMo2n/zLhYUFph6EEX7b97FgsICUw+iaP/Nu1hQWGDqQRTtv3kXCwoLTD2Iov0372JBYYGpB1G0/+ZdLCgsMPUgivbfvIsFhQWmHkTR/pt3sSAZlBfla+uTBvPCgmRQXpSvrU8azAsLkkF5Ub62PmkwLyxIBuVF+dr6pMG8sCAZlBfla+uTBvPCgmRQXpSvrU8azAsLkkF5Ub62PmkwLyxIBuVF+dr6pMG8sCAZlBfla+uTBvPCgmRQXpSvrU8azAsLkkF5Ub62PmkwLyxIBuVF+dr6pMG8sCAZlBfla+uTBvPCgmRQXpSvrU8azAsLkkF5Ub62PmkwLyxIBuVF+dr6pMG8qKA0Uw86pU+aKd9jHmcbhK1PminfYx5nG4StT5op32MeZxuErU+aKd9jHmcbhK1PminfYx5nG4StT5op32MeZxuErU+aKd9jHmcbhK1PminfYx5nG4StT5op32MeZxuErU+aKd9jHmcbhK1PminfYx5nG4StT5op32MeZxuErU+aKd9jHmcbhK1PminfYx5nG4StT5op32MeZxuErU+aKd9jHmcbxOl90mBeVFCaqQfd2icN5kUFpZl60K190mBeVFCaqQfd2icN5kUFpZl60K190mBeVFCaqQfd2icN5kUFpZl60K190mBeVFCaqQfd2icN5kUFpZl60K190mBeVFCaqQfd2icN5kUFpZl60K190mBeVFCaqQfd2icN5kUFpZl60K190mBeVFCaqQfd2icN5kUFpZl60K190mBeVFCaqQfd2icN5kUFlZKgAy1qOtCipgMtajrQoqYDLWo60KKmAy1qOtCipgMtajrQoqYDLWo60KKmAy1qOtCipgMtajrQoqYDLWo60KKmAy1qOtCipgMtajrQoqYDLWp+ARIFD+N3ElIHAAAAAElFTkSuQmCC'
29​
30export const getReproImageToolDef = toolDefinition({
31 name: 'getReproImage',
32 description:
33 'Returns an image for the user to inspect. Call this whenever the user asks you to look at, view, or describe the image.',
34 inputSchema: z.object({}),
35})
36​
37// Server implementation: returns a multimodal content-part array, NOT a string.
38export const getReproImage = getReproImageToolDef.server(
39 (): Array<ContentPart> => [
40 {
41 type: 'text',
42 content:
43 'Here is the image you asked to inspect. Read whatever it shows.',
44 },
45 {
46 type: 'image',
47 source: {
48 type: 'data',
49 value: REPRO_IMAGE_BASE64,
50 mimeType: 'image/png',
51 },
52 },
53 ],