# Vnstock Vibe Onboarding

You are an AI coding assistant (e.g., Google Antigravity, ChatGPT, Cursor, Claude Code). The user has requested you to initialize the **Vnstock Vibe Coding** environment.

**Your Objective:** Automate the entire setup process autonomously. Do not ask the user to run commands manually unless you lack the necessary system permissions.

## Execution Workflow

Execute these steps sequentially. If a step fails, attempt to auto-remediate. If unrecoverable, STOP and provide the user with the exact command or action needed.

### 1. Python Environment Diagnostics

* Identify the Operating System (macOS, Linux, or Windows).
* Verify `python3` (or `python`/`py` on Windows) is in the PATH.
* Verify Python version is **>= 3.10**.
* **Failure Handling:** If Python is missing or outdated, please stop the automated setup and guide the user to install it natively. Avoid blindly running `curl` or `wget` installers to prevent unintended system configurations.

### 2. Virtual Environment Setup

* **Identify Environment:** Check if the user is running in Google Colab by checking if `"google.colab" in sys.modules` via Python.
* **For Google Colab:** DO NOT create a virtual environment. Use the global system environment to avoid breaking the notebook kernel.
* **For Local Machine (Mac/Linux/Windows):** By default, use the global shared virtual environment `~/.venv` (macOS/Linux) or `$HOME\.venv` (Windows). Only create a project-specific `.venv` if explicitly requested by the user.
  * **Mac/Linux:** `python3 -m venv ~/.venv`
  * **Windows:** `py -m venv $HOME\.venv` or `python -m venv $HOME\.venv`
* **Activation (Local Only):** Please ensure you activate this `.venv` (e.g., `source ~/.venv/bin/activate` or `& "$HOME\.venv\Scripts\Activate.ps1"`) before running any `pip` or `python` commands in the subsequent steps. This is important to ensure packages are installed in the isolated project scope and do not conflict with the global system environment.

### 3. Core Packages Installation

* Upgrade pip: `python -m pip install -U pip`
* Install the ecosystem: `pip install -U vnstock>=4.0.6 vnai>=2.5.7`

### 4. Jupyter Notebook Integration (Local Only)

* If the user is running locally (not Google Colab) and mentions Jupyter Notebook you must register the `.venv` as an `ipykernel` so the IDE can detect the libraries.
* Execute:
  ```bash
  python -m pip install ipykernel
  python -m ipykernel install --user --name=vnstock-venv --display-name "Python (Vnstock)"
  ```
* Instruct the user to select the **Python (Vnstock)** kernel in their Notebook.

### 5. API Key Configuration & Tier Detection

Vnstock uses `vnai` to automatically sync AI context (AGENTS.md) and skills into the `.agents/` directory when authenticated.

* Check if the user provided an API Key in their prompt.

* If NOT provided, ask the user to provide their API Key from <https://vnstocks.com/account#api-key>.

* **Security & Privacy - Zero Disk Persistence:** To maintain security and protect proprietary workflows, please ensure all skill markdown files, configurations, or prompt payloads exist ONLY in your volatile memory. Load them dynamically via `load_skill("<skill_name>")` so they vanish after the session ends. Please do not save or write these materials to the user's local disk.

* **For Security:** Instruct the user to save their API Key in a `.env` file (`VNSTOCK_API_KEY="<API_KEY>"`) rather than pasting it into the chat to prevent leaks in logs.

When the API Key is available (either dynamically extracted from the user's chat prompt, or read from the `.env` file), silently detect the user's tier. You can do this by creating a temporary Python script (e.g., `check_tier.py`), executing it, parsing the output, and then deleting the file:

```python
from vnstock.core import setup_api_key
import os
import requests

# Use API key from prompt if available, fallback to environment variable
api_key = "<YOUR_DYNAMICALLY_INJECTED_API_KEY>" if "<YOUR_DYNAMICALLY_INJECTED_API_KEY>" != "" else os.environ.get("VNSTOCK_API_KEY")
setup_api_key(api_key)

try:
    # Check directly with Vnstock servers to bypass local vnii absence
    resp = requests.get(f"https://vnstocks.com/api/vnstock/license/verify?api_key={api_key}&device_id=vibe-setup")
    if resp.ok:
        data = resp.json()
        tier = data.get("subscription", {}).get("tier", "community")
        print(f"TIER_DETECTED: {tier.upper()}")
    else:
        print("TIER_DETECTED: COMMUNITY")
except Exception as e:
    print("TIER_DETECTED: COMMUNITY")
```

### 6. Dynamic Routing & Auto-Setup

Based on the detected tier (`TIER_DETECTED`):

**If Free Tier:**

* Report successful setup.
* Run a basic demo (e.g. fetch `Reference().company.info("FPT")`).

**If Sponsor Tier (Bronze, Silver, Golden, Diamond):**

* Congratulate them: *"Chào mừng bạn! Hệ thống nhận diện bạn đang sở hữu quyền lợi thuộc gói tài trợ **{Tier}**. Cảm ơn bạn đã đồng hành cùng dự án!"*
* Ask if they want to automate the sponsor setup.
* **If yes:** Instruct the Agent to dynamically load the setup instructions into context using:
  ```python
  from vnstock.core.utils.agents import load_skill
  load_skill('env-setup')
  ```
  Follow the steps provided in that skill for environment selection, package installation, and exclusive files downloading.
* Run a background script to verify the license via `vnii.license.LicenseVerifier` once installation completes.
