-
Notifications
You must be signed in to change notification settings - Fork 20
Rewrite artifacts documentation for clarity and correctness #694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Timonwa
merged 4 commits into
main
from
692-test-and-review-the-adk-ts-artifacts-documentation
May 4, 2026
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4dcdba9
Rewrite artifacts documentation section for clarity and correctness
Timonwa 9fd1420
Fix type mismatches and restore index to sidebar
Timonwa 053edcb
Remove "index" page from artifacts documentation meta
Timonwa b07fba7
Export BaseArtifactService from artifacts package
Timonwa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
820 changes: 140 additions & 680 deletions
820
apps/docs/content/docs/framework/artifacts/best-practices.mdx
Large diffs are not rendered by default.
Oops, something went wrong.
483 changes: 228 additions & 255 deletions
483
apps/docs/content/docs/framework/artifacts/context-integration.mdx
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,90 +1,152 @@ | ||
| --- | ||
| title: Recipes | ||
| description: Practical patterns for saving, loading, and transforming artifacts | ||
| description: Copy-paste patterns for common artifact workflows in ADK-TS — upload processing, media generation, and result caching. | ||
| --- | ||
|
|
||
| ## Upload → Process → Save Results | ||
| import { Cards, Card } from "fumadocs-ui/components/card"; | ||
|
|
||
| These are self-contained patterns you can drop into your own agents. Each recipe covers a common workflow — pick the one that matches your use case and adapt the filenames and logic to fit. | ||
|
|
||
| ## Upload → Process → Save results | ||
|
|
||
| When a user uploads a file (e.g. via auto-save or a direct tool call), you often want to parse it, compute something, and save the result as a new artifact so the agent can reference it later without re-processing. | ||
|
|
||
| This recipe receives a CSV filename, parses it, and saves a JSON analysis artifact. Because it uses `createTool`, the agent can call it by name once the upload is in place: | ||
|
|
||
| ```typescript | ||
| import { createTool } from "@iqai/adk"; | ||
| import * as z from "zod"; | ||
|
|
||
| export const analyzeCsv = createTool({ | ||
| name: "analyze_csv", | ||
| description: "Analyze uploaded CSV", | ||
| description: "Analyze an uploaded CSV file and save the results as JSON", | ||
| schema: z.object({ filename: z.string() }), | ||
| fn: async ({ filename }, ctx) => { | ||
| // Confirm the file exists in this session before trying to load it | ||
| const files = await ctx.listArtifacts(); | ||
| if (!files.includes(filename)) return { error: "File not found", files }; | ||
| if (!files.includes(filename)) | ||
| return { error: "File not found", available: files }; | ||
|
|
||
| const artifact = await ctx.loadArtifact(filename); | ||
| if (!artifact?.inlineData) return { error: "Could not load file" }; | ||
|
|
||
| // Decode and parse the CSV | ||
| const text = Buffer.from(artifact.inlineData.data, "base64").toString( | ||
| "utf-8", | ||
| ); | ||
| const [header, ...rows] = text.split("\n").filter(Boolean); | ||
| const headers = header.split(",").map(h => h.trim()); | ||
|
|
||
| const result = { | ||
| summary: "CSV analysis complete", | ||
| rowCount: rows.length, | ||
| columnCount: headers.length, | ||
| headers, | ||
| }; | ||
|
|
||
| const resultArtifact = { | ||
| // Save the analysis as a new artifact | ||
| const outputFilename = `analysis_${filename.replace(/\.[^/.]+$/, "")}.json`; | ||
| await ctx.saveArtifact(outputFilename, { | ||
| inlineData: { | ||
| data: Buffer.from(JSON.stringify(result, null, 2)).toString("base64"), | ||
| mimeType: "application/json", | ||
| }, | ||
| }; | ||
| const out = `analysis_${filename.replace(/\.[^/.]+$/, "")}.json`; | ||
| await ctx.saveArtifact(out, resultArtifact); | ||
| return { output: out, ...result }; | ||
| }); | ||
|
|
||
| return { output: outputFilename, ...result }; | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Generate Media → Save + Reuse | ||
| ## Generate media → Save + reuse | ||
|
|
||
| If your agent generates an image, SVG, or other binary output that's expensive to produce, save it as an artifact on first generation and load the cached version on subsequent calls instead of regenerating it. | ||
|
|
||
| This recipe generates an SVG banner inside a callback and caches it — repeated invocations return the stored version immediately: | ||
|
|
||
| ```typescript | ||
| import type { CallbackContext } from "@iqai/adk"; | ||
|
|
||
| export async function generateSvg(ctx: CallbackContext) { | ||
| const svg = `<svg width="200" height="100" xmlns="http://www.w3.org/2000/svg"><rect width="200" height="100" fill="black"/></svg>`; | ||
| await ctx.saveArtifact("banner.svg", { | ||
| export async function generateAndCacheSvg(ctx: CallbackContext) { | ||
| const cacheKey = "banner.svg"; | ||
|
|
||
| // Return the cached version if it already exists | ||
| const cached = await ctx.loadArtifact(cacheKey); | ||
| if (cached?.inlineData) { | ||
| return Buffer.from(cached.inlineData.data, "base64").toString("utf-8"); | ||
| } | ||
|
|
||
| // Generate the SVG and save it for future calls | ||
| const svg = `<svg width="200" height="100" xmlns="http://www.w3.org/2000/svg"> | ||
| <rect width="200" height="100" fill="black"/> | ||
| </svg>`; | ||
|
|
||
| await ctx.saveArtifact(cacheKey, { | ||
| inlineData: { | ||
| data: Buffer.from(svg).toString("base64"), | ||
| mimeType: "image/svg+xml", | ||
| }, | ||
| }); | ||
|
|
||
| // Later | ||
| const loaded = await ctx.loadArtifact("banner.svg"); | ||
| if (loaded) { | ||
| const svgText = Buffer.from(loaded.inlineData.data, "base64").toString( | ||
| "utf-8", | ||
| ); | ||
| // Use svgText (serve to UI, embed in response, etc.) | ||
| } | ||
| return svg; | ||
| } | ||
| ``` | ||
|
|
||
| ## Cache Expensive Binary Output | ||
| ## Cache expensive computation | ||
|
|
||
| Use the same check-before-compute pattern for any operation with a deterministic or memoizable output — model inference on a fixed input, a database aggregation, a report generation step. | ||
|
|
||
| This recipe checks for a cached result before running the expensive work, and saves it so the next call skips the computation entirely: | ||
|
|
||
| ```typescript | ||
| import type { CallbackContext } from "@iqai/adk"; | ||
|
|
||
| export async function cachedChart(ctx: CallbackContext) { | ||
| const cacheKey = "chart.png"; | ||
| export async function cachedComputation(ctx: CallbackContext) { | ||
| const cacheKey = "result.json"; | ||
|
|
||
| // Return early if the result is already cached | ||
| const cached = await ctx.loadArtifact(cacheKey); | ||
| if (cached) return { info: "hit", cacheKey }; | ||
| if (cached) return { hit: true, cacheKey }; | ||
|
|
||
| // Run the expensive operation and cache the result | ||
| const result = { value: Math.random(), computedAt: Date.now() }; | ||
|
|
||
| const png = Buffer.from(/* expensive render */); | ||
| await ctx.saveArtifact(cacheKey, { | ||
| inlineData: { data: png.toString("base64"), mimeType: "image/png" }, | ||
| inlineData: { | ||
| data: Buffer.from(JSON.stringify(result)).toString("base64"), | ||
| mimeType: "application/json", | ||
| }, | ||
| }); | ||
| return { info: "miss", cacheKey }; | ||
|
|
||
| return { hit: false, cacheKey }; | ||
| } | ||
| ``` | ||
|
|
||
| ## Save and load plain text | ||
|
|
||
| For simple string content you don't need `inlineData` at all. The `text` Part format is shorter to write and easier to read back — no base64 encoding or decoding required: | ||
|
|
||
| ```typescript | ||
| // Save | ||
| await ctx.saveArtifact("notes.txt", { text: "Meeting notes: ..." }); | ||
|
|
||
| // Load — artifact.text is the string directly | ||
| const notes = await ctx.loadArtifact("notes.txt"); | ||
| console.log(notes?.text); | ||
| ``` | ||
|
|
||
| Use this for log output, agent-generated summaries, plain JSON strings, or any content where binary encoding adds no value. | ||
|
|
||
| ## Next steps | ||
|
|
||
| <Cards> | ||
| <Card | ||
| title="🔌 Context Integration" | ||
| description="Full API reference for CallbackContext and ToolContext artifact methods" | ||
| href="/docs/framework/artifacts/context-integration" | ||
| /> | ||
| <Card | ||
| title="🗂️ Scoping & Versioning" | ||
| description="Use user: prefix for cross-session persistence" | ||
| href="/docs/framework/artifacts/scoping-and-versioning" | ||
| /> | ||
| </Cards> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.