AI AgentStructured Tool Call

Password Generator AI Integration Guide

Generate secure random passwords with browser-native cryptography and explicit character set rules.

1. Function Schema

Import directly into Dify, Coze, Cursor, or any tool-calling agent runtime.

Function Schema
{
  "name": "generate_password",
  "description": "Generate one or more secure random passwords.",
  "parameters": {
    "type": "object",
    "properties": {
      "length": {
        "type": "integer",
        "minimum": 8,
        "maximum": 128,
        "default": 16
      },
      "count": {
        "type": "integer",
        "minimum": 1,
        "maximum": 20,
        "default": 1
      },
      "use_upper": { "type": "boolean", "default": true },
      "use_lower": { "type": "boolean", "default": true },
      "use_digits": { "type": "boolean", "default": true },
      "use_symbols": { "type": "boolean", "default": true }
    }
  }
}

2. System Prompt / SOP

Paste this into your system prompt to keep the generated tool implementation aligned with the contract.

System Prompt SOP
# Task Constraints: Password Generator Module

When generating passwords for users, you MUST use cryptographically secure randomness.

## Core Prohibitions
- NEVER use Math.random() for password generation.
- NEVER promise a password is "unhackable" or "military grade".
- NEVER allow an empty character set.

## Standard Implementation
1. Build the allowed character set from the selected flags.
2. If the user disables every flag, either fail validation or default to lowercase and state that behavior clearly.
3. Use crypto.getRandomValues() to generate random indices.
4. Return one password or an array of passwords depending on count.

## Guidance
- Prefer lengths of 16+ for general use.
- Explain that symbols can reduce compatibility on some legacy systems.
- Treat password generation as local-only logic. Do not log or persist generated secrets.

3. Core Script

Secure password generation should stay entirely inside the local runtime and use crypto.getRandomValues() for every generated character.

Core Script
function main(args) {
  const {
    length = 16,
    count = 1,
    use_upper = true,
    use_lower = true,
    use_digits = true,
    use_symbols = true,
  } = args;

  const UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  const LOWER = "abcdefghijklmnopqrstuvwxyz";
  const DIGITS = "0123456789";
  const SYMBOLS = "!@#$%^&*()_+-=[]{}|;:,.<>?";

  let charset = "";
  if (use_upper) charset += UPPER;
  if (use_lower) charset += LOWER;
  if (use_digits) charset += DIGITS;
  if (use_symbols) charset += SYMBOLS;

  if (!charset) {
    return { success: false, result: [], error: "At least one character group must be enabled" };
  }

  const passwords = Array.from({ length: count }, () => {
    const bytes = new Uint32Array(length);
    crypto.getRandomValues(bytes);
    return Array.from(bytes, (value) => charset[value % charset.length]).join("");
  });

  return {
    success: true,
    result: count === 1 ? passwords[0] : passwords,
  };
}