Replace hasArtifact with listArtifacts

LLMs don't need to check existence - they can just list all artifacts.
Simpler API that returns all filenames at once.

Changes:
- Replace hasArtifact(filename) with listArtifacts() returning string[]
- Add 'list' action handler that returns all artifact keys
- Update examples in prompt to use listArtifacts()
This commit is contained in:
Mario Zechner 2025-10-10 01:49:03 +02:00
parent 7a859cbe52
commit 2e56f77599
2 changed files with 12 additions and 12 deletions

View file

@ -41,20 +41,19 @@ export class ArtifactsRuntimeProvider implements SandboxRuntimeProvider {
// Auto-parse/stringify for .json files
const isJsonFile = (filename: string) => filename.endsWith(".json");
(window as any).hasArtifact = async (filename: string): Promise<boolean> => {
(window as any).listArtifacts = async (): Promise<string[]> => {
// Online: ask extension
if ((window as any).sendRuntimeMessage) {
const response = await (window as any).sendRuntimeMessage({
type: "artifact-operation",
action: "has",
filename,
action: "list",
});
if (!response.success) throw new Error(response.error);
return response.result;
}
// Offline: check snapshot
// Offline: return snapshot keys
else {
return !!(window as any).artifacts?.[filename];
return Object.keys((window as any).artifacts || {});
}
};
@ -141,9 +140,9 @@ export class ArtifactsRuntimeProvider implements SandboxRuntimeProvider {
try {
switch (action) {
case "has": {
const exists = this.artifactsPanel.artifacts.has(filename);
respond({ success: true, result: exists });
case "list": {
const filenames = Array.from(this.artifactsPanel.artifacts.keys());
respond({ success: true, result: filenames });
break;
}

View file

@ -187,14 +187,14 @@ ${ARTIFACTS_RUNTIME_EXAMPLE}
export const ARTIFACTS_RUNTIME_PROVIDER_DESCRIPTION = `
Artifact Management (persistent session files you can access/modify programmatically):
- await hasArtifact(filename) - Check if artifact exists, returns boolean
* Example: if (await hasArtifact('data.json')) { ... }
- await listArtifacts() - Get list of all artifact filenames, returns string[]
* Example: const files = await listArtifacts(); // ['data.json', 'notes.md', 'chart.png']
- await getArtifact(filename) - Read artifact content, returns string or object
* Auto-parses .json files to objects, otherwise returns raw string content
* Example: const data = await getArtifact('data.json'); // Returns parsed object
* Example: const markdown = await getArtifact('notes.md'); // Returns string
- await createOrUpdateArtifact(filename, content, mimeType?) - Create or update a persistent artifact
* Automatically creates if new, updates if exists (no need to check hasArtifact first)
* Automatically creates if new, updates if exists
* Auto-stringifies objects for .json files
* Example: await createOrUpdateArtifact('data.json', {items: []}) // Auto-stringifies
* Example: await createOrUpdateArtifact('research-notes.md', '# Research Notes\\n', 'text/markdown')
@ -203,7 +203,8 @@ Artifact Management (persistent session files you can access/modify programmatic
* Example: await deleteArtifact('old-notes.md')
Powerful pattern for evolving data:
const data = await hasArtifact('data.json') ? await getArtifact('data.json') : {items: []};
const files = await listArtifacts();
const data = files.includes('data.json') ? await getArtifact('data.json') : {items: []};
data.items.push(newScrapedItem);
await createOrUpdateArtifact('data.json', data);