glab-agent 0.1.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.
- package/LICENSE +21 -0
- package/README.md +152 -0
- package/bin/glab-agent.mjs +18 -0
- package/package.json +59 -0
- package/src/local-agent/agent-config.ts +315 -0
- package/src/local-agent/agent-provider.ts +59 -0
- package/src/local-agent/agent-runner.ts +244 -0
- package/src/local-agent/claude-runner.ts +136 -0
- package/src/local-agent/cli.ts +1497 -0
- package/src/local-agent/codex-runner.ts +153 -0
- package/src/local-agent/gitlab-glab-client.ts +722 -0
- package/src/local-agent/health-server.ts +56 -0
- package/src/local-agent/heartbeat.ts +33 -0
- package/src/local-agent/log-rotate.ts +56 -0
- package/src/local-agent/logger.ts +92 -0
- package/src/local-agent/metrics.ts +51 -0
- package/src/local-agent/mr-actions.ts +121 -0
- package/src/local-agent/notifier.ts +190 -0
- package/src/local-agent/process-manager.ts +193 -0
- package/src/local-agent/reply-runner.ts +111 -0
- package/src/local-agent/repo-cache.ts +144 -0
- package/src/local-agent/report.ts +183 -0
- package/src/local-agent/skill-import.ts +344 -0
- package/src/local-agent/skill-inject.ts +109 -0
- package/src/local-agent/skill-parse.ts +47 -0
- package/src/local-agent/smoke-test.ts +443 -0
- package/src/local-agent/state-store.ts +186 -0
- package/src/local-agent/token-check.ts +37 -0
- package/src/local-agent/watcher.ts +1226 -0
- package/src/local-agent/wiki-sync.ts +290 -0
- package/src/local-agent/worktree-manager.ts +141 -0
- package/src/text.ts +16 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 glab-agent contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# glab-agent
|
|
2
|
+
|
|
3
|
+
GitLab-native AI agent orchestration platform. Define coding agents in YAML, manage them through GitLab's native interface, and enforce team standards through shared Skills.
|
|
4
|
+
|
|
5
|
+
## Why Local Agent?
|
|
6
|
+
|
|
7
|
+
| | Cloud Agents (Devin, Jules) | **glab-agent** |
|
|
8
|
+
|---|---|---|
|
|
9
|
+
| Code location | Uploaded to cloud sandbox | **Stays on your GitLab** |
|
|
10
|
+
| Data security | Code passes through third-party servers | **Code never leaves your network** |
|
|
11
|
+
| Customization | Platform defaults | **YAML-defined agents, skills, triggers** |
|
|
12
|
+
| Team standards | Each person configures individually | **Shared Skills enforce team conventions** |
|
|
13
|
+
| Provider lock-in | Single model | **Claude, Codex — switch with one line** (Gemini planned) |
|
|
14
|
+
| Observability | Platform UI | **Reuses your existing GitLab boards** |
|
|
15
|
+
|
|
16
|
+
## Quick Start
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
# Install globally
|
|
20
|
+
npm install -g glab-agent
|
|
21
|
+
|
|
22
|
+
# Initialize in your project
|
|
23
|
+
cd your-project
|
|
24
|
+
glab-agent init my-bot --provider claude
|
|
25
|
+
|
|
26
|
+
# Set your GitLab token
|
|
27
|
+
echo "GITLAB_TOKEN=glpat-xxx" >> .glab-agent/.env
|
|
28
|
+
|
|
29
|
+
# Start the agent
|
|
30
|
+
glab-agent start my-bot
|
|
31
|
+
|
|
32
|
+
# Verify it's running
|
|
33
|
+
glab-agent status
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Then go to any GitLab issue and type `@your-bot-username` — the agent will pick it up, write code, run tests, and create a merge request.
|
|
37
|
+
|
|
38
|
+
## How It Works
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
┌─────────── GitLab ───────────┐
|
|
42
|
+
│ Issues → @mention → Todos │
|
|
43
|
+
└──────────────┬───────────────┘
|
|
44
|
+
│ poll
|
|
45
|
+
┌──────▼──────┐
|
|
46
|
+
│ Watcher │ Filter by triggers, manage labels
|
|
47
|
+
└──────┬──────┘
|
|
48
|
+
┌──────▼──────┐
|
|
49
|
+
│ AI Agent │ Read issue → Code → Test → Push → Create MR
|
|
50
|
+
│ (Claude/ │
|
|
51
|
+
│ Codex) │
|
|
52
|
+
└─────────────┘
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**Watcher** polls GitLab for new todos, filters by your configured triggers, creates an isolated git worktree, and launches the AI agent. The **Agent** autonomously reads the issue, writes code, runs tests, commits, pushes, and creates a merge request.
|
|
56
|
+
|
|
57
|
+
## Agent Definition (YAML)
|
|
58
|
+
|
|
59
|
+
```yaml
|
|
60
|
+
# .glab-agent/agents/backend-bot.yaml
|
|
61
|
+
name: backend-bot
|
|
62
|
+
provider: claude
|
|
63
|
+
gitlab:
|
|
64
|
+
token_env: GITLAB_TOKEN
|
|
65
|
+
prompt:
|
|
66
|
+
preamble: "You are a backend engineer focused on API correctness and test coverage."
|
|
67
|
+
triggers:
|
|
68
|
+
labels: [backend]
|
|
69
|
+
actions: [mentioned]
|
|
70
|
+
skills:
|
|
71
|
+
- test-before-commit # Reference a shared skill by name
|
|
72
|
+
- security-scan
|
|
73
|
+
poll_interval_seconds: 60
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Shared Skills
|
|
77
|
+
|
|
78
|
+
Skills are reusable prompt fragments in SKILL.md format (YAML frontmatter + markdown body). Store them in `.glab-agent/skills/`:
|
|
79
|
+
|
|
80
|
+
```markdown
|
|
81
|
+
<!-- .glab-agent/skills/test-before-commit.md -->
|
|
82
|
+
---
|
|
83
|
+
name: test-before-commit
|
|
84
|
+
description: Run tests before committing
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
After modifying code, run tests to ensure they pass.
|
|
88
|
+
If tests fail, fix the issues before committing.
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Import skills from GitHub (including [Anthropic's official skill library](https://github.com/anthropics/skills)):
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
glab-agent skills # List all shared skills
|
|
95
|
+
glab-agent skills import anthropics/skills # Import from GitHub
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## CLI Commands
|
|
99
|
+
|
|
100
|
+
| Command | Description |
|
|
101
|
+
|---------|-------------|
|
|
102
|
+
| `glab-agent init [name]` | Initialize project and optionally create an agent |
|
|
103
|
+
| `glab-agent list` | List all agent definitions |
|
|
104
|
+
| `glab-agent start <name>` | Start agent as background watcher |
|
|
105
|
+
| `glab-agent stop <name>` | Stop agent |
|
|
106
|
+
| `glab-agent status [name]` | Show agent status with heartbeat |
|
|
107
|
+
| `glab-agent watch <name>` | Real-time agent monitoring |
|
|
108
|
+
| `glab-agent run-once <name>` | Run single watcher cycle |
|
|
109
|
+
| `glab-agent cancel <name>` | Cancel current task |
|
|
110
|
+
| `glab-agent retry <name> <iid>` | Retry a failed issue |
|
|
111
|
+
| `glab-agent why <iid>` | Diagnose why an issue wasn't picked up |
|
|
112
|
+
| `glab-agent history <name>` | Show execution history |
|
|
113
|
+
| `glab-agent skills` | List shared skills |
|
|
114
|
+
| `glab-agent gc <name>` | Clean up old worktrees |
|
|
115
|
+
| `glab-agent doctor` | Check prerequisites |
|
|
116
|
+
|
|
117
|
+
All commands support `--project <dir>` to target a different directory.
|
|
118
|
+
|
|
119
|
+
## GitLab as Control Plane
|
|
120
|
+
|
|
121
|
+
No extra dashboards needed. glab-agent maps to GitLab's native features:
|
|
122
|
+
|
|
123
|
+
- **User Status** — Agent shows as 🟢 Ready / 🔧 Working / 🔴 Offline in team member list
|
|
124
|
+
- **Issue Board** — Agent work appears as cards moving through Backlog → In Progress → In Review → Done
|
|
125
|
+
- **Labels** — Status machine: `In Progress`, `In Review`, `Done`, `Error`
|
|
126
|
+
- **@mention** — Trigger agents just like you'd assign a colleague
|
|
127
|
+
- **User Bio** — Agent's skills and instructions visible on its GitLab profile
|
|
128
|
+
|
|
129
|
+
## Reliability
|
|
130
|
+
|
|
131
|
+
- **API retry** — Exponential backoff (1s/2s/4s) for transient failures
|
|
132
|
+
- **Circuit breaker** — Stops polling after 5 consecutive failures, backs off up to 5 minutes
|
|
133
|
+
- **Atomic state writes** — Write-to-temp + rename prevents corruption on crash
|
|
134
|
+
- **Heartbeat monitoring** — Detects stale watchers via `glab-agent status`
|
|
135
|
+
- **Graceful shutdown** — SIGTERM updates GitLab status before exit
|
|
136
|
+
|
|
137
|
+
## Observability
|
|
138
|
+
|
|
139
|
+
- **Structured logging** — JSON Lines format via `GITLAB_AGENT_LOG_FORMAT=json`
|
|
140
|
+
- **Metrics** — Append-only JSONL per agent (`.glab-agent/metrics/`)
|
|
141
|
+
- **Heartbeat** — Cycle count and last error in `.glab-agent/heartbeat/`
|
|
142
|
+
|
|
143
|
+
## Requirements
|
|
144
|
+
|
|
145
|
+
- Node.js >= 20
|
|
146
|
+
- [glab](https://gitlab.com/gitlab-org/cli) — GitLab CLI
|
|
147
|
+
- [claude](https://claude.ai/code) or [codex](https://openai.com/codex) — AI coding CLI
|
|
148
|
+
- A GitLab personal access token with `api` scope
|
|
149
|
+
|
|
150
|
+
## License
|
|
151
|
+
|
|
152
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { execFileSync } from "node:child_process";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { resolve, dirname } from "node:path";
|
|
5
|
+
|
|
6
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
7
|
+
const installDir = resolve(dirname(__filename), "..");
|
|
8
|
+
const tsxPath = resolve(installDir, "node_modules", ".bin", "tsx");
|
|
9
|
+
const cliPath = resolve(installDir, "src", "local-agent", "cli.ts");
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
execFileSync(tsxPath, [cliPath, ...process.argv.slice(2)], {
|
|
13
|
+
stdio: "inherit",
|
|
14
|
+
env: process.env
|
|
15
|
+
});
|
|
16
|
+
} catch (error) {
|
|
17
|
+
process.exit(error?.status ?? 1);
|
|
18
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "glab-agent",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Multi-agent GitLab To-Do watcher with YAML-defined agents, skills, and GitLab registry.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "glab-agent contributors",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"gitlab",
|
|
10
|
+
"agent",
|
|
11
|
+
"ai",
|
|
12
|
+
"coding-agent",
|
|
13
|
+
"claude",
|
|
14
|
+
"codex",
|
|
15
|
+
"automation",
|
|
16
|
+
"devops",
|
|
17
|
+
"cli"
|
|
18
|
+
],
|
|
19
|
+
"packageManager": "pnpm@9.15.9",
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=20.0.0"
|
|
22
|
+
},
|
|
23
|
+
"bin": {
|
|
24
|
+
"glab-agent": "./bin/glab-agent.mjs"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"bin/",
|
|
28
|
+
"src/"
|
|
29
|
+
],
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsc -p tsconfig.build.json",
|
|
35
|
+
"test": "vitest run",
|
|
36
|
+
"agent": "tsx src/local-agent/cli.ts",
|
|
37
|
+
"smoke-test": "zsh scripts/smoke-test.sh",
|
|
38
|
+
"smoke-test:full": "zsh scripts/smoke-test.sh --wait-finish",
|
|
39
|
+
"docs-lint": "zsh scripts/docs-lint.sh",
|
|
40
|
+
"check": "zsh scripts/ci.sh",
|
|
41
|
+
"check:full": "zsh scripts/ci.sh --full",
|
|
42
|
+
"deploy": "zsh scripts/cd.sh",
|
|
43
|
+
"deploy:fast": "zsh scripts/cd.sh --skip-ci",
|
|
44
|
+
"gitlab:up": "docker compose -f docker/docker-compose.gitlab.yml up -d",
|
|
45
|
+
"gitlab:down": "docker compose -f docker/docker-compose.gitlab.yml down",
|
|
46
|
+
"gitlab:seed": "bash docker/seed-gitlab.sh",
|
|
47
|
+
"gitlab:reset": "docker compose -f docker/docker-compose.gitlab.yml down -v && rm -rf docker/data docker/ssh .env.test.local .test-repo && echo 'Reset complete'",
|
|
48
|
+
"gitlab:setup": "pnpm gitlab:up && pnpm gitlab:seed"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/node": "^20.17.30",
|
|
52
|
+
"typescript": "^5.8.3",
|
|
53
|
+
"vitest": "^1.6.1"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"tsx": "^4.19.3",
|
|
57
|
+
"yaml": "^2.8.3"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { parse as parseYaml } from "yaml";
|
|
4
|
+
import { parseSkillMd } from "./skill-parse.js";
|
|
5
|
+
|
|
6
|
+
// ── Agent Definition Schema ──────────────────────────────────────────────────
|
|
7
|
+
|
|
8
|
+
export interface AgentSkill {
|
|
9
|
+
name: string;
|
|
10
|
+
description: string;
|
|
11
|
+
prompt_inject: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface AgentTriggers {
|
|
15
|
+
actions: string[];
|
|
16
|
+
labels: string[];
|
|
17
|
+
exclude_labels: string[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface AgentPromptConfig {
|
|
21
|
+
preamble?: string;
|
|
22
|
+
append?: string;
|
|
23
|
+
template?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface AgentGitlabConfig {
|
|
27
|
+
host?: string;
|
|
28
|
+
project_id?: number;
|
|
29
|
+
token_env: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface AgentNotifications {
|
|
33
|
+
webhook_url?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface AgentDefinition {
|
|
37
|
+
name: string;
|
|
38
|
+
display_name: string;
|
|
39
|
+
description: string;
|
|
40
|
+
|
|
41
|
+
gitlab: AgentGitlabConfig;
|
|
42
|
+
provider: "codex" | "claude";
|
|
43
|
+
repo_path?: string;
|
|
44
|
+
|
|
45
|
+
prompt: AgentPromptConfig;
|
|
46
|
+
triggers: AgentTriggers;
|
|
47
|
+
skills: AgentSkill[];
|
|
48
|
+
skill_refs: string[];
|
|
49
|
+
|
|
50
|
+
poll_interval_seconds: number;
|
|
51
|
+
concurrency: number;
|
|
52
|
+
timeout_seconds?: number;
|
|
53
|
+
model?: string;
|
|
54
|
+
notifications: AgentNotifications;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// ── Loader ───────────────────────────────────────────────────────────────────
|
|
58
|
+
|
|
59
|
+
type RawValue = Record<string, unknown>;
|
|
60
|
+
|
|
61
|
+
function asString(value: unknown, fallback: string): string {
|
|
62
|
+
return typeof value === "string" ? value : fallback;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function asNumber(value: unknown, fallback: number): number {
|
|
66
|
+
const n = Number(value);
|
|
67
|
+
return Number.isFinite(n) && n > 0 ? n : fallback;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function asStringArray(value: unknown): string[] {
|
|
71
|
+
if (!Array.isArray(value)) return [];
|
|
72
|
+
return value.filter((v): v is string => typeof v === "string");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function parseSkills(raw: unknown): AgentSkill[] {
|
|
76
|
+
if (!Array.isArray(raw)) return [];
|
|
77
|
+
return raw
|
|
78
|
+
.filter((item): item is RawValue => typeof item === "object" && item !== null)
|
|
79
|
+
.map((item) => ({
|
|
80
|
+
name: asString(item.name, "unnamed"),
|
|
81
|
+
description: asString(item.description, ""),
|
|
82
|
+
prompt_inject: asString(item.prompt_inject, "")
|
|
83
|
+
}))
|
|
84
|
+
.filter((s) => s.prompt_inject.length > 0);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function parseSkillRefs(raw: unknown): string[] {
|
|
88
|
+
if (!Array.isArray(raw)) return [];
|
|
89
|
+
return raw.filter((item): item is string => typeof item === "string");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function parseTriggers(raw: unknown): AgentTriggers {
|
|
93
|
+
if (typeof raw !== "object" || raw === null) {
|
|
94
|
+
return { actions: ["mentioned", "directly_addressed"], labels: [], exclude_labels: [] };
|
|
95
|
+
}
|
|
96
|
+
const obj = raw as RawValue;
|
|
97
|
+
return {
|
|
98
|
+
actions: asStringArray(obj.actions).length > 0
|
|
99
|
+
? asStringArray(obj.actions)
|
|
100
|
+
: ["mentioned", "directly_addressed"],
|
|
101
|
+
labels: asStringArray(obj.labels),
|
|
102
|
+
exclude_labels: asStringArray(obj.exclude_labels)
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function parseNotifications(raw: unknown): AgentNotifications {
|
|
107
|
+
if (typeof raw !== "object" || raw === null) return {};
|
|
108
|
+
const obj = raw as RawValue;
|
|
109
|
+
return {
|
|
110
|
+
webhook_url: typeof obj.webhook_url === "string" ? obj.webhook_url : undefined
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function parsePromptConfig(raw: unknown): AgentPromptConfig {
|
|
115
|
+
if (typeof raw !== "object" || raw === null) return {};
|
|
116
|
+
const obj = raw as RawValue;
|
|
117
|
+
return {
|
|
118
|
+
preamble: typeof obj.preamble === "string" ? obj.preamble : undefined,
|
|
119
|
+
append: typeof obj.append === "string" ? obj.append : undefined,
|
|
120
|
+
template: typeof obj.template === "string" ? obj.template : undefined
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function parseGitlabConfig(raw: unknown): AgentGitlabConfig {
|
|
125
|
+
if (typeof raw !== "object" || raw === null) {
|
|
126
|
+
return { token_env: "GITLAB_TOKEN" };
|
|
127
|
+
}
|
|
128
|
+
const obj = raw as RawValue;
|
|
129
|
+
return {
|
|
130
|
+
host: typeof obj.host === "string" ? obj.host : undefined,
|
|
131
|
+
project_id: typeof obj.project_id === "number" ? obj.project_id : undefined,
|
|
132
|
+
token_env: asString(obj.token_env, "GITLAB_TOKEN")
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function parseAgentDefinition(raw: unknown, fileName: string): AgentDefinition {
|
|
137
|
+
if (typeof raw !== "object" || raw === null) {
|
|
138
|
+
throw new Error(`Invalid agent definition in ${fileName}: expected an object.`);
|
|
139
|
+
}
|
|
140
|
+
const obj = raw as RawValue;
|
|
141
|
+
const nameFromFile = path.basename(fileName, path.extname(fileName));
|
|
142
|
+
const name = asString(obj.name, nameFromFile);
|
|
143
|
+
|
|
144
|
+
const provider = asString(obj.provider, "claude");
|
|
145
|
+
if (provider !== "codex" && provider !== "claude") {
|
|
146
|
+
throw new Error(`Invalid provider "${provider}" in ${fileName}. Must be "codex" or "claude".`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
name,
|
|
151
|
+
display_name: asString(obj.display_name, name),
|
|
152
|
+
description: asString(obj.description, ""),
|
|
153
|
+
gitlab: parseGitlabConfig(obj.gitlab),
|
|
154
|
+
provider,
|
|
155
|
+
repo_path: typeof obj.repo_path === "string" ? obj.repo_path : undefined,
|
|
156
|
+
prompt: parsePromptConfig(obj.prompt),
|
|
157
|
+
triggers: parseTriggers(obj.triggers),
|
|
158
|
+
skills: parseSkills(obj.skills),
|
|
159
|
+
skill_refs: parseSkillRefs(obj.skills),
|
|
160
|
+
poll_interval_seconds: asNumber(obj.poll_interval_seconds, 60),
|
|
161
|
+
concurrency: asNumber(obj.concurrency, 1),
|
|
162
|
+
timeout_seconds: typeof obj.timeout_seconds === "number" && obj.timeout_seconds > 0
|
|
163
|
+
? obj.timeout_seconds
|
|
164
|
+
: undefined,
|
|
165
|
+
model: typeof obj.model === "string" ? obj.model : undefined,
|
|
166
|
+
notifications: parseNotifications(obj.notifications)
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export async function loadAgentDefinition(yamlPath: string): Promise<AgentDefinition> {
|
|
171
|
+
const content = await readFile(yamlPath, "utf8");
|
|
172
|
+
const raw = parseYaml(content);
|
|
173
|
+
return parseAgentDefinition(raw, path.basename(yamlPath));
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export async function loadAgentByName(agentsDir: string, name: string): Promise<AgentDefinition> {
|
|
177
|
+
const yamlPath = path.join(agentsDir, `${name}.yaml`);
|
|
178
|
+
return loadAgentDefinition(yamlPath);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export async function discoverAgents(agentsDir: string): Promise<AgentDefinition[]> {
|
|
182
|
+
let entries: string[];
|
|
183
|
+
try {
|
|
184
|
+
entries = await readdir(agentsDir);
|
|
185
|
+
} catch (error) {
|
|
186
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return [];
|
|
187
|
+
throw error;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const yamlFiles = entries
|
|
191
|
+
.filter((f) => f.endsWith(".yaml") || f.endsWith(".yml"))
|
|
192
|
+
.sort();
|
|
193
|
+
|
|
194
|
+
const definitions: AgentDefinition[] = [];
|
|
195
|
+
for (const file of yamlFiles) {
|
|
196
|
+
const def = await loadAgentDefinition(path.join(agentsDir, file));
|
|
197
|
+
// Skip files that are not agent definitions (e.g., openai.yaml interface-only)
|
|
198
|
+
if (def.description || def.skills.length > 0 || def.prompt.preamble || def.prompt.template) {
|
|
199
|
+
definitions.push(def);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return definitions;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const AGENT_DIR = ".glab-agent";
|
|
206
|
+
|
|
207
|
+
export function agentBaseDir(repoPath: string): string {
|
|
208
|
+
return path.join(repoPath, AGENT_DIR);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function agentAgentsDir(repoPath: string): string {
|
|
212
|
+
return path.join(repoPath, AGENT_DIR, "agents");
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export function agentEnvPath(repoPath: string): string {
|
|
216
|
+
return path.join(repoPath, AGENT_DIR, ".env");
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export function agentStateDir(repoPath: string): string {
|
|
220
|
+
return path.join(repoPath, AGENT_DIR, "state");
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function agentStatePath(repoPath: string, agentName: string): string {
|
|
224
|
+
return path.join(agentStateDir(repoPath), `${agentName}.json`);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function agentLogPath(repoPath: string, agentName: string): string {
|
|
228
|
+
return path.join(repoPath, AGENT_DIR, "logs", agentName);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export function agentWorktreeRoot(repoPath: string, agentName: string): string {
|
|
232
|
+
return path.join(repoPath, AGENT_DIR, "worktrees", agentName);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function agentPidPath(repoPath: string, agentName: string): string {
|
|
236
|
+
return path.join(repoPath, AGENT_DIR, "pid", `${agentName}.pid`);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function agentHeartbeatPath(repoPath: string, agentName: string): string {
|
|
240
|
+
return path.join(repoPath, AGENT_DIR, "heartbeat", `${agentName}.json`);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export function agentSkillsDir(repoPath: string): string {
|
|
244
|
+
return path.join(repoPath, AGENT_DIR, "skills");
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export async function loadSharedSkill(skillsDir: string, name: string): Promise<AgentSkill> {
|
|
248
|
+
// Try YAML first, then fall back to .md (SKILL.md format)
|
|
249
|
+
let content: string;
|
|
250
|
+
let format: "yaml" | "md";
|
|
251
|
+
try {
|
|
252
|
+
content = await readFile(path.join(skillsDir, `${name}.yaml`), "utf8");
|
|
253
|
+
format = "yaml";
|
|
254
|
+
} catch (yamlErr) {
|
|
255
|
+
if ((yamlErr as NodeJS.ErrnoException).code !== "ENOENT") throw yamlErr;
|
|
256
|
+
try {
|
|
257
|
+
content = await readFile(path.join(skillsDir, `${name}.md`), "utf8");
|
|
258
|
+
format = "md";
|
|
259
|
+
} catch (mdErr) {
|
|
260
|
+
if ((mdErr as NodeJS.ErrnoException).code !== "ENOENT") throw mdErr;
|
|
261
|
+
throw new Error(`Skill "${name}" not found as .yaml or .md in ${skillsDir}`);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (format === "md") {
|
|
266
|
+
const parsed = parseSkillMd(content);
|
|
267
|
+
if (!parsed) throw new Error(`Invalid SKILL.md format for "${name}"`);
|
|
268
|
+
return parsed;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const raw = parseYaml(content);
|
|
272
|
+
if (typeof raw !== "object" || raw === null) {
|
|
273
|
+
throw new Error(`Invalid skill definition in ${name}.yaml`);
|
|
274
|
+
}
|
|
275
|
+
const obj = raw as Record<string, unknown>;
|
|
276
|
+
return {
|
|
277
|
+
name: asString(obj.name, name),
|
|
278
|
+
description: asString(obj.description, ""),
|
|
279
|
+
prompt_inject: asString(obj.prompt_inject, "")
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export async function discoverSharedSkills(skillsDir: string): Promise<AgentSkill[]> {
|
|
284
|
+
let entries: string[];
|
|
285
|
+
try {
|
|
286
|
+
entries = await readdir(skillsDir);
|
|
287
|
+
} catch (error) {
|
|
288
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return [];
|
|
289
|
+
throw error;
|
|
290
|
+
}
|
|
291
|
+
const yamlFiles = entries.filter(f => f.endsWith(".yaml") || f.endsWith(".yml") || f.endsWith(".md")).sort();
|
|
292
|
+
const skills: AgentSkill[] = [];
|
|
293
|
+
for (const file of yamlFiles) {
|
|
294
|
+
const skill = await loadSharedSkill(skillsDir, path.basename(file, path.extname(file)));
|
|
295
|
+
if (skill.prompt_inject.length > 0) {
|
|
296
|
+
skills.push(skill);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return skills;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export async function resolveSkills(def: AgentDefinition, skillsDir: string): Promise<AgentSkill[]> {
|
|
303
|
+
const resolved: AgentSkill[] = [...def.skills];
|
|
304
|
+
for (const ref of def.skill_refs) {
|
|
305
|
+
try {
|
|
306
|
+
const shared = await loadSharedSkill(skillsDir, ref);
|
|
307
|
+
if (!resolved.some(s => s.name === shared.name)) {
|
|
308
|
+
resolved.push(shared);
|
|
309
|
+
}
|
|
310
|
+
} catch {
|
|
311
|
+
// Shared skill not found, skip silently
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return resolved;
|
|
315
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export type AgentProvider = "codex" | "claude";
|
|
2
|
+
|
|
3
|
+
export type AgentProviderSource = "env_override" | "environment_default";
|
|
4
|
+
|
|
5
|
+
export interface ResolvedAgentProvider {
|
|
6
|
+
provider: AgentProvider;
|
|
7
|
+
source: AgentProviderSource;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function normalizeEnvValue(value?: string): string | undefined {
|
|
11
|
+
const trimmed = value?.trim();
|
|
12
|
+
return trimmed ? trimmed : undefined;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function isClaudeCodeEnvironment(env: NodeJS.ProcessEnv = process.env): boolean {
|
|
16
|
+
return Boolean(
|
|
17
|
+
normalizeEnvValue(env.CLAUDE_CODE_ENTRYPOINT) ??
|
|
18
|
+
normalizeEnvValue(env.CLAUDE_CODE_REMOTE) ??
|
|
19
|
+
normalizeEnvValue(env.CLAUDE_CODE_ENVIRONMENT_KIND)
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function resolveAgentProvider(env: NodeJS.ProcessEnv = process.env): ResolvedAgentProvider {
|
|
24
|
+
const explicit = normalizeEnvValue(env.AGENT_PROVIDER);
|
|
25
|
+
|
|
26
|
+
if (explicit) {
|
|
27
|
+
if (explicit === "codex" || explicit === "claude") {
|
|
28
|
+
return {
|
|
29
|
+
provider: explicit,
|
|
30
|
+
source: "env_override"
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
throw new Error(`AGENT_PROVIDER must be one of: codex, claude. Received: ${explicit}`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
provider: isClaudeCodeEnvironment(env) ? "claude" : "codex",
|
|
39
|
+
source: "environment_default"
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function getAgentDisplayName(provider: AgentProvider): string {
|
|
44
|
+
return provider === "claude" ? "Claude Code" : "Codex";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function getAgentNotePrefixes(provider: AgentProvider): {
|
|
48
|
+
accepted: string;
|
|
49
|
+
completed: string;
|
|
50
|
+
failed: string;
|
|
51
|
+
} {
|
|
52
|
+
const displayName = getAgentDisplayName(provider);
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
accepted: `${displayName} 已接单。`,
|
|
56
|
+
completed: `${displayName} 已完成。`,
|
|
57
|
+
failed: `${displayName} 执行失败。`
|
|
58
|
+
};
|
|
59
|
+
}
|