Skip to content

Instantly share code, notes, and snippets.

@nberlette
Created March 4, 2025 10:59
Show Gist options
  • Save nberlette/9fa8ad99881df03e3ba7a53f0068115f to your computer and use it in GitHub Desktop.
Save nberlette/9fa8ad99881df03e3ba7a53f0068115f to your computer and use it in GitHub Desktop.
Workers AI Playground Prompts

Deno Code Generator

System Prompt

You are a friendly expert software engineer. If the user requests you generate code, assume 
they want it written in TypeScript for Deno, and follow these rules to the letter: The code
should avoid using any external dependencies, unless absolutely necessary or specifically
allowed by the user; however, you can use Node.js builtin APIs as long as their specifiers 
are prefixed with `node:`, e.g. `import process from "node:process";`. For modules like 
`process` or `Buffer`, you **must** explicitly import them, as Deno does not expose Node 
globals by default. All API features you create must be fully functional, with nothing 
omitted for brevity. 

Your response should ONLY be the generated code, in its entirety, and nothing else. Do not
wrap the code in a code fence. Do not include any explanations/instructions/overviews. Do
not format the code as markdown. Respond with the code only, and nothing else whatsoever.

If the user seeks to converse with you, always be informative, polite, and respectful.

User Prompt

Create a robust localStorage-backed virtual file system (VFS) class, that can be used as a drop-in
replacement with the majority of the methods from the `node:fs` module. It should include sync and
async variants where applicable. Async methods should accept callbacks, and return a Promise if no
callback is provided.

Sample Result Comparisons

Model: openhermes-2.5-mistral-7b-awq

```typescript
import { readFile, writeFile, unlink, mkdir, rmdir, readdir, readlink, lstat, stat, fstat, open, close, ftruncate, fsync, utimes, utime, chmod, fchmod, chown, fchown, chownSync, fchownSync, lchown, lchownSync, link, lstatSync, readlinkSync, readdirSync, rename, renameSync, rmdirSync, stat, statSync, utimes, utimeSync, chmodSync, fchmodSync, chownSync, fchownSync, lchownSync, openSync, readFileSync, writeFileSync, unlinkSync, mkdirSync, fsyncSync, utimesSync, chmodSync, fchmodSync, chownSync, fchownSync, lchownSync, linkSync, renameSync, rmdirSync, lstatSync, readlinkSync, readdirSync, utime, utimeSync, chmodSync, fchmodSync, chownSync, fchownSync, lchownSync, openSync, readFileSync, writeFileSync, unlinkSync, mkdirSync, fsyncSync, utimesSync, chmodSync, fchmodSync, chownSync, fchownSync, lchownSync, linkSync, renameSync, rmdirSync, lstatSync, readlinkSync, readdirSync, utime, utimeSync, chmodSync, fchmodSync, chownSync, fchownSync, lchownSync, openSync, readFileSync, writeFileSync, unlinkSync, mkdirSync, fsyncSync, utimesSync, chmodSync, fchmodSync, chownSync, fchownSync, lchownSync, linkSync, renameSync, rmdirSync, lstatSync, readlinkSync, readdirSync, utime, utimeSync, chmodSync, fchmodSync, chownSync, fchownSync, lchownSync, openSync, readFileSync, writeFileSync, unlinkSync, mkdirSync, fsyncSync, utimesSync, chmodSync, fchmodSync, chownSync, fchownSync, lchownSync, linkSync, renameSync, rmdirSync, lstatSync, readlinkSync, readdirSync, utime, utimeSync, chmodSync, fchmodSync, chownSync, fchownSync, lchownSync, openSync, readFileSync, writeFileSync, unlinkSync, mkdirSync, fsyncSync, utimesSync, chmodSync, fchmodSync, chownSync, fchownSync, lchownSync, linkSync, renameSync, rmdirSync, lstatSync, readlinkSync, readdirSync, utime, utimeSync, chmodSync, fchmodSync, chownSync, fchownSync, lchownSync, openSync, readFileSync, writeFileSync, unlinkSync, mkdirSync, fsyncSync, utimesSync, chmodSync, fchmodSync, chownSync, fchownSync, lchownSync, linkSync, renameSync, rmdirSync, lstatSync, readlinkSync, readdirSync, utime, utimeSync, chmodSync, fchmodSync, chownSync, fchownSync, lchownSync, openSync, readFileSync, writeFileSync, unlinkSync, mkdirSync, fsyncSync, utimesSync, chmodSync, fchmodSync, chownSync, fchownSync, lchownSync, linkSync, renameSync, rmdirSync, lstatSync, readlinkSync, readdirSync, utime, utimeSync, chmodSync, fchmodSync, chownSync, fchownSync, lchownSync, openSync, readFileSync, writeFileSync, unlinkSync, mkdirSync, fsyncSync, utimesSync, chmodSync, fchmodSync, chownSync, fchownSync, lchownSync, linkSync, renameSync, rmdirSync, lstatSync, readlinkSync, readdirSync, utime, utimeSync, chmodSync, fchmodSync, chownSync, fchownSync, lchownSync, openSync, readFileSync, writeFileSync, unlinkSync, mkdirSync,

Well that's obviously not what we asked for. Pure unadulterated crap.

Model: llama-3.3-70b-instruct-fp8-fast

```typescript
import { Buffer } from "node:buffer";
import { TextEncoder, TextDecoder } from "node:util";

interface Stats {
  dev: number;
  ino: number;
  mode: number;
  nlink: number;
  uid: number;
  gid: number;
  rdev: number;
  size: number;
  blksize: number;
  blocks: number;
  atimeMs: number;
  mtimeMs: number;
  ctimeMs: number;
  birthtimeMs: number;
  atime: Date;
  mtime: Date;
  ctime: Date;
  birthtime: Date;
}

interface Dir {
  path: string;
  dirs: string[];
  files: string[];
}

interface Dirent {
  name: string;
  isBlockDevice(): boolean;
  isCharacterDevice(): boolean;
  isDirectory(): boolean;
  isFIFO(): boolean;
  isFile(): boolean;
  isSocket(): boolean;
  isSymbolicLink(): boolean;
}

class VFS {
  private storage: Storage;

  constructor() {
    this.storage = Deno.window.localStorage;
  }

  private getKey(path: string): string {
    return `vfs:${path}`;
  }

  private getFileContent(path: string): string | null {
    const key = this.getKey(path);
    return this.storage.getItem(key);
  }

  private setFileContent(path: string, content: string): void {
    const key = this.getKey(path);
    this.storage.setItem(key, content);
  }

  private removeFileContent(path: string): void {
    const key = this.getKey(path);
    this.storage.removeItem(key);
  }

  private getDirContent(path: string): Dir | null {
    const key = this.getKey(path);
    const content = this.storage.getItem(key);
    if (content === null) {
      return null;
    }
    return JSON.parse(content);
  }

  private setDirContent(path: string, content: Dir): void {
    const key = this.getKey(path);
    this.storage.setItem(key, JSON.stringify(content));
  }

  private removeDirContent(path: string): void {
    const key = this.getKey(path);
    this.storage.removeItem(key);
  }

  mkdirSync(path: string, options?: { recursive?: boolean }): void {
    if (options && options.recursive) {
      const dirs = path.split("/");
      let currentDir = "";
      for (const dir of dirs) {
        if (dir !== "") {
          currentDir += dir + "/";
          if (this.getDirContent(currentDir) === null) {
            this.setDirContent(currentDir, { path: currentDir, dirs: [], files: [] });
          }
        }
      }
    } else {
      if (this.getDirContent(path) === null) {
        this.setDirContent(path, { path, dirs: [], files: [] });
      }
    }
  }

  mkdir(path: string, options?: { recursive?: boolean }, callback?: (err: Error | null) => void): void {
    try {
      this.mkdirSync(path, options);
      if (callback) {
        callback(null);
      }
    } catch (err) {
      if (callback) {
        callback(err);
      } else {
        throw err;
      }
    }
  }

  mkdir(path: string, options?: { recursive?: boolean }): Promise<void> {
    return new Promise((resolve, reject) => {
      this.mkdir(path, options, (err) => {
        if (err) {
          reject(err);
        } else {
          resolve();
        }
      });
    });
  }

  readdirSync(path: string): Dirent[] {
    const dir = this.getDirContent(path);
    if (dir === null) {
      throw new Error(`Directory not found: ${path}`);
    }
    const dirents: Dirent[] = [];
    for (const file of dir.files) {
      dirents.push({
        name: file,
        isBlockDevice: () => false,
        isCharacterDevice: () => false,
        isDirectory: () => false,
        isFIFO: () => false,
        isFile: () => true,
        isSocket: () => false,
        isSymbolicLink: () => false,
      });
    }
    for (const dir of dir.dirs) {
      dirents.push({
        name: dir,
        isBlockDevice: () => false,
        isCharacterDevice: () => false,
        isDirectory: () => true,
        isFIFO: () => false,
        isFile: () => false,
        isSocket: () => false,
        isSymbolicLink: () => false,
      });
    }
    return dirents;
  }

  readdir(path: string, callback?: (err: Error | null, dirents: Dirent[]) => void): void {
    try {
      const dirents = this.readdirSync(path);
      if (callback) {
        callback(null, dirents);
      }
    } catch (err) {
      if (callback) {
        callback(err, null);
      } else {
        throw err;
      }
    }
  }

  readdir(path: string): Promise<Dirent[]> {
    return new Promise((resolve, reject) => {
      this.readdir(path, (err, dirents) => {
        if (err) {
          reject(err);
        } else {
          resolve(dirents);
        }
      });
    });
  }

  statSync(path: string): Stats {
    const fileContent = this.getFileContent(path);
    if (fileContent === null) {
      const dir = this.getDirContent(path);
      if (dir === null) {
        throw new Error(`File or directory not found: ${path}`);
      }
      return {
        dev: 0,
        ino: 0,
        mode: 0o777,
        nlink: 1,
        uid: 0,
        gid: 0,
        rdev: 0,
        size: 0,
        blksize: 0,
        blocks: 0,
        atimeMs: 0,
        mtimeMs: 0,
        ctimeMs: 0,
        birthtimeMs: 0,
        atime: new Date(),
        mtime: new Date(),
        ctime: new Date(),
        birthtime: new Date(),
      };
    } else {
      return {
        dev: 0,
        ino: 0,
        mode: 0o666,
        nlink: 1,
        uid: 0,
        gid: 0,
        rdev: 0,
        size: fileContent.length,
        blksize: 0,
        blocks: 0,
        atimeMs: 0,
        mtimeMs: 0,
        ctimeMs: 0,
        birthtimeMs: 0,
        atime: new Date(),
        mtime: new Date(),
        ctime: new Date(),
        birthtime: new Date(),
      };
    }
  }

  stat(path: string, callback?: (err: Error | null, stats: Stats) => void): void {
    try {
      const stats = this.statSync(path);
      if (callback) {
        callback(null, stats);
      }
    } catch (err) {
      if (callback) {
        callback(err, null);
      } else {
        throw err;
      }
    }
  }

  stat(path: string): Promise<Stats> {
    return new Promise((resolve, reject) => {
      this.stat(path, (err, stats) => {
        if (err) {
          reject(err);
        } else {
          resolve(stats);
        }
      });
    });
  }

  unlinkSync(path: string): void {
    this.removeFileContent(path);
  }

  unlink(path: string, callback?: (err: Error | null) => void): void {
    try {
      this.unlinkSync(path);
      if (callback) {
        callback(null);
      }
    } catch (err) {
      if (callback) {
        callback(err);
      } else {
        throw err;
      }
    }
  }

  unlink(path: string): Promise<void> {
    return new Promise((resolve, reject) => {
      this.unlink(path, (err) => {
        if (err) {
          reject(err);
        } else {
          resolve();
        }
      });
    });
  }

  rmdirSync(path: string): void {
    this.removeDirContent(path);
  }

  rmdir(path: string, callback?: (err: Error | null) => void): void {
    try {
      this.rmdirSync(path);
      if (callback) {
        callback(null);
      }
    } catch (err) {
      if (callback) {
        callback(err);
      } else {
        throw err;
      }
    }
  }

  rmdir(path: string): Promise<void> {
    return new Promise((resolve, reject) => {
      this.rmdir(path, (err) => {
        if (err) {
          reject(err);
        } else {
          resolve();
        }
      });
    });
  }

  readFileSync(path: string, encoding?: string): string | Buffer {
    const fileContent = this.getFileContent(path);
    if (fileContent === null) {
      throw new Error(`File not found: ${path}`);
    }
    if (encoding) {
      return fileContent;
    } else {
      return Buffer.from(fileContent, "utf-8");
    }
  }

  readFile(path: string, encoding?: string, callback?: (err: Error | null, data: string | Buffer) => void): void {
    try {
      const data = this.readFileSync(path, encoding);
      if (callback) {
        callback(null, data);
      }
    } catch (err) {
      if

User:

You have duplicate implementations for the asynchronous methods. Those should each be combined into a single overloaded method. One overload should accept a callback of this type: `type Callback<T> = { ((err: null, res: T): void; (err: Error, res?: null | undefined): void; };`, and should have a return type of `void`. The other overload should accept no callback and instead return `Promise<T>`, where `T` is the same type as the second argument in the first callback signature. 

Assistant:

```typescript
import { Buffer } from "node:buffer";
import { TextEncoder, TextDecoder } from "node:util";

interface Stats {
  dev: number;
  ino: number;
  mode: number;
  nlink: number;
  uid: number;
  gid: number;
  rdev: number;
  size: number;
  blksize: number;
  blocks: number;
  atimeMs: number;
  mtimeMs: number;
  ctimeMs: number;
  birthtimeMs: number;
  atime: Date;
  mtime: Date;
  ctime: Date;
  birthtime: Date;
}

interface Dir {
  path: string;
  dirs: string[];
  files: string[];
}

interface Dirent {
  name: string;
  isBlockDevice(): boolean;
  isCharacterDevice(): boolean;
  isDirectory(): boolean;
  isFIFO(): boolean;
  isFile(): boolean;
  isSocket(): boolean;
  isSymbolicLink(): boolean;
}

type Callback<T> = ((err: null, res: T) => void) & ((err: Error, res?: null | undefined) => void);

class VFS {
  private storage: Storage;

  constructor() {
    this.storage = Deno.window.localStorage;
  }

  private getKey(path: string): string {
    return `vfs:${path}`;
  }

  private getFileContent(path: string): string | null {
    const key = this.getKey(path);
    return this.storage.getItem(key);
  }

  private setFileContent(path: string, content: string): void {
    const key = this.getKey(path);
    this.storage.setItem(key, content);
  }

  private removeFileContent(path: string): void {
    const key = this.getKey(path);
    this.storage.removeItem(key);
  }

  private getDirContent(path: string): Dir | null {
    const key = this.getKey(path);
    const content = this.storage.getItem(key);
    if (content === null) {
      return null;
    }
    return JSON.parse(content);
  }

  private setDirContent(path: string, content: Dir): void {
    const key = this.getKey(path);
    this.storage.setItem(key, JSON.stringify(content));
  }

  private removeDirContent(path: string): void {
    const key = this.getKey(path);
    this.storage.removeItem(key);
  }

  mkdirSync(path: string, options?: { recursive?: boolean }): void {
    if (options && options.recursive) {
      const dirs = path.split("/");
      let currentDir = "";
      for (const dir of dirs) {
        if (dir !== "") {
          currentDir += dir + "/";
          if (this.getDirContent(currentDir) === null) {
            this.setDirContent(currentDir, { path: currentDir, dirs: [], files: [] });
          }
        }
      }
    } else {
      if (this.getDirContent(path) === null) {
        this.setDirContent(path, { path, dirs: [], files: [] });
      }
    }
  }

  mkdir(path: string, options?: { recursive?: boolean }, callback?: Callback<void>): void | Promise<void> {
    if (callback) {
      try {
        this.mkdirSync(path, options);
        callback(null, undefined);
      } catch (err) {
        callback(err, null);
      }
    } else {
      return new Promise((resolve, reject) => {
        try {
          this.mkdirSync(path, options);
          resolve();
        } catch (err) {
          reject(err);
        }
      });
    }
  }

  readdirSync(path: string): Dirent[] {
    const dir = this.getDirContent(path);
    if (dir === null) {
      throw new Error(`Directory not found: ${path}`);
    }
    const dirents: Dirent[] = [];
    for (const file of dir.files) {
      dirents.push({
        name: file,
        isBlockDevice: () => false,
        isCharacterDevice: () => false,
        isDirectory: () => false,
        isFIFO: () => false,
        isFile: () => true,
        isSocket: () => false,
        isSymbolicLink: () => false,
      });
    }
    for (const dir of dir.dirs) {
      dirents.push({
        name: dir,
        isBlockDevice: () => false,
        isCharacterDevice: () => false,
        isDirectory: () => true,
        isFIFO: () => false,
        isFile: () => false,
        isSocket: () => false,
        isSymbolicLink: () => false,
      });
    }
    return dirents;
  }

  readdir(path: string, callback?: Callback<Dirent[]>): void | Promise<Dirent[]> {
    if (callback) {
      try {
        const dirents = this.readdirSync(path);
        callback(null, dirents);
      } catch (err) {
        callback(err, null);
      }
    } else {
      return new Promise((resolve, reject) => {
        try {
          const dirents = this.readdirSync(path);
          resolve(dirents);
        } catch (err) {
          reject(err);
        }
      });
    }
  }

  statSync(path: string): Stats {
    const fileContent = this.getFileContent(path);
    if (fileContent === null) {
      const dir = this.getDirContent(path);
      if (dir === null) {
        throw new Error(`File or directory not found: ${path}`);
      }
      return {
        dev: 0,
        ino: 0,
        mode: 0o777,
        nlink: 1,
        uid: 0,
        gid: 0,
        rdev: 0,
        size: 0,
        blksize: 0,
        blocks: 0,
        atimeMs: 0,
        mtimeMs: 0,
        ctimeMs: 0,
        birthtimeMs: 0,
        atime: new Date(),
        mtime: new Date(),
        ctime: new Date(),
        birthtime: new Date(),
      };
    } else {
      return {
        dev: 0,
        ino: 0,
        mode: 0o666,
        nlink: 1,
        uid: 0,
        gid: 0,
        rdev: 0,
        size: fileContent.length,
        blksize: 0,
        blocks: 0,
        atimeMs: 0,
        mtimeMs: 0,
        ctimeMs: 0,
        birthtimeMs: 0,
        atime: new Date(),
        mtime: new Date(),
        ctime: new Date(),
        birthtime: new Date(),
      };
    }
  }

  stat(path: string, callback?: Callback<Stats>): void | Promise<Stats> {
    if (callback) {
      try {
        const stats = this.statSync(path);
        callback(null, stats);
      } catch (err) {
        callback(err, null);
      }
    } else {
      return new Promise((resolve, reject) => {
        try {
          const stats = this.statSync(path);
          resolve(stats);
        } catch (err) {
          reject(err);
        }
      });
    }
  }

  unlinkSync(path: string): void {
    this.removeFileContent(path);
  }

  unlink(path: string, callback?: Callback<void>): void | Promise<void> {
    if (callback) {
      try {
        this.unlinkSync(path);
        callback(null, undefined);
      } catch (err) {
        callback(err, null);
      }
    } else {
      return new Promise((resolve, reject) => {
        try {
          this.unlinkSync(path);
          resolve();
        } catch (err) {
          reject(err);
        }
      });
    }
  }

  rmdirSync(path: string): void {
    this.removeDirContent(path);
  }

  rmdir(path: string, callback?: Callback<void>): void | Promise<void> {
    if (callback) {
      try {
        this.rmdirSync(path);
        callback(null, undefined);
      } catch (err) {
        callback(err, null);
      }
    } else {
      return new Promise((resolve, reject) => {
        try {
          this.rmdirSync(path);
          resolve();
        } catch (err) {
          reject(err);
        }
      });
    }
  }

  readFileSync(path: string, encoding?: string): string | Buffer {
    const fileContent = this.getFileContent(path);
    if (fileContent === null) {
      throw new Error(`File not found: ${path}`);
    }
    if (encoding) {
      return fileContent;
    } else {
      return Buffer.from(fileContent, "utf-8");
    }
  }

  readFile(path: string, encoding?: string, callback?: Callback<string | Buffer>): void | Promise<string | Buffer> {
    if (callback) {
      try {
        const data = this.readFileSync(path, encoding);
        callback(null, data);
      } catch (err) {
        callback(err, null);
      }
    } else {
      return new Promise((resolve, reject) => {
        try {
          const data = this.readFileSync(path, encoding);
          resolve(data);
        } catch (err) {
          reject(err);
        }
      });
    }
  }

  writeFileSync(path: string, data: string | Buffer, options?: { encoding?: string, mode?: number, flag?: string }): void {
    let content: string;
    if (typeof data === "string") {
      content = data;
    } else {
      content = data.toString("utf-8");
    }
    this.setFileContent(path, content);
  }

  writeFile(path: string

Wow, significantly better than the openhermes model. Definitely still lacking in some key areas, and I had to give it some strong coaching in the middle, but at least this one appears to follow instructions and produced something that's almost functional.

I really wish Cloudflare would increase their token limits though. 256 tokens in a non-streaming response (and 2048 in streaming) is really just... pathetic. Such a low ceiling effectively just prevents their platform from being used for any sort of serious application in today's data-rich world. Hopefully someone clues them in and they bump those limits up soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment