berget 1.3.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/.env.example +5 -0
  2. package/.github/workflows/publish.yml +56 -0
  3. package/.github/workflows/test.yml +38 -0
  4. package/AGENTS.md +184 -0
  5. package/README.md +177 -38
  6. package/TODO.md +2 -0
  7. package/blog-post.md +176 -0
  8. package/dist/index.js +11 -8
  9. package/dist/package.json +14 -3
  10. package/dist/src/commands/api-keys.js +4 -2
  11. package/dist/src/commands/chat.js +182 -23
  12. package/dist/src/commands/code.js +1424 -0
  13. package/dist/src/commands/index.js +2 -0
  14. package/dist/src/constants/command-structure.js +12 -0
  15. package/dist/src/schemas/opencode-schema.json +1121 -0
  16. package/dist/src/services/chat-service.js +10 -10
  17. package/dist/src/services/cluster-service.js +1 -1
  18. package/dist/src/utils/default-api-key.js +2 -2
  19. package/dist/src/utils/env-manager.js +86 -0
  20. package/dist/src/utils/error-handler.js +10 -3
  21. package/dist/src/utils/markdown-renderer.js +4 -4
  22. package/dist/src/utils/opencode-validator.js +122 -0
  23. package/dist/src/utils/token-manager.js +2 -2
  24. package/dist/tests/commands/chat.test.js +109 -0
  25. package/dist/tests/commands/code.test.js +414 -0
  26. package/dist/tests/utils/env-manager.test.js +148 -0
  27. package/dist/tests/utils/opencode-validator.test.js +103 -0
  28. package/dist/vitest.config.js +9 -0
  29. package/index.ts +67 -32
  30. package/opencode.json +182 -0
  31. package/package.json +14 -3
  32. package/src/client.ts +20 -20
  33. package/src/commands/api-keys.ts +93 -60
  34. package/src/commands/auth.ts +4 -2
  35. package/src/commands/billing.ts +6 -3
  36. package/src/commands/chat.ts +291 -97
  37. package/src/commands/clusters.ts +2 -2
  38. package/src/commands/code.ts +1696 -0
  39. package/src/commands/index.ts +2 -0
  40. package/src/commands/models.ts +3 -3
  41. package/src/commands/users.ts +2 -2
  42. package/src/constants/command-structure.ts +112 -58
  43. package/src/schemas/opencode-schema.json +991 -0
  44. package/src/services/api-key-service.ts +1 -1
  45. package/src/services/auth-service.ts +27 -25
  46. package/src/services/chat-service.ts +37 -44
  47. package/src/services/cluster-service.ts +5 -5
  48. package/src/services/collaborator-service.ts +3 -3
  49. package/src/services/flux-service.ts +2 -2
  50. package/src/services/helm-service.ts +2 -2
  51. package/src/services/kubectl-service.ts +3 -6
  52. package/src/types/api.d.ts +1032 -1010
  53. package/src/types/json.d.ts +3 -3
  54. package/src/utils/default-api-key.ts +54 -42
  55. package/src/utils/env-manager.ts +98 -0
  56. package/src/utils/error-handler.ts +24 -15
  57. package/src/utils/logger.ts +12 -12
  58. package/src/utils/markdown-renderer.ts +18 -18
  59. package/src/utils/opencode-validator.ts +134 -0
  60. package/src/utils/token-manager.ts +35 -23
  61. package/tests/commands/chat.test.ts +129 -0
  62. package/tests/commands/code.test.ts +505 -0
  63. package/tests/utils/env-manager.test.ts +199 -0
  64. package/tests/utils/opencode-validator.test.ts +118 -0
  65. package/tsconfig.json +8 -8
  66. package/vitest.config.ts +8 -0
  67. package/-27b-it +0 -0
package/.env.example ADDED
@@ -0,0 +1,5 @@
1
+ # OpenCode Configuration
2
+ # Copy this file to .env and adjust values as needed
3
+
4
+ # Berget AI API Key - Required for authentication
5
+ BERGET_API_KEY=your_api_key_here
@@ -0,0 +1,56 @@
1
+ name: Publish to NPM
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ release:
8
+ types: [published]
9
+
10
+ jobs:
11
+ test:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - name: Checkout code
15
+ uses: actions/checkout@v4
16
+
17
+ - name: Setup Node.js
18
+ uses: actions/setup-node@v4
19
+ with:
20
+ node-version: '18'
21
+ cache: 'npm'
22
+
23
+ - name: Install dependencies
24
+ run: npm ci
25
+
26
+ - name: Run tests
27
+ run: npm run test:run
28
+
29
+ - name: Build project
30
+ run: npm run build
31
+
32
+ publish:
33
+ needs: test
34
+ runs-on: ubuntu-latest
35
+ if: github.ref == 'refs/heads/main' || github.event_name == 'release'
36
+ steps:
37
+ - name: Checkout code
38
+ uses: actions/checkout@v4
39
+
40
+ - name: Setup Node.js
41
+ uses: actions/setup-node@v4
42
+ with:
43
+ node-version: '18'
44
+ registry-url: 'https://registry.npmjs.org'
45
+ cache: 'npm'
46
+
47
+ - name: Install dependencies
48
+ run: npm ci
49
+
50
+ - name: Build project
51
+ run: npm run build
52
+
53
+ - name: Publish to NPM
54
+ run: npm publish
55
+ env:
56
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
@@ -0,0 +1,38 @@
1
+ name: Test
2
+
3
+ on:
4
+ pull_request:
5
+ branches:
6
+ - main
7
+ push:
8
+ branches:
9
+ - main
10
+
11
+ jobs:
12
+ test:
13
+ runs-on: ubuntu-latest
14
+ strategy:
15
+ matrix:
16
+ node-version: [18, 20]
17
+
18
+ steps:
19
+ - name: Checkout code
20
+ uses: actions/checkout@v4
21
+
22
+ - name: Setup Node.js ${{ matrix.node-version }}
23
+ uses: actions/setup-node@v4
24
+ with:
25
+ node-version: ${{ matrix.node-version }}
26
+ cache: 'npm'
27
+
28
+ - name: Install dependencies
29
+ run: npm ci
30
+
31
+ - name: Run tests
32
+ run: npm run test:run
33
+
34
+ - name: Build project
35
+ run: npm run build
36
+
37
+ - name: Check TypeScript
38
+ run: npx tsc --noEmit
package/AGENTS.md ADDED
@@ -0,0 +1,184 @@
1
+ # Berget Code Agents
2
+
3
+ This document describes the specialized agents available in this project for use with OpenCode.
4
+
5
+ ## Available Agents
6
+
7
+ ### Primary Agents
8
+
9
+ #### fullstack
10
+
11
+ Router/coordinator agent for full-stack development with schema-driven architecture. Handles routing between different personas based on file paths and task requirements.
12
+
13
+ **Use when:**
14
+
15
+ - Working across multiple parts of a monorepo
16
+ - Need to coordinate between frontend, backend, devops, and app
17
+ - Starting new projects and need to determine tech stack
18
+
19
+ **Key features:**
20
+
21
+ - Schema-driven development (database → OpenAPI → types)
22
+ - Automatic routing to appropriate persona
23
+ - Tech stack discovery and recommendations
24
+
25
+ #### frontend
26
+
27
+ Builds Scandinavian, type-safe UIs with React, Tailwind, and Shadcn.
28
+
29
+ **Use when:**
30
+
31
+ - Working with React components (.tsx files)
32
+ - Frontend development in /apps/frontend
33
+ - UI/UX implementation
34
+
35
+ **Key features:**
36
+
37
+ - Design system integration
38
+ - Semantic tokens and accessibility
39
+ - Props-first component architecture
40
+
41
+ #### backend
42
+
43
+ Functional, modular Koa + TypeScript services with schema-first approach and code quality focus.
44
+
45
+ **Use when:**
46
+
47
+ - Working with Koa routers and services
48
+ - Backend development in /services
49
+ - API development and database work
50
+
51
+ **Key features:**
52
+
53
+ - Zod validation and OpenAPI generation
54
+ - Code quality and refactoring principles
55
+ - PR workflow integration
56
+
57
+ #### devops
58
+
59
+ Declarative GitOps infrastructure with FluxCD, Kustomize, Helm, and operators.
60
+
61
+ **Use when:**
62
+
63
+ - Working with Kubernetes manifests
64
+ - Infrastructure in /infra or /k8s
65
+ - CI/CD and deployment configurations
66
+
67
+ **Key features:**
68
+
69
+ - GitOps workflows
70
+ - Operator-first approach
71
+ - SemVer with release candidates
72
+
73
+ #### app
74
+
75
+ Expo + React Native applications with props-first architecture and offline awareness.
76
+
77
+ **Use when:**
78
+
79
+ - Mobile app development with Expo
80
+ - React Native projects in /apps/app
81
+ - Cross-platform mobile development
82
+
83
+ **Key features:**
84
+
85
+ - Shared design tokens with frontend
86
+ - Offline-first architecture
87
+ - Expo integration
88
+
89
+ ### Subagents
90
+
91
+ #### security
92
+
93
+ Security specialist for penetration testing, OWASP compliance, and vulnerability assessments.
94
+
95
+ **Use when:**
96
+
97
+ - Need security review of code changes
98
+ - OWASP Top 10 compliance checks
99
+ - Vulnerability assessments
100
+
101
+ **Key features:**
102
+
103
+ - OWASP standards compliance
104
+ - Security best practices
105
+ - Actionable remediation strategies
106
+
107
+ #### quality
108
+
109
+ Quality assurance specialist for testing, building, and PR management.
110
+
111
+ **Use when:**
112
+
113
+ - Need to run test suites and build processes
114
+ - Creating or updating pull requests
115
+ - Monitoring GitHub for reviewer comments
116
+ - Ensuring code quality standards
117
+
118
+ **Key features:**
119
+
120
+ - Comprehensive testing and building workflows
121
+ - PR creation and management
122
+ - GitHub integration for reviewer feedback
123
+ - CLI command expertise for quality assurance
124
+
125
+ ## Usage
126
+
127
+ ### Switching Agents
128
+
129
+ Use the `<tab>` key to cycle through primary agents during a session.
130
+
131
+ ### Manual Agent Selection
132
+
133
+ Use commands to switch to specific agents:
134
+
135
+ - `/fullstack` - Switch to Fullstack agent
136
+ - `/frontend` - Switch to Frontend agent
137
+ - `/backend` - Switch to Backend agent
138
+ - `/devops` - Switch to DevOps agent
139
+ - `/app` - Switch to App agent
140
+ - `/quality` - Switch to Quality agent for testing and PR management
141
+
142
+ ### Using Subagents
143
+
144
+ Mention subagents with `@` symbol:
145
+
146
+ ```
147
+ @security review this authentication implementation
148
+ @quality run tests and create PR for these changes
149
+ ```
150
+
151
+ ## Routing Rules
152
+
153
+ The fullstack agent automatically routes tasks based on file patterns:
154
+
155
+ - `/apps/frontend` or `.tsx` files → frontend
156
+ - `/apps/app` or Expo/React Native → app
157
+ - `/infra`, `/k8s`, FluxCD, Helm → devops
158
+ - `/services`, Koa routers → backend
159
+
160
+ ## Configuration
161
+
162
+ All agents are configured in `opencode.json` with:
163
+
164
+ - Specialized prompts and temperature settings
165
+ - Appropriate tool permissions
166
+ - Model optimizations for their specific tasks
167
+
168
+ ## Environment Setup
169
+
170
+ Copy `.env.example` to `.env` and configure:
171
+
172
+ ```
173
+ BERGET_API_KEY=your_api_key_here
174
+ ```
175
+
176
+ ## Workflow
177
+
178
+ All agents follow these principles:
179
+
180
+ - Never work directly in main branch
181
+ - Follow branch strategy and commit conventions
182
+ - Create PRs for new functionality
183
+ - Run tests before committing
184
+ - Address reviewer feedback promptly
package/README.md CHANGED
@@ -1,33 +1,193 @@
1
1
  # Berget CLI
2
2
 
3
- A command-line interface for interacting with the Berget AI infrastructure.
3
+ A command-line tool for interacting with Berget AI's infrastructure and AI models.
4
4
 
5
5
  ## Installation
6
6
 
7
7
  ```bash
8
8
  npm install -g berget
9
+ # or use directly with npx
10
+ npx berget --help
9
11
  ```
10
12
 
11
13
  ## Authentication
12
14
 
13
- The CLI uses OAuth-based authentication with automatic token refresh.
15
+ Before you can use the CLI, you need to authenticate:
14
16
 
15
- ### Login
17
+ ```bash
18
+ # Login with OAuth
19
+ npx berget auth login
20
+
21
+ # Create an API key
22
+ npx berget api-keys create --name "My CLI Key"
23
+
24
+ # Or use environment variable
25
+ export BERGET_API_KEY=sk_ber_your_api_key_here
26
+ ```
27
+
28
+ ## Chat Command
29
+
30
+ ### Basic Usage
31
+
32
+ ```bash
33
+ # Interactive chat session
34
+ npx berget chat run
35
+
36
+ # Use specific model
37
+ npx berget chat run openai/gpt-oss
38
+
39
+ # Send direct message
40
+ npx berget chat run openai/gpt-oss "Explain what Docker is"
41
+
42
+ # Use pipe for input
43
+ echo "What is Kubernetes?" | npx berget chat run openai/gpt-oss
44
+ ```
45
+
46
+ ### Practical Use Cases
47
+
48
+ #### 1. Git Commit Messages
16
49
 
17
50
  ```bash
18
- berget auth login
51
+ # Generate commit message from git diff
52
+ git diff | npx berget chat run openai/gpt-oss "Create a conventional commit message for this diff. Reply with only the message:"
53
+
54
+ # Use as alias
55
+ alias gitcommit='git diff | npx berget chat run openai/gpt-oss "Generate a conventional commit message for this diff. Reply with only the commit message, nothing else:"'
19
56
  ```
20
57
 
21
- This will open a browser window to authenticate with Berget. After successful authentication, your access token and refresh token will be stored securely in `~/.berget/auth.json`.
58
+ #### 2. Code Review and Explanations
22
59
 
23
- ### Token Refresh
60
+ ```bash
61
+ # Explain code
62
+ cat src/main.js | npx berget chat run openai/gpt-oss "Explain what this code does:"
24
63
 
25
- The CLI automatically handles token refresh when:
64
+ # Find bugs
65
+ cat problematic-file.py | npx berget chat run openai/gpt-oss "Analyze this code and find potential bugs:"
26
66
 
27
- 1. The access token is about to expire (within 10 minutes of expiration)
28
- 2. A request returns a 401 Unauthorized error
67
+ # Improvement suggestions
68
+ git diff | npx berget chat run openai/gpt-oss "Give suggestions for improvements to this code change:"
69
+ ```
29
70
 
30
- The refresh mechanism uses the stored refresh token to obtain a new access token without requiring you to log in again. If the refresh token itself is expired or invalid, you'll be prompted to log in again.
71
+ #### 3. Documentation
72
+
73
+ ```bash
74
+ # Generate README
75
+ ls -la | npx berget chat run openai/gpt-oss "Create a README.md for this project based on the file structure:"
76
+
77
+ # Comment code
78
+ cat uncommented-code.js | npx berget chat run openai/gpt-oss "Add JSDoc comments to this code:"
79
+ ```
80
+
81
+ #### 4. System Administration
82
+
83
+ ```bash
84
+ # Analyze logs
85
+ tail -n 100 /var/log/nginx/error.log | npx berget chat run openai/gpt-oss "Analyze these error logs and suggest solutions:"
86
+
87
+ # Explain commands
88
+ npx berget chat run openai/gpt-oss "Explain what this bash command does: find . -name '*.js' -exec grep -l 'TODO' {} \;"
89
+ ```
90
+
91
+ ## Useful Bash/Zsh Aliases
92
+
93
+ Add these to your `~/.bashrc`, `~/.zshrc` or similar:
94
+
95
+ ```bash
96
+ # Git-related aliases
97
+ alias gai='git diff | npx berget chat run openai/gpt-oss "Generate a conventional commit message for this diff. Reply with only the commit message, nothing else:"'
98
+ alias gexplain='git log --oneline -10 | npx berget chat run openai/gpt-oss "Explain what these commits do:"'
99
+ alias gsec='~/bin/security-check'
100
+
101
+ # Code-related aliases
102
+ alias explain='npx berget chat run openai/gpt-oss "Explain this code:"'
103
+ alias review='npx berget chat run openai/gpt-oss "Review this code and give improvement suggestions:"'
104
+ alias debug='npx berget chat run openai/gpt-oss "Find and explain potential bugs in this code:"'
105
+
106
+ # Documentation aliases
107
+ alias docgen='npx berget chat run openai/gpt-oss "Generate documentation for this code:"'
108
+ alias readme='ls -la | npx berget chat run openai/gpt-oss "Create a README.md for this project:"'
109
+
110
+ # System aliases
111
+ alias loganalyze='npx berget chat run openai/gpt-oss "Analyze these logs and suggest solutions:"'
112
+ alias cmdexplain='npx berget chat run openai/gpt-oss "Explain this command:"'
113
+
114
+ # Quick AI assistant
115
+ alias ai='npx berget chat run openai/gpt-oss'
116
+ alias ask='npx berget chat run openai/gpt-oss'
117
+ ```
118
+
119
+ ## Advanced Examples
120
+
121
+ See the `examples/` folder for complete scripts:
122
+
123
+ - **smart-commit.sh** - Automatic generation of conventional commit messages
124
+ - **ai-review.sh** - AI-driven code review
125
+ - **security-check.sh** - Security review of commits
126
+
127
+ ```bash
128
+ # Copy example scripts
129
+ cp examples/*.sh ~/bin/
130
+ chmod +x ~/bin/*.sh
131
+
132
+ # Use them
133
+ ~/bin/smart-commit.sh
134
+ ~/bin/ai-review.sh src/main.js
135
+ ~/bin/security-check.sh
136
+ ```
137
+
138
+ ## Environment Variables
139
+
140
+ ```bash
141
+ # API key (recommended)
142
+ export BERGET_API_KEY=sk_ber_your_api_key_here
143
+
144
+ # Debug mode
145
+ export LOG_LEVEL=debug
146
+
147
+ # Custom API base URL (if using your own instance)
148
+ export API_BASE_URL=https://your-custom-api.example.com
149
+ ```
150
+
151
+ ## Tips and Tricks
152
+
153
+ 1. **Use pipes**: Combine with other Unix tools for powerful workflows
154
+ 2. **Short prompts**: Be specific but concise in your prompts for best results
155
+ 3. **Streaming**: Streaming is enabled by default for faster responses
156
+ 4. **Model selection**: Experiment with different models for different tasks
157
+ 5. **Aliases**: Create aliases for common use cases to save time
158
+
159
+ ## Command Reference
160
+
161
+ - `auth login` - Login to Berget
162
+ - `auth logout` - Logout from Berget
163
+ - `auth whoami` - Show current user information
164
+ - `api-keys list` - List API keys
165
+ - `api-keys create` - Create a new API key
166
+ - `models list` - List available AI models
167
+ - `chat run` - Start a chat session with an AI model
168
+ - `chat list` - List available chat models
169
+
170
+ For a complete list of commands, run:
171
+
172
+ ```bash
173
+ npx berget --help
174
+ ```
175
+
176
+ ## Troubleshooting
177
+
178
+ ```bash
179
+ # Enable debug mode
180
+ npx berget --debug chat run openai/gpt-oss "test"
181
+
182
+ # Check authentication
183
+ npx berget auth whoami
184
+
185
+ # List available models
186
+ npx berget chat list
187
+
188
+ # Check API key status
189
+ npx berget api-keys list
190
+ ```
31
191
 
32
192
  ## Development
33
193
 
@@ -36,12 +196,12 @@ The refresh mechanism uses the stored refresh token to obtain a new access token
36
196
  Clone the repository and install dependencies:
37
197
 
38
198
  ```bash
39
- git clone https://github.com/berget/cli.git
199
+ git clone https://github.com/berget-ai/cli.git
40
200
  cd cli
41
201
  npm install
42
202
  ```
43
203
 
44
- ### Testing Locally
204
+ ### Test Locally
45
205
 
46
206
  Use the `start` script to test the CLI locally with the `--local` flag:
47
207
 
@@ -62,31 +222,10 @@ npm start -- auth whoami
62
222
  npm start -- auth whoami --debug
63
223
  ```
64
224
 
65
- The `--debug` flag provides detailed information about token refresh attempts and API responses.
66
-
67
- ### Testing Token Refresh
68
-
69
- To test the token refresh mechanism:
225
+ ## Contributing
70
226
 
71
- 1. Log in with `npm start -- auth login`
72
- 2. Make a request that requires authentication, like `npm start -- auth whoami`
73
- 3. To force a token refresh, you can:
74
- - Wait until the token is close to expiration
75
- - Manually edit `~/.berget/auth.json` and set `expires_at` to a past timestamp
76
- - Use the `--debug` flag to see the token refresh process in action
227
+ Berget CLI is open source. Contributions are welcome!
77
228
 
78
- ## Commands
79
-
80
- - `auth login` - Log in to Berget
81
- - `auth logout` - Log out from Berget
82
- - `auth whoami` - Show current user information
83
- - `api-keys list` - List API keys
84
- - `api-keys create` - Create a new API key
85
- - `models list` - List available AI models
86
- - `chat run` - Start a chat session with an AI model
87
-
88
- For a complete list of commands, run:
89
-
90
- ```bash
91
- berget --help
92
- ```
229
+ - GitHub: [berget-ai/cli](https://github.com/berget-ai/cli)
230
+ - Issues: [Report bugs](https://github.com/berget-ai/cli/issues)
231
+ - Documentation: [docs.berget.ai](https://docs.berget.ai)
package/TODO.md CHANGED
@@ -1,10 +1,12 @@
1
1
  # Berget CLI - TODO
2
2
 
3
3
  ## Implementerade funktioner
4
+
4
5
  - [x] Inloggning med BankID
5
6
  - [x] Skapa och hantera API-nycklar
6
7
 
7
8
  ## Kommande funktioner
9
+
8
10
  - [ ] Implementera riktiga API-anrop för klusterhantering
9
11
  - [ ] Implementera riktiga API-anrop för Flux-integration
10
12
  - [ ] Implementera riktiga API-anrop för Helm-kommandon