mirror of
https://github.com/expo/agent-cli-detector.git
synced 2026-11-14 08:38:15 -10:00
Compare commits
16
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d2f8b90f53 | ||
|
|
e308d262b1 | ||
|
|
09f4c420ad | ||
|
|
9205525ad4 | ||
|
|
cf68eb6e0c | ||
|
|
b7c8ccd0aa | ||
|
|
1cad0bda77 | ||
|
|
8e4fdd3c6f | ||
|
|
238a06ac5f | ||
|
|
2369c9194f | ||
|
|
eecb49a358 | ||
|
|
9010110dea | ||
|
|
8738a0aa09 | ||
|
|
0855546c89 | ||
|
|
a9720cb533 | ||
|
|
b742350c0b |
No files matched your search
@@ -0,0 +1,41 @@
|
||||
name: publish
|
||||
|
||||
on:
|
||||
release:
|
||||
types: [published]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
id-token: write
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Node
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 24
|
||||
registry-url: https://registry.npmjs.org
|
||||
cache: npm
|
||||
|
||||
- name: Update npm
|
||||
run: npm install -g npm@latest
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm ci
|
||||
|
||||
- name: Verify release tag matches package.json version
|
||||
run: |
|
||||
PKG_VERSION="v$(node -p "require('./package.json').version")"
|
||||
if [ "$GITHUB_REF_NAME" != "$PKG_VERSION" ]; then
|
||||
echo "Release tag $GITHUB_REF_NAME does not match package.json version $PKG_VERSION"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: Publish
|
||||
run: npm publish
|
||||
@@ -1,6 +1,7 @@
|
||||
name: test
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
pull_request:
|
||||
push:
|
||||
|
||||
|
||||
@@ -2,60 +2,73 @@
|
||||
|
||||
Detect whether a JavaScript CLI is running inside a coding agent.
|
||||
|
||||
The package is designed around environment-variable detection and data-driven agent definitions. Parent-process inspection is available as an experimental opt-in feature for callers that can tolerate best-effort process-tree detection.
|
||||
Please [open an issue](https://github.com/expo/agent-cli-detector/issues/new)
|
||||
if your coding agent isn't properly detected :)
|
||||
|
||||
## Install
|
||||
[](https://www.npmjs.com/package/agent-cli-detector)
|
||||
[](https://github.com/expo/agent-cli-detector/actions)
|
||||
|
||||
Install from npm:
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
```bash
|
||||
npm install agent-cli-detector
|
||||
```
|
||||
|
||||
## Library usage
|
||||
## Usage
|
||||
|
||||
```ts
|
||||
import { detectAgent, isRunningFromAgent } from "agent-cli-detector";
|
||||
```js
|
||||
import { detectAgent } from "agent-cli-detector";
|
||||
|
||||
const result = detectAgent();
|
||||
|
||||
if (result.detected) {
|
||||
console.log(`running from ${result.agent.name}`);
|
||||
|
||||
if (result.agent.sessionId) {
|
||||
console.log(`session: ${result.agent.sessionId}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (isRunningFromAgent()) {
|
||||
// Adjust CLI behavior for agent-driven execution.
|
||||
console.log("The name of the coding agent is:", result.agent.name);
|
||||
} else {
|
||||
console.log("This program is not running inside a coding agent");
|
||||
}
|
||||
```
|
||||
|
||||
## CLI usage
|
||||
## Supported agents
|
||||
|
||||
```sh
|
||||
npx agent-cli-detector
|
||||
npx agent-cli-detector --json
|
||||
```
|
||||
Officially supported coding agents:
|
||||
|
||||
After installing the package, the CLI command is available as `detect-agent`:
|
||||
| Name | ID | Session ID |
|
||||
| ----------------------------------------------------------------- | ------------- | ---------- |
|
||||
| [Antigravity](https://antigravity.google/) | `antigravity` | ✅ |
|
||||
| [Bolt](https://bolt.new/) | `bolt` | 🚫 |
|
||||
| [Claude Code](https://claude.com/claude-code) | `claude-code` | ✅ |
|
||||
| [Cline](https://cline.bot/) | `cline` | 🚫 |
|
||||
| [Codex](https://developers.openai.com/codex/) | `codex` | ✅ |
|
||||
| [Cursor](https://cursor.com/) | `cursor` | ✅ |
|
||||
| [Devin](https://devin.ai/) | `devin` | 🚫 |
|
||||
| [Factory Droid](https://docs.factory.ai/droid-cli/overview) | `droid` | 🚫 |
|
||||
| [Gemini CLI](https://github.com/google-gemini/gemini-cli) | `gemini` | 🚫 |
|
||||
| [GitHub Copilot CLI](https://github.com/github/copilot-cli) | `copilot` | ✅ |
|
||||
| [Grok](https://x.ai/build) | `grok` | ✅ |
|
||||
| [Grok Bot](https://x.ai/bot) | `grok-bot` | ✅ |
|
||||
| [Kilo Code](https://kilocode.ai/) | `kilocode` | ✅ |
|
||||
| [Kiro](https://kiro.dev/) | `kiro` | ✅ |
|
||||
| [Muse Code][muse-code] | `muse` | 🚫 |
|
||||
| [OpenCode](https://opencode.ai/) | `opencode` | 🚫 |
|
||||
| [Pi](https://github.com/badlogic/pi-mono) | `pi` | 🚫 |
|
||||
| [Replit](https://replit.com/) | `replit` | ✅ |
|
||||
| [Rork](https://rork.com/) | `rork` | 🚫 |
|
||||
|
||||
```sh
|
||||
detect-agent
|
||||
detect-agent --json
|
||||
```
|
||||
[muse-code]: https://developer.meta.com/ai/products/muse-code/
|
||||
|
||||
Exit codes:
|
||||
Detection is data-driven: the exact environment variables and process
|
||||
patterns for each agent live in [`src/agents.ts`](src/agents.ts).
|
||||
|
||||
- `0`: a coding agent was detected
|
||||
- `1`: no coding agent was detected
|
||||
OpenCode 1.0.19 and later is detected from environment variables by default.
|
||||
OpenCode 0.x requires experimental process-tree detection.
|
||||
|
||||
## Result shape
|
||||
## API
|
||||
|
||||
`detectAgent()` returns a normalized agent identity instead of the internal detector definition:
|
||||
### `detectAgent([options])`
|
||||
|
||||
```ts
|
||||
Runs detection and returns a `DetectionResult`:
|
||||
|
||||
```js
|
||||
{
|
||||
detected: true,
|
||||
agent: {
|
||||
@@ -66,44 +79,80 @@ Exit codes:
|
||||
}
|
||||
```
|
||||
|
||||
`sessionId` is normalized from agent-specific environment variables when available, such as `CODEX_THREAD_ID`, `CURSOR_CONVERSATION_ID`, `CLAUDE_CODE_SESSION_ID`, `ANTIGRAVITY_TRAJECTORY_ID`, `KIRO_SESSION_ID`, `KILO_RUN_ID`, and `COPILOT_AGENT_SESSION_ID`.
|
||||
### `result.detected`
|
||||
|
||||
## Supported agents
|
||||
A boolean. Will be `true` if the code is running inside a coding agent,
|
||||
otherwise `false`.
|
||||
|
||||
The built-in detector uses environment variables by default:
|
||||
### `result.agent.id`
|
||||
|
||||
- `environment`: matches agent-specific environment variables in `process.env`
|
||||
A stable identifier for the detected agent (see the ID column in the
|
||||
support table above). Prefer comparing against this instead of
|
||||
`result.agent.name`.
|
||||
|
||||
Process-tree inspection is experimental and turned off by default. Enable it explicitly if you need best-effort parent-process matching:
|
||||
### `result.agent.name`
|
||||
|
||||
```ts
|
||||
A string containing the display name of the detected coding agent.
|
||||
|
||||
Don't depend on the value of this string not to change for a specific
|
||||
agent. If you find yourself writing `result.agent.name === "Claude Code"`,
|
||||
you most likely want to use `result.agent.id === "claude-code"` instead.
|
||||
|
||||
### `result.agent.sessionId`
|
||||
|
||||
A string containing the agent's session identifier, normalized from
|
||||
agent-specific environment variables such as `CODEX_THREAD_ID`,
|
||||
`CURSOR_CONVERSATION_ID`, `CLAUDE_CODE_SESSION_ID`,
|
||||
`ANTIGRAVITY_TRAJECTORY_ID`, `KIRO_SESSION_ID`, `KILO_RUN_ID`, and
|
||||
`COPILOT_AGENT_SESSION_ID`, `REPLIT_SESSION`, and `GROK_SESSION_ID`.
|
||||
Omitted when the agent doesn't expose one
|
||||
(see the Session ID column in the support table above).
|
||||
|
||||
### `isRunningFromAgent([options])`
|
||||
|
||||
A convenience shortcut that returns `result.detected` as a boolean:
|
||||
|
||||
```js
|
||||
import { isRunningFromAgent } from "agent-cli-detector";
|
||||
|
||||
if (isRunningFromAgent()) {
|
||||
// Adjust CLI behavior for agent-driven execution.
|
||||
}
|
||||
```
|
||||
|
||||
## CLI
|
||||
|
||||
The package also ships a CLI:
|
||||
|
||||
```bash
|
||||
npx agent-cli-detector
|
||||
npx agent-cli-detector --json
|
||||
```
|
||||
|
||||
Exit codes:
|
||||
|
||||
- `0`: a coding agent was detected
|
||||
- `1`: no coding agent was detected
|
||||
|
||||
## Process-tree detection (experimental)
|
||||
|
||||
Detection uses environment variables by default. Parent-process
|
||||
inspection is available as an opt-in for callers that can tolerate
|
||||
best-effort process-tree matching:
|
||||
|
||||
```js
|
||||
const result = detectAgent({ experimentalProcessTree: true });
|
||||
```
|
||||
|
||||
Process-tree inspection has only been tested on macOS and does not work on Windows.
|
||||
Some sandboxes block `ps`, and process names can vary by launcher, so
|
||||
process-tree detection should be treated as experimental. It has only
|
||||
been tested on macOS and does not work on Windows.
|
||||
|
||||
| Agent | Environment strategy | Process-tree strategy | Session ID |
|
||||
| --- | --- | --- | --- |
|
||||
| Antigravity | `ANTIGRAVITY_AGENT=1`, `ANTIGRAVITY_TRAJECTORY_ID` | `antigravity` | `ANTIGRAVITY_TRAJECTORY_ID` |
|
||||
| Claude Code | `CLAUDECODE=1`, `CLAUDE_CODE_SESSION_ID` | `claude` | `CLAUDE_CODE_SESSION_ID` |
|
||||
| Cline | `CLINE_WRAPPER_PATH` | `cline` | - |
|
||||
| Codex | `CODEX_CI=1`, `CODEX_SHELL=1`, `CODEX_THREAD_ID` | `codex` | `CODEX_THREAD_ID` |
|
||||
| Cursor | `CURSOR_AGENT=1`, `CURSOR_CONVERSATION_ID` | `cursor` | `CURSOR_CONVERSATION_ID` |
|
||||
| Devin | - | command containing `devin` | - |
|
||||
| Gemini CLI | `GEMINI_CLI=1` | `gemini` | - |
|
||||
| GitHub Copilot CLI | `COPILOT_CLI=1`, `COPILOT_AGENT_SESSION_ID`, `COPILOT_RUN_APP=1` | `github-copilot-cli` or `copilot` | `COPILOT_AGENT_SESSION_ID` |
|
||||
| Kiro | `KIRO_SESSION_ID` | command containing `kiro` | `KIRO_SESSION_ID` |
|
||||
| Kilo Code | `KILO=1`, `KILOCODE_VERSION`, `KILO_RUN_ID` | `kilo` or `kilocode` | `KILO_RUN_ID` |
|
||||
| OpenCode | - | `opencode` | - |
|
||||
| Pi | `PI_CODING_AGENT=true` | - | - |
|
||||
|
||||
Some sandboxes block `ps`, and process names can vary by launcher, so process-tree detection should be treated as experimental.
|
||||
|
||||
## Extending
|
||||
## Custom agents
|
||||
|
||||
Add an agent by supplying an `AgentDefinition`:
|
||||
|
||||
```ts
|
||||
```js
|
||||
import { detectAgent, defaultAgents } from "agent-cli-detector";
|
||||
|
||||
const result = detectAgent({
|
||||
@@ -119,6 +168,8 @@ const result = detectAgent({
|
||||
});
|
||||
```
|
||||
|
||||
## Custom strategies
|
||||
|
||||
Add a detection strategy by implementing `DetectionStrategy`:
|
||||
|
||||
```ts
|
||||
@@ -134,4 +185,4 @@ const strategy: DetectionStrategy = {
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
[MIT](LICENSE)
|
||||
Generated
+1449
-3
File diff suppressed because it is too large.
Load diff
+28
-10
@@ -1,31 +1,40 @@
|
||||
{
|
||||
"name": "agent-cli-detector",
|
||||
"version": "0.1.1",
|
||||
"version": "0.1.8",
|
||||
"description": "Detect whether a JavaScript CLI is running inside a coding agent.",
|
||||
"type": "module",
|
||||
"sideEffects": false,
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"bin": {
|
||||
"detect-agent": "dist/cli.js"
|
||||
"agent-cli-detector": "dist/cli.js"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/index.js"
|
||||
"import": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"default": "./dist/index.js"
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/index.d.cts",
|
||||
"default": "./dist/index.cjs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"dist",
|
||||
"README.md"
|
||||
"README.md",
|
||||
"LICENSE"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18.18"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc -p tsconfig.json && node -e \"require('node:fs').chmodSync('dist/cli.js', 0o755)\"",
|
||||
"build": "tsup && node -e \"require('node:fs').chmodSync('dist/cli.js', 0o755)\"",
|
||||
"clean": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\"",
|
||||
"test": "npm run build && node --test test/*.test.mjs",
|
||||
"prepack": "npm run clean && npm test"
|
||||
"test": "npm run build && node --test test/detector.test.mjs test/package.test.mjs",
|
||||
"prepack": "npm run clean && npm run build && node --test test/detector.test.mjs"
|
||||
},
|
||||
"keywords": [
|
||||
"agent",
|
||||
@@ -38,6 +47,15 @@
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/node": "^24.0.4",
|
||||
"tsup": "^8.5.0",
|
||||
"typescript": "^5.8.3"
|
||||
}
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/expo/agent-cli-detector.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/expo/agent-cli-detector/issues"
|
||||
},
|
||||
"homepage": "https://github.com/expo/agent-cli-detector#readme"
|
||||
}
|
||||
@@ -12,6 +12,15 @@ export const defaultAgents = [
|
||||
process: [{ pattern: /^codex$/i }],
|
||||
sessionEnv: ["CODEX_THREAD_ID"]
|
||||
},
|
||||
{
|
||||
id: "grok-bot",
|
||||
name: "Grok Bot",
|
||||
env: [
|
||||
{ name: "SAND_BOX_BOOT_ID" },
|
||||
{ name: "SAND_BOX_STORE_ID" }
|
||||
],
|
||||
sessionEnv: ["CURSOR_CONVERSATION_ID"]
|
||||
},
|
||||
{
|
||||
id: "cursor",
|
||||
name: "Cursor",
|
||||
@@ -35,6 +44,10 @@ export const defaultAgents = [
|
||||
{
|
||||
id: "opencode",
|
||||
name: "OpenCode",
|
||||
env: [
|
||||
{ name: "OPENCODE", value: "1" },
|
||||
{ name: "OPENCODE_PID" }
|
||||
],
|
||||
process: [{ pattern: /^opencode$/i }]
|
||||
},
|
||||
{
|
||||
@@ -42,12 +55,26 @@ export const defaultAgents = [
|
||||
name: "Devin",
|
||||
process: [{ pattern: /devin/i }]
|
||||
},
|
||||
{
|
||||
id: "droid",
|
||||
name: "Factory Droid",
|
||||
env: [{ name: "FACTORY_ENV", value: "production" }]
|
||||
},
|
||||
{
|
||||
id: "gemini",
|
||||
name: "Gemini CLI",
|
||||
env: [{ name: "GEMINI_CLI", value: "1" }],
|
||||
process: [{ pattern: /^gemini$/i }]
|
||||
},
|
||||
{
|
||||
id: "grok",
|
||||
name: "Grok",
|
||||
env: [
|
||||
{ name: "GROK_AGENT", value: "1" },
|
||||
{ name: "GROK_SESSION_ID" }
|
||||
],
|
||||
sessionEnv: ["GROK_SESSION_ID"]
|
||||
},
|
||||
{
|
||||
id: "antigravity",
|
||||
name: "Antigravity",
|
||||
@@ -58,11 +85,31 @@ export const defaultAgents = [
|
||||
process: [{ pattern: /^antigravity$/i }],
|
||||
sessionEnv: ["ANTIGRAVITY_TRAJECTORY_ID"]
|
||||
},
|
||||
{
|
||||
id: "bolt",
|
||||
name: "Bolt",
|
||||
env: [
|
||||
{ name: "BOLT_ENV" },
|
||||
{ name: "BOLT_ORIGIN" },
|
||||
{ name: "BOLT_SERVER_URL" }
|
||||
]
|
||||
},
|
||||
{
|
||||
id: "pi",
|
||||
name: "Pi",
|
||||
env: [{ name: "PI_CODING_AGENT", value: "true" }]
|
||||
},
|
||||
{
|
||||
id: "replit",
|
||||
name: "Replit",
|
||||
env: [{ name: "REPLIT_SESSION" }],
|
||||
sessionEnv: ["REPLIT_SESSION"]
|
||||
},
|
||||
{
|
||||
id: "rork",
|
||||
name: "Rork",
|
||||
env: [{ name: "RORK_API_URL" }]
|
||||
},
|
||||
{
|
||||
id: "kiro",
|
||||
name: "Kiro",
|
||||
@@ -70,6 +117,11 @@ export const defaultAgents = [
|
||||
process: [{ pattern: /kiro/i }],
|
||||
sessionEnv: ["KIRO_SESSION_ID"]
|
||||
},
|
||||
{
|
||||
id: "muse",
|
||||
name: "Muse Code",
|
||||
env: [{ name: "MUSE_RELEASE_INFO" }]
|
||||
},
|
||||
{
|
||||
id: "cline",
|
||||
name: "Cline",
|
||||
|
||||
+8
-1
@@ -1,16 +1,23 @@
|
||||
export type AgentId =
|
||||
| "antigravity"
|
||||
| "bolt"
|
||||
| "cline"
|
||||
| "codex"
|
||||
| "copilot"
|
||||
| "cursor"
|
||||
| "devin"
|
||||
| "droid"
|
||||
| "gemini"
|
||||
| "grok"
|
||||
| "grok-bot"
|
||||
| "kiro"
|
||||
| "kilocode"
|
||||
| "claude-code"
|
||||
| "muse"
|
||||
| "opencode"
|
||||
| "pi";
|
||||
| "pi"
|
||||
| "replit"
|
||||
| "rork";
|
||||
|
||||
export type DetectionStrategyName = "environment" | "process-tree";
|
||||
|
||||
|
||||
@@ -49,6 +49,157 @@ test("detects kiro and normalizes its session id", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("detects replit and normalizes its session id", () => {
|
||||
const result = detectAgent({
|
||||
env: {
|
||||
REPLIT_SESSION: "session-123"
|
||||
}
|
||||
});
|
||||
|
||||
assert.equal(result.detected, true);
|
||||
assert.deepEqual(result.agent, {
|
||||
id: "replit",
|
||||
name: "Replit",
|
||||
sessionId: "session-123"
|
||||
});
|
||||
});
|
||||
|
||||
test("detects grok and normalizes its session id", () => {
|
||||
const result = detectAgent({
|
||||
env: {
|
||||
GROK_AGENT: "1",
|
||||
GROK_SESSION_ID: "01a035a8-166c-7771-8e5e-f67e0dd98c83"
|
||||
}
|
||||
});
|
||||
|
||||
assert.equal(result.detected, true);
|
||||
assert.deepEqual(result.agent, {
|
||||
id: "grok",
|
||||
name: "Grok",
|
||||
sessionId: "01a035a8-166c-7771-8e5e-f67e0dd98c83"
|
||||
});
|
||||
});
|
||||
|
||||
test("detects Grok Bot from SAND_BOX_BOOT_ID and normalizes the conversation id", () => {
|
||||
const result = detectAgent({
|
||||
env: {
|
||||
CURSOR_AGENT: "1",
|
||||
CURSOR_CONVERSATION_ID: "conversation-123",
|
||||
SAND_BOX_BOOT_ID: "boot-456"
|
||||
}
|
||||
});
|
||||
|
||||
assert.equal(result.detected, true);
|
||||
assert.deepEqual(result.agent, {
|
||||
id: "grok-bot",
|
||||
name: "Grok Bot",
|
||||
sessionId: "conversation-123"
|
||||
});
|
||||
});
|
||||
|
||||
test("detects cursor from CURSOR_AGENT without Grok Bot signals", () => {
|
||||
const result = detectAgent({
|
||||
env: {
|
||||
CURSOR_AGENT: "1",
|
||||
CURSOR_CONVERSATION_ID: "conversation-123"
|
||||
}
|
||||
});
|
||||
|
||||
assert.equal(result.detected, true);
|
||||
assert.deepEqual(result.agent, {
|
||||
id: "cursor",
|
||||
name: "Cursor",
|
||||
sessionId: "conversation-123"
|
||||
});
|
||||
});
|
||||
|
||||
test("detects Grok Bot from SAND_BOX_STORE_ID alone", () => {
|
||||
const result = detectAgent({
|
||||
env: {
|
||||
SAND_BOX_STORE_ID: "store-789"
|
||||
}
|
||||
});
|
||||
|
||||
assert.equal(result.detected, true);
|
||||
assert.deepEqual(result.agent, {
|
||||
id: "grok-bot",
|
||||
name: "Grok Bot"
|
||||
});
|
||||
});
|
||||
|
||||
test("detects Factory Droid from FACTORY_ENV", () => {
|
||||
const result = detectAgent({
|
||||
env: {
|
||||
FACTORY_ENV: "production"
|
||||
}
|
||||
});
|
||||
|
||||
assert.equal(result.detected, true);
|
||||
assert.deepEqual(result.agent, {
|
||||
id: "droid",
|
||||
name: "Factory Droid"
|
||||
});
|
||||
});
|
||||
|
||||
test("detects rork from RORK_API_URL", () => {
|
||||
const result = detectAgent({
|
||||
env: {
|
||||
RORK_API_URL: "https://api.rork.com"
|
||||
}
|
||||
});
|
||||
|
||||
assert.equal(result.detected, true);
|
||||
assert.deepEqual(result.agent, {
|
||||
id: "rork",
|
||||
name: "Rork"
|
||||
});
|
||||
});
|
||||
|
||||
test("detects Muse Code from MUSE_RELEASE_INFO", () => {
|
||||
const result = detectAgent({
|
||||
env: {
|
||||
MUSE_RELEASE_INFO: JSON.stringify({
|
||||
channel: "muse-stable",
|
||||
version: "0.1.0-R708.1"
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
assert.equal(result.detected, true);
|
||||
assert.deepEqual(result.agent, {
|
||||
id: "muse",
|
||||
name: "Muse Code"
|
||||
});
|
||||
});
|
||||
|
||||
test("detects bolt from any known environment variable", () => {
|
||||
for (const envName of ["BOLT_ENV", "BOLT_ORIGIN", "BOLT_SERVER_URL"]) {
|
||||
const result = detectAgent({
|
||||
env: {
|
||||
[envName]: "1"
|
||||
}
|
||||
});
|
||||
|
||||
assert.equal(result.detected, true);
|
||||
assert.deepEqual(result.agent, {
|
||||
id: "bolt",
|
||||
name: "Bolt"
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
test("detects opencode from any known environment variable", () => {
|
||||
for (const env of [{ OPENCODE: "1" }, { OPENCODE_PID: "12345" }]) {
|
||||
const result = detectAgent({ env });
|
||||
|
||||
assert.equal(result.detected, true);
|
||||
assert.deepEqual(result.agent, {
|
||||
id: "opencode",
|
||||
name: "OpenCode"
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
test("default strategies only use environment variables", () => {
|
||||
assert.deepEqual(
|
||||
createDefaultStrategies().map((strategy) => strategy.name),
|
||||
|
||||
+45
-27
@@ -6,15 +6,13 @@ import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import test from "node:test";
|
||||
|
||||
const npm = process.platform === "win32" ? "npm.cmd" : "npm";
|
||||
const projectRoot = path.resolve(fileURLToPath(new URL("..", import.meta.url)));
|
||||
|
||||
test("packed package can be installed, imported, and run as a CLI", () => {
|
||||
const workspace = mkdtempSync(path.join(tmpdir(), "agent-cli-detector-"));
|
||||
|
||||
try {
|
||||
const packOutput = execFileSync(
|
||||
npm,
|
||||
const packOutput = execNpm(
|
||||
["pack", "--ignore-scripts", "--json", "--pack-destination", workspace],
|
||||
{
|
||||
cwd: projectRoot,
|
||||
@@ -28,39 +26,23 @@ test("packed package can be installed, imported, and run as a CLI", () => {
|
||||
assert.deepEqual(packageContents, [
|
||||
"LICENSE",
|
||||
"README.md",
|
||||
"dist/agents.d.ts",
|
||||
"dist/agents.d.ts.map",
|
||||
"dist/agents.js",
|
||||
"dist/agents.js.map",
|
||||
"dist/cli.cjs",
|
||||
"dist/cli.cjs.map",
|
||||
"dist/cli.d.cts",
|
||||
"dist/cli.d.ts",
|
||||
"dist/cli.d.ts.map",
|
||||
"dist/cli.js",
|
||||
"dist/cli.js.map",
|
||||
"dist/detector.d.ts",
|
||||
"dist/detector.d.ts.map",
|
||||
"dist/detector.js",
|
||||
"dist/detector.js.map",
|
||||
"dist/index.cjs",
|
||||
"dist/index.cjs.map",
|
||||
"dist/index.d.cts",
|
||||
"dist/index.d.ts",
|
||||
"dist/index.d.ts.map",
|
||||
"dist/index.js",
|
||||
"dist/index.js.map",
|
||||
"dist/strategies/environment.d.ts",
|
||||
"dist/strategies/environment.d.ts.map",
|
||||
"dist/strategies/environment.js",
|
||||
"dist/strategies/environment.js.map",
|
||||
"dist/strategies/process-tree.d.ts",
|
||||
"dist/strategies/process-tree.d.ts.map",
|
||||
"dist/strategies/process-tree.js",
|
||||
"dist/strategies/process-tree.js.map",
|
||||
"dist/types.d.ts",
|
||||
"dist/types.d.ts.map",
|
||||
"dist/types.js",
|
||||
"dist/types.js.map",
|
||||
"package.json"
|
||||
]);
|
||||
|
||||
writeFileSync(path.join(workspace, "package.json"), '{"type":"module"}\n');
|
||||
execFileSync(npm, ["install", "--ignore-scripts", "--no-audit", "--no-fund", tarball], {
|
||||
execNpm(["install", "--ignore-scripts", "--no-audit", "--no-fund", tarball], {
|
||||
cwd: workspace,
|
||||
stdio: "pipe"
|
||||
});
|
||||
@@ -93,10 +75,37 @@ test("packed package can be installed, imported, and run as a CLI", () => {
|
||||
running: true
|
||||
});
|
||||
|
||||
const requireOutput = execFileSync(
|
||||
process.execPath,
|
||||
[
|
||||
"--eval",
|
||||
[
|
||||
"const { detectAgent, isRunningFromAgent } = require('agent-cli-detector');",
|
||||
"const result = detectAgent({ env: { CLAUDECODE: '1', CLAUDE_CODE_SESSION_ID: 'session-123' } });",
|
||||
"console.log(JSON.stringify({ result, running: isRunningFromAgent({ env: { CLAUDECODE: '1' } }) }));"
|
||||
].join("")
|
||||
],
|
||||
{
|
||||
cwd: workspace,
|
||||
encoding: "utf8"
|
||||
}
|
||||
);
|
||||
assert.deepEqual(JSON.parse(requireOutput), {
|
||||
result: {
|
||||
detected: true,
|
||||
agent: {
|
||||
id: "claude-code",
|
||||
name: "Claude Code",
|
||||
sessionId: "session-123"
|
||||
}
|
||||
},
|
||||
running: true
|
||||
});
|
||||
|
||||
const installedPackage = JSON.parse(
|
||||
readFileSync(path.join(workspace, "node_modules/agent-cli-detector/package.json"), "utf8")
|
||||
);
|
||||
const binPath = path.join(workspace, "node_modules/agent-cli-detector", installedPackage.bin["detect-agent"]);
|
||||
const binPath = path.join(workspace, "node_modules/agent-cli-detector", installedPackage.bin["agent-cli-detector"]);
|
||||
const cliOutput = execFileSync(process.execPath, [binPath, "--json"], {
|
||||
cwd: workspace,
|
||||
env: {
|
||||
@@ -118,3 +127,12 @@ test("packed package can be installed, imported, and run as a CLI", () => {
|
||||
rmSync(workspace, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
function execNpm(args, options) {
|
||||
if (process.env.npm_execpath !== undefined) {
|
||||
return execFileSync(process.execPath, [process.env.npm_execpath, ...args], options);
|
||||
}
|
||||
|
||||
const npm = process.platform === "win32" ? "npm.cmd" : "npm";
|
||||
return execFileSync(npm, args, options);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { defineConfig } from "tsup";
|
||||
|
||||
export default defineConfig({
|
||||
entry: {
|
||||
index: "src/index.ts",
|
||||
cli: "src/cli.ts"
|
||||
},
|
||||
format: ["esm", "cjs"],
|
||||
dts: true,
|
||||
sourcemap: true,
|
||||
clean: true,
|
||||
splitting: false,
|
||||
target: "node18",
|
||||
outExtension({ format }) {
|
||||
return {
|
||||
js: format === "cjs" ? ".cjs" : ".js"
|
||||
};
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user