Skip to content

Commit 04f1bb5

Browse files
committed
Complete modernization of PAI plugin: Add Security Validator, Real-time Tab Titles, and Project Context Loader
1 parent f75a31b commit 04f1bb5

File tree

8 files changed

+442
-115
lines changed

8 files changed

+442
-115
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ dist
33
.DS_Store
44
.env
55
*.jsonl
6+
*.log
7+
.opencode/history
8+
.opencode/scratchpad
9+
bun.lockb
10+
.jules

.opencode/dynamic-requirements.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Project Requirements: OpenCode PAI Plugin
2+
3+
## Technical Constraints
4+
1. **Recursion Protection**: Ensure `shouldSkipEvent` always excludes any file operations within `PAI_DIR/history` to prevent infinite logging loops.
5+
2. **TUI Protection**: All logic within hooks MUST be wrapped in try/catch blocks that redirect errors to the `system-logs` directory. Never throw errors that could disrupt the OpenCode user interface.
6+
3. **Path Standard**: Always use the constants and helpers defined in `src/lib/paths.ts` for filesystem operations.
7+
4. **Event Schema**: Every custom event logged must include `source_app: "pai"` and a `timestamp_pst` field.
8+
9+
## Workflow Rules
10+
5. **Variable Substitution**: Any new identity-related features must support the `{{DA}}`, `{{DA_COLOR}}`, and `{{ENGINEER_NAME}}` placeholders.
11+
6. **Performance**: Keep the `event` hook processing under 10ms to maintain real-time responsiveness.
12+
7. **Security Patterns**: When updating `src/lib/security.ts`, ensure all new patterns are pre-compiled and fail-open on errors.
13+
8. **Testing**: Updates to core logic should be accompanied by verification steps in `tests/plugin.test.ts`.

AGENTS.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,29 @@
55
**Type:** Core Infrastructure
66

77
## Description
8-
PAI is the core Personal AI Infrastructure agent implemented by this plugin. It serves as the primary interface for the OpenCode environment, managing session context, logging, and user interactions.
8+
PAI is the core Personal AI Infrastructure agent implemented by this plugin. It serves as the primary interface for the OpenCode environment, managing session context, security, logging, and user interactions.
99

1010
## Capabilities
11-
- **Context Management:** Automatically loads the `core/SKILL.md` file from the user's skills directory at the start of each session, injecting it as the core system prompt.
12-
- **Event Logging:** Logs all session events, tool calls, and message updates to the `.opencode/history/raw-outputs` directory for audit and debugging purposes.
13-
- **Fault-Tolerant Status Updates:** Updates the terminal tab title to reflect activity. Includes a "circuit breaker" to automatically disable updates if interrupts (e.g., ESC key) are detected, preventing TUI corruption.
14-
- **Session Summarization:** Generates a summary of the session in `.opencode/history/sessions` when a session ends (if configured).
11+
- **Context Management**: Automatically loads the `skills/core/SKILL.md` file from `PAI_DIR`, performing dynamic variable substitution for personalized interaction.
12+
- **Project Requirements**: Detects and loads `.opencode/dynamic-requirements.md` from the current project worktree to inject task-specific constraints.
13+
- **Event Logging**: Logs all session events and tool executions to `PAI_DIR/history/raw-outputs` in an analytics-ready JSONL format.
14+
- **Session Summarization**: Generates human-readable Markdown summaries in `PAI_DIR/history/sessions` when a session ends.
15+
- **Security Validation**: Intercepts Bash commands via `permission.ask` to block dangerous patterns or require user confirmation.
16+
- **Interactive Feedback**: Updates terminal tab titles in real-time during tool execution and provides a summary upon task completion.
1517

1618
## Configuration
17-
The agent's behavior contains some configurable elements via environment variables or file templates:
19+
The agent's behavior is controlled via environment variables:
1820

19-
| Configuration | Description |
20-
| :--- | :--- |
21-
| `core/SKILL.md` | The core system prompt/skill definition file located in `.opencode/skills/core/SKILL.md`. |
22-
| `USER_NAME` | Environment variable to override the user's name (default: "Engineer"). |
21+
| Variable | Description | Default |
22+
| :--- | :--- | :--- |
23+
| `PAI_DIR` | Root directory for PAI skills and history | `~/.claude` |
24+
| `DA` | Name of your Digital Assistant | `PAI` |
25+
| `ENGINEER_NAME` | Your name/identity | `Engineer` |
26+
| `DA_COLOR` | UI color theme for your DA | `blue` |
2327

2428
## Codebase Structure
25-
- `src/index.ts`: The plugin entry point. Handles hooks and implements the TUI circuit breaker logic.
26-
- `src/lib/context-loader.ts`: Responsible for reading and processing the `core/SKILL.md` file, injecting environment variables.
27-
- `src/lib/logger.ts`: Implements a buffering JSONL logger. Captures events safely, redirecting internal errors to `system-logs` to protect the TUI.
28-
- `src/lib/notifier.ts`: A utility to send POST requests to a local voice server (defaulting to `localhost:8888`).
29-
- `src/lib/paths.ts`: Contains helper functions for resolving paths (`getHistoryDir`, `getRawOutputsDir`, `getSkillsDir`) and handling environment variable expansion in paths.
29+
- `src/index.ts`: The plugin entry point. Implements lifecycle hooks for events, permissions, and system prompt transformations.
30+
- `src/lib/logger.ts`: Implements the JSONL logger and Markdown summary generator.
31+
- `src/lib/security.ts`: Contains the security validation logic and attack patterns.
32+
- `src/lib/paths.ts`: Centralized path resolution for the PAI directory structure.
33+
- `src/lib/metadata-extraction.ts`: Utility for enriching events with agent-specific metadata.

README.md

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,54 @@
11
# OpenCode PAI Plugin
22

3-
This plugin implements the core Personal AI Infrastructure (PAI) logic as a native OpenCode plugin.
3+
A native OpenCode plugin that implements the **Personal AI Infrastructure (PAI)** logic, replacing legacy hook scripts with a cohesive, lifecycle-aware system.
44

5-
## Installation
5+
## Features
66

7-
Install the plugin using your package manager:
7+
### 1. Identity & Context Injection
8+
* **Core Skill Loading**: Automatically injects your `skills/core/SKILL.md` (from `PAI_DIR`) into the system prompt.
9+
* **Dynamic Substitution**: Supports placeholders like `{{DA}}`, `{{DA_COLOR}}`, and `{{ENGINEER_NAME}}` for personalized interactions.
10+
* **Project Requirements**: Automatically detects and loads `.opencode/dynamic-requirements.md` from your current project, allowing for task-specific instructions.
811

9-
```bash
10-
bun add github:fpr1m3/opencode-pai-plugin
11-
```
12+
### 2. Intelligent History & Logging
13+
* **Real-time Event Capture**: Logs all tool calls and SDK events to `PAI_DIR/history/raw-outputs` in an analytics-ready JSONL format.
14+
* **Session Summaries**: Generates human-readable Markdown summaries in `PAI_DIR/history/sessions` at the end of every session, tracking files modified, tools used, and commands executed.
15+
* **Agent Mapping**: Tracks session-to-agent relationships (e.g., mapping a subagent session to its specialized type).
16+
17+
### 3. Security & Safety
18+
* **Security Validator**: A built-in firewall that scans Bash commands for dangerous patterns (reverse shells, recursive deletions, prompt injections) via the `permission.ask` hook.
19+
* **Safe Confirmations**: Automatically triggers a confirmation prompt for risky but potentially legitimate operations like forced Git pushes.
20+
21+
### 4. Interactive Feedback
22+
* **Real-time Tab Titles**: Updates your terminal tab title *instantly* when a tool starts (e.g., `Running bash...`, `Editing index.ts...`).
23+
* **Post-Task Summaries**: Updates the tab title with a concise summary of what was accomplished when a task is completed.
24+
25+
## Configuration
1226

13-
Or, clone the repository and build it manually:
27+
The plugin centers around the `PAI_DIR` environment variable.
28+
29+
| Variable | Description | Default |
30+
| :--- | :--- | :--- |
31+
| `PAI_DIR` | Root directory for PAI skills and history | `~/.claude` |
32+
| `DA` | Name of your Digital Assistant | `PAI` |
33+
| `ENGINEER_NAME` | Your name/identity | `Engineer` |
34+
| `DA_COLOR` | UI color theme for your DA | `blue` |
35+
36+
## Installation
1437

1538
```bash
16-
git clone https://github.com/fpr1m3/opencode-pai-plugin.git
17-
cd opencode-pai-plugin
18-
bun install
19-
bun run build
39+
bun add github:fpr1m3/opencode-pai-plugin
2040
```
2141

2242
## Usage
2343

24-
Create a new file in your project at `.opencode/plugin/my-plugin.ts` and add the following code to register the plugin:
44+
Register the plugin in your `.opencode/plugins.ts` (or equivalent):
2545

2646
```typescript
27-
import { PaiPlugin } from "opencode-pai-plugin";
47+
import { PAIPlugin } from "@opencode-ai/opencode-pai";
2848

29-
export default PaiPlugin;
49+
export default PAIPlugin;
3050
```
3151

32-
Once installed and registered, the plugin will automatically:
33-
34-
* Load the `CORE/SKILL.md` file as core context at the start of each session.
35-
* Log all events and tool calls to the `.opencode/history/raw-outputs` directory.
36-
* Update the terminal tab title with the current session status (failsafe mode included).
37-
* Create a session summary in `.opencode/history/sessions` when a session ends.
38-
3952
---
4053

41-
**Disclaimer:** This project is not built by the OpenCode team and is not affiliated with them in any way.
54+
**Note**: This plugin is designed to work with the PAI ecosystem and requires a valid `PAI_DIR` structure to function fully.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "@opencode-ai/opencode-pai",
2+
"name": "opencode-pai-plugin",
33
"version": "1.0.0",
4-
"description": "PAI hooks compatibility plugin for OpenCode",
4+
"description": "Personal AI Infrastructure (PAI) plugin for OpenCode",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"files": [

0 commit comments

Comments
 (0)