diff --git a/os/skills/vertex-ai/CHANGELOG.md b/os/skills/vertex-ai/CHANGELOG.md new file mode 100644 index 0000000..f14d335 --- /dev/null +++ b/os/skills/vertex-ai/CHANGELOG.md @@ -0,0 +1,138 @@ +# Changelog - Google Vertex AI Skill + +All notable changes to the Vertex AI skill will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [0.1.0] - 2025-12-24 + +### Added +- Initial release of Google Vertex AI skill for PromptWar̊e ØS +- Video generation support using Veo 3.1 model + - Text-to-video generation + - Duration control (customizable video length) + - Style customization (cinematic, vibrant, etc.) + - Aspect ratio support (16:9, 9:16, 1:1) +- Image generation support using Imagen model + - Text-to-image generation + - Style options (photorealistic, artistic, product-design) + - Resolution control + - Multiple image generation (batch processing) +- Authentication handling + - Application Default Credentials (ADC) detection + - Service account support via GOOGLE_APPLICATION_CREDENTIALS + - gcloud CLI integration + - OAuth 2.0 guidance and setup instructions +- Error handling and user guidance + - Clear error messages with troubleshooting steps + - Authentication status checking + - API error interpretation + - Permission and quota guidance +- Command-line tool (`vertex-ai.ts`) + - Standard `--help` flag support + - Three main commands: `generate-video`, `generate-image`, `check-auth` + - JSR-based imports (jsr:@std/cli/parse-args) + - Remote execution support (no download required) +- Documentation + - SKILL.md: Complete skill specification + - README.md: Quick start guide + - EXAMPLES.md: Comprehensive examples and best practices + - INTEGRATION.md: Agent integration patterns + - CHANGELOG.md: Version history +- Testing + - vertex-ai.test.ts: Unit tests and behavior documentation + - Structure validation tests + - Help message format verification + +### Technical Details +- Language: TypeScript +- Runtime: Deno +- Architecture: Stateless, microservice-based +- API Integration: Google Cloud Vertex AI REST API +- Authentication: Google Cloud OAuth 2.0 / ADC +- License: Public Prompt License - Apache Variant (PPL-A) + +### API Endpoints +- Video: `publishers/google/models/veo-3.1:predict` +- Image: `publishers/google/models/imagegeneration:predict` + +### Supported Models +- Veo 3.1 (Video generation) +- Imagen (Image generation) + +### Dependencies +- jsr:@std/cli@^1.0.0 (parseArgs) +- Deno standard library +- Google Cloud Vertex AI API + +### Known Limitations +- Requires active Google Cloud project with billing +- Subject to Vertex AI API quotas +- Video generation is asynchronous and may take several minutes +- Regional availability varies by model +- **Service account JWT authentication not fully implemented** (uses gcloud CLI as fallback) +- Maximum 10 images per generation request + +### Future Roadmap +See INTEGRATION.md for planned enhancements: +- Additional model support +- Advanced parameter controls +- Automatic status polling +- Integrated file management +- Prompt templates library +- Cost estimation features + +## [Unreleased] + +### Planned for 0.2.0 +- Full service account JWT authentication implementation +- Job status polling with automatic completion detection +- File download and storage integration +- Enhanced prompt templates +- Cost estimation before generation +- Support for additional Imagen model variants + +### Under Consideration +- Video editing capabilities (trim, merge) +- Image post-processing (resize, filter) +- Prompt suggestion engine +- Result caching mechanism +- Multi-region failover +- Quota management tools + +--- + +## Version History + +- **0.1.0** (2025-12-24): Initial release with video and image generation + +--- + +## Contributing + +When contributing to this skill: + +1. Update this CHANGELOG with your changes +2. Follow [Semantic Versioning](https://semver.org/) +3. Add entries under [Unreleased] until release +4. Move changes to a new version section on release +5. Update version in SKILL.md frontmatter + +### Changelog Categories + +- **Added**: New features +- **Changed**: Changes in existing functionality +- **Deprecated**: Soon-to-be removed features +- **Removed**: Removed features +- **Fixed**: Bug fixes +- **Security**: Security vulnerability fixes + +--- + +## License + +Copyright (c) 2025 Ship.Fail +Licensed under the Public Prompt License - Apache Variant (PPL-A) + +See the LICENSE file in the repository root for details. diff --git a/os/skills/vertex-ai/EXAMPLES.md b/os/skills/vertex-ai/EXAMPLES.md new file mode 100644 index 0000000..95eb822 --- /dev/null +++ b/os/skills/vertex-ai/EXAMPLES.md @@ -0,0 +1,158 @@ +# Google Vertex AI Configuration Examples + +This file contains example configurations for using the Vertex AI skill. + +## Environment Variables + +```bash +# Set your Google Cloud project ID +export GCP_PROJECT_ID="your-project-id" + +# Set default location/region +export GCP_LOCATION="us-central1" + +# For service account authentication +export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account-key.json" +``` + +## Example Prompts + +### Video Generation Examples + +#### Example 1: Nature Scene +```bash +deno run --allow-net --allow-env --allow-read vertex-ai.ts generate-video \ + --prompt "A serene sunset over snow-capped mountains with a lake reflecting the golden sky" \ + --project "$GCP_PROJECT_ID" \ + --duration "8s" \ + --style "cinematic" +``` + +#### Example 2: Technology Demo +```bash +deno run --allow-net --allow-env --allow-read vertex-ai.ts generate-video \ + --prompt "A humanoid robot assembling electronic components in a modern factory" \ + --project "$GCP_PROJECT_ID" \ + --duration "10s" \ + --aspect-ratio "16:9" +``` + +#### Example 3: Urban Scene +```bash +deno run --allow-net --allow-env --allow-read vertex-ai.ts generate-video \ + --prompt "Time-lapse of a busy city street at night with neon lights and traffic" \ + --project "$GCP_PROJECT_ID" \ + --duration "5s" \ + --style "vibrant" +``` + +### Image Generation Examples + +#### Example 1: Photorealistic +```bash +deno run --allow-net --allow-env --allow-read vertex-ai.ts generate-image \ + --prompt "A photorealistic portrait of a person in a futuristic spacesuit on Mars" \ + --project "$GCP_PROJECT_ID" \ + --style "photorealistic" \ + --resolution "1024x1024" +``` + +#### Example 2: Artistic Style +```bash +deno run --allow-net --allow-env --allow-read vertex-ai.ts generate-image \ + --prompt "An abstract painting of a city skyline at sunset in vibrant colors" \ + --project "$GCP_PROJECT_ID" \ + --style "artistic" \ + --num-images 4 +``` + +#### Example 3: Product Design +```bash +deno run --allow-net --allow-env --allow-read vertex-ai.ts generate-image \ + --prompt "A sleek minimalist smartwatch with a transparent display" \ + --project "$GCP_PROJECT_ID" \ + --style "product-design" \ + --resolution "512x512" +``` + +## Common Use Cases + +### 1. Marketing Content +Generate promotional videos and images for products or services. + +### 2. Concept Visualization +Create visual representations of ideas, concepts, or designs. + +### 3. Storyboarding +Generate scenes for video storyboards or presentations. + +### 4. Social Media Content +Create engaging visual content for social media platforms. + +### 5. Prototyping +Quickly visualize design concepts without manual creation. + +## Best Practices + +### Prompt Engineering + +1. **Be Specific**: Include details about subject, setting, style, and mood + - ❌ "A car" + - ✅ "A sleek red sports car driving on a coastal highway at sunset" + +2. **Use Descriptive Language**: Add adjectives and context + - ❌ "A building" + - ✅ "A modern glass skyscraper with curved architecture reflecting clouds" + +3. **Specify Style**: Mention the desired artistic or visual style + - Examples: "photorealistic", "cinematic", "artistic", "minimalist" + +4. **Include Composition Details**: Describe camera angles, lighting, etc. + - "viewed from above", "soft lighting", "wide angle shot" + +### Performance Optimization + +1. **Start Small**: Begin with shorter videos and lower resolutions to test +2. **Choose Appropriate Regions**: Use regions closest to your location +3. **Batch Requests**: Group similar generation requests together +4. **Monitor Quotas**: Keep track of API usage and quotas + +### Cost Management + +1. **Use Development Projects**: Test with separate GCP projects +2. **Set Budget Alerts**: Configure billing alerts in Google Cloud Console +3. **Cache Results**: Save generated content to avoid regeneration +4. **Optimize Prompts**: Refine prompts to reduce iteration count + +## Troubleshooting + +### Authentication Issues + +**Problem**: "No authentication token available" +**Solution**: Run `gcloud auth application-default login` or set `GOOGLE_APPLICATION_CREDENTIALS` + +**Problem**: "Permission denied" +**Solution**: Ensure your account has the `aiplatform.endpoints.predict` permission + +### API Errors + +**Problem**: "API not enabled" +**Solution**: Enable Vertex AI API at https://console.cloud.google.com/apis/library/aiplatform.googleapis.com + +**Problem**: "Quota exceeded" +**Solution**: Check your quota limits in the GCP Console and request increases if needed + +### Generation Issues + +**Problem**: "Generation failed" +**Solution**: Review your prompt for policy violations or clarity issues + +**Problem**: "Long generation time" +**Solution**: Video generation can take several minutes; check job status with returned job ID + +## Additional Resources + +- [Vertex AI Documentation](https://cloud.google.com/vertex-ai/docs) +- [Veo Model Documentation](https://cloud.google.com/vertex-ai/docs/generative-ai/video/overview) +- [Imagen Documentation](https://cloud.google.com/vertex-ai/docs/generative-ai/image/overview) +- [Google Cloud Pricing](https://cloud.google.com/vertex-ai/pricing) diff --git a/os/skills/vertex-ai/INTEGRATION.md b/os/skills/vertex-ai/INTEGRATION.md new file mode 100644 index 0000000..9d92549 --- /dev/null +++ b/os/skills/vertex-ai/INTEGRATION.md @@ -0,0 +1,304 @@ +# Vertex AI Skill Integration Guide + +This guide demonstrates how an AI agent uses the Google Vertex AI skill within PromptWar̊e ØS. + +## Skill Activation + +When a user makes a request like: +- "Generate a video of..." +- "Create an image showing..." +- "Design a visual of..." + +The agent should activate the Vertex AI skill by ingesting `os://skills/vertex-ai/SKILL.md`. + +## Workflow Example + +### Scenario 1: Video Generation Request + +**User Request**: "Generate a video of a robot walking through a forest" + +**Agent Process**: + +1. **Activate Skill** + ``` + Ingest: os://skills/vertex-ai/SKILL.md + ``` + +2. **Identify Requirements** + - Task: Video generation + - Model: Veo 3.1 + - Prompt: "A robot walking through a forest" + - Need: Project ID, authentication + +3. **Check Authentication** + ```bash + deno run --allow-net --allow-env --allow-read \ + https://raw.githubusercontent.com/ShipFail/promptware/refs/heads/main/os/skills/vertex-ai/vertex-ai.ts \ + check-auth + ``` + +4. **Handle Authentication Status** + - If authenticated: Proceed to generation + - If not: Guide user through setup: + ``` + Please authenticate with Google Cloud: + 1. Run: gcloud auth application-default login + 2. Or set: export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json + ``` + +5. **Execute Generation** + ```bash + deno run --allow-net --allow-env --allow-read \ + https://raw.githubusercontent.com/ShipFail/promptware/refs/heads/main/os/skills/vertex-ai/vertex-ai.ts \ + generate-video \ + --prompt "A humanoid robot walking through a lush green forest" \ + --project "user-project-id" \ + --duration "8s" \ + --style "cinematic" + ``` + +6. **Handle Response** + - Parse the JSON response + - Extract video URL or job ID + - Inform user of completion or provide job tracking info + +### Scenario 2: Image Generation Request + +**User Request**: "Create an image of a futuristic car" + +**Agent Process**: + +1. **Activate Skill** + ``` + Ingest: os://skills/vertex-ai/SKILL.md + ``` + +2. **Identify Requirements** + - Task: Image generation + - Model: Imagen + - Prompt: "A futuristic car" + - Optional: Style preferences + +3. **Check Authentication** (same as video) + +4. **Execute Generation** + ```bash + deno run --allow-net --allow-env --allow-read \ + https://raw.githubusercontent.com/ShipFail/promptware/refs/heads/main/os/skills/vertex-ai/vertex-ai.ts \ + generate-image \ + --prompt "A sleek futuristic electric car with aerodynamic design" \ + --project "user-project-id" \ + --style "photorealistic" \ + --resolution "1024x1024" + ``` + +5. **Handle Response** + - Parse the JSON response + - Extract base64 image data or URL + - Present image to user or save to file + +## Error Handling Patterns + +### Pattern 1: Missing Credentials +``` +Error: No authentication token available + +Agent Response: +"I need access to your Google Cloud credentials. Please set up authentication: + +Option 1 (Recommended): +$ gcloud auth application-default login + +Option 2 (Service Account): +$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json + +After setting up, I'll retry the generation." +``` + +### Pattern 2: API Not Enabled +``` +Error: HTTP 403 - Vertex AI API not enabled + +Agent Response: +"The Vertex AI API is not enabled in your project. Please: + +1. Visit: https://console.cloud.google.com/apis/library/aiplatform.googleapis.com +2. Select your project: [project-id] +3. Click 'Enable' + +Once enabled, I'll retry the generation." +``` + +### Pattern 3: Invalid Prompt +``` +Error: Content policy violation + +Agent Response: +"The prompt may have triggered content policy filters. Let me try: +1. Rephrasing the prompt to be more specific +2. Removing potentially sensitive terms +3. Using alternative descriptive language" +``` + +## Advanced Usage Patterns + +### Multi-Step Generation + +**User Request**: "Create a marketing video with matching thumbnail" + +**Agent Process**: +1. Generate video first (Veo 3.1) +2. Wait for video completion +3. Generate thumbnail image (Imagen) matching video style +4. Present both assets to user + +### Iterative Refinement + +**User Request**: "Generate a video, but make it more cinematic" + +**Agent Process**: +1. First attempt with basic prompt +2. Analyze user feedback +3. Refine prompt with "cinematic" style tag +4. Regenerate with enhanced parameters + +### Batch Processing + +**User Request**: "Generate 4 variations of a product image" + +**Agent Process**: +1. Use `--num-images 4` parameter +2. Single API call generates multiple images +3. Present all variations to user +4. Allow user to select preferred version + +## Best Practices for Agents + +### 1. Prompt Enhancement +Always enhance user prompts with: +- Specific details (colors, mood, style) +- Technical specifications (resolution, duration) +- Contextual information (lighting, perspective) + +Example transformation: +- User: "A car" +- Enhanced: "A sleek red sports car on a coastal highway at sunset with dramatic lighting" + +### 2. Project Context Management +- Remember user's project ID across session +- Cache authentication status +- Store previously used parameters as defaults + +### 3. Cost Awareness +- Inform user about approximate costs +- Suggest starting with shorter/smaller generations +- Recommend batch processing for similar requests + +### 4. Progress Communication +- Video generation takes time - set expectations +- Provide status updates during processing +- Explain asynchronous job tracking + +### 5. Error Recovery +- Always provide clear next steps +- Offer alternatives when errors occur +- Cache working configurations + +## Integration with Other Skills + +### Combining with Jekyll Skill +``` +User: "Create a blog post with a video demo" + +Agent: +1. Generate video (Vertex AI skill) +2. Create blog post (Jekyll skill) +3. Embed video in post assets +4. Verify and publish +``` + +### Combining with Vault Manager +``` +User: "Generate images and store them securely" + +Agent: +1. Generate images (Vertex AI skill) +2. Store in encrypted vault (Vault Manager skill) +3. Provide secure access links +``` + +## Monitoring and Debugging + +### Logging +The tool outputs structured information: +- Request parameters +- API endpoints called +- Response data +- Error messages with context + +### Debug Mode +For troubleshooting, agents should: +1. Check authentication first +2. Validate project configuration +3. Test with simple prompts +4. Review API response details + +## Security Considerations + +### Credential Handling +- Never log access tokens +- Use environment variables for credentials +- Recommend service accounts for production +- Guide users on proper key management + +### Prompt Safety +- Validate prompts for policy compliance +- Warn about sensitive content +- Suggest alternatives for flagged content + +### API Key Rotation +- Encourage regular credential rotation +- Support multiple authentication methods +- Handle token expiration gracefully + +## Performance Optimization + +### Caching Strategy +- Cache authentication tokens (respect TTL) +- Store successful prompt patterns +- Remember user preferences + +### Region Selection +- Recommend nearest regions +- Fall back to default if unavailable +- Consider quota availability + +### Request Batching +- Group similar requests +- Use multi-image generation +- Minimize API calls + +## Future Enhancements + +Potential additions to this skill: +1. **Model Selection**: Support for additional Vertex AI models +2. **Advanced Parameters**: More fine-grained control options +3. **Status Polling**: Automatic job status checking +4. **Result Storage**: Integrated file management +5. **Prompt Templates**: Pre-built prompt patterns +6. **Cost Estimation**: Preview costs before generation + +## Resources + +- Skill Documentation: `os://skills/vertex-ai/SKILL.md` +- Examples: `os://skills/vertex-ai/EXAMPLES.md` +- Tool: `os://skills/vertex-ai/vertex-ai.ts` +- Tests: `os://skills/vertex-ai/vertex-ai.test.ts` + +## Support + +For issues or questions: +1. Check EXAMPLES.md for troubleshooting +2. Review error messages for guidance +3. Consult Google Cloud documentation +4. Verify project and API configuration diff --git a/os/skills/vertex-ai/README.md b/os/skills/vertex-ai/README.md new file mode 100644 index 0000000..90409d6 --- /dev/null +++ b/os/skills/vertex-ai/README.md @@ -0,0 +1,122 @@ +# Google Vertex AI Skill + +This skill provides integration with Google Cloud Vertex AI's generative AI capabilities. + +## Overview + +The Vertex AI skill enables agents to generate: +- **Videos** using the Veo 3.1 model +- **Images** using the Imagen model family + +## Files + +- `SKILL.md` - Main skill specification and documentation +- `vertex-ai.ts` - Command-line tool for Vertex AI API integration +- `README.md` - This file + +## Quick Start + +### Prerequisites + +1. Google Cloud project with billing enabled +2. Vertex AI API enabled in your project +3. Authentication configured (see Authentication section) + +### Authentication + +Set up Google Cloud authentication using the recommended method: + +#### Method 1: Application Default Credentials (Recommended) +```bash +gcloud auth application-default login +``` + +**Important Note**: Service account JWT authentication is not yet implemented. If you have a service account JSON key file, the tool will detect it but ask you to use gcloud CLI instead. + +#### Method 2: OAuth 2.0 +Visit the [Google Cloud Console](https://console.cloud.google.com/apis/credentials) to set up OAuth credentials, then use gcloud CLI to authenticate. + +### Usage Examples + +#### Check Authentication +```bash +deno run --allow-net --allow-env --allow-read vertex-ai.ts check-auth +``` + +#### Generate a Video +```bash +deno run --allow-net --allow-env --allow-read vertex-ai.ts generate-video \ + --prompt "A serene sunset over mountains" \ + --project "your-project-id" \ + --location "us-central1" \ + --duration "5s" +``` + +#### Generate an Image +```bash +deno run --allow-net --allow-env --allow-read vertex-ai.ts generate-image \ + --prompt "A futuristic cityscape at night" \ + --project "your-project-id" \ + --location "us-central1" \ + --style "photorealistic" +``` + +## Architecture + +This skill follows the PromptWar̊e ØS architecture: + +- **Zero-Footprint**: No downloads required, runs remotely via `deno run ` +- **Stateless**: Each invocation is independent +- **JSR Imports**: Uses standard Deno JSR packages +- **CLI Standard**: Supports `--help` for all commands + +## API Endpoints + +The tool interacts with these Vertex AI endpoints: + +- Video Generation: `publishers/google/models/veo-3.1:predict` +- Image Generation: `publishers/google/models/imagegeneration:predict` + +## Supported Models + +- **Veo 3.1**: Advanced video generation model +- **Imagen**: High-quality image generation model family + +## Development + +### Testing + +To test the tool locally: + +```bash +# Check help output +deno run vertex-ai.ts --help + +# Test authentication check +deno run --allow-net --allow-env --allow-read vertex-ai.ts check-auth +``` + +### Adding New Features + +When adding new capabilities: + +1. Update `vertex-ai.ts` with new command handlers +2. Update `SKILL.md` with usage documentation +3. Add examples to this README +4. Test with `--help` flag + +## Limitations + +- Requires active Google Cloud billing account +- Subject to Vertex AI API quotas +- Video generation is asynchronous and may take minutes +- Regional availability varies by model +- **Service account JWT authentication not yet implemented** - use gcloud CLI +- Maximum 10 images per request + +## License + +Copyright (c) 2025 Ship.Fail +Licensed under the Public Prompt License - Apache Variant (PPL-A) + +See the LICENSE file in the repository root for details. diff --git a/os/skills/vertex-ai/SKILL.md b/os/skills/vertex-ai/SKILL.md new file mode 100644 index 0000000..e819649 --- /dev/null +++ b/os/skills/vertex-ai/SKILL.md @@ -0,0 +1,229 @@ +--- +type: skill +title: "Google Vertex AI Module" +version: "0.1.0" +tags: + - vertex-ai + - google-cloud + - ai-generation + - video + - image +tools: + - ./vertex-ai.ts +--- + +This skill enables you to leverage Google Cloud Vertex AI's powerful generative AI capabilities, including video generation with Veo 3.1 and image generation with Imagen models. + +--- + +## When to use this skill + +Use this skill whenever you are asked to: + +* Generate videos from text descriptions +* Create images from text prompts +* Design visual content using AI models +* Utilize Google Cloud's Vertex AI generative capabilities + +If the user's request involves video or image generation and mentions Google Cloud or Vertex AI, this is the primary skill to activate. + +--- + +## Capabilities + +### Video Generation (Veo 3.1) +* Generate high-quality videos from text prompts +* Support for various video styles and durations +* Advanced video synthesis capabilities + +### Image Generation (Imagen) +* Create images from text descriptions +* Support for various styles and resolutions +* High-quality image synthesis + +--- + +## Authentication + +This skill requires Google Cloud authentication. The tool will: + +1. **Check for existing credentials**: Automatically detect Application Default Credentials (ADC) +2. **Provide setup instructions**: If credentials are missing, display OAuth login URL and setup steps +3. **Guide authentication**: Walk you through the Google Cloud authentication flow + +**Important Limitation**: Service account JWT authentication is not yet fully implemented. Please use the gcloud CLI method for authentication. + +### Setting up authentication + +If you haven't authenticated yet, you'll need to: + +1. Have a Google Cloud project with Vertex AI API enabled +2. Set up Application Default Credentials using the recommended method: + * **Recommended**: Run `gcloud auth application-default login` + * Alternative: Set `GOOGLE_APPLICATION_CREDENTIALS` environment variable (requires gcloud CLI as fallback) + +**Note**: Service account JSON key files are detected but not yet supported for direct authentication. The tool will guide you to use gcloud CLI instead. + +The tool will detect your authentication status and provide specific instructions if needed. + +--- + +## Inputs and outputs + +### Inputs you should expect + +* **For video generation**: + * Text prompt describing the video to generate + * Optional: Duration, style preferences, aspect ratio + * Google Cloud project ID + * Location/region (default: us-central1) + +* **For image generation**: + * Text prompt describing the image to create + * Optional: Style, resolution, number of images + * Google Cloud project ID + * Location/region (default: us-central1) + +### Outputs you must produce + +* **For video generation**: + * Video file URL or download link + * Generation metadata (model used, parameters) + * Status and completion information + +* **For image generation**: + * Image file URL(s) or base64-encoded data + * Generation metadata + * Status information + +--- + +## Tool Usage + +### Library Functions + +| Function | Tool Path | Description | +| :--- | :--- | :--- | +| `vertex-ai` | `os://skills/vertex-ai/vertex-ai.ts` | Main tool for interacting with Vertex AI APIs | + +### Command Structure + +The `vertex-ai.ts` tool supports the following operations: + +```bash +# Generate a video +deno run --allow-net --allow-env --allow-read vertex-ai.ts generate-video \ + --prompt "A serene sunset over mountains" \ + --project "your-project-id" \ + --location "us-central1" + +# Generate an image +deno run --allow-net --allow-env --allow-read vertex-ai.ts generate-image \ + --prompt "A futuristic cityscape" \ + --project "your-project-id" \ + --location "us-central1" + +# Check authentication status +deno run --allow-net --allow-env --allow-read vertex-ai.ts check-auth + +# Show help +deno run vertex-ai.ts --help +``` + +--- + +## Workflow + +When a user requests video or image generation: + +1. **Understand the request** + * Identify if it's video or image generation + * Extract the text prompt + * Note any specific requirements (style, duration, resolution) + +2. **Check authentication** + * Use the tool to verify Google Cloud credentials + * If not authenticated, guide the user through setup + +3. **Generate content** + * Call the appropriate generation command + * Pass the prompt and any parameters + * Monitor the generation process + +4. **Deliver results** + * Provide the generated content URL or data + * Share any relevant metadata + * Confirm successful generation + +--- + +## Error Handling + +The tool handles common errors: + +* **Authentication errors**: Provides clear setup instructions +* **API errors**: Displays error messages and suggests fixes +* **Network errors**: Indicates connectivity issues +* **Permission errors**: Guides on enabling Vertex AI API + +Always check the tool's output for specific error messages and follow the suggested remediation steps. + +--- + +## Examples + +### Example 1: Generate a video + +```bash +# User request: "Generate a video of a robot walking through a forest" +deno run --allow-net --allow-env --allow-read vertex-ai.ts generate-video \ + --prompt "A humanoid robot walking through a lush green forest" \ + --project "my-project" \ + --duration "5s" +``` + +### Example 2: Generate an image + +```bash +# User request: "Create an image of a futuristic car" +deno run --allow-net --allow-env --allow-read vertex-ai.ts generate-image \ + --prompt "A sleek futuristic electric car in chrome finish" \ + --project "my-project" \ + --style "photorealistic" +``` + +--- + +## Best Practices + +1. **Clear prompts**: Use descriptive, specific prompts for better results +2. **Project setup**: Ensure Vertex AI API is enabled in your Google Cloud project +3. **Region selection**: Choose a region close to your location for better performance +4. **Cost awareness**: Video and image generation incur costs; monitor your usage +5. **Prompt refinement**: Iterate on prompts to achieve desired results + +--- + +## Limitations + +* Requires active Google Cloud project with billing enabled +* Subject to Vertex AI API quotas and limits +* Video generation may take several minutes depending on duration +* Content must comply with Google Cloud's usage policies +* Some features may be region-specific +* **Service account JWT authentication not yet implemented** - use gcloud CLI instead +* Maximum 10 images per generation request (use multiple requests for more) + +--- + +## Non-goals + +This skill does **not**: + +* Manage Google Cloud project setup or billing +* Store or manage generated content long-term +* Provide video or image editing capabilities +* Handle content moderation or filtering +* Manage API quotas or cost optimization + +Those aspects are the responsibility of the user's Google Cloud configuration and organizational policies. diff --git a/os/skills/vertex-ai/vertex-ai.test.ts b/os/skills/vertex-ai/vertex-ai.test.ts new file mode 100644 index 0000000..b57a9d1 --- /dev/null +++ b/os/skills/vertex-ai/vertex-ai.test.ts @@ -0,0 +1,111 @@ +/** + * vertex-ai.test.ts + * Unit tests for Vertex AI skill tool + * Copyright (c) 2025 Ship.Fail + * Licensed under the Public Prompt License - Apache Variant (PPL-A) + */ + +import { assertEquals, assertExists } from "jsr:@std/assert"; + +/** + * Note: These are documentation tests for expected behavior. + * Full integration tests require Google Cloud credentials and will be skipped in CI. + */ + +Deno.test("vertex-ai tool should export main function", async () => { + // This test verifies the tool can be imported + const module = await import("./vertex-ai.ts"); + assertExists(module, "Module should be importable"); +}); + +Deno.test("vertex-ai tool structure validation", () => { + // Verify the tool file exists and has the correct structure + const toolPath = new URL("./vertex-ai.ts", import.meta.url); + assertExists(toolPath, "Tool file should exist"); +}); + +/** + * Expected behavior tests (documented, not executable without credentials) + */ + +Deno.test({ + name: "vertex-ai generate-video should validate required parameters", + ignore: true, // Requires GCP credentials + fn: async () => { + // Expected behavior: + // - Should require --prompt parameter + // - Should require --project parameter + // - Should default --location to us-central1 + // - Should accept optional --duration, --style, --aspect-ratio + }, +}); + +Deno.test({ + name: "vertex-ai generate-image should validate required parameters", + ignore: true, // Requires GCP credentials + fn: async () => { + // Expected behavior: + // - Should require --prompt parameter + // - Should require --project parameter + // - Should default --location to us-central1 + // - Should accept optional --style, --resolution, --num-images + }, +}); + +Deno.test({ + name: "vertex-ai check-auth should detect credentials", + ignore: true, // Requires GCP credentials + fn: async () => { + // Expected behavior: + // - Should check for GOOGLE_APPLICATION_CREDENTIALS env var + // - Should attempt to use gcloud CLI + // - Should provide setup instructions if not authenticated + // - Should return 0 exit code if authenticated, 1 if not + }, +}); + +Deno.test({ + name: "vertex-ai should handle API errors gracefully", + ignore: true, // Requires GCP credentials + fn: async () => { + // Expected behavior: + // - Should catch and display 403 errors with troubleshooting tips + // - Should handle network errors + // - Should provide clear error messages + // - Should exit with non-zero code on errors + }, +}); + +// Help message format tests +Deno.test("vertex-ai help message should be well-formatted", async () => { + // The help message should include: + // - Command usage + // - List of commands (generate-video, generate-image, check-auth) + // - Options with descriptions + // - Examples + // - Authentication section + + // This can be validated by reading the HELP_MESSAGE constant + const content = await Deno.readTextFile(new URL("./vertex-ai.ts", import.meta.url)); + + // Check for key sections in help + assertEquals(content.includes("vertex-ai - Google Vertex AI Module Tool"), true); + assertEquals(content.includes("generate-video"), true); + assertEquals(content.includes("generate-image"), true); + assertEquals(content.includes("check-auth"), true); + assertEquals(content.includes("--help"), true); + assertEquals(content.includes("--prompt"), true); + assertEquals(content.includes("--project"), true); +}); + +// Command structure tests +Deno.test("vertex-ai should use parseArgs from JSR", async () => { + const content = await Deno.readTextFile(new URL("./vertex-ai.ts", import.meta.url)); + assertEquals(content.includes('import { parseArgs } from "jsr:@std/cli/parse-args"'), true); +}); + +Deno.test("vertex-ai should have proper license header", async () => { + const content = await Deno.readTextFile(new URL("./vertex-ai.ts", import.meta.url)); + assertEquals(content.includes("Copyright (c) 2025 Ship.Fail"), true); + assertEquals(content.includes("Licensed under the Public Prompt License"), true); +}); diff --git a/os/skills/vertex-ai/vertex-ai.ts b/os/skills/vertex-ai/vertex-ai.ts new file mode 100644 index 0000000..ad5bc18 --- /dev/null +++ b/os/skills/vertex-ai/vertex-ai.ts @@ -0,0 +1,471 @@ +#!/usr/bin/env -S deno run --allow-net --allow-env --allow-read + +/** + * vertex-ai.ts + * Google Vertex AI integration tool for PromptWar̊e ØS + * Copyright (c) 2025 Ship.Fail + * Licensed under the Public Prompt License - Apache Variant (PPL-A) + */ + +import { parseArgs } from "jsr:@std/cli/parse-args"; + +// Model constants +const MODEL_VEO_3_1 = "veo-3.1"; +const MODEL_IMAGEN = "imagegeneration"; + +// Validation constants +const MAX_NUM_IMAGES = 10; + +// Type definitions +interface VideoPrediction { + videoUri?: string; + jobId?: string; +} + +interface ImagePrediction { + bytesBase64Encoded?: string; + imageUri?: string; +} + +interface VertexAIConfig { + project: string; + location: string; + apiEndpoint: string; +} + +const HELP_MESSAGE = ` +vertex-ai - Google Vertex AI Module Tool + +Usage: + vertex-ai [options] + +Commands: + generate-video Generate a video using Veo 3.1 model + generate-image Generate an image using Imagen model + check-auth Check authentication status + +Options: + --help, -h Show this help message + --prompt, -p Text prompt for generation (required for generate-*) + --project Google Cloud project ID (required) + --location, -l Region location (default: us-central1) + --duration, -d Video duration (e.g., "5s", "10s") + --style, -s Style for generation (e.g., "photorealistic", "cinematic") + --aspect-ratio, -a Aspect ratio (e.g., "16:9", "9:16", "1:1") + --resolution, -r Image resolution (e.g., "1024x1024", "512x512") + --num-images, -n Number of images to generate (default: 1) + +Examples: + # Generate a video + vertex-ai generate-video \\ + --prompt "A robot walking through a forest" \\ + --project "my-project" \\ + --duration "5s" + + # Generate an image + vertex-ai generate-image \\ + --prompt "A futuristic cityscape" \\ + --project "my-project" \\ + --style "photorealistic" + + # Check authentication + vertex-ai check-auth + +Authentication: + This tool requires Google Cloud authentication. Set up credentials using: + - gcloud auth application-default login (recommended) + - GOOGLE_APPLICATION_CREDENTIALS environment variable + + Note: Service account JWT authentication is not yet implemented. + Please use gcloud CLI for authentication. +`; + +/** + * Get access token from Application Default Credentials + * Note: Service account JWT authentication is not fully implemented + */ +async function getAccessToken(): Promise { + try { + // Try to get credentials from environment + const credsPath = Deno.env.get("GOOGLE_APPLICATION_CREDENTIALS"); + + if (credsPath) { + const credsContent = await Deno.readTextFile(credsPath); + const creds = JSON.parse(credsContent); + + // For service account, we need to create a JWT and exchange for access token + // This functionality is not yet implemented + if (creds.type === "service_account") { + console.error("✗ Service account JWT authentication not implemented"); + console.error("\nPlease use gcloud CLI instead:"); + console.error(" $ gcloud auth application-default login"); + console.error("\nAlternatively, use user credentials instead of a service account."); + return null; + } + } + + // Try to use gcloud command if available + const command = new Deno.Command("gcloud", { + args: ["auth", "application-default", "print-access-token"], + stdout: "piped", + stderr: "piped", + }); + + const output = await command.output(); + + if (output.success) { + const token = new TextDecoder().decode(output.stdout).trim(); + return token; + } + + return null; + } catch (error) { + // Log authentication errors for debugging + if (error instanceof Deno.errors.NotFound) { + // Credentials file not found - this is expected if not configured + return null; + } + if (error instanceof Error) { + console.error(`Authentication check error: ${error.message}`); + } + return null; + } +} + +/** + * Check authentication status + */ +async function checkAuth(): Promise { + console.log("Checking Google Cloud authentication...\n"); + + const token = await getAccessToken(); + + if (token) { + console.log("✓ Authentication successful!"); + console.log("✓ Access token obtained"); + return true; + } else { + console.log("✗ Authentication failed or not configured\n"); + console.log("To set up authentication, use one of these methods:\n"); + console.log("1. Application Default Credentials (ADC):"); + console.log(" $ gcloud auth application-default login\n"); + console.log("2. Service Account:"); + console.log(" $ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json\n"); + console.log("3. OAuth 2.0 (for interactive use):"); + console.log(" Visit: https://console.cloud.google.com/apis/credentials\n"); + console.log("After setting up credentials, run this command again to verify."); + return false; + } +} + +/** + * Make authenticated request to Vertex AI API + */ +async function makeVertexAIRequest( + config: VertexAIConfig, + endpoint: string, + body: unknown, +): Promise { + const token = await getAccessToken(); + + if (!token) { + throw new Error("No authentication token available. Run 'check-auth' for setup instructions."); + } + + const url = `https://${config.location}-${config.apiEndpoint}/v1/projects/${config.project}/locations/${config.location}/${endpoint}`; + + console.log(`Calling Vertex AI API: ${endpoint}`); + + const response = await fetch(url, { + method: "POST", + headers: { + "Authorization": `Bearer ${token}`, + "Content-Type": "application/json", + }, + body: JSON.stringify(body), + }); + + return response; +} + +/** + * Generate a video using Veo 3.1 model + */ +async function generateVideo(options: { + prompt: string; + project: string; + location: string; + duration?: string; + style?: string; + aspectRatio?: string; +}): Promise { + console.log("Generating video with Veo 3.1...\n"); + console.log(`Prompt: ${options.prompt}`); + console.log(`Project: ${options.project}`); + console.log(`Location: ${options.location}`); + if (options.duration) console.log(`Duration: ${options.duration}`); + if (options.style) console.log(`Style: ${options.style}`); + if (options.aspectRatio) console.log(`Aspect Ratio: ${options.aspectRatio}`); + console.log(); + + const config: VertexAIConfig = { + project: options.project, + location: options.location, + apiEndpoint: "aiplatform.googleapis.com", + }; + + const requestBody = { + instances: [{ + prompt: options.prompt, + ...(options.duration && { duration: options.duration }), + ...(options.style && { style: options.style }), + ...(options.aspectRatio && { aspectRatio: options.aspectRatio }), + }], + }; + + try { + const response = await makeVertexAIRequest( + config, + `publishers/google/models/${MODEL_VEO_3_1}:predict`, + requestBody, + ); + + if (!response.ok) { + const errorText = await response.text(); + console.error(`API Error (${response.status}): ${errorText}`); + + if (response.status === 403) { + console.log("\nTroubleshooting:"); + console.log("1. Ensure Vertex AI API is enabled in your project"); + console.log("2. Check that you have the necessary permissions"); + console.log("3. Verify your project ID is correct"); + } + + Deno.exit(1); + } + + const result = await response.json(); + console.log("\n✓ Video generation request submitted successfully!"); + console.log("\nResponse:"); + console.log(JSON.stringify(result, null, 2)); + + if (result.predictions && result.predictions[0]) { + const prediction = result.predictions[0] as VideoPrediction; + if (prediction.videoUri) { + console.log(`\n✓ Video URL: ${prediction.videoUri}`); + } + if (prediction.jobId) { + console.log(`\n✓ Job ID: ${prediction.jobId}`); + console.log(" (Video generation may take several minutes. Check status with this job ID)"); + } + } + } catch (error) { + console.error("\nError generating video:"); + if (error instanceof Error) { + console.error(error.message); + } else { + console.error(error); + } + Deno.exit(1); + } +} + +/** + * Generate an image using Imagen model + */ +async function generateImage(options: { + prompt: string; + project: string; + location: string; + style?: string; + resolution?: string; + numImages?: number; +}): Promise { + console.log("Generating image with Imagen...\n"); + console.log(`Prompt: ${options.prompt}`); + console.log(`Project: ${options.project}`); + console.log(`Location: ${options.location}`); + if (options.style) console.log(`Style: ${options.style}`); + if (options.resolution) console.log(`Resolution: ${options.resolution}`); + if (options.numImages) console.log(`Number of images: ${options.numImages}`); + console.log(); + + const config: VertexAIConfig = { + project: options.project, + location: options.location, + apiEndpoint: "aiplatform.googleapis.com", + }; + + const requestBody = { + instances: [{ + prompt: options.prompt, + ...(options.style && { style: options.style }), + ...(options.resolution && { resolution: options.resolution }), + }], + parameters: { + sampleCount: options.numImages || 1, + }, + }; + + try { + const response = await makeVertexAIRequest( + config, + `publishers/google/models/${MODEL_IMAGEN}:predict`, + requestBody, + ); + + if (!response.ok) { + const errorText = await response.text(); + console.error(`API Error (${response.status}): ${errorText}`); + + if (response.status === 403) { + console.log("\nTroubleshooting:"); + console.log("1. Ensure Vertex AI API is enabled in your project"); + console.log("2. Check that you have the necessary permissions"); + console.log("3. Verify your project ID is correct"); + } + + Deno.exit(1); + } + + const result = await response.json(); + console.log("\n✓ Image generation completed successfully!"); + console.log("\nResponse:"); + console.log(JSON.stringify(result, null, 2)); + + if (result.predictions) { + console.log(`\n✓ Generated ${result.predictions.length} image(s)`); + result.predictions.forEach((pred: ImagePrediction, i: number) => { + if (pred.bytesBase64Encoded) { + console.log(` Image ${i + 1}: Base64 data available (length: ${pred.bytesBase64Encoded.length})`); + } + if (pred.imageUri) { + console.log(` Image ${i + 1}: ${pred.imageUri}`); + } + }); + } + } catch (error) { + console.error("\nError generating image:"); + if (error instanceof Error) { + console.error(error.message); + } else { + console.error(error); + } + Deno.exit(1); + } +} + +/** + * Main entry point + */ +async function main() { + const args = parseArgs(Deno.args, { + boolean: ["help"], + string: ["prompt", "project", "location", "duration", "style", "aspect-ratio", "resolution", "num-images"], + alias: { + h: "help", + p: "prompt", + l: "location", + d: "duration", + s: "style", + a: "aspect-ratio", + r: "resolution", + n: "num-images", + }, + default: { + location: "us-central1", + "num-images": "1", + }, + }); + + if (args.help || args._.length === 0) { + console.log(HELP_MESSAGE); + Deno.exit(0); + } + + const command = args._[0]?.toString(); + + switch (command) { + case "check-auth": { + const isAuthenticated = await checkAuth(); + Deno.exit(isAuthenticated ? 0 : 1); + break; + } + + case "generate-video": { + if (!args.prompt) { + console.error("Error: --prompt is required for video generation"); + console.log("\nUse --help for usage information"); + Deno.exit(1); + } + if (!args.project) { + console.error("Error: --project is required"); + console.log("\nUse --help for usage information"); + Deno.exit(1); + } + + await generateVideo({ + prompt: args.prompt, + project: args.project, + location: args.location, + duration: args.duration, + style: args.style, + aspectRatio: args["aspect-ratio"], + }); + break; + } + + case "generate-image": { + if (!args.prompt) { + console.error("Error: --prompt is required for image generation"); + console.log("\nUse --help for usage information"); + Deno.exit(1); + } + if (!args.project) { + console.error("Error: --project is required"); + console.log("\nUse --help for usage information"); + Deno.exit(1); + } + + // Validate num-images parameter + const numImagesStr = args["num-images"]; + const numImages = parseInt(numImagesStr, 10); + + if (isNaN(numImages)) { + console.error("Error: --num-images must be a valid integer"); + Deno.exit(1); + } + + if (numImages < 1) { + console.error("Error: --num-images must be at least 1"); + Deno.exit(1); + } + + if (numImages > MAX_NUM_IMAGES) { + console.error(`Error: --num-images cannot exceed ${MAX_NUM_IMAGES}`); + console.error("(To generate more images, make multiple requests)"); + Deno.exit(1); + } + + await generateImage({ + prompt: args.prompt, + project: args.project, + location: args.location, + style: args.style, + resolution: args.resolution, + numImages: numImages, + }); + break; + } + + default: + console.error(`Unknown command: ${command}`); + console.log("\nAvailable commands: generate-video, generate-image, check-auth"); + console.log("Use --help for more information"); + Deno.exit(1); + } +} + +if (import.meta.main) { + main(); +}