API de fichiers OPFS
@grant CAT.agent.opfs
L'API de fichiers OPFS (Origin Private File System) permet à un script de lire et d'écrire des fichiers dans l'espace de travail de l'Agent. Tous les chemins sont relatifs au répertoire agents/workspace/.
write — écrire un fichier
const result = await CAT.agent.opfs.write(path, content);
Paramètres :
| Paramètre | Type | Description |
|---|---|---|
path | string | Chemin du fichier (obligatoire) ; prend en charge les répertoires imbriqués |
content | string | Blob | Contenu du fichier |
Formats content pris en charge :
| Format | Description |
|---|---|
| Chaîne de texte simple | Enregistrée comme fichier texte UTF-8 |
| Chaîne d'URL de données | Décodée automatiquement et enregistrée en binaire (ex. data:image/png;base64,...) |
Objet Blob | Données binaires enregistrées directement |
Retourne WriteResult :
| Champ | Type | Description |
|---|---|---|
path | string | Chemin où le fichier a été enregistré |
size | number | Taille du fichier (octets) |
// Write a text file
await CAT.agent.opfs.write("data/config.json", JSON.stringify({ key: "value" }));
// Write a binary file (data URL)
const canvas = document.createElement("canvas");
const dataUrl = canvas.toDataURL("image/png");
await CAT.agent.opfs.write("images/chart.png", dataUrl);
Les répertoires parents sont créés automatiquement s'ils n'existent pas. Si le fichier existe déjà, son contenu est écrasé.
read — lire un fichier
const result = await CAT.agent.opfs.read(path, format?);
Paramètres :
| Paramètre | Type | Défaut | Description |
|---|---|---|---|
path | string | — | Chemin du fichier (obligatoire) |
format | "text" | "blob" | "text" | Format de lecture |
Retourne ReadResult :
| Champ | Type | Présent quand | Description |
|---|---|---|---|
path | string | toujours | chemin du fichier |
size | number | toujours | Taille du fichier |
content | string | format="text" | Contenu texte du fichier |
data | Blob | format="blob" | L'objet Blob du fichier (transféré via un clone structuré) |
mimeType | string | format="blob" | Type MIME détecté automatiquement |
Deux modes de lecture :
// Text mode — suited to JSON and text files
const config = await CAT.agent.opfs.read("data/config.json");
const data = JSON.parse(config.content);
// Blob mode — suited to images and binary files
const image = await CAT.agent.opfs.read("images/chart.png", "blob");
// image.data is a real Blob object (not a scope-restricted blob: URL)
// Create a local URL with URL.createObjectURL(image.data) in whatever
// context needs it, or hand the Blob directly to any API that accepts one
Détection automatique du type MIME :
| Extension | Type MIME |
|---|---|
.jpg / .jpeg | image/jpeg |
.png | image/png |
.gif | image/gif |
.webp | image/webp |
.svg | image/svg+xml |
.mp3 | audio/mpeg |
.wav | audio/wav |
.mp4 | video/mp4 |
.pdf | application/pdf |
.json | application/json |
.txt | text/plain |
.html | text/html |
.css | text/css |
.js | application/javascript |
| autre | application/octet-stream |
list — lister un répertoire
const entries = await CAT.agent.opfs.list(path?);
Paramètres :
| Paramètre | Type | Défaut | Description |
|---|---|---|---|
path | string | "" | Chemin du répertoire ; une chaîne vide signifie le répertoire racine |
Retourne FileEntry[] :
| Champ | Type | Description |
|---|---|---|
name | string | Nom du fichier/répertoire |
type | "file" | "directory" | Type |
size | number | Taille du fichier (type file uniquement) |
const entries = await CAT.agent.opfs.list("data/");
for (const entry of entries) {
if (entry.type === "file") {
console.log(`${entry.name} (${entry.size} bytes)`);
} else {
console.log(`${entry.name}/`);
}
}
delete — supprimer un fichier ou un répertoire
const result = await CAT.agent.opfs.delete(path);
Prend en charge la suppression récursive d'un répertoire et de tout ce qu'il contient.
Retourne :
{ success: true }