mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-11-17 00:20:51 +00:00
feat: charts and tables in AI chat replies and artifacts (#12374)
* feat: render charts and tables in AI chat replies and artifacts * fix: count display blocks when the chat commits a reply * fix: keep display blocks in artifact PDF and HTML exports * chore: update ee-repo-ref to a5d1e052df3e920d3f0b50bdb148c37bf8eec2c4 This commit updates the EE repository reference after PR #978 was merged in windmill-ee-private. Previous ee-repo-ref: 4ee687c09398105fd7ffe1702493ced097ef6359 New ee-repo-ref: a5d1e052df3e920d3f0b50bdb148c37bf8eec2c4 Automated by sync-ee-ref workflow. --------- Co-authored-by: windmill-internal-app[bot] <windmill-internal-app[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
windmill-internal-app[bot]
parent
3e7adc1c4f
commit
6ba400239b
@@ -4080,3 +4080,63 @@
|
||||
skipJudge: true
|
||||
judgeChecklist:
|
||||
- explains the MCP setup from the docs: the MCP URL with a token, added to Claude
|
||||
|
||||
# --- Charts and tables in replies (```wm-display blocks) ---
|
||||
# The chat renders a ```wm-display block holding a chart or table value; anything the catalog
|
||||
# rejects shows as code with an error. Pin that the model reaches for it when a chart is the
|
||||
# answer, that what it writes validates, and that a plain question gets prose.
|
||||
|
||||
- id: global-display-chart-in-reply
|
||||
prompt: |-
|
||||
Our deploys last week were Mon 4, Tue 7, Wed 3, Thu 9, Fri 6. Can you chart that for me?
|
||||
runtime:
|
||||
maxTurns: 6
|
||||
sessionChat: true
|
||||
validate:
|
||||
draftCountExactly: 0
|
||||
toolExpect:
|
||||
forbiddenToolsUsed:
|
||||
- deploy_workspace_item
|
||||
- delete_workspace_item
|
||||
assistantExpect:
|
||||
displayBlocks: required
|
||||
judgeAssistantText: true
|
||||
judgeChecklist:
|
||||
- the reply shows the five days' deploy counts as a chart rather than only as text
|
||||
- the chart carries the values given (Mon 4, Tue 7, Wed 3, Thu 9, Fri 6)
|
||||
- does not create a script or flow just to draw the chart
|
||||
|
||||
- id: global-display-chart-in-artifact
|
||||
prompt: |-
|
||||
Write me a short doc I can share summarizing last month's support tickets by channel:
|
||||
email 120, chat 85, phone 30, other 12. Include a chart.
|
||||
runtime:
|
||||
maxTurns: 8
|
||||
sessionChat: true
|
||||
validate:
|
||||
draftCountExactly: 0
|
||||
toolExpect:
|
||||
requiredToolsUsed:
|
||||
- create_artifact
|
||||
toolCallArgs:
|
||||
- tool: create_artifact
|
||||
field: content
|
||||
stringIncludesAnyOf:
|
||||
- "```wm-display"
|
||||
skipJudge: true
|
||||
judgeChecklist:
|
||||
- creates an artifact whose content includes a chart of the ticket counts by channel
|
||||
|
||||
- id: global-display-not-for-prose
|
||||
prompt: |-
|
||||
What's the difference between a flow and a script in Windmill?
|
||||
runtime:
|
||||
maxTurns: 6
|
||||
sessionChat: true
|
||||
validate:
|
||||
draftCountExactly: 0
|
||||
assistantExpect:
|
||||
displayBlocks: forbidden
|
||||
skipJudge: true
|
||||
judgeChecklist:
|
||||
- answers in prose without a chart or table block
|
||||
|
||||
@@ -311,6 +311,12 @@ export interface AssistantValidationSpec {
|
||||
* assistant say the right thing" expectations to the judge checklist.
|
||||
*/
|
||||
forbiddenMentions?: string[];
|
||||
/**
|
||||
* `required`: the reply holds at least one ```wm-display block, and every one validates
|
||||
* against the chat's display catalog (what the chat renders as a chart or table).
|
||||
* `forbidden`: the reply holds none.
|
||||
*/
|
||||
displayBlocks?: "required" | "forbidden";
|
||||
}
|
||||
|
||||
export interface EvalCase {
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
validateScriptState,
|
||||
validateAssistantExpectations,
|
||||
validateToolExpectations,
|
||||
validateDisplayBlocks,
|
||||
} from "./validators";
|
||||
|
||||
describe("validateScriptState", () => {
|
||||
@@ -1373,3 +1374,19 @@ describe("validateCliWorkspace", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("validateDisplayBlocks", () => {
|
||||
const fence = (value: unknown) => "```wm-display\n" + JSON.stringify(value) + "\n```";
|
||||
const chart = { chart: { type: "bar", data: [{ day: "Mon", n: 4 }], x: "day", y: "n" } };
|
||||
|
||||
it("requires every block to validate against the display catalog", () => {
|
||||
const ok = validateDisplayBlocks(`Here:\n${fence(chart)}`, "required");
|
||||
expect(ok.every((c) => c.passed)).toBe(true);
|
||||
const bad = validateDisplayBlocks(
|
||||
`${fence(chart)}\n${fence({ chart: { ...chart.chart, y: "missing" } })}`,
|
||||
"required"
|
||||
);
|
||||
expect(bad.every((c) => c.passed)).toBe(false);
|
||||
expect(validateDisplayBlocks("plain prose", "required").every((c) => c.passed)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import path from "node:path";
|
||||
import ts from "typescript";
|
||||
import { parseFence } from "../../frontend/src/lib/components/display/displayCatalog";
|
||||
import type {
|
||||
AppValidationSpec,
|
||||
AssistantValidationSpec,
|
||||
@@ -207,9 +208,38 @@ export function validateAssistantExpectations(input: {
|
||||
);
|
||||
}
|
||||
|
||||
if (expect.displayBlocks) {
|
||||
checks.push(...validateDisplayBlocks(text, expect.displayBlocks));
|
||||
}
|
||||
|
||||
return checks;
|
||||
}
|
||||
|
||||
const DISPLAY_BLOCK = /```wm-display[^\n]*\n([\s\S]*?)```/g;
|
||||
|
||||
/** The ```wm-display blocks in `text`, checked with the parser the chat renders them with. */
|
||||
export function validateDisplayBlocks(
|
||||
text: string,
|
||||
mode: "required" | "forbidden"
|
||||
): BenchmarkCheck[] {
|
||||
const blocks = [...text.matchAll(DISPLAY_BLOCK)].map((m) => m[1]);
|
||||
if (mode === "forbidden") {
|
||||
return [check("reply holds no wm-display block", blocks.length === 0, truncateForDetails(text))];
|
||||
}
|
||||
const failures = blocks
|
||||
.map((source, i) => ({ i, parsed: parseFence(source) }))
|
||||
.filter(({ parsed }) => !parsed.ok)
|
||||
.map(({ i, parsed }) => `block ${i + 1}: ${parsed.ok ? "" : parsed.error}`);
|
||||
return [
|
||||
check("reply holds a wm-display block", blocks.length > 0, truncateForDetails(text)),
|
||||
check(
|
||||
"every wm-display block validates against the display catalog",
|
||||
blocks.length > 0 && failures.length === 0,
|
||||
failures.join("\n") || undefined
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
export function validateToolExpectations(input: {
|
||||
run: ModeRunOutput<unknown>;
|
||||
toolExpect?: ToolValidationSpec;
|
||||
|
||||
@@ -1 +1 @@
|
||||
bf04cefe64776cb7262f041105b6ceca2953d5f4
|
||||
a5d1e052df3e920d3f0b50bdb148c37bf8eec2c4
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -216,7 +216,7 @@
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"subject": {
|
||||
"description": "What to get authoring instructions for: a workspace item type (script, flow, resource, app), \"agent\" for a saved AI agent (an `ai_agent` resource, as the Agents page lists it), \"pipeline\" for building a data pipeline (a DAG of annotated scripts wired by storage assets — NOT a flow), or \"datatable\" for how data tables work (schema changes and migrations, roles and permissions) plus the wmill.datatable() SQL SDK used inside runnables, or \"workspace_settings\" for the keys and rules of the workspace settings (admins only). Schedules, triggers, and variables don't need instructions — their tool schemas describe everything.",
|
||||
"description": "What to get authoring instructions for: a workspace item type (script, flow, resource, app), \"agent\" for a saved AI agent (an `ai_agent` resource, as the Agents page lists it), \"pipeline\" for building a data pipeline (a DAG of annotated scripts wired by storage assets — NOT a flow), or \"datatable\" for how data tables work (schema changes and migrations, roles and permissions) plus the wmill.datatable() SQL SDK used inside runnables, \"workspace_settings\" for the keys and rules of the workspace settings (admins only), or \"display\" for the tables and charts a return value renders as. Schedules, triggers, and variables don't need instructions — their tool schemas describe everything.",
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"script",
|
||||
@@ -226,7 +226,8 @@
|
||||
"agent",
|
||||
"datatable",
|
||||
"pipeline",
|
||||
"workspace_settings"
|
||||
"workspace_settings",
|
||||
"display"
|
||||
]
|
||||
},
|
||||
"language": {
|
||||
|
||||
Generated
+144
File diff suppressed because one or more lines are too long
@@ -4,7 +4,7 @@
|
||||
anonymous usage-stats payload. It answers "does anyone use this, and which variant do they pick"
|
||||
without any identifying data leaving the instance.
|
||||
|
||||
It currently carries 100 registered actions across forty features (`access_view`, `ai_session`, `ai_chat`,
|
||||
It currently carries 102 registered actions across forty features (`access_view`, `ai_session`, `ai_chat`,
|
||||
`ai_fix`, `ai_agent`, `ai_decision`, `ai_agent_eval`, `app_sandbox`, `datatable`, `db_manager`, `flow_editor`,
|
||||
`flow_run`, `flow_step`, `home`, `header_nav`, `run_form`, `sharing`, `debugger`, `trigger`,
|
||||
`command_script`, `mcp`, `mcp_workspace`, `mcp_external_client`, `hub_script`, `usage_meter`, `sso_groups_claim`,
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
"package": "svelte-package -o package && node scripts/package-system-prompts.js && node scripts/fix-svelte-module-imports.js",
|
||||
"generate-backend-client": "openapi-ts --input ../backend/windmill-api/openapi.yaml --output ./src/lib/gen --useOptions --enums javascript --format false",
|
||||
"generate-backend-client-mac": "openapi-ts --input ../backend/windmill-api/openapi.yaml --output ./src/lib/gen --useOptions --enums javascript",
|
||||
"gen:display-catalog": "WM_GEN_DISPLAY_CATALOG=1 vitest run --project server src/lib/components/display/displayCatalogGen.test.ts",
|
||||
"gen:mcp-session": "WM_GEN_MCP_SESSION=1 vitest run --project server src/lib/components/copilot/chat/global/mcpSessionGen.test.ts",
|
||||
"pretest": "tsc --incremental -p tests/tsconfig.json",
|
||||
"filter-classes": "node filterTailwindClasses.js",
|
||||
|
||||
@@ -38,6 +38,8 @@
|
||||
import ToggleButtonGroup from './common/toggleButton-v2/ToggleButtonGroup.svelte'
|
||||
import ToggleButton from './common/toggleButton-v2/ToggleButton.svelte'
|
||||
import MapResult from './MapResult.svelte'
|
||||
import ChartResult from './display/ChartResult.svelte'
|
||||
import { isChartResult } from './display/displayCatalog'
|
||||
import DownloadCsv from './table/DownloadCsv.svelte'
|
||||
import { convertJsonToCsv } from './table/tableUtils'
|
||||
import Tooltip from './Tooltip.svelte'
|
||||
@@ -89,6 +91,7 @@
|
||||
| 'nondisplayable'
|
||||
| 'pdf'
|
||||
| 'aiagent'
|
||||
| 'chart'
|
||||
| undefined
|
||||
let resultKind: ResultKind = $state()
|
||||
/** Kinds whose renderer leaves the page: S3/ducklake previews fetch the file or
|
||||
@@ -378,7 +381,11 @@
|
||||
}
|
||||
|
||||
if (keys.length != 0) {
|
||||
if (
|
||||
// Only a chart the catalog accepts: any other `{ chart }` result stays JSON. The
|
||||
// catalog caps its rows, so it is exempt from the display size limit.
|
||||
if (keys.length == 1 && keys[0] === 'chart' && isChartResult(result)) {
|
||||
return 'chart'
|
||||
} else if (
|
||||
keys.length == 1 &&
|
||||
[...IMG_KINDS, 'html', 'map', 'pdf', 'file', 'error'].includes(keys[0])
|
||||
) {
|
||||
@@ -907,6 +914,8 @@
|
||||
{@html renderResultMarkup(result.html)}
|
||||
{/if}
|
||||
</div>
|
||||
{:else if !forceJson && resultKind === 'chart'}
|
||||
<ChartResult chart={result.chart} />
|
||||
{:else if !forceJson && resultKind === 'map'}
|
||||
<div class="h-full" data-interactive>
|
||||
<MapResult
|
||||
|
||||
@@ -30,13 +30,20 @@
|
||||
// markdown surfaces don't execute mermaid — the chat opts in.
|
||||
renderMermaid = false,
|
||||
// Draw diagrams in the light theme whatever the app's theme (printing).
|
||||
lightMermaid = false
|
||||
lightMermaid = false,
|
||||
// Render ```wm-display blocks (charts, tables) through DisplayResult. The fence only
|
||||
// admits inert kinds (`fenceSchema`), so any surface may opt in.
|
||||
renderDisplay = false,
|
||||
// Draw display blocks for paper: plain tables rather than the paginated grid.
|
||||
staticDisplay = false
|
||||
}: {
|
||||
showApplyButton?: boolean
|
||||
onApplyCode?: () => void
|
||||
applyButtonIcon?: typeof ClipboardCopy
|
||||
renderMermaid?: boolean
|
||||
lightMermaid?: boolean
|
||||
renderDisplay?: boolean
|
||||
staticDisplay?: boolean
|
||||
} = $props()
|
||||
|
||||
const astNode = getAstNode()
|
||||
@@ -124,7 +131,10 @@
|
||||
)
|
||||
|
||||
let language = $derived(
|
||||
(astNode.current.children?.[0]?.properties?.class as string | undefined)?.split('-')[1]
|
||||
(astNode.current.children?.[0]?.properties?.class as string | undefined)
|
||||
?.split(' ')
|
||||
.find((c) => c.startsWith('language-'))
|
||||
?.slice('language-'.length)
|
||||
)
|
||||
|
||||
let smartLang = $derived(getSmartLang(language))
|
||||
@@ -135,6 +145,14 @@
|
||||
<div class="relative w-full bg-surface-secondary rounded-md overflow-hidden">
|
||||
{#if renderMermaid && language === 'mermaid'}
|
||||
<MermaidDisplay code={code ?? ''} forceLight={lightMermaid} />
|
||||
{:else if renderDisplay && language === 'wm-display'}
|
||||
<!-- Keep this import lazy: DisplayResult's imports reach back into the chat, and a static
|
||||
import here left Home's session composer stuck on its loading placeholder. -->
|
||||
{#await import('./display/DisplayFence.svelte')}
|
||||
<div data-display-pending="true"></div>
|
||||
{:then { default: DisplayFence }}
|
||||
<DisplayFence code={code ?? ''} staticRender={staticDisplay} />
|
||||
{/await}
|
||||
{:else}
|
||||
<HighlightCode
|
||||
className="px-3 py-2.5 !text-xs"
|
||||
|
||||
@@ -59,6 +59,7 @@ import { prepareNavigatorUserMessage } from './navigator/core'
|
||||
import { sendUserToast } from '$lib/toast'
|
||||
import { AI_PROVIDERS, workspaceAIClients, getStreamedCompletionText } from '../lib'
|
||||
import { logFeatureUsage } from '$lib/utils/featureUsage'
|
||||
import { logDisplayBlocks } from '$lib/components/display/displayTelemetry'
|
||||
import { modelSupportsVision } from '../modelConfig'
|
||||
import { getEffectiveModelContextWindow } from '../modelConfig'
|
||||
import {
|
||||
@@ -4617,6 +4618,7 @@ export class AIChatManager implements ChatViewHost {
|
||||
// deduped in commitInterruptedTurn.
|
||||
if (this.currentReply) {
|
||||
partialReply = this.currentReply
|
||||
logDisplayBlocks(this.currentReply)
|
||||
}
|
||||
if (this.currentReply || this.currentReasoning) {
|
||||
this.displayMessages = [
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
import MarkdownCodeBlock from '$lib/components/MarkdownCodeBlock.svelte'
|
||||
</script>
|
||||
|
||||
<!-- The viewer's code block without the chat context its own renderer needs. Mermaid is on as
|
||||
in the viewer: only the session's own artifacts are exported. -->
|
||||
<MarkdownCodeBlock renderMermaid lightMermaid />
|
||||
<!-- The viewer's code block without the chat context its own renderer needs. Mermaid and display
|
||||
blocks are on as in the viewer: only the session's own artifacts are exported. -->
|
||||
<MarkdownCodeBlock renderMermaid lightMermaid renderDisplay staticDisplay />
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<script lang="ts">
|
||||
import MarkdownCodeBlock from '$lib/components/MarkdownCodeBlock.svelte'
|
||||
</script>
|
||||
|
||||
<!-- A `pre` renderer for untrusted artifact markdown: ```wm-display fences render (the fence
|
||||
admits only inert kinds), mermaid stays source. -->
|
||||
<MarkdownCodeBlock renderDisplay />
|
||||
@@ -2,5 +2,6 @@
|
||||
import MarkdownCodeBlock from '$lib/components/MarkdownCodeBlock.svelte'
|
||||
</script>
|
||||
|
||||
<!-- A `pre` renderer for ArtifactBody: the shared code block, with ```mermaid fences drawn. -->
|
||||
<MarkdownCodeBlock renderMermaid />
|
||||
<!-- A `pre` renderer for ArtifactBody: the shared code block, with ```mermaid fences drawn and
|
||||
```wm-display fences rendered. -->
|
||||
<MarkdownCodeBlock renderMermaid renderDisplay />
|
||||
|
||||
@@ -216,8 +216,19 @@ async function drawDiagrams(html: string): Promise<string> {
|
||||
return template.innerHTML
|
||||
}
|
||||
|
||||
/** `html` with its ```wm-display fences drawn as images and tables. */
|
||||
async function drawDisplays(html: string): Promise<string> {
|
||||
if (!html.includes('language-wm-display')) return html
|
||||
// Built in the template's inert document, like the diagrams.
|
||||
const template = document.createElement('template')
|
||||
template.innerHTML = html
|
||||
const { drawDisplayFences } = await import('$lib/components/display/displayExport')
|
||||
await drawDisplayFences(template.content)
|
||||
return template.innerHTML
|
||||
}
|
||||
|
||||
/** The page a download gets: `artifactHtmlDocument` with its diagrams drawn and fonts embedded. */
|
||||
export async function artifactHtmlFile(name: string, content: string): Promise<string> {
|
||||
const html = await drawDiagrams(renderArtifactHtml(content))
|
||||
const html = await drawDisplays(await drawDiagrams(renderArtifactHtml(content)))
|
||||
return htmlPage(name, html, { fontCss: await artifactFontCss(html) })
|
||||
}
|
||||
|
||||
@@ -120,12 +120,13 @@ async function imagesSettled(doc: Document): Promise<void> {
|
||||
await Promise.race([Promise.all(pending), new Promise((resolve) => setTimeout(resolve, 10_000))])
|
||||
}
|
||||
|
||||
/** Mermaid draws asynchronously; printing before it lands puts the diagram source in the PDF.
|
||||
* A diagram that fails to parse settles too (as its source), so this waits only on renders
|
||||
* still in flight; the cap covers one that never returns. */
|
||||
/** Mermaid draws asynchronously, and display blocks load their renderer on demand; printing
|
||||
* before they land puts their source in the PDF. One that fails to parse settles too (as its
|
||||
* source), so this waits only on renders still in flight; the cap covers one that never returns. */
|
||||
async function diagramsSettled(doc: Document): Promise<void> {
|
||||
const deadline = Date.now() + 10_000
|
||||
while (doc.querySelector('[data-mermaid-pending="true"]') && Date.now() < deadline) {
|
||||
const pending = '[data-mermaid-pending="true"], [data-display-pending="true"]'
|
||||
while (doc.querySelector(pending) && Date.now() < deadline) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 100))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +94,8 @@ import {
|
||||
getRawAppPrompt,
|
||||
getResourcePrompt,
|
||||
getAgentPrompt,
|
||||
getScriptPrompt
|
||||
getScriptPrompt,
|
||||
getDisplayPrompt
|
||||
} from '$system_prompts'
|
||||
import type {
|
||||
ChatCompletionSystemMessageParam,
|
||||
@@ -283,7 +284,14 @@ const INSTRUCTION_SUBJECTS = [
|
||||
// annotated scripts in a folder, so it gets authoring guidance, not a CRUD type.
|
||||
// `workspace_settings` is the settings document an admin reads and changes.
|
||||
// `agent` is a resource of type `ai_agent`, with authoring guidance of its own.
|
||||
const INSTRUCTION_SUBJECTS_EXTRA = ['agent', 'datatable', 'pipeline', 'workspace_settings'] as const
|
||||
// `display` is the rich-result format: tables and charts a return value renders as.
|
||||
const INSTRUCTION_SUBJECTS_EXTRA = [
|
||||
'agent',
|
||||
'datatable',
|
||||
'pipeline',
|
||||
'workspace_settings',
|
||||
'display'
|
||||
] as const
|
||||
export const ALL_INSTRUCTION_SUBJECTS = [
|
||||
...INSTRUCTION_SUBJECTS,
|
||||
...INSTRUCTION_SUBJECTS_EXTRA
|
||||
@@ -354,7 +362,7 @@ const scriptLangSchema = z.enum($ScriptLang.enum)
|
||||
|
||||
const getInstructionsSchema = z.object({
|
||||
subject: instructionSubjectSchema.describe(
|
||||
'What to get authoring instructions for: a workspace item type (script, flow, resource, app), "agent" for a saved AI agent (an `ai_agent` resource, as the Agents page lists it), "pipeline" for building a data pipeline (a DAG of annotated scripts wired by storage assets — NOT a flow), or "datatable" for how data tables work (schema changes and migrations, roles and permissions) plus the wmill.datatable() SQL SDK used inside runnables, or "workspace_settings" for the keys and rules of the workspace settings (admins only). Schedules, triggers, and variables don\'t need instructions — their tool schemas describe everything.'
|
||||
'What to get authoring instructions for: a workspace item type (script, flow, resource, app), "agent" for a saved AI agent (an `ai_agent` resource, as the Agents page lists it), "pipeline" for building a data pipeline (a DAG of annotated scripts wired by storage assets — NOT a flow), or "datatable" for how data tables work (schema changes and migrations, roles and permissions) plus the wmill.datatable() SQL SDK used inside runnables, "workspace_settings" for the keys and rules of the workspace settings (admins only), or "display" for the tables and charts a return value renders as. Schedules, triggers, and variables don\'t need instructions — their tool schemas describe everything.'
|
||||
),
|
||||
language: scriptLangSchema
|
||||
.optional()
|
||||
@@ -1781,6 +1789,7 @@ ${
|
||||
Rules:${draftsOnlyRule}
|
||||
${findItemsRule}
|
||||
${groupMembershipRule}- Listings and reads return only what this user can see: items in folders they cannot read are hidden from you both. When an item the user names is missing, say it is not among what they can see and may be in a folder they cannot read, where someone with access can run it — never state that it does not exist or is not deployed.
|
||||
- To show a chart or a sortable table in a reply or an artifact, write a \`\`\`wm-display fenced block holding one JSON value in the chart or table format of get_instructions with subject "display" (read it first). Reach for it when a chart answers the question better than prose or a markdown table; keep small tables as markdown.
|
||||
- An agent, as the Agents page lists it, is a resource of type \`ai_agent\`: read it with read_workspace_item type "resource".${when(
|
||||
// An agent draft is a resource draft that the server checks under the flow builder right.
|
||||
canDraftResources && canWriteFlowDraft,
|
||||
@@ -2314,6 +2323,8 @@ export function getInstructions(
|
||||
return getPipelineInstructions()
|
||||
case 'workspace_settings':
|
||||
return getWorkspaceSettingsPrompt()
|
||||
case 'display':
|
||||
return getDisplayPrompt()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,4 +37,5 @@
|
||||
onApplyCode={handleApplyCode}
|
||||
applyButtonIcon={aiChatManager.pendingNewCode ? Check : Play}
|
||||
renderMermaid={!chatHost.untrustedTranscript}
|
||||
renderDisplay
|
||||
/>
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
<script lang="ts">
|
||||
import { Chart } from '$lib/components/chartjs-wrappers/chartJs'
|
||||
import { useIsDarkMode } from '$lib/components/DarkModeObserver.svelte'
|
||||
import type { ChartBody } from './displayCatalog'
|
||||
import { chartConfig } from './chartConfig'
|
||||
|
||||
let { chart }: { chart: ChartBody } = $props()
|
||||
|
||||
const isDarkMode = useIsDarkMode()
|
||||
|
||||
let container: HTMLDivElement | undefined = $state()
|
||||
// Read from the document the chart is in, not the app's: a print frame is always light.
|
||||
let theme = $state({ dark: false, surface: '#ffffff' })
|
||||
$effect(() => {
|
||||
void isDarkMode.val
|
||||
if (!container) return
|
||||
const dark = container.ownerDocument.documentElement.classList.contains('dark')
|
||||
// The gap between adjacent fills is a border in the colour of whatever surface the chart
|
||||
// sits on, which differs between run pages, chat, artifacts and print.
|
||||
let surface = dark ? '#1a1a19' : '#ffffff'
|
||||
for (let el: Element | null = container; el; el = el.parentElement) {
|
||||
const bg = getComputedStyle(el).backgroundColor
|
||||
if (bg && bg !== 'transparent' && !/rgba\(.*,\s*0\)$/.test(bg)) {
|
||||
surface = bg
|
||||
break
|
||||
}
|
||||
}
|
||||
theme = { dark, surface }
|
||||
})
|
||||
|
||||
let config = $derived(chartConfig(chart, theme))
|
||||
</script>
|
||||
|
||||
<div
|
||||
bind:this={container}
|
||||
class="relative w-full h-72 p-2"
|
||||
data-interactive
|
||||
data-chart-type={chart.type}
|
||||
>
|
||||
<Chart type={config.type} data={config.data} options={config.options ?? {}} />
|
||||
</div>
|
||||
@@ -0,0 +1,85 @@
|
||||
<script lang="ts">
|
||||
import { onDestroy } from 'svelte'
|
||||
import DisplayResult from '$lib/components/DisplayResult.svelte'
|
||||
import HighlightCode from '$lib/components/HighlightCode.svelte'
|
||||
import { Alert, Skeleton } from '$lib/components/common'
|
||||
import { json } from 'svelte-highlight/languages'
|
||||
import ChartResult from './ChartResult.svelte'
|
||||
import { fenceItems, parseFence, tableRows, type FenceParse } from './displayCatalog'
|
||||
|
||||
const STREAM_QUIET_MS = 300
|
||||
|
||||
let {
|
||||
code,
|
||||
staticRender = false
|
||||
}: {
|
||||
code: string
|
||||
/** Plain tables instead of the interactive grid, for print: a paginated grid on paper
|
||||
* keeps only its first page of rows. */
|
||||
staticRender?: boolean
|
||||
} = $props()
|
||||
|
||||
let parsed: FenceParse = $derived(parseFence(code))
|
||||
// True until the source has gone quiet: a block still streaming in shows a placeholder,
|
||||
// not the parse error of its half-written JSON.
|
||||
let streaming = $state(true)
|
||||
let quietTimer: ReturnType<typeof setTimeout> | undefined
|
||||
|
||||
$effect(() => {
|
||||
void code
|
||||
streaming = true
|
||||
clearTimeout(quietTimer)
|
||||
quietTimer = setTimeout(() => (streaming = false), STREAM_QUIET_MS)
|
||||
})
|
||||
|
||||
onDestroy(() => clearTimeout(quietTimer))
|
||||
</script>
|
||||
|
||||
{#if parsed.ok && staticRender}
|
||||
<div class="flex flex-col gap-2" data-wm-display="ok">
|
||||
{#each fenceItems(parsed.result) as item, i (i)}
|
||||
{#if 'chart' in item}
|
||||
<ChartResult chart={item.chart} />
|
||||
{:else}
|
||||
{@const table = tableRows(item)}
|
||||
<!-- A plain table on purpose: the document's own styles lay it out for paper. -->
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
{#each table.columns as column, c (c)}<th>{column}</th>{/each}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{#each table.rows as row, r (r)}
|
||||
<tr
|
||||
>{#each row as cell, c (c)}<td>{cell ?? ''}</td>{/each}</tr
|
||||
>
|
||||
{/each}
|
||||
</tbody>
|
||||
</table>
|
||||
{/if}
|
||||
{/each}
|
||||
</div>
|
||||
{:else if parsed.ok}
|
||||
<div class="bg-surface p-1" data-wm-display="ok">
|
||||
<DisplayResult result={parsed.result} noControls hideAsJson disableExpand />
|
||||
</div>
|
||||
{:else if streaming}
|
||||
<div class="p-2" data-wm-display="pending" data-display-pending="true">
|
||||
<Skeleton layout={[[12]]} />
|
||||
</div>
|
||||
{:else}
|
||||
<div class="flex flex-col" data-wm-display="invalid">
|
||||
<Alert type="warning" size="xs" title="This display block could not be rendered" notRounded>
|
||||
<pre class="whitespace-pre-wrap text-2xs">{parsed.error}</pre>
|
||||
</Alert>
|
||||
<HighlightCode
|
||||
className="px-3 py-2.5 !text-xs"
|
||||
{code}
|
||||
highlightLanguage={json}
|
||||
language={undefined}
|
||||
buttonsOnHover
|
||||
customScrollbarX
|
||||
/>
|
||||
</div>
|
||||
{/if}
|
||||
@@ -0,0 +1,176 @@
|
||||
import type { ChartConfiguration } from 'chart.js'
|
||||
import type { ChartBody } from './displayCatalog'
|
||||
|
||||
// Categorical slots in fixed order, stepped separately for each surface (validated for
|
||||
// colour-vision deficiency). Never cycled: the catalog caps series and pie slices to their count.
|
||||
const LIGHT = [
|
||||
'#2a78d6',
|
||||
'#eb6834',
|
||||
'#1baf7a',
|
||||
'#eda100',
|
||||
'#e87ba4',
|
||||
'#008300',
|
||||
'#6250d6',
|
||||
'#e34948'
|
||||
]
|
||||
const DARK = [
|
||||
'#3987e5',
|
||||
'#d95926',
|
||||
'#199e70',
|
||||
'#c98500',
|
||||
'#d55181',
|
||||
'#008300',
|
||||
'#9085e9',
|
||||
'#e66767'
|
||||
]
|
||||
|
||||
export interface ChartTheme {
|
||||
dark: boolean
|
||||
/** The colour behind the chart: gaps between adjacent fills are borders drawn in it. */
|
||||
surface: string
|
||||
}
|
||||
|
||||
function num(v: unknown): number | null {
|
||||
return typeof v === 'number' ? v : null
|
||||
}
|
||||
|
||||
/** The chart.js configuration a catalog chart draws with, on screen and in exports alike. */
|
||||
export function chartConfig(chart: ChartBody, { dark, surface }: ChartTheme): ChartConfiguration {
|
||||
const palette = dark ? DARK : LIGHT
|
||||
const ink = dark ? '#c3c2b7' : '#55544f'
|
||||
const grid = dark ? 'rgba(255,255,255,0.08)' : 'rgba(0,0,0,0.07)'
|
||||
const ys = typeof chart.y === 'string' ? [chart.y] : chart.y
|
||||
const stacked = !!chart.stacked
|
||||
const labels = chart.data.map((r) => String(r[chart.x] ?? ''))
|
||||
|
||||
const axis = (label: string | undefined, stack: boolean) => ({
|
||||
stacked: stack,
|
||||
title: { display: !!label, text: label ?? '', color: ink },
|
||||
ticks: { color: ink },
|
||||
grid: { color: grid },
|
||||
border: { color: grid }
|
||||
})
|
||||
const common = {
|
||||
responsive: true,
|
||||
maintainAspectRatio: false,
|
||||
animation: false as const,
|
||||
plugins: {
|
||||
legend: {
|
||||
display: chart.type === 'pie' || ys.length > 1,
|
||||
position: 'bottom' as const,
|
||||
labels: { color: ink, boxWidth: 10, boxHeight: 10 }
|
||||
},
|
||||
title: {
|
||||
display: !!chart.title,
|
||||
text: chart.title ?? '',
|
||||
color: ink,
|
||||
align: 'start' as const,
|
||||
font: { size: 13, weight: 600 as const }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch (chart.type) {
|
||||
case 'bar':
|
||||
return {
|
||||
type: 'bar',
|
||||
data: {
|
||||
labels,
|
||||
datasets: ys.map((y, i) => ({
|
||||
label: y,
|
||||
data: chart.data.map((r) => num(r[y])),
|
||||
backgroundColor: palette[i],
|
||||
borderRadius: 4,
|
||||
borderSkipped: stacked ? false : ('start' as const),
|
||||
borderColor: surface,
|
||||
borderWidth: stacked ? { top: 2 } : 0,
|
||||
maxBarThickness: 48
|
||||
}))
|
||||
},
|
||||
options: {
|
||||
...common,
|
||||
indexAxis: chart.horizontal ? 'y' : 'x',
|
||||
scales: chart.horizontal
|
||||
? { y: axis(chart.x_label, stacked), x: axis(chart.y_label, stacked) }
|
||||
: { x: axis(chart.x_label, stacked), y: axis(chart.y_label, stacked) }
|
||||
}
|
||||
}
|
||||
case 'line':
|
||||
return {
|
||||
type: 'line',
|
||||
data: {
|
||||
labels,
|
||||
datasets: ys.map((y, i) => ({
|
||||
label: y,
|
||||
data: chart.data.map((r) => num(r[y])),
|
||||
borderColor: palette[i],
|
||||
backgroundColor: palette[i],
|
||||
borderWidth: 2,
|
||||
pointRadius: chart.data.length > 60 ? 0 : 3,
|
||||
pointHoverRadius: 5,
|
||||
fill: stacked ? 'stack' : false,
|
||||
tension: 0
|
||||
}))
|
||||
},
|
||||
options: {
|
||||
...common,
|
||||
interaction: { mode: 'index', intersect: false },
|
||||
scales: { x: axis(chart.x_label, false), y: axis(chart.y_label, stacked) }
|
||||
}
|
||||
}
|
||||
case 'scatter':
|
||||
return {
|
||||
type: 'scatter',
|
||||
data: {
|
||||
datasets: ys.map((y, i) => ({
|
||||
label: y,
|
||||
data: chart.data
|
||||
.map((r) => ({ x: num(r[chart.x]), y: num(r[y]) }))
|
||||
.filter((p): p is { x: number; y: number } => p.x !== null && p.y !== null),
|
||||
backgroundColor: palette[i],
|
||||
borderColor: surface,
|
||||
borderWidth: 2,
|
||||
pointRadius: 5,
|
||||
pointHoverRadius: 7
|
||||
}))
|
||||
},
|
||||
options: {
|
||||
...common,
|
||||
scales: { x: axis(chart.x_label, false), y: axis(chart.y_label, false) }
|
||||
}
|
||||
}
|
||||
case 'pie': {
|
||||
// A pie keeps its largest slices and folds the rest into one, so no slice reuses a hue.
|
||||
let slices: { label: string; value: number; other?: boolean }[] = chart.data.map((r) => ({
|
||||
label: String(r[chart.x] ?? ''),
|
||||
value: num(r[ys[0]]) ?? 0
|
||||
}))
|
||||
if (slices.length > palette.length) {
|
||||
const sorted = [...slices].sort((a, b) => b.value - a.value)
|
||||
const rest = sorted.slice(palette.length - 1).reduce((s, x) => s + x.value, 0)
|
||||
slices = [
|
||||
...sorted.slice(0, palette.length - 1),
|
||||
{ label: 'Other', value: rest, other: true }
|
||||
]
|
||||
}
|
||||
return {
|
||||
type: 'pie',
|
||||
data: {
|
||||
labels: slices.map((s) => s.label),
|
||||
datasets: [
|
||||
{
|
||||
label: ys[0],
|
||||
data: slices.map((s) => s.value),
|
||||
backgroundColor: slices.map((s, i) =>
|
||||
s.other ? (dark ? '#6f6e68' : '#9b9a93') : palette[i]
|
||||
),
|
||||
borderColor: surface,
|
||||
borderWidth: 2
|
||||
}
|
||||
]
|
||||
},
|
||||
options: common
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { isChartResult, parseFence } from './displayCatalog'
|
||||
|
||||
const rows = [
|
||||
{ status: 'success', runs: 12 },
|
||||
{ status: 'failure', runs: 3 }
|
||||
]
|
||||
|
||||
describe('display catalog', () => {
|
||||
it('accepts a chart and reports a missing column by name', () => {
|
||||
expect(
|
||||
parseFence(JSON.stringify({ chart: { type: 'bar', data: rows, x: 'status', y: 'runs' } })).ok
|
||||
).toBe(true)
|
||||
const bad = parseFence(
|
||||
JSON.stringify({ chart: { type: 'bar', data: rows, x: 'status', y: 'count' } })
|
||||
)
|
||||
expect(bad.ok).toBe(false)
|
||||
expect(!bad.ok && bad.error).toContain('column "count" is not in any row of data')
|
||||
const pie = parseFence(
|
||||
JSON.stringify({ chart: { type: 'pie', data: rows, x: 'status', y: ['runs', 'status'] } })
|
||||
)
|
||||
expect(!pie.ok && pie.error).toContain('a pie chart takes exactly one y column')
|
||||
expect(!pie.ok && pie.error).toContain('y column "status" must hold numbers')
|
||||
})
|
||||
|
||||
// The fence renders on the public shared-session page, which must issue no request.
|
||||
it('rejects kinds that fetch or run markup, alone or inside render_all', () => {
|
||||
for (const item of [
|
||||
{ html: '<img src="https://example.com/x.png">' },
|
||||
{ markdown: '' },
|
||||
{ s3: 'a/b.png' },
|
||||
{ map: { lat: 0, lon: 0 } }
|
||||
]) {
|
||||
expect(parseFence(JSON.stringify(item)).ok).toBe(false)
|
||||
expect(parseFence(JSON.stringify({ render_all: [item] })).ok).toBe(false)
|
||||
}
|
||||
})
|
||||
|
||||
it('does not classify an arbitrary `chart` result as a chart', () => {
|
||||
expect(isChartResult({ chart: 'https://example.com/chart.png' })).toBe(false)
|
||||
expect(isChartResult({ chart: { type: 'bar', data: rows, x: 'status', y: 'runs' } })).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,208 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
/**
|
||||
* The rich-result kinds a ```wm-display fence may hold, and the `chart` kind a script result may
|
||||
* take. The schemas validate what the model writes, gate `chart` in `DisplayResult`, and are
|
||||
* exported as JSON Schema into the model's guidance (`displayCatalog.gen.test.ts`).
|
||||
*
|
||||
* The fence only admits kinds that issue no request and run no markup: it renders on the public
|
||||
* shared-session page, which promises to fetch nothing. Do not add `html`, `svg`, images, `map`,
|
||||
* `markdown` (its images fetch), `file`, `s3object`, `approval` or `pdf` to `fenceSchema`.
|
||||
*/
|
||||
|
||||
export const MAX_CHART_ROWS = 5000
|
||||
/** One per categorical palette slot in `ChartResult`: a ninth series would need a cycled hue. */
|
||||
export const MAX_CHART_SERIES = 8
|
||||
export const MAX_TABLE_ROWS = 5000
|
||||
|
||||
const cell = z.union([z.string(), z.number(), z.boolean(), z.null()])
|
||||
|
||||
const row = z
|
||||
.record(z.string(), cell)
|
||||
.describe('One row: column name to value (string, number, boolean or null)')
|
||||
|
||||
export const chartBodySchema = z
|
||||
.strictObject({
|
||||
type: z
|
||||
.enum(['bar', 'line', 'pie', 'scatter'])
|
||||
.describe(
|
||||
'bar or line for categories or time, pie for parts of a whole, scatter for two numeric columns'
|
||||
),
|
||||
title: z.string().optional(),
|
||||
data: z
|
||||
.array(row)
|
||||
.min(1)
|
||||
.max(MAX_CHART_ROWS)
|
||||
.describe('The rows to plot, the same shape as a table-row-object result'),
|
||||
x: z
|
||||
.string()
|
||||
.describe(
|
||||
'Column holding the category, label or date of each row (bar, line, pie), or the numeric x value (scatter)'
|
||||
),
|
||||
y: z
|
||||
.union([z.string(), z.array(z.string()).min(1).max(MAX_CHART_SERIES)])
|
||||
.describe('Numeric column, or columns, to plot: one series each. A pie takes exactly one'),
|
||||
stacked: z.boolean().optional().describe('bar and line: stack the series'),
|
||||
horizontal: z.boolean().optional().describe('bar: draw the bars horizontally'),
|
||||
x_label: z.string().optional(),
|
||||
y_label: z.string().optional()
|
||||
})
|
||||
.superRefine((chart, ctx) => {
|
||||
const ys = typeof chart.y === 'string' ? [chart.y] : chart.y
|
||||
if (chart.type === 'pie' && ys.length !== 1) {
|
||||
ctx.addIssue({
|
||||
code: 'custom',
|
||||
path: ['y'],
|
||||
message: 'a pie chart takes exactly one y column'
|
||||
})
|
||||
}
|
||||
const columns = new Set(chart.data.flatMap((r) => Object.keys(r)))
|
||||
for (const [key, column] of [['x', chart.x], ...ys.map((c) => ['y', c] as const)] as const) {
|
||||
if (!columns.has(column)) {
|
||||
ctx.addIssue({
|
||||
code: 'custom',
|
||||
path: [key],
|
||||
message: `column "${column}" is not in any row of data`
|
||||
})
|
||||
}
|
||||
}
|
||||
for (const column of ys) {
|
||||
const bad = chart.data.findIndex(
|
||||
(r) => r[column] !== undefined && r[column] !== null && typeof r[column] !== 'number'
|
||||
)
|
||||
if (bad !== -1) {
|
||||
ctx.addIssue({
|
||||
code: 'custom',
|
||||
path: ['data', bad, column],
|
||||
message: `y column "${column}" must hold numbers`
|
||||
})
|
||||
}
|
||||
}
|
||||
if (chart.type === 'scatter') {
|
||||
const bad = chart.data.findIndex((r) => typeof r[chart.x] !== 'number')
|
||||
if (bad !== -1) {
|
||||
ctx.addIssue({
|
||||
code: 'custom',
|
||||
path: ['data', bad, chart.x],
|
||||
message: `a scatter chart's x column "${chart.x}" must hold numbers`
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
export type ChartBody = z.infer<typeof chartBodySchema>
|
||||
|
||||
export const chartSchema = z.strictObject({ chart: chartBodySchema })
|
||||
|
||||
const tableRowObjectSchema = z.strictObject({
|
||||
'table-row-object': z
|
||||
.array(row)
|
||||
.min(1)
|
||||
.max(MAX_TABLE_ROWS)
|
||||
.describe('Rows as objects; the columns are the union of their keys')
|
||||
})
|
||||
|
||||
const tableColSchema = z.strictObject({
|
||||
'table-col': z
|
||||
.record(z.string(), z.array(cell).max(MAX_TABLE_ROWS))
|
||||
.describe('Columns: column name to its values, all the same length')
|
||||
})
|
||||
|
||||
const tableRowSchema = z.strictObject({
|
||||
'table-row': z
|
||||
.array(z.array(cell))
|
||||
.min(2)
|
||||
.max(MAX_TABLE_ROWS)
|
||||
.describe('Rows as arrays; the first row holds the column names')
|
||||
})
|
||||
|
||||
const fenceItemSchema = z.union([chartSchema, tableRowObjectSchema, tableColSchema, tableRowSchema])
|
||||
|
||||
export const fenceSchema = z.union([
|
||||
fenceItemSchema,
|
||||
z.strictObject({
|
||||
render_all: z
|
||||
.array(fenceItemSchema)
|
||||
.min(1)
|
||||
.max(10)
|
||||
.describe('Several items shown one after another')
|
||||
})
|
||||
])
|
||||
|
||||
export type FenceResult = z.infer<typeof fenceSchema>
|
||||
export type FenceItem = z.infer<typeof fenceItemSchema>
|
||||
export type FenceTable = Exclude<FenceItem, { chart: unknown }>
|
||||
|
||||
/** The items a fence value renders, in order. */
|
||||
export function fenceItems(value: FenceResult): FenceItem[] {
|
||||
return 'render_all' in value ? value.render_all : [value]
|
||||
}
|
||||
|
||||
type Cell = string | number | boolean | null
|
||||
|
||||
/** The columns and rows of a catalog table value, in the order the table renders them. */
|
||||
export function tableRows(item: FenceTable): { columns: string[]; rows: Cell[][] } {
|
||||
if ('table-row' in item) {
|
||||
const [header, ...rows] = item['table-row']
|
||||
return { columns: header.map((c) => String(c ?? '')), rows }
|
||||
}
|
||||
if ('table-col' in item) {
|
||||
const cols = item['table-col']
|
||||
const columns = Object.keys(cols)
|
||||
const length = Math.max(0, ...columns.map((c) => cols[c].length))
|
||||
return {
|
||||
columns,
|
||||
rows: Array.from({ length }, (_, i) => columns.map((c) => cols[c][i] ?? null))
|
||||
}
|
||||
}
|
||||
const objects = item['table-row-object']
|
||||
const columns = [...new Set(objects.flatMap((o) => Object.keys(o)))]
|
||||
return { columns, rows: objects.map((o) => columns.map((c) => o[c] ?? null)) }
|
||||
}
|
||||
|
||||
export function isChartResult(result: unknown): result is { chart: ChartBody } {
|
||||
return chartSchema.safeParse(result).success
|
||||
}
|
||||
|
||||
/** The fence kinds, as `feature_usage` keys: a closed set. */
|
||||
export type FenceKind = 'chart' | 'table' | 'render_all'
|
||||
|
||||
export function fenceKind(result: FenceResult): FenceKind {
|
||||
if ('render_all' in result) return 'render_all'
|
||||
if ('chart' in result) return 'chart'
|
||||
return 'table'
|
||||
}
|
||||
|
||||
export type FenceParse = { ok: true; result: FenceResult } | { ok: false; error: string }
|
||||
|
||||
export function parseFence(source: string): FenceParse {
|
||||
let json: unknown
|
||||
try {
|
||||
json = JSON.parse(source)
|
||||
} catch (e) {
|
||||
return { ok: false, error: `Not valid JSON: ${(e as Error).message}` }
|
||||
}
|
||||
const parsed = fenceSchema.safeParse(json)
|
||||
if (parsed.success) return { ok: true, result: parsed.data }
|
||||
return { ok: false, error: describeFenceError(json, parsed.error) }
|
||||
}
|
||||
|
||||
const FENCE_KEYS = ['chart', 'table-row-object', 'table-col', 'table-row', 'render_all']
|
||||
|
||||
// A union's own error lists every branch's failure; report the branch the object's key names.
|
||||
function describeFenceError(json: unknown, error: z.ZodError): string {
|
||||
if (json && typeof json === 'object' && !Array.isArray(json)) {
|
||||
const keys = Object.keys(json)
|
||||
if (keys.length !== 1 || !FENCE_KEYS.includes(keys[0])) {
|
||||
return `Expected an object with exactly one of the keys ${FENCE_KEYS.join(', ')}; got ${keys.length ? keys.join(', ') : 'no key'}`
|
||||
}
|
||||
const key = keys[0]
|
||||
const branch =
|
||||
key === 'render_all'
|
||||
? fenceSchema.options[1]
|
||||
: fenceItemSchema.options.find((o) => key in o.shape)
|
||||
const inner = branch?.safeParse(json)
|
||||
if (inner && !inner.success) return z.prettifyError(inner.error)
|
||||
}
|
||||
return z.prettifyError(error)
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
import { readFileSync, writeFileSync } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { z } from 'zod'
|
||||
import { fenceSchema } from './displayCatalog'
|
||||
|
||||
// The model's guidance for charts and tables is generated from the catalog, so the schema it
|
||||
// reads is the one its output is validated against. Regenerate with `npm run gen:display-catalog`.
|
||||
const OUT = join(__dirname, '../../../../../system_prompts/base/display-catalog.gen.md')
|
||||
|
||||
const EXAMPLES: { title: string; value: unknown }[] = [
|
||||
{
|
||||
title: 'Bar chart, one series per y column',
|
||||
value: {
|
||||
chart: {
|
||||
type: 'bar',
|
||||
title: 'Runs per day',
|
||||
data: [
|
||||
{ day: '2026-11-10', success: 42, failure: 3 },
|
||||
{ day: '2026-11-11', success: 51, failure: 1 }
|
||||
],
|
||||
x: 'day',
|
||||
y: ['success', 'failure'],
|
||||
stacked: true
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
title: 'Pie chart (exactly one y column)',
|
||||
value: {
|
||||
chart: {
|
||||
type: 'pie',
|
||||
data: [
|
||||
{ language: 'python3', scripts: 18 },
|
||||
{ language: 'bun', scripts: 25 }
|
||||
],
|
||||
x: 'language',
|
||||
y: 'scripts'
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
title: 'A chart and a table together',
|
||||
value: {
|
||||
render_all: [
|
||||
{
|
||||
chart: {
|
||||
type: 'line',
|
||||
data: [
|
||||
{ hour: '10:00', p95_ms: 120 },
|
||||
{ hour: '11:00', p95_ms: 180 }
|
||||
],
|
||||
x: 'hour',
|
||||
y: 'p95_ms',
|
||||
y_label: 'ms'
|
||||
}
|
||||
},
|
||||
{
|
||||
'table-row-object': [
|
||||
{ hour: '10:00', p95_ms: 120, jobs: 310 },
|
||||
{ hour: '11:00', p95_ms: 180, jobs: 402 }
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
function render(): string {
|
||||
const schema = z.toJSONSchema(fenceSchema, { unrepresentable: 'any' })
|
||||
const parts = [
|
||||
'### Chart and table schema',
|
||||
'',
|
||||
"A chart or table value, as JSON Schema. Beyond it: `x` and every `y` must be columns of `data`, `y` columns hold numbers, a scatter chart's `x` column holds numbers, and a pie chart takes exactly one `y`.",
|
||||
'',
|
||||
'```json',
|
||||
JSON.stringify(schema),
|
||||
'```',
|
||||
''
|
||||
]
|
||||
for (const { title, value } of EXAMPLES) {
|
||||
parts.push(`${title}:`, '', '```json', JSON.stringify(value, null, 2), '```', '')
|
||||
}
|
||||
return parts.join('\n')
|
||||
}
|
||||
|
||||
describe('display catalog guidance', () => {
|
||||
it('examples are valid fence values', () => {
|
||||
for (const { title, value } of EXAMPLES) {
|
||||
expect(fenceSchema.safeParse(value).success, title).toBe(true)
|
||||
}
|
||||
})
|
||||
|
||||
it('matches the catalog (npm run gen:display-catalog)', () => {
|
||||
const content = render()
|
||||
if (process.env.WM_GEN_DISPLAY_CATALOG) writeFileSync(OUT, content)
|
||||
expect(readFileSync(OUT, 'utf8')).toBe(content)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,60 @@
|
||||
import type { ChartBody, FenceResult, FenceTable } from './displayCatalog'
|
||||
import { fenceItems, parseFence, tableRows } from './displayCatalog'
|
||||
import { chartConfig } from './chartConfig'
|
||||
|
||||
async function chartImage(doc: Document, chart: ChartBody): Promise<HTMLElement> {
|
||||
const { Chart } = await import('chart.js/auto')
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = 800
|
||||
canvas.height = 400
|
||||
const config = chartConfig(chart, { dark: false, surface: '#ffffff' })
|
||||
const drawn = new Chart(canvas, {
|
||||
...config,
|
||||
options: { ...config.options, responsive: false, devicePixelRatio: 2 }
|
||||
})
|
||||
const src = drawn.toBase64Image()
|
||||
drawn.destroy()
|
||||
const figure = doc.createElement('figure')
|
||||
figure.className = 'wm-diagram'
|
||||
const img = doc.createElement('img')
|
||||
img.src = src
|
||||
img.alt = chart.title ?? `${chart.type} chart`
|
||||
img.style.maxWidth = '100%'
|
||||
figure.appendChild(img)
|
||||
return figure
|
||||
}
|
||||
|
||||
function tableElement(doc: Document, item: FenceTable): HTMLElement {
|
||||
const { columns, rows } = tableRows(item)
|
||||
const table = doc.createElement('table')
|
||||
const head = table.createTHead().insertRow()
|
||||
for (const c of columns) head.appendChild(doc.createElement('th')).textContent = c
|
||||
const body = table.createTBody()
|
||||
for (const row of rows) {
|
||||
const tr = body.insertRow()
|
||||
for (const cell of row) tr.insertCell().textContent = cell === null ? '' : String(cell)
|
||||
}
|
||||
return table
|
||||
}
|
||||
|
||||
async function displayElements(doc: Document, value: FenceResult): Promise<HTMLElement[]> {
|
||||
const out: HTMLElement[] = []
|
||||
for (const item of fenceItems(value)) {
|
||||
out.push('chart' in item ? await chartImage(doc, item.chart) : tableElement(doc, item))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces each valid ```wm-display fence under `root` with what it renders as: a chart as a PNG,
|
||||
* a table as a plain HTML table, so a downloaded document carries no script. An invalid fence
|
||||
* stays as its source, as it does on screen.
|
||||
*/
|
||||
export async function drawDisplayFences(root: DocumentFragment | Element): Promise<void> {
|
||||
const doc = root.ownerDocument ?? document
|
||||
for (const code of Array.from(root.querySelectorAll('pre > code.language-wm-display'))) {
|
||||
const parsed = parseFence(code.textContent ?? '')
|
||||
if (!parsed.ok) continue
|
||||
code.parentElement!.replaceWith(...(await displayElements(doc, parsed.result)))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { logFeatureUsage } from '$lib/utils/featureUsage'
|
||||
import { fenceKind, parseFence, type FenceKind } from './displayCatalog'
|
||||
|
||||
const DISPLAY_BLOCK = /```wm-display[^\n]*\n([\s\S]*?)```/g
|
||||
|
||||
/** Counts the ```wm-display blocks of a finished assistant reply, by kind or as `invalid`. */
|
||||
export function logDisplayBlocks(reply: string): void {
|
||||
if (!reply.includes('```wm-display')) return
|
||||
for (const [, source] of reply.matchAll(DISPLAY_BLOCK)) {
|
||||
const parsed = parseFence(source)
|
||||
const key: FenceKind | 'invalid' = parsed.ok ? fenceKind(parsed.result) : 'invalid'
|
||||
logFeatureUsage('ai_chat', 'display_block', { key })
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
import AIChatMessage from '$lib/components/copilot/chat/AIChatMessage.svelte'
|
||||
import ToolGroupDisplay from '$lib/components/copilot/chat/ToolGroupDisplay.svelte'
|
||||
import ArtifactBody from '$lib/components/copilot/chat/artifacts/ArtifactBody.svelte'
|
||||
import DisplayCodeBlock from '$lib/components/copilot/chat/artifacts/DisplayCodeBlock.svelte'
|
||||
import { setChatViewHost } from '$lib/components/copilot/chat/chatViewHost'
|
||||
import { readOnlyChatViewHost } from '$lib/components/copilot/chat/readOnlyChatViewHost.svelte'
|
||||
import { chatItemKey, groupToolRuns } from '$lib/components/copilot/chat/toolGroups'
|
||||
@@ -113,7 +114,11 @@
|
||||
<Drawer bind:this={drawer} size="900px">
|
||||
<DrawerContent title={openArtifact?.name ?? 'Artifact'} on:close={() => drawer?.closeDrawer()}>
|
||||
{#if openArtifact}
|
||||
<ArtifactBody content={openArtifact.content} source={openArtifact.kind !== 'md'} />
|
||||
<ArtifactBody
|
||||
content={openArtifact.content}
|
||||
source={openArtifact.kind !== 'md'}
|
||||
pre={DisplayCodeBlock}
|
||||
/>
|
||||
{:else}
|
||||
<span class="text-xs text-secondary">This artifact was not part of the share.</span>
|
||||
{/if}
|
||||
|
||||
@@ -52,6 +52,7 @@ a CLI-only workflow file (`flow-cli.md`, `script-cli.md`, `raw-app-cli.md`).
|
||||
| pipeline | `getPipelinePrompt()` | `write-pipeline` | `pipeline-base.md` |
|
||||
| data tables | `getDatatablePrompt()` | `datatables` | `datatable.md` |
|
||||
| workspace settings | `getWorkspaceSettingsPrompt()` | `workspace-settings` | `workspace-settings.md` |
|
||||
| rich results | `getDisplayPrompt()` | `rich-results` | `display-results.md`, `display-catalog.gen.md` (written by `npm run gen:display-catalog` in `frontend/` from the zod catalog) |
|
||||
| triggers, schedules, preview, CLI commands | — | same names | CLI only |
|
||||
|
||||
The app chat embeds `RAW_APP_BASE` directly (`chat/app/core.ts`).
|
||||
|
||||
+1
@@ -5,6 +5,7 @@ export declare function getResourcePrompt(): string;
|
||||
export declare function getAgentPrompt(): string;
|
||||
export declare function getRawAppPrompt(language?: string): string;
|
||||
export declare function getPipelinePrompt(): string;
|
||||
export declare function getDisplayPrompt(): string;
|
||||
export declare function getDatatablePrompt(): string;
|
||||
export declare function getWorkspaceSettingsPrompt(): string;
|
||||
export declare function getDatatableSdkReference(language?: string): string;
|
||||
|
||||
@@ -80,6 +80,14 @@ export function getPipelinePrompt(): string {
|
||||
].filter(Boolean).join('\n\n');
|
||||
}
|
||||
|
||||
// Helper for rich results: tables, charts and the other shapes a return value renders as
|
||||
export function getDisplayPrompt(): string {
|
||||
return [
|
||||
prompts.DISPLAY_RESULTS_BASE,
|
||||
prompts.DISPLAY_CATALOG
|
||||
].filter(Boolean).join('\n\n');
|
||||
}
|
||||
|
||||
// Helper for data table schema changes, migrations and permissions (chat consumers)
|
||||
export function getDatatablePrompt(): string {
|
||||
return [
|
||||
|
||||
+2
@@ -10,6 +10,8 @@ export declare const DATATABLE_BASE: string;
|
||||
export declare const WORKSPACE_SETTINGS_BASE: string;
|
||||
export declare const WORKFLOW_AS_CODE_BASE: string;
|
||||
export declare const FLOW_CHAT_SPECIAL_MODULES: string;
|
||||
export declare const DISPLAY_RESULTS_BASE: string;
|
||||
export declare const DISPLAY_CATALOG: string;
|
||||
export declare const SDK_TYPESCRIPT: string;
|
||||
export declare const SDK_PYTHON: string;
|
||||
export declare const WAC_SDK_TYPESCRIPT: string;
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,37 @@
|
||||
# Rich results
|
||||
|
||||
Windmill renders a script's or flow's return value according to its shape: on run pages, in the
|
||||
script and flow editors, in app result displays and in AI chat run cards. Anything that matches none
|
||||
of the shapes below is shown as JSON.
|
||||
|
||||
## Shapes
|
||||
|
||||
- `{ "table-row-object": [{...}, ...] }`, or a bare array of objects: a table whose columns are the
|
||||
union of the objects' keys.
|
||||
- `{ "table-col": { "col": [...], ... } }`, or a bare object of equal-length arrays: a table built
|
||||
column by column.
|
||||
- `{ "table-row": [["col", ...], [v, ...], ...] }`: a table whose first row holds the column names.
|
||||
- `{ "chart": {...} }`: a bar, line, pie or scatter chart drawn from rows (schema below). A `chart`
|
||||
value the schema rejects is shown as JSON, not as a broken chart.
|
||||
- `{ "markdown": "..." }` (or `md`): rendered markdown.
|
||||
- `{ "html": "..." }`: sanitized HTML.
|
||||
- `{ "png": "<base64>" }`, likewise `jpeg`, `gif`, `svg`: an image. `{ "pdf": "<base64>" }`: a PDF.
|
||||
- `{ "file": "<base64>", "filename": "report.csv" }`: a download.
|
||||
- `{ "map": { "lat": 0, "lon": 0, "zoom": 3, "markers": [{ "lat": 0, "lon": 0, "title": "..." }] } }`:
|
||||
a map.
|
||||
- `{ "s3": "path/to/key" }`: an object in the workspace storage, previewed by type.
|
||||
- `{ "render_all": [ ... ] }`: each item rendered in turn with the rules above.
|
||||
|
||||
Each shape is an object with that one key (plus `filename` for a file, and an optional `storage` for
|
||||
`s3`). A result carrying any other key beside it is shown as JSON.
|
||||
|
||||
## Choosing one
|
||||
|
||||
A chart answers a question at a glance: a trend over time (line), a comparison across categories
|
||||
(bar), parts of a whole with few parts (pie), or a relation between two numbers (scatter). A table
|
||||
answers lookups and holds the exact values. When both help, return both in a `render_all`: the chart
|
||||
first, then the table of the same rows.
|
||||
|
||||
Keep a chart to what it shows well: at most 8 series. A pie with more than 8 slices keeps the 7
|
||||
largest and folds the rest into "Other", so prefer a bar chart for many categories. Aggregate before
|
||||
charting: a chart of thousands of raw points says less than one of the totals per day.
|
||||
@@ -1714,6 +1714,14 @@ TOPICS: dict[str, dict] = {
|
||||
'chat_helper': 'getWorkspaceSettingsPrompt',
|
||||
'parts': ['workspace-settings.md'],
|
||||
},
|
||||
'display': {
|
||||
'skill': 'rich-results',
|
||||
'description': 'MUST use when a script or flow should return a table, chart, markdown, image, file or map instead of plain JSON.',
|
||||
'chat_helper': 'getDisplayPrompt',
|
||||
# display-catalog.gen.md is generated from the frontend's zod catalog by
|
||||
# `npm run gen:display-catalog`; the chat validates charts against the same schema.
|
||||
'parts': ['display-results.md', 'display-catalog.gen.md'],
|
||||
},
|
||||
'cli_commands': {
|
||||
'skill': 'cli-commands',
|
||||
'description': 'MUST use when using the CLI, including debugging job failures and inspecting run history via `wmill job`.',
|
||||
@@ -1738,6 +1746,8 @@ CHAT_CONSTANTS = {
|
||||
'workspace-settings.md': 'WORKSPACE_SETTINGS_BASE',
|
||||
'workflow-as-code.md': 'WORKFLOW_AS_CODE_BASE',
|
||||
'flow-chat-special-modules.md': 'FLOW_CHAT_SPECIAL_MODULES',
|
||||
'display-results.md': 'DISPLAY_RESULTS_BASE',
|
||||
'display-catalog.gen.md': 'DISPLAY_CATALOG',
|
||||
}
|
||||
|
||||
|
||||
@@ -2676,6 +2686,11 @@ export function getPipelinePrompt(): string {
|
||||
__RETURN_pipeline__
|
||||
}
|
||||
|
||||
// Helper for rich results: tables, charts and the other shapes a return value renders as
|
||||
export function getDisplayPrompt(): string {
|
||||
__RETURN_display__
|
||||
}
|
||||
|
||||
// Helper for data table schema changes, migrations and permissions (chat consumers)
|
||||
export function getDatatablePrompt(): string {
|
||||
__RETURN_datatable__
|
||||
@@ -2748,6 +2763,7 @@ export declare function getResourcePrompt(): string;
|
||||
export declare function getAgentPrompt(): string;
|
||||
export declare function getRawAppPrompt(language?: string): string;
|
||||
export declare function getPipelinePrompt(): string;
|
||||
export declare function getDisplayPrompt(): string;
|
||||
export declare function getDatatablePrompt(): string;
|
||||
export declare function getWorkspaceSettingsPrompt(): string;
|
||||
export declare function getDatatableSdkReference(language?: string): string;
|
||||
|
||||
Reference in New Issue
Block a user