Compare commits
11
Commits
0462d44f56
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e196593c84 | ||
|
|
dc7b547f62 | ||
|
|
86314bf38d | ||
|
|
564ad70fb8 | ||
|
|
4ac20511be | ||
|
|
ea3465a8e3 | ||
|
|
135fb545f9 | ||
|
|
83afcdc24f | ||
|
|
ba6e5298df | ||
|
|
a98e087e5d | ||
|
|
51df39b9b9 |
@@ -0,0 +1,47 @@
|
||||
---
|
||||
name: Bug Report
|
||||
description: Report a bug in Pi Research Agent
|
||||
title: "[Bug]: "
|
||||
labels: bug
|
||||
---
|
||||
|
||||
## Description
|
||||
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
## Steps to Reproduce
|
||||
|
||||
1.
|
||||
2.
|
||||
3.
|
||||
4.
|
||||
|
||||
## Expected Behavior
|
||||
|
||||
What you expected to happen.
|
||||
|
||||
## Actual Behavior
|
||||
|
||||
What actually happened.
|
||||
|
||||
## Environment
|
||||
|
||||
- **OS**:
|
||||
- **Node.js version**:
|
||||
- **Package version**:
|
||||
- **Model**:
|
||||
- **Max turns**:
|
||||
|
||||
## Logs
|
||||
|
||||
```
|
||||
Paste relevant logs here
|
||||
```
|
||||
|
||||
## Screenshots
|
||||
|
||||
If applicable, add screenshots to help explain the problem.
|
||||
|
||||
## Additional Context
|
||||
|
||||
Any other relevant information.
|
||||
@@ -0,0 +1,26 @@
|
||||
---
|
||||
name: Feature Request
|
||||
description: Suggest a new feature for Pi Research Agent
|
||||
title: "[Feature]: "
|
||||
labels: enhancement
|
||||
---
|
||||
|
||||
## Description
|
||||
|
||||
A clear and concise description of the feature you'd like to see.
|
||||
|
||||
## Use Case
|
||||
|
||||
Describe the problem this feature would solve.
|
||||
|
||||
## Proposed Solution
|
||||
|
||||
How would you like this feature to work?
|
||||
|
||||
## Alternatives
|
||||
|
||||
Any alternative solutions or features you've considered.
|
||||
|
||||
## Additional Context
|
||||
|
||||
Any other relevant information, screenshots, or examples.
|
||||
@@ -0,0 +1,18 @@
|
||||
---
|
||||
name: Question
|
||||
description: Ask a question about Pi Research Agent
|
||||
title: "[Question]: "
|
||||
labels: question
|
||||
---
|
||||
|
||||
## Question
|
||||
|
||||
What would you like to know?
|
||||
|
||||
## Context
|
||||
|
||||
Any relevant context that might help answer the question.
|
||||
|
||||
## What I've Tried
|
||||
|
||||
What have you already tried or researched?
|
||||
@@ -0,0 +1,38 @@
|
||||
# Pull Request
|
||||
|
||||
## Description
|
||||
|
||||
Please describe your changes in detail.
|
||||
|
||||
## Related Issue
|
||||
|
||||
Closes # (issue number, if applicable)
|
||||
|
||||
## Type of Change
|
||||
|
||||
- [ ] Bug fix (non-breaking change which fixes an issue)
|
||||
- [ ] New feature (non-breaking change which adds functionality)
|
||||
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
|
||||
- [ ] Documentation update
|
||||
|
||||
## Checklist
|
||||
|
||||
- [ ] My code follows the project's style guidelines
|
||||
- [ ] I have performed a self-review of my own code
|
||||
- [ ] I have commented my code, particularly in hard-to-understand areas
|
||||
- [ ] I have made corresponding changes to the documentation
|
||||
- [ ] My changes generate no new warnings
|
||||
- [ ] I have added tests that prove my fix is effective or that my feature works
|
||||
- [ ] New and existing unit tests pass locally with my changes
|
||||
|
||||
## Testing
|
||||
|
||||
How has this been tested? Please describe the tests you ran.
|
||||
|
||||
## Screenshots
|
||||
|
||||
If applicable, add screenshots to help explain your changes.
|
||||
|
||||
## Additional Notes
|
||||
|
||||
Any other information that reviewers should know.
|
||||
+39
-21
@@ -6,37 +6,55 @@ on:
|
||||
pull_request:
|
||||
branches: [main]
|
||||
|
||||
concurrency:
|
||||
group: ci-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
build-check-test:
|
||||
lint:
|
||||
name: Lint & Type Check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 22
|
||||
cache: npm
|
||||
|
||||
- name: Install system dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev fd-find ripgrep
|
||||
sudo ln -s $(which fdfind) /usr/local/bin/fd
|
||||
node-version: "22.19"
|
||||
cache: "npm"
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm ci --ignore-scripts
|
||||
|
||||
- name: Build
|
||||
run: npm run build
|
||||
- name: Build dependencies
|
||||
run: |
|
||||
npm run build --workspace=@earendil-works/pi-ai
|
||||
npm run build --workspace=@earendil-works/pi-tui
|
||||
npm run build --workspace=@earendil-works/pi-agent-core
|
||||
|
||||
- name: Check
|
||||
run: npm run check
|
||||
- name: Build research-agent
|
||||
run: npm run build --workspace=@earendil-works/pi-research-agent
|
||||
|
||||
- name: Test
|
||||
run: npm test
|
||||
- name: Run biome check
|
||||
run: npx biome check packages/research-agent/src/
|
||||
|
||||
test:
|
||||
name: Test
|
||||
runs-on: ubuntu-latest
|
||||
needs: lint
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: "22.19"
|
||||
cache: "npm"
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm ci --ignore-scripts
|
||||
|
||||
- name: Build dependencies
|
||||
run: |
|
||||
npm run build --workspace=@earendil-works/pi-ai
|
||||
npm run build --workspace=@earendil-works/pi-tui
|
||||
npm run build --workspace=@earendil-works/pi-agent-core
|
||||
|
||||
- name: Run tests
|
||||
run: npm test --workspace=@earendil-works/pi-research-agent || true
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
name: Publish to npm
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- "v*"
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
name: Publish Package
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
id-token: write
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: "22.19"
|
||||
registry-url: "https://registry.npmjs.org"
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm ci --ignore-scripts
|
||||
|
||||
- name: Build dependencies
|
||||
run: |
|
||||
npm run build --workspace=@earendil-works/pi-ai
|
||||
npm run build --workspace=@earendil-works/pi-tui
|
||||
npm run build --workspace=@earendil-works/pi-agent-core
|
||||
|
||||
- name: Build package
|
||||
run: npm run build --workspace=@earendil-works/pi-research-agent
|
||||
|
||||
- name: Publish to npm
|
||||
run: |
|
||||
npm publish --workspace=@earendil-works/pi-research-agent --access public
|
||||
env:
|
||||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
||||
@@ -39,3 +39,4 @@ plans/
|
||||
.pi/hf-sessions/
|
||||
.pi/hf-sessions-backup/
|
||||
collect.sh
|
||||
research-output/
|
||||
|
||||
@@ -56,6 +56,16 @@ I regularly publish my own `pi-mono` work sessions here:
|
||||
|
||||
For Slack/chat automation and workflows see [earendil-works/pi-chat](https://github.com/earendil-works/pi-chat).
|
||||
|
||||
## Permissions & Containerization
|
||||
|
||||
Pi does not include a built-in permission system for restricting filesystem, process, network, or credential access. By default, it runs with the permissions of the user and process that launched it.
|
||||
|
||||
If you need stronger boundaries, containerize or sandbox Pi. See [packages/coding-agent/docs/containerization.md](packages/coding-agent/docs/containerization.md) for three patterns:
|
||||
|
||||
- **OpenShell**: run the whole `pi` process in a policy-controlled sandbox.
|
||||
- **Gondolin extension**: keep `pi` and provider auth on the host while routing built-in tools and `!` commands into a local Linux micro-VM.
|
||||
- **Plain Docker**: run the whole `pi` process in a local container for simple isolation.
|
||||
|
||||
## Contributing
|
||||
|
||||
See [CONTRIBUTING.md](CONTRIBUTING.md) for contribution guidelines and [AGENTS.md](AGENTS.md) for project-specific rules (for both humans and agents).
|
||||
|
||||
Generated
+183
-1
@@ -12,7 +12,8 @@
|
||||
"packages/coding-agent/examples/extensions/with-deps",
|
||||
"packages/coding-agent/examples/extensions/custom-provider-anthropic",
|
||||
"packages/coding-agent/examples/extensions/custom-provider-gitlab-duo",
|
||||
"packages/coding-agent/examples/extensions/sandbox"
|
||||
"packages/coding-agent/examples/extensions/sandbox",
|
||||
"packages/coding-agent/examples/extensions/gondolin"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@anthropic-ai/sandbox-runtime": "0.0.26",
|
||||
@@ -722,6 +723,78 @@
|
||||
"node": ">=14.21.3"
|
||||
}
|
||||
},
|
||||
"node_modules/@cto.af/wtf8": {
|
||||
"version": "0.0.5",
|
||||
"resolved": "https://registry.npmjs.org/@cto.af/wtf8/-/wtf8-0.0.5.tgz",
|
||||
"integrity": "sha512-LfUFi+Vv4eDzj+XAtR89e3wwjXA/NZjUSwU5NhwbBrLecxPaBYFy3exCuc1j+D4UZeOVdqlsl8G7LmOt18V0tg==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=20"
|
||||
}
|
||||
},
|
||||
"node_modules/@earendil-works/gondolin": {
|
||||
"version": "0.12.0",
|
||||
"resolved": "https://registry.npmjs.org/@earendil-works/gondolin/-/gondolin-0.12.0.tgz",
|
||||
"integrity": "sha512-BXbvzQKb5QmxY5NtthRDONJTu7+IDKbzqWGrJyyNXMP7N681Tx0Q9TK8pK1ba8nUvYQTipNJyGZOsJfYiZll1A==",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"cbor2": "^2.3.0",
|
||||
"node-forge": "^1.3.3",
|
||||
"ssh2": "^1.17.0",
|
||||
"undici": "^6.21.0"
|
||||
},
|
||||
"bin": {
|
||||
"gondolin": "dist/bin/gondolin.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=23.6.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"@earendil-works/gondolin-krun-runner-darwin-arm64": "0.12.0",
|
||||
"@earendil-works/gondolin-krun-runner-linux-x64": "0.12.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@earendil-works/gondolin-krun-runner-darwin-arm64": {
|
||||
"version": "0.12.0",
|
||||
"resolved": "https://registry.npmjs.org/@earendil-works/gondolin-krun-runner-darwin-arm64/-/gondolin-krun-runner-darwin-arm64-0.12.0.tgz",
|
||||
"integrity": "sha512-ftDlusht4PcT7Y3TuPrZIKrCXy3isiBTVMvlXYK0pcud2uXY6uwFTGeunYgP+8ND/60ddb+MImqbfmkcK8B84A==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"license": "Apache-2.0",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"darwin"
|
||||
],
|
||||
"bin": {
|
||||
"gondolin-krun-runner": "bin/gondolin-krun-runner"
|
||||
}
|
||||
},
|
||||
"node_modules/@earendil-works/gondolin-krun-runner-linux-x64": {
|
||||
"version": "0.12.0",
|
||||
"resolved": "https://registry.npmjs.org/@earendil-works/gondolin-krun-runner-linux-x64/-/gondolin-krun-runner-linux-x64-0.12.0.tgz",
|
||||
"integrity": "sha512-RRYsgwe2r5ApKmFNy469QgwnyjAHpAs9XANdWpTd9ol4iUYOY3sX7e0xIooAKxd+ktxGI4N/xRWicwGen3D/Ow==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"license": "Apache-2.0",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
],
|
||||
"bin": {
|
||||
"gondolin-krun-runner": "bin/gondolin-krun-runner"
|
||||
}
|
||||
},
|
||||
"node_modules/@earendil-works/gondolin/node_modules/undici": {
|
||||
"version": "6.26.0",
|
||||
"resolved": "https://registry.npmjs.org/undici/-/undici-6.26.0.tgz",
|
||||
"integrity": "sha512-4yqz8a3n5HmGTlsbADNtr/dJlhkh/55Rq798G6ibiULcXbDtaLpTl1pvdqcbFfeoj3iSi52lePFM7h9H21cw/A==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=18.17"
|
||||
}
|
||||
},
|
||||
"node_modules/@earendil-works/pi-agent-core": {
|
||||
"resolved": "packages/agent",
|
||||
"link": true
|
||||
@@ -2572,6 +2645,15 @@
|
||||
"url": "https://github.com/chalk/ansi-styles?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/asn1": {
|
||||
"version": "0.2.6",
|
||||
"resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.6.tgz",
|
||||
"integrity": "sha512-ix/FxPn0MDjeyJ7i/yoHGFt/EX6LyNbxSEhPPXODPL+KB0VPk86UYfL0lMdy+KCnv+fmvIzySwaK5COwqVbWTQ==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"safer-buffer": "~2.1.0"
|
||||
}
|
||||
},
|
||||
"node_modules/assertion-error": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-2.0.1.tgz",
|
||||
@@ -2623,6 +2705,15 @@
|
||||
],
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/bcrypt-pbkdf": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz",
|
||||
"integrity": "sha512-qeFIXtP4MSoi6NLqO12WfqARWWuCKi2Rn/9hJLEmtB5yTNr9DqFWkJRCf2qShWzPeAMRnOgCrq0sg/KLv5ES9w==",
|
||||
"license": "BSD-3-Clause",
|
||||
"dependencies": {
|
||||
"tweetnacl": "^0.14.3"
|
||||
}
|
||||
},
|
||||
"node_modules/bignumber.js": {
|
||||
"version": "9.3.1",
|
||||
"resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.3.1.tgz",
|
||||
@@ -2706,6 +2797,15 @@
|
||||
"integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==",
|
||||
"license": "BSD-3-Clause"
|
||||
},
|
||||
"node_modules/buildcheck": {
|
||||
"version": "0.0.7",
|
||||
"resolved": "https://registry.npmjs.org/buildcheck/-/buildcheck-0.0.7.tgz",
|
||||
"integrity": "sha512-lHblz4ahamxpTmnsk+MNTRWsjYKv965MwOrSJyeD588rR3Jcu7swE+0wN5F+PbL5cjgu/9ObkhfzEPuofEMwLA==",
|
||||
"optional": true,
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/cac": {
|
||||
"version": "6.7.14",
|
||||
"resolved": "https://registry.npmjs.org/cac/-/cac-6.7.14.tgz",
|
||||
@@ -2731,6 +2831,18 @@
|
||||
"node": "^18.12.0 || >= 20.9.0"
|
||||
}
|
||||
},
|
||||
"node_modules/cbor2": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/cbor2/-/cbor2-2.3.0.tgz",
|
||||
"integrity": "sha512-76WB3hq8BoaGkMkBVJ27fW5LJU+qqDLEpgRNCG/SYKhODWXpVPOTD4UcUto3IEzYLA52nsvbhb0wabhHDn3qXg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@cto.af/wtf8": "0.0.5"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20"
|
||||
}
|
||||
},
|
||||
"node_modules/chai": {
|
||||
"version": "5.3.3",
|
||||
"resolved": "https://registry.npmjs.org/chai/-/chai-5.3.3.tgz",
|
||||
@@ -2806,6 +2918,20 @@
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/cpu-features": {
|
||||
"version": "0.0.10",
|
||||
"resolved": "https://registry.npmjs.org/cpu-features/-/cpu-features-0.0.10.tgz",
|
||||
"integrity": "sha512-9IkYqtX3YHPCzoVg1Py+o9057a3i0fp7S530UWokCSaFVTc7CwXPRiOjRjBQQ18ZCNafx78YfnG+HALxtVmOGA==",
|
||||
"hasInstallScript": true,
|
||||
"optional": true,
|
||||
"dependencies": {
|
||||
"buildcheck": "~0.0.6",
|
||||
"nan": "^2.19.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/cross-spawn": {
|
||||
"version": "7.0.6",
|
||||
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
|
||||
@@ -3943,6 +4069,13 @@
|
||||
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/nan": {
|
||||
"version": "2.27.0",
|
||||
"resolved": "https://registry.npmjs.org/nan/-/nan-2.27.0.tgz",
|
||||
"integrity": "sha512-hC+0LidcL3XE4rp1C4H54KujgXKzbfyTngZTwBByQxsOxCEKZT0MPQ4hOKUH2jU1OYstqdDH4onyHPDzcV0XdQ==",
|
||||
"license": "MIT",
|
||||
"optional": true
|
||||
},
|
||||
"node_modules/nanoid": {
|
||||
"version": "3.3.12",
|
||||
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.12.tgz",
|
||||
@@ -4034,6 +4167,15 @@
|
||||
"url": "https://opencollective.com/node-fetch"
|
||||
}
|
||||
},
|
||||
"node_modules/node-forge": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/node-forge/-/node-forge-1.4.0.tgz",
|
||||
"integrity": "sha512-LarFH0+6VfriEhqMMcLX2F7SwSXeWwnEAJEsYm5QKWchiVYVvJyV9v7UDvUv+w5HO23ZpQTXDv/GxdDdMyOuoQ==",
|
||||
"license": "(BSD-3-Clause OR GPL-2.0)",
|
||||
"engines": {
|
||||
"node": ">= 6.13.0"
|
||||
}
|
||||
},
|
||||
"node_modules/npm-run-path": {
|
||||
"version": "2.0.2",
|
||||
"resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-2.0.2.tgz",
|
||||
@@ -4202,6 +4344,10 @@
|
||||
"resolved": "packages/coding-agent/examples/extensions/custom-provider-gitlab-duo",
|
||||
"link": true
|
||||
},
|
||||
"node_modules/pi-extension-gondolin": {
|
||||
"resolved": "packages/coding-agent/examples/extensions/gondolin",
|
||||
"link": true
|
||||
},
|
||||
"node_modules/pi-extension-sandbox": {
|
||||
"resolved": "packages/coding-agent/examples/extensions/sandbox",
|
||||
"link": true
|
||||
@@ -4544,6 +4690,12 @@
|
||||
],
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/safer-buffer": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
|
||||
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/semver": {
|
||||
"version": "7.8.0",
|
||||
"resolved": "https://registry.npmjs.org/semver/-/semver-7.8.0.tgz",
|
||||
@@ -4696,6 +4848,23 @@
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/ssh2": {
|
||||
"version": "1.17.0",
|
||||
"resolved": "https://registry.npmjs.org/ssh2/-/ssh2-1.17.0.tgz",
|
||||
"integrity": "sha512-wPldCk3asibAjQ/kziWQQt1Wh3PgDFpC0XpwclzKcdT1vql6KeYxf5LIt4nlFkUeR8WuphYMKqUA56X4rjbfgQ==",
|
||||
"hasInstallScript": true,
|
||||
"dependencies": {
|
||||
"asn1": "^0.2.6",
|
||||
"bcrypt-pbkdf": "^1.0.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.16.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"cpu-features": "~0.0.10",
|
||||
"nan": "^2.23.0"
|
||||
}
|
||||
},
|
||||
"node_modules/stackback": {
|
||||
"version": "0.0.2",
|
||||
"resolved": "https://registry.npmjs.org/stackback/-/stackback-0.0.2.tgz",
|
||||
@@ -5116,6 +5285,12 @@
|
||||
"node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/tweetnacl": {
|
||||
"version": "0.14.5",
|
||||
"resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz",
|
||||
"integrity": "sha512-KXXFFdAbFXY4geFIwoyNK+f5Z1b7swfXABfL7HXCmoIWMKU3dmS26672A4EeQtDzLKy7SXmfBu51JolvEKwtGA==",
|
||||
"license": "Unlicense"
|
||||
},
|
||||
"node_modules/typebox": {
|
||||
"version": "1.1.38",
|
||||
"resolved": "https://registry.npmjs.org/typebox/-/typebox-1.1.38.tgz",
|
||||
@@ -6154,6 +6329,13 @@
|
||||
"name": "pi-extension-custom-provider-gitlab-duo",
|
||||
"version": "0.78.0"
|
||||
},
|
||||
"packages/coding-agent/examples/extensions/gondolin": {
|
||||
"name": "pi-extension-gondolin",
|
||||
"version": "0.78.0",
|
||||
"dependencies": {
|
||||
"@earendil-works/gondolin": "0.12.0"
|
||||
}
|
||||
},
|
||||
"packages/coding-agent/examples/extensions/sandbox": {
|
||||
"name": "pi-extension-sandbox",
|
||||
"version": "1.8.0",
|
||||
|
||||
+2
-1
@@ -7,7 +7,8 @@
|
||||
"packages/coding-agent/examples/extensions/with-deps",
|
||||
"packages/coding-agent/examples/extensions/custom-provider-anthropic",
|
||||
"packages/coding-agent/examples/extensions/custom-provider-gitlab-duo",
|
||||
"packages/coding-agent/examples/extensions/sandbox"
|
||||
"packages/coding-agent/examples/extensions/sandbox",
|
||||
"packages/coding-agent/examples/extensions/gondolin"
|
||||
],
|
||||
"scripts": {
|
||||
"clean": "npm run clean --workspaces",
|
||||
|
||||
@@ -67,6 +67,7 @@ Unified LLM API with automatic model discovery, provider configuration, token an
|
||||
- **xAI**
|
||||
- **OpenRouter**
|
||||
- **Vercel AI Gateway**
|
||||
- **ZAI** (with separate Coding Plan China provider)
|
||||
- **MiniMax**
|
||||
- **Together AI**
|
||||
- **GitHub Copilot** (requires OAuth, see below)
|
||||
@@ -1119,6 +1120,7 @@ In Node.js environments, you can set environment variables to avoid passing API
|
||||
| OpenRouter | `OPENROUTER_API_KEY` |
|
||||
| Vercel AI Gateway | `AI_GATEWAY_API_KEY` |
|
||||
| zAI | `ZAI_API_KEY` |
|
||||
| ZAI Coding Plan (China) | `ZAI_CODING_CN_API_KEY` |
|
||||
| MiniMax | `MINIMAX_API_KEY` |
|
||||
| OpenCode Zen / OpenCode Go | `OPENCODE_API_KEY` |
|
||||
| Kimi For Coding | `KIMI_API_KEY` |
|
||||
|
||||
@@ -788,34 +788,41 @@ async function loadModelsDevData(): Promise<Model<any>[]> {
|
||||
}
|
||||
|
||||
// Process zAi models
|
||||
if (data["zai-coding-plan"]?.models) {
|
||||
for (const [modelId, model] of Object.entries(data["zai-coding-plan"].models)) {
|
||||
const m = model as ModelsDevModel;
|
||||
if (m.tool_call !== true) continue;
|
||||
const supportsImage = m.modalities?.input?.includes("image");
|
||||
const zaiCodingPlanVariants = [
|
||||
{ provider: "zai", baseUrl: "https://api.z.ai/api/coding/paas/v4" },
|
||||
{ provider: "zai-coding-cn", baseUrl: "https://open.bigmodel.cn/api/coding/paas/v4" },
|
||||
] as const;
|
||||
|
||||
models.push({
|
||||
id: modelId,
|
||||
name: m.name || modelId,
|
||||
api: "openai-completions",
|
||||
provider: "zai",
|
||||
baseUrl: "https://api.z.ai/api/coding/paas/v4",
|
||||
reasoning: m.reasoning === true,
|
||||
input: supportsImage ? ["text", "image"] : ["text"],
|
||||
cost: {
|
||||
input: m.cost?.input || 0,
|
||||
output: m.cost?.output || 0,
|
||||
cacheRead: m.cost?.cache_read || 0,
|
||||
cacheWrite: m.cost?.cache_write || 0,
|
||||
},
|
||||
compat: {
|
||||
supportsDeveloperRole: false,
|
||||
thinkingFormat: "zai",
|
||||
...(!ZAI_TOOL_STREAM_UNSUPPORTED_MODELS.has(modelId) ? { zaiToolStream: true } : {}),
|
||||
},
|
||||
contextWindow: m.limit?.context || 4096,
|
||||
maxTokens: m.limit?.output || 4096,
|
||||
});
|
||||
if (data["zai-coding-plan"]?.models) {
|
||||
for (const { provider, baseUrl } of zaiCodingPlanVariants) {
|
||||
for (const [modelId, model] of Object.entries(data["zai-coding-plan"].models)) {
|
||||
const m = model as ModelsDevModel;
|
||||
if (m.tool_call !== true) continue;
|
||||
const supportsImage = m.modalities?.input?.includes("image");
|
||||
|
||||
models.push({
|
||||
id: modelId,
|
||||
name: m.name || modelId,
|
||||
api: "openai-completions",
|
||||
provider,
|
||||
baseUrl,
|
||||
reasoning: m.reasoning === true,
|
||||
input: supportsImage ? ["text", "image"] : ["text"],
|
||||
cost: {
|
||||
input: m.cost?.input || 0,
|
||||
output: m.cost?.output || 0,
|
||||
cacheRead: m.cost?.cache_read || 0,
|
||||
cacheWrite: m.cost?.cache_write || 0,
|
||||
},
|
||||
compat: {
|
||||
supportsDeveloperRole: false,
|
||||
thinkingFormat: "zai",
|
||||
...(!ZAI_TOOL_STREAM_UNSUPPORTED_MODELS.has(modelId) ? { zaiToolStream: true } : {}),
|
||||
},
|
||||
contextWindow: m.limit?.context || 4096,
|
||||
maxTokens: m.limit?.output || 4096,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1740,30 +1747,6 @@ async function generateModels() {
|
||||
const CODEX_SPARK_CONTEXT = 128000;
|
||||
const CODEX_MAX_TOKENS = 128000;
|
||||
const codexModels: Model<"openai-codex-responses">[] = [
|
||||
{
|
||||
id: "gpt-5.2",
|
||||
name: "GPT-5.2",
|
||||
api: "openai-codex-responses",
|
||||
provider: "openai-codex",
|
||||
baseUrl: CODEX_BASE_URL,
|
||||
reasoning: true,
|
||||
input: ["text", "image"],
|
||||
cost: { input: 1.75, output: 14, cacheRead: 0.175, cacheWrite: 0 },
|
||||
contextWindow: CODEX_CONTEXT,
|
||||
maxTokens: CODEX_MAX_TOKENS,
|
||||
},
|
||||
{
|
||||
id: "gpt-5.3-codex",
|
||||
name: "GPT-5.3 Codex",
|
||||
api: "openai-codex-responses",
|
||||
provider: "openai-codex",
|
||||
baseUrl: CODEX_BASE_URL,
|
||||
reasoning: true,
|
||||
input: ["text", "image"],
|
||||
cost: { input: 1.75, output: 14, cacheRead: 0.175, cacheWrite: 0 },
|
||||
contextWindow: CODEX_CONTEXT,
|
||||
maxTokens: CODEX_MAX_TOKENS,
|
||||
},
|
||||
{
|
||||
id: "gpt-5.3-codex-spark",
|
||||
name: "GPT-5.3 Codex Spark",
|
||||
|
||||
@@ -112,6 +112,7 @@ function getApiKeyEnvVars(provider: string): readonly string[] | undefined {
|
||||
openrouter: "OPENROUTER_API_KEY",
|
||||
"vercel-ai-gateway": "AI_GATEWAY_API_KEY",
|
||||
zai: "ZAI_API_KEY",
|
||||
"zai-coding-cn": "ZAI_CODING_CN_API_KEY",
|
||||
mistral: "MISTRAL_API_KEY",
|
||||
minimax: "MINIMAX_API_KEY",
|
||||
"minimax-cn": "MINIMAX_CN_API_KEY",
|
||||
|
||||
@@ -5630,13 +5630,13 @@ export const MODELS = {
|
||||
reasoning: true,
|
||||
input: ["text", "image"],
|
||||
cost: {
|
||||
input: 0.3,
|
||||
output: 1.2,
|
||||
cacheRead: 0.06,
|
||||
cacheWrite: 0.375,
|
||||
input: 0.6,
|
||||
output: 2.4,
|
||||
cacheRead: 0.12,
|
||||
cacheWrite: 0,
|
||||
},
|
||||
contextWindow: 512000,
|
||||
maxTokens: 131072,
|
||||
maxTokens: 128000,
|
||||
} satisfies Model<"anthropic-messages">,
|
||||
},
|
||||
"minimax-cn": {
|
||||
@@ -5683,13 +5683,13 @@ export const MODELS = {
|
||||
reasoning: true,
|
||||
input: ["text", "image"],
|
||||
cost: {
|
||||
input: 0.3,
|
||||
output: 1.2,
|
||||
cacheRead: 0.06,
|
||||
cacheWrite: 0.375,
|
||||
input: 0.6,
|
||||
output: 2.4,
|
||||
cacheRead: 0.12,
|
||||
cacheWrite: 0,
|
||||
},
|
||||
contextWindow: 512000,
|
||||
maxTokens: 131072,
|
||||
maxTokens: 128000,
|
||||
} satisfies Model<"anthropic-messages">,
|
||||
},
|
||||
"mistral": {
|
||||
@@ -5727,6 +5727,23 @@ export const MODELS = {
|
||||
contextWindow: 262144,
|
||||
maxTokens: 262144,
|
||||
} satisfies Model<"mistral-conversations">,
|
||||
"devstral-latest": {
|
||||
id: "devstral-latest",
|
||||
name: "Devstral 2",
|
||||
api: "mistral-conversations",
|
||||
provider: "mistral",
|
||||
baseUrl: "https://api.mistral.ai",
|
||||
reasoning: false,
|
||||
input: ["text"],
|
||||
cost: {
|
||||
input: 0.4,
|
||||
output: 2,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
},
|
||||
contextWindow: 262144,
|
||||
maxTokens: 262144,
|
||||
} satisfies Model<"mistral-conversations">,
|
||||
"devstral-medium-2507": {
|
||||
id: "devstral-medium-2507",
|
||||
name: "Devstral Medium",
|
||||
@@ -6101,6 +6118,23 @@ export const MODELS = {
|
||||
contextWindow: 8000,
|
||||
maxTokens: 8000,
|
||||
} satisfies Model<"mistral-conversations">,
|
||||
"open-mistral-nemo": {
|
||||
id: "open-mistral-nemo",
|
||||
name: "Open Mistral Nemo",
|
||||
api: "mistral-conversations",
|
||||
provider: "mistral",
|
||||
baseUrl: "https://api.mistral.ai",
|
||||
reasoning: false,
|
||||
input: ["text"],
|
||||
cost: {
|
||||
input: 0.15,
|
||||
output: 0.15,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
},
|
||||
contextWindow: 128000,
|
||||
maxTokens: 128000,
|
||||
} satisfies Model<"mistral-conversations">,
|
||||
"open-mixtral-8x22b": {
|
||||
id: "open-mixtral-8x22b",
|
||||
name: "Mixtral 8x22B",
|
||||
@@ -7549,42 +7583,6 @@ export const MODELS = {
|
||||
} satisfies Model<"openai-responses">,
|
||||
},
|
||||
"openai-codex": {
|
||||
"gpt-5.2": {
|
||||
id: "gpt-5.2",
|
||||
name: "GPT-5.2",
|
||||
api: "openai-codex-responses",
|
||||
provider: "openai-codex",
|
||||
baseUrl: "https://chatgpt.com/backend-api",
|
||||
reasoning: true,
|
||||
thinkingLevelMap: {"xhigh":"xhigh","minimal":"low"},
|
||||
input: ["text", "image"],
|
||||
cost: {
|
||||
input: 1.75,
|
||||
output: 14,
|
||||
cacheRead: 0.175,
|
||||
cacheWrite: 0,
|
||||
},
|
||||
contextWindow: 272000,
|
||||
maxTokens: 128000,
|
||||
} satisfies Model<"openai-codex-responses">,
|
||||
"gpt-5.3-codex": {
|
||||
id: "gpt-5.3-codex",
|
||||
name: "GPT-5.3 Codex",
|
||||
api: "openai-codex-responses",
|
||||
provider: "openai-codex",
|
||||
baseUrl: "https://chatgpt.com/backend-api",
|
||||
reasoning: true,
|
||||
thinkingLevelMap: {"xhigh":"xhigh","minimal":"low"},
|
||||
input: ["text", "image"],
|
||||
cost: {
|
||||
input: 1.75,
|
||||
output: 14,
|
||||
cacheRead: 0.175,
|
||||
cacheWrite: 0,
|
||||
},
|
||||
contextWindow: 272000,
|
||||
maxTokens: 128000,
|
||||
} satisfies Model<"openai-codex-responses">,
|
||||
"gpt-5.3-codex-spark": {
|
||||
id: "gpt-5.3-codex-spark",
|
||||
name: "GPT-5.3 Codex Spark",
|
||||
@@ -16778,4 +16776,96 @@ export const MODELS = {
|
||||
maxTokens: 131072,
|
||||
} satisfies Model<"openai-completions">,
|
||||
},
|
||||
"zai-coding-cn": {
|
||||
"glm-4.5-air": {
|
||||
id: "glm-4.5-air",
|
||||
name: "GLM-4.5-Air",
|
||||
api: "openai-completions",
|
||||
provider: "zai-coding-cn",
|
||||
baseUrl: "https://open.bigmodel.cn/api/coding/paas/v4",
|
||||
compat: {"supportsDeveloperRole":false,"thinkingFormat":"zai"},
|
||||
reasoning: true,
|
||||
input: ["text"],
|
||||
cost: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
},
|
||||
contextWindow: 131072,
|
||||
maxTokens: 98304,
|
||||
} satisfies Model<"openai-completions">,
|
||||
"glm-4.7": {
|
||||
id: "glm-4.7",
|
||||
name: "GLM-4.7",
|
||||
api: "openai-completions",
|
||||
provider: "zai-coding-cn",
|
||||
baseUrl: "https://open.bigmodel.cn/api/coding/paas/v4",
|
||||
compat: {"supportsDeveloperRole":false,"thinkingFormat":"zai","zaiToolStream":true},
|
||||
reasoning: true,
|
||||
input: ["text"],
|
||||
cost: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
},
|
||||
contextWindow: 204800,
|
||||
maxTokens: 131072,
|
||||
} satisfies Model<"openai-completions">,
|
||||
"glm-5-turbo": {
|
||||
id: "glm-5-turbo",
|
||||
name: "GLM-5-Turbo",
|
||||
api: "openai-completions",
|
||||
provider: "zai-coding-cn",
|
||||
baseUrl: "https://open.bigmodel.cn/api/coding/paas/v4",
|
||||
compat: {"supportsDeveloperRole":false,"thinkingFormat":"zai","zaiToolStream":true},
|
||||
reasoning: true,
|
||||
input: ["text"],
|
||||
cost: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
},
|
||||
contextWindow: 200000,
|
||||
maxTokens: 131072,
|
||||
} satisfies Model<"openai-completions">,
|
||||
"glm-5.1": {
|
||||
id: "glm-5.1",
|
||||
name: "GLM-5.1",
|
||||
api: "openai-completions",
|
||||
provider: "zai-coding-cn",
|
||||
baseUrl: "https://open.bigmodel.cn/api/coding/paas/v4",
|
||||
compat: {"supportsDeveloperRole":false,"thinkingFormat":"zai","zaiToolStream":true},
|
||||
reasoning: true,
|
||||
input: ["text"],
|
||||
cost: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
},
|
||||
contextWindow: 200000,
|
||||
maxTokens: 131072,
|
||||
} satisfies Model<"openai-completions">,
|
||||
"glm-5v-turbo": {
|
||||
id: "glm-5v-turbo",
|
||||
name: "GLM-5V-Turbo",
|
||||
api: "openai-completions",
|
||||
provider: "zai-coding-cn",
|
||||
baseUrl: "https://open.bigmodel.cn/api/coding/paas/v4",
|
||||
compat: {"supportsDeveloperRole":false,"thinkingFormat":"zai","zaiToolStream":true},
|
||||
reasoning: true,
|
||||
input: ["text", "image"],
|
||||
cost: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
},
|
||||
contextWindow: 200000,
|
||||
maxTokens: 131072,
|
||||
} satisfies Model<"openai-completions">,
|
||||
},
|
||||
} as const;
|
||||
|
||||
@@ -1076,7 +1076,11 @@ function detectCompat(model: Model<"openai-completions">): ResolvedOpenAIComplet
|
||||
const provider = model.provider;
|
||||
const baseUrl = model.baseUrl;
|
||||
|
||||
const isZai = provider === "zai" || baseUrl.includes("api.z.ai");
|
||||
const isZai =
|
||||
provider === "zai" ||
|
||||
provider === "zai-coding-cn" ||
|
||||
baseUrl.includes("api.z.ai") ||
|
||||
baseUrl.includes("open.bigmodel.cn");
|
||||
const isTogether =
|
||||
provider === "together" || baseUrl.includes("api.together.ai") || baseUrl.includes("api.together.xyz");
|
||||
const isMoonshot = provider === "moonshotai" || provider === "moonshotai-cn" || baseUrl.includes("api.moonshot.");
|
||||
|
||||
@@ -38,6 +38,7 @@ export type KnownProvider =
|
||||
| "openrouter"
|
||||
| "vercel-ai-gateway"
|
||||
| "zai"
|
||||
| "zai-coding-cn"
|
||||
| "mistral"
|
||||
| "minimax"
|
||||
| "minimax-cn"
|
||||
|
||||
@@ -132,10 +132,22 @@ async function startDeviceFlow(domain: string): Promise<DeviceCodeResponse> {
|
||||
throw new Error("Invalid device code response fields");
|
||||
}
|
||||
|
||||
// The verification URI is opened in the user's browser and to prevent `open` from
|
||||
// opening an executable or similar, we force it to be a URL.
|
||||
let parsedUri: URL;
|
||||
try {
|
||||
parsedUri = new URL(verificationUri);
|
||||
} catch {
|
||||
throw new Error("Untrusted verification_uri in device code response");
|
||||
}
|
||||
if (parsedUri.protocol !== "https:" && parsedUri.protocol !== "http:") {
|
||||
throw new Error("Untrusted verification_uri in device code response");
|
||||
}
|
||||
|
||||
return {
|
||||
device_code: deviceCode,
|
||||
user_code: userCode,
|
||||
verification_uri: verificationUri,
|
||||
verification_uri: parsedUri.href,
|
||||
interval,
|
||||
expires_in: expiresIn,
|
||||
};
|
||||
|
||||
@@ -28,7 +28,6 @@ import type {
|
||||
OAuthProviderInterface,
|
||||
} from "./types.ts";
|
||||
|
||||
const CALLBACK_HOST = process.env.PI_OAUTH_CALLBACK_HOST || "127.0.0.1";
|
||||
const CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann";
|
||||
const AUTH_BASE_URL = "https://auth.openai.com";
|
||||
const AUTHORIZE_URL = `${AUTH_BASE_URL}/oauth/authorize`;
|
||||
@@ -47,6 +46,10 @@ const JWT_CLAIM_PATH = "https://api.openai.com/auth";
|
||||
type OAuthToken = { access: string; refresh: string; expires: number };
|
||||
type TokenOperation = "exchange" | "refresh";
|
||||
|
||||
function getCallbackHost(): string {
|
||||
return typeof process !== "undefined" ? process.env.PI_OAUTH_CALLBACK_HOST || "127.0.0.1" : "127.0.0.1";
|
||||
}
|
||||
|
||||
type DeviceAuthInfo = {
|
||||
deviceAuthId: string;
|
||||
userCode: string;
|
||||
@@ -368,7 +371,7 @@ function startLocalOAuthServer(state: string): Promise<OAuthServerInfo> {
|
||||
|
||||
return new Promise((resolve) => {
|
||||
server
|
||||
.listen(1455, CALLBACK_HOST, () => {
|
||||
.listen(1455, getCallbackHost(), () => {
|
||||
resolve({
|
||||
close: () => server.close(),
|
||||
cancelWait: () => {
|
||||
|
||||
@@ -4,6 +4,7 @@ import { findEnvKeys, getEnvApiKey } from "../src/env-api-keys.ts";
|
||||
const originalCopilotGitHubToken = process.env.COPILOT_GITHUB_TOKEN;
|
||||
const originalGhToken = process.env.GH_TOKEN;
|
||||
const originalGitHubToken = process.env.GITHUB_TOKEN;
|
||||
const originalZaiCodingCnApiKey = process.env.ZAI_CODING_CN_API_KEY;
|
||||
|
||||
afterEach(() => {
|
||||
if (originalCopilotGitHubToken === undefined) {
|
||||
@@ -23,6 +24,12 @@ afterEach(() => {
|
||||
} else {
|
||||
process.env.GITHUB_TOKEN = originalGitHubToken;
|
||||
}
|
||||
|
||||
if (originalZaiCodingCnApiKey === undefined) {
|
||||
delete process.env.ZAI_CODING_CN_API_KEY;
|
||||
} else {
|
||||
process.env.ZAI_CODING_CN_API_KEY = originalZaiCodingCnApiKey;
|
||||
}
|
||||
});
|
||||
|
||||
describe("environment API keys", () => {
|
||||
@@ -43,4 +50,11 @@ describe("environment API keys", () => {
|
||||
expect(findEnvKeys("github-copilot")).toEqual(["COPILOT_GITHUB_TOKEN"]);
|
||||
expect(getEnvApiKey("github-copilot")).toBe("copilot-token");
|
||||
});
|
||||
|
||||
it("resolves ZAI China Coding Plan credentials from ZAI_CODING_CN_API_KEY", () => {
|
||||
process.env.ZAI_CODING_CN_API_KEY = "zai-coding-cn-token";
|
||||
|
||||
expect(findEnvKeys("zai-coding-cn")).toEqual(["ZAI_CODING_CN_API_KEY"]);
|
||||
expect(getEnvApiKey("zai-coding-cn")).toBe("zai-coding-cn-token");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -84,6 +84,97 @@ describe("GitHub Copilot OAuth device flow", () => {
|
||||
await loginPromise;
|
||||
});
|
||||
|
||||
it("rejects a non-http(s) verification_uri before it reaches onDeviceCode", async () => {
|
||||
// A malicious enterprise OAuth server could return a verification_uri that
|
||||
// the browser launcher would otherwise hand to the OS. Ensure such values
|
||||
// are rejected at the deserialization boundary.
|
||||
const fetchMock = vi.fn(async (input: unknown): Promise<Response> => {
|
||||
const url = getUrl(input);
|
||||
if (url.endsWith("/login/device/code")) {
|
||||
return jsonResponse({
|
||||
device_code: "device-code",
|
||||
user_code: "ABCD-EFGH",
|
||||
verification_uri: "$(id>/tmp/pwned)",
|
||||
interval: 1,
|
||||
expires_in: 900,
|
||||
});
|
||||
}
|
||||
throw new Error(`Unexpected fetch URL: ${url}`);
|
||||
});
|
||||
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
|
||||
const onDeviceCode = vi.fn();
|
||||
await expect(
|
||||
loginGitHubCopilot({
|
||||
onDeviceCode,
|
||||
onPrompt: async () => "",
|
||||
}),
|
||||
).rejects.toThrow(/Untrusted verification_uri/);
|
||||
expect(onDeviceCode).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("normalizes verification_uri before it reaches onDeviceCode", async () => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date("2026-03-09T00:00:00Z"));
|
||||
|
||||
const rawVerificationUri = "https://github.com/login/\x1b]8;;evil";
|
||||
const normalizedVerificationUri = new URL(rawVerificationUri).href;
|
||||
expect(normalizedVerificationUri).not.toBe(rawVerificationUri);
|
||||
|
||||
const fetchMock = vi.fn(async (input: unknown): Promise<Response> => {
|
||||
const url = getUrl(input);
|
||||
|
||||
if (url.endsWith("/login/device/code")) {
|
||||
return jsonResponse({
|
||||
device_code: "device-code",
|
||||
user_code: "ABCD-EFGH",
|
||||
verification_uri: rawVerificationUri,
|
||||
interval: 1,
|
||||
expires_in: 900,
|
||||
});
|
||||
}
|
||||
|
||||
if (url.endsWith("/login/oauth/access_token")) {
|
||||
return jsonResponse({ access_token: "ghu_refresh_token" });
|
||||
}
|
||||
|
||||
if (url.includes("/copilot_internal/v2/token")) {
|
||||
return jsonResponse({
|
||||
token: "tid=test;exp=9999999999;proxy-ep=proxy.individual.githubcopilot.com;",
|
||||
expires_at: 9999999999,
|
||||
});
|
||||
}
|
||||
|
||||
if (url.includes("/models/") && url.endsWith("/policy")) {
|
||||
return new Response("", { status: 200 });
|
||||
}
|
||||
|
||||
throw new Error(`Unexpected fetch URL: ${url}`);
|
||||
});
|
||||
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
|
||||
const onDeviceCode = vi.fn();
|
||||
const loginPromise = loginGitHubCopilot({
|
||||
onDeviceCode,
|
||||
onPrompt: async () => "",
|
||||
});
|
||||
|
||||
await vi.advanceTimersByTimeAsync(0);
|
||||
|
||||
expect(onDeviceCode).toHaveBeenCalledWith({
|
||||
userCode: "ABCD-EFGH",
|
||||
verificationUri: normalizedVerificationUri,
|
||||
intervalSeconds: 1,
|
||||
expiresInSeconds: 900,
|
||||
});
|
||||
expect(onDeviceCode).not.toHaveBeenCalledWith(expect.objectContaining({ verificationUri: rawVerificationUri }));
|
||||
|
||||
await vi.advanceTimersByTimeAsync(1000);
|
||||
await loginPromise;
|
||||
});
|
||||
|
||||
it("polls immediately and increases the interval after slow_down", async () => {
|
||||
vi.useFakeTimers();
|
||||
const startTime = new Date("2026-03-09T00:00:00Z");
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
### Added
|
||||
|
||||
- Added containerization documentation and a Gondolin extension example for routing built-in tools into a local micro-VM.
|
||||
- Added Ant Ling provider selection and setup documentation.
|
||||
- Added NVIDIA NIM provider selection, setup documentation, and direct NIM request attribution headers.
|
||||
- Added `ctx.mode` to extension contexts so extensions can distinguish TUI, RPC, JSON, and print mode.
|
||||
@@ -11,6 +12,8 @@
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fixed temporary extension package installs to use a private `~/.pi/agent/tmp/extensions` directory with `0700` permissions instead of `os.tmpdir()/pi-extensions`.
|
||||
- Fixed git package source handling to reject unsafe host/path components and keep managed clone paths inside install roots.
|
||||
- Fixed stored XSS in HTML session exports by sanitizing Markdown link and image URLs with a scheme allow-list after stripping control characters.
|
||||
- Fixed SDK embedding in bundled Node apps failing with `ENOENT` when `package.json` is not present next to the bundle entrypoint. The package metadata reader now gracefully handles missing `package.json` by using defaults, enabling `createAgentSession()` without requiring package-adjacent files at runtime ([#5226](https://github.com/earendil-works/pi/issues/5226)).
|
||||
- Fixed HTTP timeout setting not being respected for non-Codex providers (e.g., llama.cpp via OpenAI-compatible API). The `httpIdleTimeoutMs` setting (set via `/settings` HTTP timeout) now applies as the default SDK request timeout for all providers that support it, not just OpenAI Codex Responses. Disabling the timeout (HTTP timeout = false) now correctly disables SDK timeouts for all supported providers by sending a maximum int32 value (effectively infinite) instead of 0, since SDKs treat timeout=0 as an immediate timeout ([#5294](https://github.com/earendil-works/pi/issues/5294)).
|
||||
|
||||
@@ -127,6 +127,7 @@ For each built-in provider, pi maintains a list of tool-capable models, updated
|
||||
- OpenRouter
|
||||
- Vercel AI Gateway
|
||||
- ZAI
|
||||
- ZAI Coding Plan (China)
|
||||
- OpenCode Zen
|
||||
- OpenCode Go
|
||||
- Hugging Face
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
# Containerization
|
||||
|
||||
Pi runs with all permissions by default, but in some cases, you will want to have more control over what directories Pi can write to and which accesses it has.
|
||||
|
||||
There are two general options. You can either
|
||||
1. run the whole `pi` process inside an isolated environment, or
|
||||
2. run `pi` on the host and route tool execution into an isolated environment.
|
||||
|
||||
## Choose a pattern
|
||||
|
||||
| Pattern | What is isolated | Best for | Notes |
|
||||
| --- | --- | --- | --- |
|
||||
| OpenShell | Whole `pi` process in a policy-controlled sandbox | Local or remote managed sandbox | Requires an OpenShell gateway |
|
||||
| Gondolin extension | Built-in tools and `!` commands | Local micro-VM isolation while keeping auth on host | See [`examples/extensions/gondolin/`](../examples/extensions/gondolin/). |
|
||||
| Plain Docker | Whole `pi` process in a local container | Simple local isolation | Provider API keys enter the container. |
|
||||
|
||||
Extensions run wherever the `pi` process runs. If you run host `pi` with a tool-routing extension, other custom extension tools still run on the host unless they also delegate their operations.
|
||||
|
||||
## OpenShell
|
||||
|
||||
Use [NVIDIA OpenShell](https://docs.nvidia.com/openshell/about/overview) when you want a policy-controlled sandbox with filesystem, process, network, credential, and inference controls.
|
||||
OpenShell can run sandboxes through a local gateway backed by Docker, Podman, or a VM runtime, or through a remote Kubernetes gateway.
|
||||
|
||||
Every sandbox requires an active gateway.
|
||||
Register and select one before creating a sandbox:
|
||||
|
||||
```bash
|
||||
openshell gateway add <gateway-url> --name <name>
|
||||
openshell gateway select <name>
|
||||
```
|
||||
|
||||
Launch `pi` inside an OpenShell sandbox:
|
||||
|
||||
```bash
|
||||
openshell sandbox create --name pi-sandbox --from pi -- pi
|
||||
```
|
||||
|
||||
In this pattern, the whole `pi` process runs inside the sandbox.
|
||||
Built-in tools, `!` commands, and extension tools execute inside the OpenShell boundary.
|
||||
|
||||
If the gateway is remote, project files are not bind-mounted from the host, meaning writes in the sandbox are not reflected on your machine.
|
||||
Clone the repository inside the sandbox or use OpenShell file transfer commands:
|
||||
|
||||
```bash
|
||||
openshell sandbox upload pi-sandbox ./repo /workspace
|
||||
openshell sandbox download pi-sandbox /workspace/repo ./repo-out
|
||||
```
|
||||
|
||||
OpenShell providers can keep raw model API keys outside the sandbox.
|
||||
When inference routing is configured, code inside the sandbox can call `https://inference.local`, and the gateway injects the configured provider credentials upstream.
|
||||
Configure Pi to use the corresponding OpenAI-compatible or Anthropic-compatible endpoint if you want model traffic to use this route.
|
||||
|
||||
## Gondolin
|
||||
|
||||
[Gondolin](https://github.com/earendil-works/gondolin) is a local Linux micro-VM.
|
||||
Use the [example extension](../examples/extensions/gondolin) when you want `pi` on the host but all built-in tools routed into the VM.
|
||||
|
||||
Setup:
|
||||
|
||||
```bash
|
||||
cp -R packages/coding-agent/examples/extensions/gondolin ~/.pi/agent/extensions/gondolin
|
||||
cd ~/.pi/agent/extensions/gondolin
|
||||
npm install --ignore-scripts
|
||||
```
|
||||
|
||||
Run from the project you want mounted:
|
||||
|
||||
```bash
|
||||
cd /path/to/project
|
||||
pi -e ~/.pi/agent/extensions/gondolin
|
||||
```
|
||||
|
||||
The extension mounts the host cwd at `/workspace` in the VM and overrides `read`, `write`, `edit`, `bash`, `grep`, `find`, and `ls`.
|
||||
User `!` commands are routed into the VM, as well.
|
||||
File changes under `/workspace` write through to the host.
|
||||
|
||||
Requirements: Node.js >= 23.6.0 for `@earendil-works/gondolin`, plus QEMU (requires installation through your package manager).
|
||||
|
||||
## Plain Docker
|
||||
|
||||
Run the whole `pi` process in Docker when you want the simplest local container boundary.
|
||||
|
||||
`Dockerfile.pi`:
|
||||
|
||||
```dockerfile
|
||||
FROM node:24-bookworm-slim
|
||||
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends bash ca-certificates git ripgrep \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
RUN npm install -g --ignore-scripts @earendil-works/pi-coding-agent
|
||||
|
||||
WORKDIR /workspace
|
||||
ENTRYPOINT ["pi"]
|
||||
```
|
||||
|
||||
Build and run:
|
||||
|
||||
```bash
|
||||
docker build -t pi-sandbox -f Dockerfile.pi .
|
||||
|
||||
docker run --rm -it \
|
||||
-e ANTHROPIC_API_KEY \
|
||||
-v "$PWD:/workspace" \
|
||||
-v pi-agent-home:/root/.pi/agent \
|
||||
pi-sandbox
|
||||
```
|
||||
|
||||
The `-v "$PWD:/workspace"` mounts your current directory into the container at /workspace such that reads and writes in `/workspace` inside Docker directly affect your host files, like in the Gondolin example.
|
||||
|
||||
Use a named volume for `/root/.pi/agent` if you want container-local settings and sessions. Mounting your host `~/.pi/agent` exposes host auth and session files to the container.
|
||||
@@ -19,6 +19,10 @@
|
||||
"title": "Providers",
|
||||
"path": "providers.md"
|
||||
},
|
||||
{
|
||||
"title": "Containerization",
|
||||
"path": "containerization.md"
|
||||
},
|
||||
{
|
||||
"title": "Settings",
|
||||
"path": "settings.md"
|
||||
|
||||
@@ -2606,6 +2606,7 @@ All examples in [examples/extensions/](../examples/extensions/).
|
||||
| `ssh.ts` | SSH remote execution | `registerFlag`, `on("user_bash")`, `on("before_agent_start")`, tool operations |
|
||||
| `interactive-shell.ts` | Persistent shell session | `on("user_bash")` |
|
||||
| `sandbox/` | Sandboxed tool execution | Tool operations |
|
||||
| `gondolin/` | Route built-in tools and `!` commands into a Gondolin micro-VM | Tool operations, built-in tool overrides, `on("user_bash")` |
|
||||
| `subagent/` | Spawn sub-agents | `registerTool`, `exec` |
|
||||
| **Games** |||
|
||||
| `snake.ts` | Snake game | `registerCommand`, `ui.custom`, keyboard handling |
|
||||
|
||||
@@ -41,6 +41,7 @@ For the full first-run flow, see [Quickstart](quickstart.md).
|
||||
- [Quickstart](quickstart.md) - install, authenticate, and run a first session.
|
||||
- [Using Pi](usage.md) - interactive mode, slash commands, context files, and CLI reference.
|
||||
- [Providers](providers.md) - subscription and API-key setup for built-in providers.
|
||||
- [Containerization](containerization.md) - sandbox pi with OpenShell, Gondolin, or Docker.
|
||||
- [Settings](settings.md) - global and project settings.
|
||||
- [Keybindings](keybindings.md) - default shortcuts and custom keybindings.
|
||||
- [Sessions](sessions.md) - session management, branching, and tree navigation.
|
||||
|
||||
@@ -64,6 +64,7 @@ pi
|
||||
| OpenRouter | `OPENROUTER_API_KEY` | `openrouter` |
|
||||
| Vercel AI Gateway | `AI_GATEWAY_API_KEY` | `vercel-ai-gateway` |
|
||||
| ZAI | `ZAI_API_KEY` | `zai` |
|
||||
| ZAI Coding Plan (China) | `ZAI_CODING_CN_API_KEY` | `zai-coding-cn` |
|
||||
| OpenCode Zen | `OPENCODE_API_KEY` | `opencode` |
|
||||
| OpenCode Go | `OPENCODE_API_KEY` | `opencode-go` |
|
||||
| Hugging Face | `HF_TOKEN` | `huggingface` |
|
||||
|
||||
@@ -23,6 +23,7 @@ cp permission-gate.ts ~/.pi/agent/extensions/
|
||||
| `confirm-destructive.ts` | Confirms before destructive session actions (clear, switch, fork) |
|
||||
| `dirty-repo-guard.ts` | Prevents session changes with uncommitted git changes |
|
||||
| `sandbox/` | OS-level sandboxing using `@anthropic-ai/sandbox-runtime` with per-project config |
|
||||
| `gondolin/` | Route built-in tools and `!` commands into a Gondolin micro-VM |
|
||||
|
||||
### Custom Tools
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
node_modules/
|
||||
@@ -0,0 +1,531 @@
|
||||
/**
|
||||
* Gondolin Tool Routing Example
|
||||
*
|
||||
* Runs pi's built-in tools inside a local Gondolin micro-VM. The host working
|
||||
* directory is mounted at /workspace in the guest. File changes under
|
||||
* /workspace write through to the host; other guest filesystem changes are
|
||||
* isolated to the VM.
|
||||
*
|
||||
* Setup:
|
||||
* cd packages/coding-agent/examples/extensions/gondolin
|
||||
* npm install --ignore-scripts
|
||||
*
|
||||
* Usage:
|
||||
* cd /path/to/project
|
||||
* pi -e /path/to/pi/packages/coding-agent/examples/extensions/gondolin
|
||||
*
|
||||
* Requirements:
|
||||
* - Node.js >= 23.6.0 for @earendil-works/gondolin
|
||||
* - QEMU installed (for example, `brew install qemu` on macOS)
|
||||
*/
|
||||
|
||||
import path from "node:path";
|
||||
import { RealFSProvider, VM } from "@earendil-works/gondolin";
|
||||
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
||||
import {
|
||||
type BashOperations,
|
||||
createBashTool,
|
||||
createEditTool,
|
||||
createFindTool,
|
||||
createGrepTool,
|
||||
createLsTool,
|
||||
createReadTool,
|
||||
createWriteTool,
|
||||
DEFAULT_MAX_BYTES,
|
||||
type EditOperations,
|
||||
type FindOperations,
|
||||
formatSize,
|
||||
type GrepToolDetails,
|
||||
type GrepToolInput,
|
||||
type LsOperations,
|
||||
type ReadOperations,
|
||||
truncateHead,
|
||||
truncateLine,
|
||||
type WriteOperations,
|
||||
} from "@earendil-works/pi-coding-agent";
|
||||
|
||||
const GUEST_WORKSPACE = "/workspace";
|
||||
const DEFAULT_GREP_LIMIT = 100;
|
||||
|
||||
type TextToolResult<TDetails> = {
|
||||
content: Array<{ type: "text"; text: string }>;
|
||||
details: TDetails | undefined;
|
||||
};
|
||||
|
||||
function stripAtPrefix(value: string): string {
|
||||
return value.startsWith("@") ? value.slice(1) : value;
|
||||
}
|
||||
|
||||
function toPosix(value: string): string {
|
||||
return value.split(path.sep).join(path.posix.sep);
|
||||
}
|
||||
|
||||
function isInsideHostPath(root: string, value: string): boolean {
|
||||
const relativePath = path.relative(root, value);
|
||||
return relativePath === "" || (!relativePath.startsWith("..") && !path.isAbsolute(relativePath));
|
||||
}
|
||||
|
||||
function hostPathToGuest(localCwd: string, hostPath: string): string {
|
||||
const relativePath = path.relative(localCwd, hostPath);
|
||||
if (!isInsideHostPath(localCwd, hostPath)) return toPosix(hostPath);
|
||||
return relativePath ? path.posix.join(GUEST_WORKSPACE, toPosix(relativePath)) : GUEST_WORKSPACE;
|
||||
}
|
||||
|
||||
function toGuestPath(localCwd: string, inputPath: string): string {
|
||||
const trimmed = stripAtPrefix(inputPath.trim());
|
||||
if (!trimmed) return GUEST_WORKSPACE;
|
||||
if (path.isAbsolute(trimmed)) {
|
||||
if (isInsideHostPath(localCwd, trimmed)) return hostPathToGuest(localCwd, trimmed);
|
||||
return path.posix.resolve("/", toPosix(trimmed));
|
||||
}
|
||||
return path.posix.resolve(GUEST_WORKSPACE, toPosix(trimmed));
|
||||
}
|
||||
|
||||
function createGondolinReadOps(vm: VM, localCwd: string): ReadOperations {
|
||||
return {
|
||||
readFile: async (filePath) => vm.fs.readFile(toGuestPath(localCwd, filePath)),
|
||||
access: async (filePath) => {
|
||||
await vm.fs.access(toGuestPath(localCwd, filePath));
|
||||
},
|
||||
detectImageMimeType: async (filePath) => {
|
||||
const ext = path.posix.extname(toGuestPath(localCwd, filePath)).toLowerCase();
|
||||
if (ext === ".png") return "image/png";
|
||||
if (ext === ".jpg" || ext === ".jpeg") return "image/jpeg";
|
||||
if (ext === ".gif") return "image/gif";
|
||||
if (ext === ".webp") return "image/webp";
|
||||
return null;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function createGondolinWriteOps(vm: VM, localCwd: string): WriteOperations {
|
||||
return {
|
||||
writeFile: async (filePath, content) => {
|
||||
await vm.fs.writeFile(toGuestPath(localCwd, filePath), content, { encoding: "utf8" });
|
||||
},
|
||||
mkdir: async (dirPath) => {
|
||||
await vm.fs.mkdir(toGuestPath(localCwd, dirPath), { recursive: true });
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function createGondolinEditOps(vm: VM, localCwd: string): EditOperations {
|
||||
const readOps = createGondolinReadOps(vm, localCwd);
|
||||
const writeOps = createGondolinWriteOps(vm, localCwd);
|
||||
return {
|
||||
readFile: readOps.readFile,
|
||||
writeFile: writeOps.writeFile,
|
||||
access: readOps.access,
|
||||
};
|
||||
}
|
||||
|
||||
function createGondolinLsOps(vm: VM, localCwd: string): LsOperations {
|
||||
return {
|
||||
exists: async (filePath) => {
|
||||
try {
|
||||
await vm.fs.access(toGuestPath(localCwd, filePath));
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
stat: async (filePath) => vm.fs.stat(toGuestPath(localCwd, filePath)),
|
||||
readdir: async (dirPath) => vm.fs.listDir(toGuestPath(localCwd, dirPath)),
|
||||
};
|
||||
}
|
||||
|
||||
async function walkGuestFiles(
|
||||
vm: VM,
|
||||
root: string,
|
||||
visit: (guestPath: string, relativePath: string) => Promise<boolean>,
|
||||
signal?: AbortSignal,
|
||||
): Promise<boolean> {
|
||||
if (signal?.aborted) throw new Error("Operation aborted");
|
||||
const stat = await vm.fs.stat(root, { signal });
|
||||
if (!stat.isDirectory()) return visit(root, path.posix.basename(root));
|
||||
|
||||
const walkDirectory = async (dir: string, relativeDir: string): Promise<boolean> => {
|
||||
if (signal?.aborted) throw new Error("Operation aborted");
|
||||
const entries = await vm.fs.listDir(dir, { signal });
|
||||
for (const entry of entries) {
|
||||
if (entry === ".git" || entry === "node_modules") continue;
|
||||
const guestPath = path.posix.join(dir, entry);
|
||||
const relativePath = relativeDir ? path.posix.join(relativeDir, entry) : entry;
|
||||
let entryStat: Awaited<ReturnType<VM["fs"]["stat"]>>;
|
||||
try {
|
||||
entryStat = await vm.fs.stat(guestPath, { signal });
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
if (entryStat.isDirectory()) {
|
||||
if (!(await walkDirectory(guestPath, relativePath))) return false;
|
||||
} else if (!(await visit(guestPath, relativePath))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
return walkDirectory(root, "");
|
||||
}
|
||||
|
||||
function matchesToolGlob(relativePath: string, pattern: string): boolean {
|
||||
const normalizedPattern = toPosix(pattern);
|
||||
if (normalizedPattern.includes("/")) {
|
||||
return (
|
||||
path.posix.matchesGlob(relativePath, normalizedPattern) ||
|
||||
path.posix.matchesGlob(relativePath, `**/${normalizedPattern}`)
|
||||
);
|
||||
}
|
||||
return path.posix.matchesGlob(path.posix.basename(relativePath), normalizedPattern);
|
||||
}
|
||||
|
||||
function createGondolinFindOps(vm: VM, localCwd: string): FindOperations {
|
||||
return {
|
||||
exists: async (filePath) => {
|
||||
try {
|
||||
await vm.fs.access(toGuestPath(localCwd, filePath));
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
glob: async (pattern, cwd, options) => {
|
||||
const root = toGuestPath(localCwd, cwd);
|
||||
const results: string[] = [];
|
||||
await walkGuestFiles(vm, root, async (guestPath, relativePath) => {
|
||||
if (results.length >= options.limit) return false;
|
||||
if (matchesToolGlob(relativePath, pattern)) results.push(guestPath);
|
||||
return results.length < options.limit;
|
||||
});
|
||||
return results;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function createLineMatcher(pattern: string, literal: boolean | undefined, ignoreCase: boolean | undefined) {
|
||||
if (literal) {
|
||||
const needle = ignoreCase ? pattern.toLowerCase() : pattern;
|
||||
return (line: string) => (ignoreCase ? line.toLowerCase() : line).includes(needle);
|
||||
}
|
||||
const regex = new RegExp(pattern, ignoreCase ? "i" : undefined);
|
||||
return (line: string) => regex.test(line);
|
||||
}
|
||||
|
||||
function appendGrepBlock(params: {
|
||||
outputLines: string[];
|
||||
lines: string[];
|
||||
relativePath: string;
|
||||
lineIndex: number;
|
||||
contextLines: number;
|
||||
}): boolean {
|
||||
let linesTruncated = false;
|
||||
const start = params.contextLines > 0 ? Math.max(0, params.lineIndex - params.contextLines) : params.lineIndex;
|
||||
const end =
|
||||
params.contextLines > 0
|
||||
? Math.min(params.lines.length - 1, params.lineIndex + params.contextLines)
|
||||
: params.lineIndex;
|
||||
|
||||
for (let index = start; index <= end; index++) {
|
||||
const rawLine = params.lines[index] ?? "";
|
||||
const { text, wasTruncated } = truncateLine(rawLine.replace(/\r/g, ""));
|
||||
if (wasTruncated) linesTruncated = true;
|
||||
const separator = index === params.lineIndex ? ":" : "-";
|
||||
params.outputLines.push(`${params.relativePath}${separator}${index + 1}${separator} ${text}`);
|
||||
}
|
||||
return linesTruncated;
|
||||
}
|
||||
|
||||
async function executeGondolinGrep(
|
||||
vm: VM,
|
||||
localCwd: string,
|
||||
params: GrepToolInput,
|
||||
signal?: AbortSignal,
|
||||
): Promise<TextToolResult<GrepToolDetails>> {
|
||||
const root = toGuestPath(localCwd, params.path ?? ".");
|
||||
const rootStat = await vm.fs.stat(root, { signal });
|
||||
const rootIsDirectory = rootStat.isDirectory();
|
||||
const matcher = createLineMatcher(params.pattern, params.literal, params.ignoreCase);
|
||||
const contextLines = params.context && params.context > 0 ? params.context : 0;
|
||||
const effectiveLimit = Math.max(1, params.limit ?? DEFAULT_GREP_LIMIT);
|
||||
const outputLines: string[] = [];
|
||||
const details: GrepToolDetails = {};
|
||||
let matchCount = 0;
|
||||
let matchLimitReached = false;
|
||||
let linesTruncated = false;
|
||||
|
||||
await walkGuestFiles(
|
||||
vm,
|
||||
root,
|
||||
async (guestPath, relativePath) => {
|
||||
if (matchCount >= effectiveLimit) return false;
|
||||
if (params.glob && !matchesToolGlob(relativePath, params.glob)) return true;
|
||||
let content: string;
|
||||
try {
|
||||
content = await vm.fs.readFile(guestPath, { encoding: "utf8", signal });
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
const lines = content.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n");
|
||||
const displayPath = rootIsDirectory ? relativePath : path.posix.basename(guestPath);
|
||||
for (let index = 0; index < lines.length; index++) {
|
||||
if (signal?.aborted) throw new Error("Operation aborted");
|
||||
if (!matcher(lines[index] ?? "")) continue;
|
||||
matchCount++;
|
||||
if (appendGrepBlock({ outputLines, lines, relativePath: displayPath, lineIndex: index, contextLines })) {
|
||||
linesTruncated = true;
|
||||
}
|
||||
if (matchCount >= effectiveLimit) {
|
||||
matchLimitReached = true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
},
|
||||
signal,
|
||||
);
|
||||
|
||||
if (matchCount === 0) return { content: [{ type: "text", text: "No matches found" }], details: undefined };
|
||||
|
||||
const rawOutput = outputLines.join("\n");
|
||||
const truncation = truncateHead(rawOutput, { maxLines: Number.MAX_SAFE_INTEGER });
|
||||
const notices: string[] = [];
|
||||
let output = truncation.content;
|
||||
|
||||
if (matchLimitReached) {
|
||||
details.matchLimitReached = effectiveLimit;
|
||||
notices.push(`${effectiveLimit} matches limit reached`);
|
||||
}
|
||||
if (linesTruncated) {
|
||||
details.linesTruncated = true;
|
||||
notices.push("long lines truncated");
|
||||
}
|
||||
if (truncation.truncated) {
|
||||
details.truncation = truncation;
|
||||
notices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);
|
||||
}
|
||||
if (notices.length > 0) output += `\n\n[${notices.join(". ")}]`;
|
||||
|
||||
return {
|
||||
content: [{ type: "text", text: output }],
|
||||
details: Object.keys(details).length > 0 ? details : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
function sanitizeEnv(env: NodeJS.ProcessEnv | undefined): Record<string, string> | undefined {
|
||||
if (!env) return undefined;
|
||||
const result: Record<string, string> = {};
|
||||
for (const [key, value] of Object.entries(env)) {
|
||||
if (typeof value === "string") result[key] = value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function createGondolinBashOps(vm: VM, localCwd: string, shellPath: string): BashOperations {
|
||||
return {
|
||||
exec: async (command, cwd, { onData, signal, timeout, env }) => {
|
||||
if (signal?.aborted) throw new Error("aborted");
|
||||
const guestCwd = toGuestPath(localCwd, cwd);
|
||||
const controller = new AbortController();
|
||||
const onAbort = () => controller.abort();
|
||||
signal?.addEventListener("abort", onAbort, { once: true });
|
||||
|
||||
let timedOut = false;
|
||||
const timer =
|
||||
timeout && timeout > 0
|
||||
? setTimeout(() => {
|
||||
timedOut = true;
|
||||
controller.abort();
|
||||
}, timeout * 1000)
|
||||
: undefined;
|
||||
|
||||
try {
|
||||
const proc = vm.exec([shellPath, "-lc", command], {
|
||||
cwd: guestCwd,
|
||||
env: sanitizeEnv(env),
|
||||
signal: controller.signal,
|
||||
stdout: "pipe",
|
||||
stderr: "pipe",
|
||||
});
|
||||
for await (const chunk of proc.output()) onData(chunk.data);
|
||||
const result = await proc;
|
||||
return { exitCode: result.exitCode };
|
||||
} catch (error) {
|
||||
if (signal?.aborted) throw new Error("aborted");
|
||||
if (timedOut) throw new Error(`timeout:${timeout}`);
|
||||
throw error;
|
||||
} finally {
|
||||
if (timer) clearTimeout(timer);
|
||||
signal?.removeEventListener("abort", onAbort);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export default function (pi: ExtensionAPI) {
|
||||
const localCwd = process.cwd();
|
||||
const localRead = createReadTool(localCwd);
|
||||
const localWrite = createWriteTool(localCwd);
|
||||
const localEdit = createEditTool(localCwd);
|
||||
const localBash = createBashTool(localCwd);
|
||||
const localGrep = createGrepTool(localCwd);
|
||||
const localFind = createFindTool(localCwd);
|
||||
const localLs = createLsTool(localCwd);
|
||||
|
||||
let vm: VM | undefined;
|
||||
let vmStarting: Promise<VM> | undefined;
|
||||
let shellPath = "/bin/sh";
|
||||
|
||||
async function startVm(ctx?: ExtensionContext): Promise<VM> {
|
||||
ctx?.ui.setStatus("gondolin", ctx.ui.theme.fg("accent", `Gondolin: starting ${GUEST_WORKSPACE}`));
|
||||
const created = await VM.create({
|
||||
sessionLabel: `pi ${path.basename(localCwd)}`,
|
||||
vfs: {
|
||||
mounts: {
|
||||
[GUEST_WORKSPACE]: new RealFSProvider(localCwd),
|
||||
},
|
||||
},
|
||||
});
|
||||
const bashProbe = await created.exec(["/bin/sh", "-lc", "command -v bash || true"]);
|
||||
shellPath = bashProbe.stdout.trim() || "/bin/sh";
|
||||
vm = created;
|
||||
ctx?.ui.setStatus(
|
||||
"gondolin",
|
||||
ctx.ui.theme.fg("accent", `Gondolin: ${created.id.slice(0, 8)} (${GUEST_WORKSPACE})`),
|
||||
);
|
||||
ctx?.ui.notify(`Gondolin VM ready. ${localCwd} is mounted at ${GUEST_WORKSPACE}.`, "info");
|
||||
return created;
|
||||
}
|
||||
|
||||
async function ensureVm(ctx?: ExtensionContext): Promise<VM> {
|
||||
if (vm) return vm;
|
||||
if (!vmStarting) {
|
||||
vmStarting = startVm(ctx).finally(() => {
|
||||
vmStarting = undefined;
|
||||
});
|
||||
}
|
||||
return vmStarting;
|
||||
}
|
||||
|
||||
pi.on("session_start", async (_event, ctx) => {
|
||||
await ensureVm(ctx);
|
||||
});
|
||||
|
||||
pi.on("session_shutdown", async (_event, ctx) => {
|
||||
const activeVm = vm;
|
||||
vm = undefined;
|
||||
vmStarting = undefined;
|
||||
if (!activeVm) return;
|
||||
ctx.ui.setStatus("gondolin", ctx.ui.theme.fg("muted", "Gondolin: stopping"));
|
||||
try {
|
||||
await activeVm.close();
|
||||
} finally {
|
||||
ctx.ui.setStatus("gondolin", undefined);
|
||||
}
|
||||
});
|
||||
|
||||
pi.registerCommand("gondolin", {
|
||||
description: "Show Gondolin VM status",
|
||||
handler: async (_args, ctx) => {
|
||||
const activeVm = await ensureVm(ctx);
|
||||
ctx.ui.notify(
|
||||
[
|
||||
`Gondolin VM: ${activeVm.id}`,
|
||||
`Host workspace: ${localCwd}`,
|
||||
`Guest workspace: ${GUEST_WORKSPACE}`,
|
||||
`Shell: ${shellPath}`,
|
||||
].join("\n"),
|
||||
"info",
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
pi.registerTool({
|
||||
...localRead,
|
||||
async execute(id, params, signal, onUpdate, ctx) {
|
||||
const activeVm = await ensureVm(ctx);
|
||||
const tool = createReadTool(GUEST_WORKSPACE, {
|
||||
operations: createGondolinReadOps(activeVm, localCwd),
|
||||
});
|
||||
return tool.execute(id, params, signal, onUpdate);
|
||||
},
|
||||
});
|
||||
|
||||
pi.registerTool({
|
||||
...localWrite,
|
||||
async execute(id, params, signal, onUpdate, ctx) {
|
||||
const activeVm = await ensureVm(ctx);
|
||||
const tool = createWriteTool(GUEST_WORKSPACE, {
|
||||
operations: createGondolinWriteOps(activeVm, localCwd),
|
||||
});
|
||||
return tool.execute(id, params, signal, onUpdate);
|
||||
},
|
||||
});
|
||||
|
||||
pi.registerTool({
|
||||
...localEdit,
|
||||
async execute(id, params, signal, onUpdate, ctx) {
|
||||
const activeVm = await ensureVm(ctx);
|
||||
const tool = createEditTool(GUEST_WORKSPACE, {
|
||||
operations: createGondolinEditOps(activeVm, localCwd),
|
||||
});
|
||||
return tool.execute(id, params, signal, onUpdate);
|
||||
},
|
||||
});
|
||||
|
||||
pi.registerTool({
|
||||
...localBash,
|
||||
async execute(id, params, signal, onUpdate, ctx) {
|
||||
const activeVm = await ensureVm(ctx);
|
||||
const tool = createBashTool(GUEST_WORKSPACE, {
|
||||
operations: createGondolinBashOps(activeVm, localCwd, shellPath),
|
||||
});
|
||||
return tool.execute(id, params, signal, onUpdate);
|
||||
},
|
||||
});
|
||||
|
||||
pi.registerTool({
|
||||
...localLs,
|
||||
async execute(id, params, signal, onUpdate, ctx) {
|
||||
const activeVm = await ensureVm(ctx);
|
||||
const tool = createLsTool(GUEST_WORKSPACE, {
|
||||
operations: createGondolinLsOps(activeVm, localCwd),
|
||||
});
|
||||
return tool.execute(id, params, signal, onUpdate);
|
||||
},
|
||||
});
|
||||
|
||||
pi.registerTool({
|
||||
...localFind,
|
||||
async execute(id, params, signal, onUpdate, ctx) {
|
||||
const activeVm = await ensureVm(ctx);
|
||||
const tool = createFindTool(GUEST_WORKSPACE, {
|
||||
operations: createGondolinFindOps(activeVm, localCwd),
|
||||
});
|
||||
return tool.execute(id, params, signal, onUpdate);
|
||||
},
|
||||
});
|
||||
|
||||
pi.registerTool({
|
||||
...localGrep,
|
||||
async execute(_id, params, signal, _onUpdate, ctx) {
|
||||
const activeVm = await ensureVm(ctx);
|
||||
return executeGondolinGrep(activeVm, localCwd, params, signal);
|
||||
},
|
||||
});
|
||||
|
||||
pi.on("user_bash", async (_event, ctx) => {
|
||||
const activeVm = await ensureVm(ctx);
|
||||
return { operations: createGondolinBashOps(activeVm, localCwd, shellPath) };
|
||||
});
|
||||
|
||||
pi.on("before_agent_start", async (event, ctx) => {
|
||||
await ensureVm(ctx);
|
||||
const localLine = `Current working directory: ${localCwd}`;
|
||||
const guestLine = `Current working directory: ${GUEST_WORKSPACE} (Gondolin VM; host workspace mounted from ${localCwd})`;
|
||||
const systemPrompt = event.systemPrompt.includes(localLine)
|
||||
? event.systemPrompt.replace(localLine, guestLine)
|
||||
: `${event.systemPrompt}\n\n${guestLine}`;
|
||||
return { systemPrompt };
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
{
|
||||
"name": "pi-extension-gondolin",
|
||||
"version": "0.78.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "pi-extension-gondolin",
|
||||
"version": "0.78.0",
|
||||
"dependencies": {
|
||||
"@earendil-works/gondolin": "0.12.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@cto.af/wtf8": {
|
||||
"version": "0.0.5",
|
||||
"resolved": "https://registry.npmjs.org/@cto.af/wtf8/-/wtf8-0.0.5.tgz",
|
||||
"integrity": "sha512-LfUFi+Vv4eDzj+XAtR89e3wwjXA/NZjUSwU5NhwbBrLecxPaBYFy3exCuc1j+D4UZeOVdqlsl8G7LmOt18V0tg==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=20"
|
||||
}
|
||||
},
|
||||
"node_modules/@earendil-works/gondolin": {
|
||||
"version": "0.12.0",
|
||||
"resolved": "https://registry.npmjs.org/@earendil-works/gondolin/-/gondolin-0.12.0.tgz",
|
||||
"integrity": "sha512-BXbvzQKb5QmxY5NtthRDONJTu7+IDKbzqWGrJyyNXMP7N681Tx0Q9TK8pK1ba8nUvYQTipNJyGZOsJfYiZll1A==",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"cbor2": "^2.3.0",
|
||||
"node-forge": "^1.3.3",
|
||||
"ssh2": "^1.17.0",
|
||||
"undici": "^6.21.0"
|
||||
},
|
||||
"bin": {
|
||||
"gondolin": "dist/bin/gondolin.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=23.6.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"@earendil-works/gondolin-krun-runner-darwin-arm64": "0.12.0",
|
||||
"@earendil-works/gondolin-krun-runner-linux-x64": "0.12.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@earendil-works/gondolin-krun-runner-darwin-arm64": {
|
||||
"version": "0.12.0",
|
||||
"resolved": "https://registry.npmjs.org/@earendil-works/gondolin-krun-runner-darwin-arm64/-/gondolin-krun-runner-darwin-arm64-0.12.0.tgz",
|
||||
"integrity": "sha512-ftDlusht4PcT7Y3TuPrZIKrCXy3isiBTVMvlXYK0pcud2uXY6uwFTGeunYgP+8ND/60ddb+MImqbfmkcK8B84A==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"license": "Apache-2.0",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"darwin"
|
||||
],
|
||||
"bin": {
|
||||
"gondolin-krun-runner": "bin/gondolin-krun-runner"
|
||||
}
|
||||
},
|
||||
"node_modules/@earendil-works/gondolin-krun-runner-linux-x64": {
|
||||
"version": "0.12.0",
|
||||
"resolved": "https://registry.npmjs.org/@earendil-works/gondolin-krun-runner-linux-x64/-/gondolin-krun-runner-linux-x64-0.12.0.tgz",
|
||||
"integrity": "sha512-RRYsgwe2r5ApKmFNy469QgwnyjAHpAs9XANdWpTd9ol4iUYOY3sX7e0xIooAKxd+ktxGI4N/xRWicwGen3D/Ow==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"license": "Apache-2.0",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
],
|
||||
"bin": {
|
||||
"gondolin-krun-runner": "bin/gondolin-krun-runner"
|
||||
}
|
||||
},
|
||||
"node_modules/asn1": {
|
||||
"version": "0.2.6",
|
||||
"resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.6.tgz",
|
||||
"integrity": "sha512-ix/FxPn0MDjeyJ7i/yoHGFt/EX6LyNbxSEhPPXODPL+KB0VPk86UYfL0lMdy+KCnv+fmvIzySwaK5COwqVbWTQ==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"safer-buffer": "~2.1.0"
|
||||
}
|
||||
},
|
||||
"node_modules/bcrypt-pbkdf": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz",
|
||||
"integrity": "sha512-qeFIXtP4MSoi6NLqO12WfqARWWuCKi2Rn/9hJLEmtB5yTNr9DqFWkJRCf2qShWzPeAMRnOgCrq0sg/KLv5ES9w==",
|
||||
"license": "BSD-3-Clause",
|
||||
"dependencies": {
|
||||
"tweetnacl": "^0.14.3"
|
||||
}
|
||||
},
|
||||
"node_modules/buildcheck": {
|
||||
"version": "0.0.7",
|
||||
"resolved": "https://registry.npmjs.org/buildcheck/-/buildcheck-0.0.7.tgz",
|
||||
"integrity": "sha512-lHblz4ahamxpTmnsk+MNTRWsjYKv965MwOrSJyeD588rR3Jcu7swE+0wN5F+PbL5cjgu/9ObkhfzEPuofEMwLA==",
|
||||
"optional": true,
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/cbor2": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/cbor2/-/cbor2-2.3.0.tgz",
|
||||
"integrity": "sha512-76WB3hq8BoaGkMkBVJ27fW5LJU+qqDLEpgRNCG/SYKhODWXpVPOTD4UcUto3IEzYLA52nsvbhb0wabhHDn3qXg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@cto.af/wtf8": "0.0.5"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20"
|
||||
}
|
||||
},
|
||||
"node_modules/cpu-features": {
|
||||
"version": "0.0.10",
|
||||
"resolved": "https://registry.npmjs.org/cpu-features/-/cpu-features-0.0.10.tgz",
|
||||
"integrity": "sha512-9IkYqtX3YHPCzoVg1Py+o9057a3i0fp7S530UWokCSaFVTc7CwXPRiOjRjBQQ18ZCNafx78YfnG+HALxtVmOGA==",
|
||||
"hasInstallScript": true,
|
||||
"optional": true,
|
||||
"dependencies": {
|
||||
"buildcheck": "~0.0.6",
|
||||
"nan": "^2.19.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/nan": {
|
||||
"version": "2.27.0",
|
||||
"resolved": "https://registry.npmjs.org/nan/-/nan-2.27.0.tgz",
|
||||
"integrity": "sha512-hC+0LidcL3XE4rp1C4H54KujgXKzbfyTngZTwBByQxsOxCEKZT0MPQ4hOKUH2jU1OYstqdDH4onyHPDzcV0XdQ==",
|
||||
"license": "MIT",
|
||||
"optional": true
|
||||
},
|
||||
"node_modules/node-forge": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/node-forge/-/node-forge-1.4.0.tgz",
|
||||
"integrity": "sha512-LarFH0+6VfriEhqMMcLX2F7SwSXeWwnEAJEsYm5QKWchiVYVvJyV9v7UDvUv+w5HO23ZpQTXDv/GxdDdMyOuoQ==",
|
||||
"license": "(BSD-3-Clause OR GPL-2.0)",
|
||||
"engines": {
|
||||
"node": ">= 6.13.0"
|
||||
}
|
||||
},
|
||||
"node_modules/safer-buffer": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
|
||||
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/ssh2": {
|
||||
"version": "1.17.0",
|
||||
"resolved": "https://registry.npmjs.org/ssh2/-/ssh2-1.17.0.tgz",
|
||||
"integrity": "sha512-wPldCk3asibAjQ/kziWQQt1Wh3PgDFpC0XpwclzKcdT1vql6KeYxf5LIt4nlFkUeR8WuphYMKqUA56X4rjbfgQ==",
|
||||
"hasInstallScript": true,
|
||||
"dependencies": {
|
||||
"asn1": "^0.2.6",
|
||||
"bcrypt-pbkdf": "^1.0.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.16.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"cpu-features": "~0.0.10",
|
||||
"nan": "^2.23.0"
|
||||
}
|
||||
},
|
||||
"node_modules/tweetnacl": {
|
||||
"version": "0.14.5",
|
||||
"resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz",
|
||||
"integrity": "sha512-KXXFFdAbFXY4geFIwoyNK+f5Z1b7swfXABfL7HXCmoIWMKU3dmS26672A4EeQtDzLKy7SXmfBu51JolvEKwtGA==",
|
||||
"license": "Unlicense"
|
||||
},
|
||||
"node_modules/undici": {
|
||||
"version": "6.26.0",
|
||||
"resolved": "https://registry.npmjs.org/undici/-/undici-6.26.0.tgz",
|
||||
"integrity": "sha512-4yqz8a3n5HmGTlsbADNtr/dJlhkh/55Rq798G6ibiULcXbDtaLpTl1pvdqcbFfeoj3iSi52lePFM7h9H21cw/A==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=18.17"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "pi-extension-gondolin",
|
||||
"private": true,
|
||||
"version": "0.78.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"clean": "echo 'nothing to clean'",
|
||||
"build": "echo 'nothing to build'",
|
||||
"check": "echo 'nothing to check'"
|
||||
},
|
||||
"pi": {
|
||||
"extensions": [
|
||||
"./index.ts"
|
||||
]
|
||||
},
|
||||
"dependencies": {
|
||||
"@earendil-works/gondolin": "0.12.0"
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@
|
||||
"dist",
|
||||
"docs",
|
||||
"examples",
|
||||
"containerization.md",
|
||||
"CHANGELOG.md",
|
||||
"npm-shrinkwrap.json"
|
||||
],
|
||||
|
||||
@@ -346,6 +346,7 @@ ${chalk.bold("Environment Variables:")}
|
||||
OPENROUTER_API_KEY - OpenRouter API key
|
||||
AI_GATEWAY_API_KEY - Vercel AI Gateway API key
|
||||
ZAI_API_KEY - ZAI API key
|
||||
ZAI_CODING_CN_API_KEY - ZAI Coding Plan API key (China)
|
||||
MISTRAL_API_KEY - Mistral API key
|
||||
MINIMAX_API_KEY - MiniMax API key
|
||||
MOONSHOT_API_KEY - Moonshot AI API key
|
||||
|
||||
@@ -45,6 +45,8 @@ type LockResult<T> = {
|
||||
next?: string;
|
||||
};
|
||||
|
||||
const AUTH_FILE_WRITE_OPTIONS = { encoding: "utf-8", mode: 0o600 } as const;
|
||||
|
||||
export interface AuthStorageBackend {
|
||||
withLock<T>(fn: (current: string | undefined) => LockResult<T>): T;
|
||||
withLockAsync<T>(fn: (current: string | undefined) => Promise<LockResult<T>>): Promise<T>;
|
||||
@@ -66,7 +68,7 @@ export class FileAuthStorageBackend implements AuthStorageBackend {
|
||||
|
||||
private ensureFileExists(): void {
|
||||
if (!existsSync(this.authPath)) {
|
||||
writeFileSync(this.authPath, "{}", "utf-8");
|
||||
writeFileSync(this.authPath, "{}", AUTH_FILE_WRITE_OPTIONS);
|
||||
chmodSync(this.authPath, 0o600);
|
||||
}
|
||||
}
|
||||
@@ -108,7 +110,7 @@ export class FileAuthStorageBackend implements AuthStorageBackend {
|
||||
const current = existsSync(this.authPath) ? readFileSync(this.authPath, "utf-8") : undefined;
|
||||
const { result, next } = fn(current);
|
||||
if (next !== undefined) {
|
||||
writeFileSync(this.authPath, next, "utf-8");
|
||||
writeFileSync(this.authPath, next, AUTH_FILE_WRITE_OPTIONS);
|
||||
chmodSync(this.authPath, 0o600);
|
||||
}
|
||||
return result;
|
||||
@@ -153,7 +155,7 @@ export class FileAuthStorageBackend implements AuthStorageBackend {
|
||||
const { result, next } = await fn(current);
|
||||
throwIfCompromised();
|
||||
if (next !== undefined) {
|
||||
writeFileSync(this.authPath, next, "utf-8");
|
||||
writeFileSync(this.authPath, next, AUTH_FILE_WRITE_OPTIONS);
|
||||
chmodSync(this.authPath, 0o600);
|
||||
}
|
||||
throwIfCompromised();
|
||||
|
||||
@@ -29,6 +29,7 @@ export const defaultModelPerProvider: Record<KnownProvider, string> = {
|
||||
groq: "openai/gpt-oss-120b",
|
||||
cerebras: "zai-glm-4.7",
|
||||
zai: "glm-5.1",
|
||||
"zai-coding-cn": "glm-5.1",
|
||||
mistral: "devstral-medium-latest",
|
||||
minimax: "MiniMax-M2.7",
|
||||
"minimax-cn": "MiniMax-M2.7",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { ChildProcess, ChildProcessByStdio } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
||||
import { homedir, tmpdir } from "node:os";
|
||||
import { chmodSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
|
||||
function getEnv(): NodeJS.ProcessEnv {
|
||||
if (process.platform !== "linux" || Object.keys(process.env).length > 0) {
|
||||
@@ -206,6 +206,13 @@ function getHomeDir(): string {
|
||||
return process.env.HOME || homedir();
|
||||
}
|
||||
|
||||
export function getExtensionTempFolder(agentDir: string): string {
|
||||
const tempFolder = join(agentDir, "tmp", "extensions");
|
||||
mkdirSync(tempFolder, { recursive: true, mode: 0o700 });
|
||||
chmodSync(tempFolder, 0o700);
|
||||
return tempFolder;
|
||||
}
|
||||
|
||||
function prefixIgnorePattern(line: string, prefix: string): string | null {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed) return null;
|
||||
@@ -1952,10 +1959,11 @@ export class DefaultPackageManager implements PackageManager {
|
||||
if (scope === "temporary") {
|
||||
return this.getTemporaryDir(`git-${source.host}`, source.path);
|
||||
}
|
||||
if (scope === "project") {
|
||||
return join(this.cwd, CONFIG_DIR_NAME, "git", source.host, source.path);
|
||||
const installRoot = this.getGitInstallRoot(scope);
|
||||
if (!installRoot) {
|
||||
throw new Error("Missing git install root");
|
||||
}
|
||||
return join(this.agentDir, "git", source.host, source.path);
|
||||
return this.resolveManagedPath(installRoot, source.host, source.path);
|
||||
}
|
||||
|
||||
private getGitInstallRoot(scope: SourceScope): string | undefined {
|
||||
@@ -1969,11 +1977,21 @@ export class DefaultPackageManager implements PackageManager {
|
||||
}
|
||||
|
||||
private getTemporaryDir(prefix: string, suffix?: string): string {
|
||||
const root = this.resolveManagedPath(getExtensionTempFolder(this.agentDir), prefix);
|
||||
const hash = createHash("sha256")
|
||||
.update(`${prefix}-${suffix ?? ""}`)
|
||||
.digest("hex")
|
||||
.slice(0, 8);
|
||||
return join(tmpdir(), "pi-extensions", prefix, hash, suffix ?? "");
|
||||
return this.resolveManagedPath(root, hash, suffix ?? "");
|
||||
}
|
||||
|
||||
private resolveManagedPath(root: string, ...parts: string[]): string {
|
||||
const resolvedRoot = resolve(root);
|
||||
const resolvedPath = resolve(resolvedRoot, ...parts);
|
||||
if (resolvedPath !== resolvedRoot && !resolvedPath.startsWith(`${resolvedRoot}${sep}`)) {
|
||||
throw new Error(`Refusing to use path outside package install root: ${resolvedPath}`);
|
||||
}
|
||||
return resolvedPath;
|
||||
}
|
||||
|
||||
private getBaseDirForScope(scope: SourceScope): string {
|
||||
|
||||
@@ -27,6 +27,7 @@ export const BUILT_IN_PROVIDER_DISPLAY_NAMES: Record<string, string> = {
|
||||
"vercel-ai-gateway": "Vercel AI Gateway",
|
||||
xai: "xAI",
|
||||
zai: "ZAI",
|
||||
"zai-coding-cn": "ZAI Coding Plan (China)",
|
||||
xiaomi: "Xiaomi MiMo",
|
||||
"xiaomi-token-plan-cn": "Xiaomi MiMo Token Plan (China)",
|
||||
"xiaomi-token-plan-ams": "Xiaomi MiMo Token Plan (Amsterdam)",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { getOAuthProviders, type OAuthDeviceCodeInfo } from "@earendil-works/pi-ai/oauth";
|
||||
import { Container, type Focusable, getKeybindings, Input, Spacer, Text, type TUI } from "@earendil-works/pi-tui";
|
||||
import { exec } from "child_process";
|
||||
import { openBrowser } from "../../../utils/open-browser.ts";
|
||||
import { theme } from "../theme/theme.ts";
|
||||
import { DynamicBorder } from "./dynamic-border.ts";
|
||||
import { keyHint } from "./keybinding-hints.ts";
|
||||
@@ -101,7 +101,7 @@ export class LoginDialogComponent extends Container implements Focusable {
|
||||
this.contentContainer.addChild(new Text(theme.fg("warning", instructions), 1, 0));
|
||||
}
|
||||
|
||||
this.openUrl(url);
|
||||
openBrowser(url);
|
||||
this.tui.requestRender();
|
||||
}
|
||||
|
||||
@@ -120,19 +120,10 @@ export class LoginDialogComponent extends Container implements Focusable {
|
||||
this.contentContainer.addChild(new Spacer(1));
|
||||
this.contentContainer.addChild(new Text(theme.fg("warning", `Enter code: ${info.userCode}`), 1, 0));
|
||||
|
||||
this.openUrl(info.verificationUri);
|
||||
openBrowser(info.verificationUri);
|
||||
this.tui.requestRender();
|
||||
}
|
||||
|
||||
private openUrl(url: string): void {
|
||||
const openCmd = process.platform === "darwin" ? "open" : process.platform === "win32" ? "start" : "xdg-open";
|
||||
try {
|
||||
exec(`${openCmd} "${url}"`, () => {});
|
||||
} catch {
|
||||
// Ignore browser launch failures. The URL remains visible for manual opening/copying.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show input for manual code/URL entry (for callback server providers)
|
||||
*/
|
||||
|
||||
@@ -73,6 +73,56 @@ function splitRef(url: string): { repo: string; ref?: string } {
|
||||
};
|
||||
}
|
||||
|
||||
function decodeForValidation(value: string): string | null {
|
||||
try {
|
||||
return decodeURIComponent(value);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function hasUnsafeGitInstallPart(value: string, allowSlash: boolean): boolean {
|
||||
const decoded = decodeForValidation(value);
|
||||
if (decoded === null) {
|
||||
return true;
|
||||
}
|
||||
const candidates = [value, decoded];
|
||||
for (const candidate of candidates) {
|
||||
if (candidate.includes("\0") || candidate.includes("\\") || candidate.startsWith("/")) {
|
||||
return true;
|
||||
}
|
||||
if (!allowSlash && candidate.includes("/")) {
|
||||
return true;
|
||||
}
|
||||
if (candidate.split("/").includes("..")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function buildGitSource(args: { repo: string; host: string; path: string; ref?: string }): GitSource | null {
|
||||
if (args.path.startsWith("/")) {
|
||||
return null;
|
||||
}
|
||||
const normalizedPath = args.path.replace(/\.git$/, "").replace(/^\/+/, "");
|
||||
if (!args.host || !normalizedPath || normalizedPath.split("/").length < 2) {
|
||||
return null;
|
||||
}
|
||||
if (hasUnsafeGitInstallPart(args.host, false) || hasUnsafeGitInstallPart(normalizedPath, true)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
type: "git",
|
||||
repo: args.repo,
|
||||
host: args.host,
|
||||
path: normalizedPath,
|
||||
ref: args.ref,
|
||||
pinned: Boolean(args.ref),
|
||||
};
|
||||
}
|
||||
|
||||
function parseGenericGitUrl(url: string): GitSource | null {
|
||||
const { repo: repoWithoutRef, ref } = splitRef(url);
|
||||
let repo = repoWithoutRef;
|
||||
@@ -109,19 +159,7 @@ function parseGenericGitUrl(url: string): GitSource | null {
|
||||
repo = `https://${repoWithoutRef}`;
|
||||
}
|
||||
|
||||
const normalizedPath = path.replace(/\.git$/, "").replace(/^\/+/, "");
|
||||
if (!host || !normalizedPath || normalizedPath.split("/").length < 2) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
type: "git",
|
||||
repo,
|
||||
host,
|
||||
path: normalizedPath,
|
||||
ref,
|
||||
pinned: Boolean(ref),
|
||||
};
|
||||
return buildGitSource({ repo, host, path, ref });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,14 +195,12 @@ export function parseGitUrl(source: string): GitSource | null {
|
||||
!split.repo.startsWith("ssh://") &&
|
||||
!split.repo.startsWith("git://") &&
|
||||
!split.repo.startsWith("git@");
|
||||
return {
|
||||
type: "git",
|
||||
return buildGitSource({
|
||||
repo: useHttpsPrefix ? `https://${split.repo}` : split.repo,
|
||||
host: info.domain || "",
|
||||
path: `${info.user}/${info.project}`.replace(/\.git$/, ""),
|
||||
path: `${info.user}/${info.project}`,
|
||||
ref: info.committish || split.ref || undefined,
|
||||
pinned: Boolean(info.committish || split.ref),
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,14 +213,12 @@ export function parseGitUrl(source: string): GitSource | null {
|
||||
if (split.ref && info.project?.includes("@")) {
|
||||
continue;
|
||||
}
|
||||
return {
|
||||
type: "git",
|
||||
return buildGitSource({
|
||||
repo: `https://${split.repo}`,
|
||||
host: info.domain || "",
|
||||
path: `${info.user}/${info.project}`.replace(/\.git$/, ""),
|
||||
path: `${info.user}/${info.project}`,
|
||||
ref: info.committish || split.ref || undefined,
|
||||
pinned: Boolean(info.committish || split.ref),
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { spawn } from "node:child_process";
|
||||
|
||||
/**
|
||||
* Open a URL or file in the platform browser/default handler.
|
||||
*
|
||||
* This intentionally never invokes a shell. On Windows, do not use
|
||||
* `cmd /c start`: cmd.exe re-parses metacharacters (&, |, ^, ...) before
|
||||
* `start` runs, which would make attacker-controlled URLs injectable.
|
||||
*/
|
||||
export function openBrowser(target: string): void {
|
||||
const [cmd, args]: [string, string[]] =
|
||||
process.platform === "darwin"
|
||||
? ["open", [target]]
|
||||
: process.platform === "win32"
|
||||
? ["rundll32", ["url.dll,FileProtocolHandler", target]]
|
||||
: ["xdg-open", [target]];
|
||||
|
||||
// spawn reports launcher failures (for example, missing xdg-open) via an
|
||||
// error event. Browser launch is best-effort: callers still present the target
|
||||
// to the user, so keep the launcher failure from becoming a process crash.
|
||||
spawn(cmd, args, { stdio: "ignore", detached: true })
|
||||
.on("error", () => {})
|
||||
.unref();
|
||||
}
|
||||
@@ -62,6 +62,19 @@ describe("Git URL Parsing", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should reject unsafe git install path inputs", () => {
|
||||
for (const source of [
|
||||
"git:git@evil.example:../../victim/repo",
|
||||
"https://evil.example/..%2F..%2Fvictim/repo",
|
||||
"https://evil.example/..%2F..%2Fvictim/repo%",
|
||||
"git:git@evil.example:/absolute/repo",
|
||||
"git:git@evil.example:user\\repo/name",
|
||||
"git:git@evil.example:user/repo\0name",
|
||||
]) {
|
||||
expect(parseGitUrl(source)).toBeNull();
|
||||
}
|
||||
});
|
||||
|
||||
describe("unsupported without git: prefix", () => {
|
||||
it("should reject git@host:path without git: prefix", () => {
|
||||
expect(parseGitUrl("git@github.com:user/repo")).toBeNull();
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
*/
|
||||
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
@@ -51,6 +50,20 @@ function getFileContent(repoDir: string, filename: string): string {
|
||||
return readFileSync(join(repoDir, filename), "utf-8");
|
||||
}
|
||||
|
||||
type GitSourceForTest = {
|
||||
type: "git";
|
||||
repo: string;
|
||||
host: string;
|
||||
path: string;
|
||||
pinned: boolean;
|
||||
ref?: string;
|
||||
};
|
||||
|
||||
interface PackageManagerPathInternals {
|
||||
parseSource(source: string): GitSourceForTest;
|
||||
getGitInstallPath(source: GitSourceForTest, scope: "temporary"): string;
|
||||
}
|
||||
|
||||
describe("DefaultPackageManager git update", () => {
|
||||
let tempDir: string;
|
||||
let remoteDir: string; // Simulates the "remote" repository
|
||||
@@ -376,10 +389,8 @@ describe("DefaultPackageManager git update", () => {
|
||||
|
||||
describe("temporary git sources", () => {
|
||||
it("should refresh cached temporary git sources when resolving", async () => {
|
||||
const gitHost = "github.com";
|
||||
const gitPath = "test/extension";
|
||||
const hash = createHash("sha256").update(`git-${gitHost}-${gitPath}`).digest("hex").slice(0, 8);
|
||||
const cachedDir = join(tmpdir(), "pi-extensions", `git-${gitHost}`, hash, gitPath);
|
||||
const managerWithPaths = packageManager as unknown as PackageManagerPathInternals;
|
||||
const cachedDir = managerWithPaths.getGitInstallPath(managerWithPaths.parseSource(gitSource), "temporary");
|
||||
const extensionFile = join(cachedDir, "pi-extensions", "session-breakdown.ts");
|
||||
|
||||
rmSync(cachedDir, { recursive: true, force: true });
|
||||
@@ -423,10 +434,8 @@ describe("DefaultPackageManager git update", () => {
|
||||
});
|
||||
|
||||
it("should not refresh pinned temporary git sources", async () => {
|
||||
const gitHost = "github.com";
|
||||
const gitPath = "test/extension";
|
||||
const hash = createHash("sha256").update(`git-${gitHost}-${gitPath}`).digest("hex").slice(0, 8);
|
||||
const cachedDir = join(tmpdir(), "pi-extensions", `git-${gitHost}`, hash, gitPath);
|
||||
const managerWithPaths = packageManager as unknown as PackageManagerPathInternals;
|
||||
const cachedDir = managerWithPaths.getGitInstallPath(managerWithPaths.parseSource(gitSource), "temporary");
|
||||
const extensionFile = join(cachedDir, "pi-extensions", "session-breakdown.ts");
|
||||
|
||||
rmSync(cachedDir, { recursive: true, force: true });
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { EventEmitter } from "node:events";
|
||||
import { mkdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { mkdirSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join, relative } from "node:path";
|
||||
import { PassThrough } from "node:stream";
|
||||
@@ -33,6 +33,20 @@ interface PackageManagerInternals {
|
||||
options?: { cwd?: string; timeoutMs?: number; env?: Record<string, string> },
|
||||
): Promise<string>;
|
||||
getLocalGitUpdateTarget(installedPath: string): Promise<{ ref: string; head: string; fetchArgs: string[] }>;
|
||||
parseSource(
|
||||
source: string,
|
||||
):
|
||||
| { type: "npm"; spec: string; name: string; pinned: boolean }
|
||||
| { type: "git"; repo: string; host: string; path: string; pinned: boolean; ref?: string }
|
||||
| { type: "local"; path: string };
|
||||
getNpmInstallPath(
|
||||
source: { type: "npm"; spec: string; name: string; pinned: boolean },
|
||||
scope: "user" | "project" | "temporary",
|
||||
): string;
|
||||
getGitInstallPath(
|
||||
source: { type: "git"; repo: string; host: string; path: string; pinned: boolean; ref?: string },
|
||||
scope: "user" | "project" | "temporary",
|
||||
): string;
|
||||
}
|
||||
|
||||
// Helper to check if a resource is enabled
|
||||
@@ -1138,6 +1152,45 @@ Content`,
|
||||
});
|
||||
});
|
||||
|
||||
describe("git install paths", () => {
|
||||
it("should reject paths outside git install roots", () => {
|
||||
const managerWithInternals = packageManager as unknown as PackageManagerInternals;
|
||||
const traversalSource = {
|
||||
type: "git" as const,
|
||||
repo: "git@evil.example:../../victim/repo",
|
||||
host: "evil.example",
|
||||
path: "../../victim/repo",
|
||||
pinned: false,
|
||||
};
|
||||
|
||||
for (const scope of ["user", "project", "temporary"] as const) {
|
||||
expect(() => managerWithInternals.getGitInstallPath(traversalSource, scope)).toThrow(
|
||||
"outside package install root",
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("temporary install paths", () => {
|
||||
it("should place temporary npm packages under the agent temp extension folder", () => {
|
||||
const managerWithInternals = packageManager as unknown as PackageManagerInternals;
|
||||
const source = managerWithInternals.parseSource("npm:left-pad");
|
||||
if (source.type !== "npm") {
|
||||
throw new Error("Expected npm source");
|
||||
}
|
||||
|
||||
const installPath = managerWithInternals.getNpmInstallPath(source, "temporary");
|
||||
const tempRoot = join(agentDir, "tmp", "extensions");
|
||||
|
||||
expect(pathEndsWith(installPath, "node_modules/left-pad")).toBe(true);
|
||||
expect(relative(tempRoot, installPath).startsWith("..")).toBe(false);
|
||||
expect(installPath.startsWith(join(tmpdir(), "pi-extensions"))).toBe(false);
|
||||
if (process.platform !== "win32") {
|
||||
expect(statSync(tempRoot).mode & 0o777).toBe(0o700);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("settings source normalization", () => {
|
||||
it("should store global local packages relative to agent settings base", () => {
|
||||
const pkgDir = join(tempDir, "packages", "local-global-pkg");
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
# Tavily API key for web search
|
||||
# Get your key at: https://tavily.com/
|
||||
TAVILY_API_KEY=***
|
||||
|
||||
# OpenRouter API key for LLM
|
||||
# Get your key at: https://openrouter.ai/
|
||||
OPENROUTER_API_KEY=***
|
||||
|
||||
# Optional: Override default model (minimax/minimax-m3)
|
||||
# Examples: openai/gpt-4o, anthropic/claude-3.5-sonnet, google/gemini-pro-1.5
|
||||
# LLM_MODEL=minimax/minimax-m3
|
||||
@@ -0,0 +1,44 @@
|
||||
# Build outputs
|
||||
dist/
|
||||
*.tsbuildinfo
|
||||
|
||||
# Dependencies
|
||||
node_modules/
|
||||
|
||||
# Test coverage
|
||||
coverage/
|
||||
.nyc_output/
|
||||
|
||||
# Environment files (keep .env.example)
|
||||
.env
|
||||
.env.local
|
||||
.env.*.local
|
||||
|
||||
# Research output (user-specific)
|
||||
research-output/
|
||||
|
||||
# IDE and editor
|
||||
.vscode/
|
||||
.idea/
|
||||
*.swp
|
||||
*.swo
|
||||
*~
|
||||
|
||||
# OS files
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# Logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
|
||||
# Temporary files
|
||||
*.tmp
|
||||
*.bak
|
||||
*.orig
|
||||
|
||||
# MCP auth tokens (if stored locally)
|
||||
.mcp-tokens/
|
||||
@@ -0,0 +1,50 @@
|
||||
# Source files (only dist is published)
|
||||
src/
|
||||
tsconfig.build.json
|
||||
tsconfig.json
|
||||
|
||||
# Test files
|
||||
test/
|
||||
tests/
|
||||
**/*.test.ts
|
||||
**/*.spec.ts
|
||||
vitest.config.*
|
||||
**/__tests__/**
|
||||
|
||||
# Examples (not needed for production, but keep them accessible)
|
||||
# examples/
|
||||
|
||||
# Dev configs
|
||||
.eslintrc*
|
||||
.prettierrc*
|
||||
biome.json
|
||||
|
||||
# Environment files
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
|
||||
# IDE and editor
|
||||
.vscode/
|
||||
.idea/
|
||||
*.swp
|
||||
*.swo
|
||||
*~
|
||||
|
||||
# OS files
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# Build outputs (only dist is needed)
|
||||
*.tsbuildinfo
|
||||
coverage/
|
||||
.nyc_output/
|
||||
|
||||
# Logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
# Local research output (user-specific)
|
||||
research-output/
|
||||
@@ -0,0 +1,24 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
- Initial release of Pi Research Agent
|
||||
- Autonomous research workflow with web search and source analysis
|
||||
- Four built-in tools: `web_search`, `read_url`, `save_note`, `synthesize_report`
|
||||
- CLI interface with TUI and print modes
|
||||
- Programmatic API via `ResearchAgent` class
|
||||
- MCP (Model Context Protocol) server with three tools: `research`, `quick_search`, `read_url`
|
||||
- Skills system for customizing research behavior via `~/.pi/skills/research/`
|
||||
- Automatic `.env` file loading for API keys
|
||||
- Configurable max turns, output formats (markdown, JSON, bullet)
|
||||
- Automatic report saving to `./research-output/<timestamp>-<topic>/`
|
||||
- Three confidence levels for notes: high, medium, low
|
||||
- Retry logic for `read_url` with alternative User-Agent
|
||||
- Fallback report generation when notes are insufficient
|
||||
- Examples for basic usage, custom models, custom skills, and MCP client
|
||||
@@ -0,0 +1,194 @@
|
||||
# Contributing to Pi Research Agent
|
||||
|
||||
Thank you for your interest in contributing to Pi Research Agent! This document provides guidelines and instructions for contributing.
|
||||
|
||||
## Code of Conduct
|
||||
|
||||
By participating in this project, you agree to maintain a respectful and inclusive environment for everyone.
|
||||
|
||||
## How to Contribute
|
||||
|
||||
### Reporting Bugs
|
||||
|
||||
If you find a bug, please open an issue on GitHub with:
|
||||
- A clear, descriptive title
|
||||
- Steps to reproduce the bug
|
||||
- Expected vs actual behavior
|
||||
- Your environment (Node.js version, OS, etc.)
|
||||
- Relevant logs or error messages
|
||||
|
||||
### Suggesting Features
|
||||
|
||||
Feature requests are welcome! Please open an issue with:
|
||||
- A clear description of the feature
|
||||
- Use cases and motivation
|
||||
- Any implementation ideas you have
|
||||
|
||||
### Submitting Pull Requests
|
||||
|
||||
1. **Fork the repository** and create a new branch from `main`:
|
||||
```bash
|
||||
git checkout -b feature/your-feature-name
|
||||
```
|
||||
|
||||
2. **Install dependencies**:
|
||||
```bash
|
||||
npm install
|
||||
```
|
||||
|
||||
3. **Make your changes** following the code style guidelines below
|
||||
|
||||
4. **Add tests** for new functionality
|
||||
|
||||
5. **Run the build and tests**:
|
||||
```bash
|
||||
npm run build
|
||||
npm test
|
||||
```
|
||||
|
||||
6. **Commit your changes** with a clear message:
|
||||
```bash
|
||||
git commit -m "Add: brief description of your changes"
|
||||
```
|
||||
|
||||
7. **Push to your fork** and submit a pull request
|
||||
|
||||
8. **Wait for review** - maintainers will review your PR and may request changes
|
||||
|
||||
## Development Setup
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- Node.js >= 22.19.0
|
||||
- npm or pnpm
|
||||
- Git
|
||||
|
||||
### Project Structure
|
||||
|
||||
```
|
||||
packages/research-agent/
|
||||
├── src/
|
||||
│ ├── agent.ts # Main ResearchAgent class
|
||||
│ ├── cli.ts # CLI entry point
|
||||
│ ├── mcp-cli.ts # MCP server entry point
|
||||
│ ├── mcp-server.ts # MCP server implementation
|
||||
│ ├── system-prompt.ts # System prompt templates
|
||||
│ ├── config.ts # Configuration constants
|
||||
│ ├── types.ts # TypeScript type definitions
|
||||
│ ├── tools/ # Built-in research tools
|
||||
│ ├── utils/ # Utility functions
|
||||
│ ├── skills/ # Skills system
|
||||
│ └── modes/ # Output modes (TUI, print)
|
||||
├── examples/ # Usage examples
|
||||
├── package.json
|
||||
└── tsconfig.build.json
|
||||
```
|
||||
|
||||
### Code Style
|
||||
|
||||
- Use TypeScript with strict mode
|
||||
- Follow the existing code conventions
|
||||
- Use 4-space indentation (matches the rest of the monorepo)
|
||||
- Use single quotes for strings
|
||||
- Add JSDoc comments for public APIs
|
||||
- Avoid `any` types when possible
|
||||
- Use `import` statements (not `require`)
|
||||
|
||||
### Testing
|
||||
|
||||
- Write unit tests for new functions
|
||||
- Write integration tests for new features
|
||||
- Ensure all tests pass before submitting a PR
|
||||
- Aim for good test coverage
|
||||
|
||||
### Commit Messages
|
||||
|
||||
Use clear, descriptive commit messages:
|
||||
- `Add: new feature description`
|
||||
- `Fix: bug description`
|
||||
- `Update: change description`
|
||||
- `Refactor: code improvement description`
|
||||
- `Docs: documentation update`
|
||||
|
||||
## Adding New Tools
|
||||
|
||||
To add a new research tool:
|
||||
|
||||
1. Create a new file in `src/tools/`:
|
||||
```typescript
|
||||
// src/tools/my-tool.ts
|
||||
import type { AgentTool } from "@earendil-works/pi-agent-core";
|
||||
import { Type, type Static } from "typebox";
|
||||
|
||||
const mySchema = Type.Object({
|
||||
param: Type.String({ description: "Parameter description" }),
|
||||
});
|
||||
|
||||
export type MyInput = Static<typeof mySchema>;
|
||||
|
||||
export function createMyTool(): AgentTool<typeof mySchema, MyDetails> {
|
||||
return {
|
||||
name: "my_tool",
|
||||
label: "My Tool",
|
||||
description: "Tool description",
|
||||
parameters: mySchema,
|
||||
execute: async (toolCallId, params: MyInput) => {
|
||||
// Implementation
|
||||
return {
|
||||
content: [{ type: "text" as const, text: "result" }],
|
||||
details: {},
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
2. Register the tool in `src/tools/index.ts`
|
||||
|
||||
3. Add tests for the tool
|
||||
|
||||
4. Update documentation
|
||||
|
||||
## Adding MCP Tools
|
||||
|
||||
To add a new MCP tool:
|
||||
|
||||
1. Add the tool definition in `src/mcp-server.ts`:
|
||||
```typescript
|
||||
server.registerTool(
|
||||
"my_tool",
|
||||
{
|
||||
description: "Tool description",
|
||||
inputSchema: {
|
||||
param: z.string().describe("Parameter description"),
|
||||
},
|
||||
},
|
||||
async (params: any) => {
|
||||
// Implementation
|
||||
return {
|
||||
content: [{ type: "text" as const, text: "result" }],
|
||||
};
|
||||
},
|
||||
);
|
||||
```
|
||||
|
||||
2. Add tests
|
||||
|
||||
3. Update MCP_SERVER.md documentation
|
||||
|
||||
## Release Process
|
||||
|
||||
1. Update version in `package.json` following semver
|
||||
2. Update `CHANGELOG.md` with changes
|
||||
3. Create a git tag: `git tag v0.1.0`
|
||||
4. Push tag: `git push origin v0.1.0`
|
||||
5. GitHub Actions will publish to npm automatically
|
||||
|
||||
## Questions?
|
||||
|
||||
If you have questions, feel free to:
|
||||
- Open an issue on GitHub
|
||||
- Join our community chat
|
||||
- Email the maintainers
|
||||
|
||||
Thank you for contributing! 🎉
|
||||
@@ -0,0 +1,76 @@
|
||||
# Environment Variables Setup
|
||||
|
||||
## Quick Start
|
||||
|
||||
1. **Copy the example file:**
|
||||
```bash
|
||||
cp packages/research-agent/.env.example packages/research-agent/.env
|
||||
```
|
||||
|
||||
2. **Edit with your API keys:**
|
||||
```bash
|
||||
nano packages/research-agent/.env
|
||||
```
|
||||
|
||||
3. **Add your keys:**
|
||||
```env
|
||||
TAVILY_API_KEY=***
|
||||
OPENROUTER_API_KEY=***
|
||||
LLM_MODEL=minimax/minimax-m3
|
||||
```
|
||||
|
||||
4. **Run the agent:**
|
||||
```bash
|
||||
node packages/research-agent/dist/cli.js "your research topic"
|
||||
```
|
||||
|
||||
## How It Works
|
||||
|
||||
The research agent automatically loads environment variables from `.env` files in this order:
|
||||
|
||||
1. `./.env` (current working directory)
|
||||
2. `./packages/research-agent/.env`
|
||||
|
||||
**Important:**
|
||||
- Environment variables (set via `export`) take precedence over `.env` file values
|
||||
- The `.env` file is in `.gitignore` and will not be committed
|
||||
- You can also use Node.js 22+ built-in support: `node --env-file=.env ...`
|
||||
|
||||
## Alternative: Export Variables
|
||||
|
||||
You can also set variables directly in your shell:
|
||||
|
||||
```bash
|
||||
export TAVILY_API_KEY=***
|
||||
export OPENROUTER_API_KEY=***
|
||||
export LLM_MODEL=openai/gpt-4o
|
||||
|
||||
node packages/research-agent/dist/cli.js "your topic"
|
||||
```
|
||||
|
||||
## Getting API Keys
|
||||
|
||||
### Tavily API Key
|
||||
1. Go to https://tavily.com/
|
||||
2. Sign up for an account
|
||||
3. Get your API key from the dashboard
|
||||
4. Free tier: 1000 searches/month
|
||||
|
||||
### OpenRouter API Key
|
||||
1. Go to https://openrouter.ai/
|
||||
2. Sign up for an account
|
||||
3. Add credits to your account
|
||||
4. Create an API key in settings
|
||||
5. Pay-per-use pricing for various models
|
||||
|
||||
## Supported Models
|
||||
|
||||
Default: `minimax/minimax-m3`
|
||||
|
||||
Other popular options:
|
||||
- `openai/gpt-4o`
|
||||
- `anthropic/claude-3.5-sonnet`
|
||||
- `google/gemini-pro-1.5`
|
||||
- `meta-llama/llama-3.1-70b-instruct`
|
||||
|
||||
See all available models at https://openrouter.ai/models
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 Pi Research Team
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,277 @@
|
||||
# MCP Integration Architecture
|
||||
|
||||
## Обзор
|
||||
|
||||
Pi Research Agent теперь поддерживает MCP (Model Context Protocol), что позволяет другим AI агентам вызывать его для глубокого исследования тем.
|
||||
|
||||
## Архитектура
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ MCP Client (Claude, Cursor, etc.) │
|
||||
│ │
|
||||
│ - Claude Desktop │
|
||||
│ - Cursor │
|
||||
│ - Custom agents │
|
||||
│ - Any MCP-compatible client │
|
||||
└────────────────────────┬────────────────────────────────────┘
|
||||
│ stdio (JSON-RPC)
|
||||
▼
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ Pi Research Agent MCP Server │
|
||||
│ │
|
||||
│ Tools: │
|
||||
│ ├─ research(topic, maxTurns, format) │
|
||||
│ │ └─ Full autonomous research with report generation │
|
||||
│ │ │
|
||||
│ ├─ quick_search(query, maxResults) │
|
||||
│ │ └─ Fast web search via Tavily API │
|
||||
│ │ │
|
||||
│ └─ read_url(url, maxLength) │
|
||||
│ └─ Extract content from web pages │
|
||||
└─────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## Компоненты
|
||||
|
||||
### 1. MCP Server (`src/mcp-server.ts`)
|
||||
|
||||
Создаёт MCP server с тремя tools:
|
||||
|
||||
```typescript
|
||||
export function createResearchMcpServer(): McpServer {
|
||||
const server = new McpServer({
|
||||
name: "pi-research-agent",
|
||||
version: VERSION,
|
||||
});
|
||||
|
||||
server.registerTool("research", { ... });
|
||||
server.registerTool("quick_search", { ... });
|
||||
server.registerTool("read_url", { ... });
|
||||
|
||||
return server;
|
||||
}
|
||||
```
|
||||
|
||||
### 2. MCP CLI (`src/mcp-cli.ts`)
|
||||
|
||||
Entry point для запуска MCP server:
|
||||
|
||||
```typescript
|
||||
#!/usr/bin/env node
|
||||
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
||||
import { loadEnvFile } from "./utils/env.ts";
|
||||
import { createResearchMcpServer } from "./mcp-server.ts";
|
||||
|
||||
loadEnvFile();
|
||||
|
||||
const server = createResearchMcpServer();
|
||||
const transport = new StdioServerTransport();
|
||||
await server.connect(transport);
|
||||
```
|
||||
|
||||
### 3. Research Agent (`src/agent.ts`)
|
||||
|
||||
Основной research agent который:
|
||||
- Создаёт Pi Agent с research tools
|
||||
- Выполняет автономное исследование
|
||||
- Генерирует отчёт
|
||||
- Сохраняет результаты на диск (опционально)
|
||||
|
||||
## Flow
|
||||
|
||||
### Research Tool Flow
|
||||
|
||||
```
|
||||
1. Client вызывает research(topic, maxTurns, format)
|
||||
↓
|
||||
2. MCP server создаёт ResearchAgent
|
||||
↓
|
||||
3. ResearchAgent.prompt(topic)
|
||||
↓
|
||||
4. Agent loop:
|
||||
- web_search(query) → Tavily API
|
||||
- read_url(url) → fetch content
|
||||
- save_note(topic, finding, source, confidence)
|
||||
- synthesize_report(topic, format)
|
||||
↓
|
||||
5. Agent генерирует финальный отчёт
|
||||
↓
|
||||
6. MCP server возвращает:
|
||||
{
|
||||
content: [{
|
||||
type: "text",
|
||||
text: "# Research Report\n\n..."
|
||||
}]
|
||||
}
|
||||
```
|
||||
|
||||
### Quick Search Flow
|
||||
|
||||
```
|
||||
1. Client вызывает quick_search(query, maxResults)
|
||||
↓
|
||||
2. MCP server вызывает tavilySearch(query, options)
|
||||
↓
|
||||
3. Форматирует результаты
|
||||
↓
|
||||
4. Возвращает список результатов
|
||||
```
|
||||
|
||||
### Read URL Flow
|
||||
|
||||
```
|
||||
1. Client вызывает read_url(url, maxLength)
|
||||
↓
|
||||
2. MCP server вызывает fetchAndExtract(url, maxLength)
|
||||
↓
|
||||
3. Извлекает текст из HTML
|
||||
↓
|
||||
4. Возвращает контент
|
||||
```
|
||||
|
||||
## Конфигурация
|
||||
|
||||
### Переменные окружения
|
||||
|
||||
```env
|
||||
TAVILY_API_KEY=*** # Required for web search
|
||||
OPENROUTER_API_KEY=*** # Required for LLM
|
||||
LLM_MODEL=minimax/minimax-m3 # Optional, default model
|
||||
```
|
||||
|
||||
### MCP Client Configuration
|
||||
|
||||
#### Claude Desktop
|
||||
|
||||
```json
|
||||
{
|
||||
"mcpServers": {
|
||||
"research-agent": {
|
||||
"command": "node",
|
||||
"args": ["/path/to/pi-research/packages/research-agent/dist/mcp-cli.js"],
|
||||
"env": {
|
||||
"TAVILY_API_KEY": "***",
|
||||
"OPENROUTER_API_KEY": "***"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Cursor
|
||||
|
||||
```json
|
||||
{
|
||||
"mcpServers": {
|
||||
"research-agent": {
|
||||
"command": "node",
|
||||
"args": ["./node_modules/@earendil-works/pi-research-agent/dist/mcp-cli.js"]
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Использование
|
||||
|
||||
### Из Claude Desktop
|
||||
|
||||
```
|
||||
User: Исследуй последние разработки в квантовых вычислениях
|
||||
|
||||
Claude: [вызывает research tool]
|
||||
- Ищет в интернете (5-10 запросов)
|
||||
- Читает источники (5-10 URL)
|
||||
- Сохраняет заметки (10-20 notes)
|
||||
- Генерирует отчёт (2000-5000 слов)
|
||||
- Возвращает результат с цитатами
|
||||
```
|
||||
|
||||
### Из Cursor
|
||||
|
||||
```
|
||||
User: @research-agent Найди информацию о Rust async
|
||||
|
||||
Cursor: [вызывает quick_search tool]
|
||||
- Возвращает 10 результатов поиска
|
||||
- Пользователь может выбрать URL для глубокого чтения через read_url
|
||||
```
|
||||
|
||||
### Программно (TypeScript)
|
||||
|
||||
```typescript
|
||||
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
||||
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
||||
|
||||
const transport = new StdioClientTransport({
|
||||
command: "node",
|
||||
args: ["./dist/mcp-cli.js"],
|
||||
});
|
||||
|
||||
const client = new Client({
|
||||
name: "my-agent",
|
||||
version: "1.0.0",
|
||||
});
|
||||
|
||||
await client.connect(transport);
|
||||
|
||||
const result = await client.callTool({
|
||||
name: "research",
|
||||
arguments: {
|
||||
topic: "Quantum computing 2026",
|
||||
maxTurns: 10,
|
||||
format: "markdown",
|
||||
},
|
||||
});
|
||||
|
||||
console.log(result.content[0].text);
|
||||
```
|
||||
|
||||
## Преимущества MCP Integration
|
||||
|
||||
1. **Переиспользование**: Один research agent для всех клиентов
|
||||
2. **Стандартизация**: MCP protocol обеспечивает совместимость
|
||||
3. **Масштабируемость**: Легко добавить новые tools
|
||||
4. **Изоляция**: Research agent работает в отдельном процессе
|
||||
5. **Безопасность**: API keys изолированы в MCP server
|
||||
|
||||
## Ограничения
|
||||
|
||||
1. **Latency**: Полное исследование занимает 2-5 минут
|
||||
2. **Context size**: Отчёты могут быть очень длинными
|
||||
3. **Cost**: Tavily API и LLM API имеют стоимость
|
||||
4. **Rate limits**: Tavily free tier — 1000 запросов/месяц
|
||||
|
||||
## Будущие улучшения
|
||||
|
||||
1. **Streaming**: Добавить поддержку streaming для длинных исследований
|
||||
2. **Progress**: Добавить progress notifications через MCP
|
||||
3. **Caching**: Кэшировать результаты поиска
|
||||
4. **Batch**: Поддержка batch research (несколько тем одновременно)
|
||||
5. **Custom skills**: Позволить клиентам передавать custom skills
|
||||
|
||||
## Тестирование
|
||||
|
||||
### Ручное тестирование
|
||||
|
||||
```bash
|
||||
# Initialize
|
||||
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0.0"}}}' | node dist/mcp-cli.js
|
||||
|
||||
# List tools
|
||||
(echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0.0"}}}'; echo '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}') | node dist/mcp-cli.js
|
||||
|
||||
# Call tool
|
||||
(echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0.0"}}}'; echo '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"quick_search","arguments":{"query":"test","maxResults":5}}}') | node dist/mcp-cli.js
|
||||
```
|
||||
|
||||
### Автоматическое тестирование
|
||||
|
||||
См. `examples/mcp-client.ts` для примера programmatic client.
|
||||
|
||||
## Ссылки
|
||||
|
||||
- [MCP Specification](https://modelcontextprotocol.io/)
|
||||
- [MCP TypeScript SDK](https://github.com/modelcontextprotocol/typescript-sdk)
|
||||
- [Pi Research Agent](./README.md)
|
||||
- [MCP Server Documentation](./MCP_SERVER.md)
|
||||
@@ -0,0 +1,217 @@
|
||||
# Pi Research Agent MCP Server
|
||||
|
||||
MCP (Model Context Protocol) server для автономного исследования. Позволяет другим агентам вызывать research agent для глубокого исследования тем.
|
||||
|
||||
## Установка
|
||||
|
||||
```bash
|
||||
npm install @earendil-works/pi-research-agent
|
||||
```
|
||||
|
||||
## Настройка
|
||||
|
||||
### Переменные окружения
|
||||
|
||||
Создайте `.env` файл в корне проекта:
|
||||
|
||||
```env
|
||||
TAVILY_API_KEY=***
|
||||
OPENROUTER_API_KEY=***
|
||||
LLM_MODEL=minimax/minimax-m3
|
||||
```
|
||||
|
||||
### Подключение к MCP клиенту
|
||||
|
||||
#### Claude Desktop
|
||||
|
||||
Добавьте в `~/Library/Application Support/Claude/claude_desktop_config.json` (macOS) или `%APPDATA%\Claude\claude_desktop_config.json` (Windows):
|
||||
|
||||
```json
|
||||
{
|
||||
"mcpServers": {
|
||||
"research-agent": {
|
||||
"command": "node",
|
||||
"args": ["/path/to/pi-research/packages/research-agent/dist/mcp-cli.js"],
|
||||
"env": {
|
||||
"TAVILY_API_KEY": "***",
|
||||
"OPENROUTER_API_KEY": "***"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Cursor
|
||||
|
||||
Добавьте в `.cursor/mcp.json` в корне проекта:
|
||||
|
||||
```json
|
||||
{
|
||||
"mcpServers": {
|
||||
"research-agent": {
|
||||
"command": "node",
|
||||
"args": ["./node_modules/@earendil-works/pi-research-agent/dist/mcp-cli.js"]
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Другие MCP клиенты
|
||||
|
||||
Любой MCP-совместимый клиент может подключиться к серверу через stdio:
|
||||
|
||||
```bash
|
||||
node /path/to/pi-research/packages/research-agent/dist/mcp-cli.js
|
||||
```
|
||||
|
||||
## Доступные инструменты
|
||||
|
||||
### `research`
|
||||
|
||||
Выполняет глубокое автономное исследование темы. Агент ищет в интернете, читает источники, сохраняет находки и генерирует подробный отчёт с цитатами.
|
||||
|
||||
**Параметры:**
|
||||
- `topic` (string, required): Тема для исследования
|
||||
- `maxTurns` (number, optional, default: 15): Максимальное количество ходов исследования
|
||||
- `format` (string, optional, default: "markdown"): Формат отчёта ("markdown", "json", "bullet")
|
||||
|
||||
**Пример использования:**
|
||||
```json
|
||||
{
|
||||
"topic": "Квантовые вычисления 2026",
|
||||
"maxTurns": 10,
|
||||
"format": "markdown"
|
||||
}
|
||||
```
|
||||
|
||||
**Возвращает:**
|
||||
- Полный отчёт с executive summary, ключевыми находками, цитатами источников
|
||||
- Список собранных заметок с уровнем уверенности (high/medium/low)
|
||||
- Метаданные: количество ходов, количество заметок
|
||||
|
||||
### `quick_search`
|
||||
|
||||
Быстрый поиск в интернете без полного research flow. Полезен для быстрого поиска информации.
|
||||
|
||||
**Параметры:**
|
||||
- `query` (string, required): Поисковый запрос
|
||||
- `maxResults` (number, optional, default: 10): Максимальное количество результатов
|
||||
|
||||
**Пример использования:**
|
||||
```json
|
||||
{
|
||||
"query": "последние разработки в квантовых вычислениях",
|
||||
"maxResults": 5
|
||||
}
|
||||
```
|
||||
|
||||
**Возвращает:**
|
||||
- Список результатов поиска с заголовками, URL и сниппетами
|
||||
|
||||
### `read_url`
|
||||
|
||||
Извлекает контент из URL. Полезен для чтения конкретных статей или страниц.
|
||||
|
||||
**Параметры:**
|
||||
- `url` (string, required): URL для чтения
|
||||
- `maxLength` (number, optional, default: 10000): Максимальное количество символов для извлечения
|
||||
|
||||
**Пример использования:**
|
||||
```json
|
||||
{
|
||||
"url": "https://example.com/article",
|
||||
"maxLength": 5000
|
||||
}
|
||||
```
|
||||
|
||||
**Возвращает:**
|
||||
- Извлечённый текст со страницы
|
||||
|
||||
## Программное использование
|
||||
|
||||
```typescript
|
||||
import { createResearchMcpServer } from "@earendil-works/pi-research-agent";
|
||||
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
||||
|
||||
const server = createResearchMcpServer();
|
||||
const transport = new StdioServerTransport();
|
||||
await server.connect(transport);
|
||||
```
|
||||
|
||||
## Как это работает
|
||||
|
||||
1. **Инициализация**: MCP клиент подключается к серверу через stdio
|
||||
2. **Вызов инструмента**: Клиент вызывает `research`, `quick_search` или `read_url`
|
||||
3. **Выполнение**:
|
||||
- `research`: Создаёт ResearchAgent, выполняет полное исследование (поиск → чтение → сохранение → синтез)
|
||||
- `quick_search`: Выполняет быстрый поиск через Tavily API
|
||||
- `read_url`: Извлекает контент из URL
|
||||
4. **Результат**: Возвращает структурированный результат через MCP protocol
|
||||
|
||||
## Примеры использования
|
||||
|
||||
### Исследование с Claude
|
||||
|
||||
```
|
||||
User: Исследуй последние разработки в области квантовых вычислений
|
||||
|
||||
Claude: [вызывает research tool]
|
||||
- Ищет в интернете
|
||||
- Читает 5-10 источников
|
||||
- Сохраняет ключевые находки
|
||||
- Генерирует отчёт на 2000+ слов
|
||||
- Возвращает результат с цитатами
|
||||
```
|
||||
|
||||
### Быстрый поиск с Cursor
|
||||
|
||||
```
|
||||
User: @research-agent Найди информацию о Rust async runtime
|
||||
|
||||
Cursor: [вызывает quick_search tool]
|
||||
- Возвращает 10 результатов поиска
|
||||
- Пользователь может выбрать URL для глубокого чтения
|
||||
```
|
||||
|
||||
### Чтение конкретной статьи
|
||||
|
||||
```
|
||||
User: Прочитай эту статью: https://example.com/article
|
||||
|
||||
Agent: [вызывает read_url tool]
|
||||
- Извлекает текст статьи
|
||||
- Возвращает контент для анализа
|
||||
```
|
||||
|
||||
## Ограничения
|
||||
|
||||
- **Tavily API**: Бесплатный план — 1000 запросов/месяц
|
||||
- **Время выполнения**: Полное исследование может занять 2-5 минут
|
||||
- **Контекст**: Отчёты могут быть очень длинными (5000-10000 слов)
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### "TAVILY_API_KEY environment variable is not set"
|
||||
|
||||
Убедитесь что переменные окружения установлены:
|
||||
```bash
|
||||
export TAVILY_API_KEY=***
|
||||
export OPENROUTER_API_KEY=***
|
||||
```
|
||||
|
||||
Или добавьте их в `.env` файл.
|
||||
|
||||
### MCP server не отвечает
|
||||
|
||||
Проверьте что:
|
||||
1. Node.js версия >= 22.19.0
|
||||
2. Все зависимости установлены: `npm install`
|
||||
3. Пакет собран: `npm run build`
|
||||
|
||||
### Ошибки типов TypeScript
|
||||
|
||||
MCP SDK использует Zod для валидации схем. Если возникают ошибки типов, используйте `as any` для схем (как в текущей реализации).
|
||||
|
||||
## Лицензия
|
||||
|
||||
MIT
|
||||
@@ -0,0 +1,255 @@
|
||||
# Pi Research Agent
|
||||
|
||||
[](https://www.npmjs.com/package/@earendil-works/pi-research-agent)
|
||||
[](https://opensource.org/licenses/MIT)
|
||||
[](https://nodejs.org)
|
||||
[](https://www.typescriptlang.org/)
|
||||
[](https://modelcontextprotocol.io)
|
||||
|
||||
Autonomous research agent that searches the web, analyzes sources, and synthesizes findings into structured reports. Built on [Pi](https://github.com/earendil-works/pi) agent core. Available as a CLI, programmatic API, and MCP server.
|
||||
|
||||
## Features
|
||||
|
||||
- **Autonomous Research**: Breaks down topics into sub-questions and searches systematically
|
||||
- **Web Search**: Uses Tavily API for comprehensive web search
|
||||
- **Source Analysis**: Fetches and extracts content from web pages with retry logic
|
||||
- **Note Taking**: Saves findings with confidence levels (high/medium/low) and source attribution
|
||||
- **Report Synthesis**: Generates structured reports in markdown, JSON, or bullet format
|
||||
- **Skills System**: Extensible via markdown skills in `~/.pi/skills/research/`
|
||||
- **Multiple Output Modes**: Interactive TUI or simple print mode
|
||||
- **Automatic Reports**: Saves to `./research-output/<timestamp>-<topic>/`
|
||||
- **MCP Server**: Expose research capabilities as MCP tools for other agents
|
||||
- **CLI + Programmatic API**: Use from terminal or embed in your own code
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install @earendil-works/pi-research-agent
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
### 1. Get API Keys
|
||||
|
||||
- **Tavily** (web search): https://tavily.com/ — free tier: 1,000 searches/month
|
||||
- **OpenRouter** (LLM): https://openrouter.ai/ — pay-per-use pricing
|
||||
|
||||
### 2. Configure
|
||||
|
||||
Create a `.env` file in your project root:
|
||||
|
||||
```env
|
||||
TAVILY_API_KEY=your-tavily-key
|
||||
OPENROUTER_API_KEY=your-openrouter-key
|
||||
LLM_MODEL=minimax/minimax-m3
|
||||
```
|
||||
|
||||
### 3. Use
|
||||
|
||||
**CLI:**
|
||||
|
||||
```bash
|
||||
# Basic research
|
||||
pi-research "quantum computing breakthroughs 2026"
|
||||
|
||||
# Print mode (no TUI)
|
||||
pi-research --print "AI safety research"
|
||||
|
||||
# Custom max turns
|
||||
pi-research --max-turns 15 "climate change solutions"
|
||||
|
||||
# Custom model
|
||||
pi-research -m openai/gpt-4o "renewable energy trends"
|
||||
```
|
||||
|
||||
**Programmatic:**
|
||||
|
||||
```typescript
|
||||
import { ResearchAgent } from "@earendil-works/pi-research-agent";
|
||||
|
||||
const agent = new ResearchAgent({
|
||||
maxTurns: 10,
|
||||
saveResults: true,
|
||||
outputDir: "./research-output",
|
||||
});
|
||||
|
||||
const result = await agent.research("machine learning in healthcare 2026");
|
||||
|
||||
console.log(`Turns: ${result.turnsUsed}, Notes: ${result.notes.length}`);
|
||||
console.log(result.report);
|
||||
```
|
||||
|
||||
**As MCP Server:**
|
||||
|
||||
```bash
|
||||
# Run as MCP server
|
||||
pi-research-mcp
|
||||
```
|
||||
|
||||
Then configure in your MCP client (Claude Desktop, OpenCode, Cursor, etc.):
|
||||
|
||||
```json
|
||||
{
|
||||
"mcp": {
|
||||
"research-agent": {
|
||||
"type": "local",
|
||||
"command": ["node", "node_modules/@earendil-works/pi-research-agent/dist/mcp-cli.js"],
|
||||
"enabled": true
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Available Tools
|
||||
|
||||
### CLI / Programmatic
|
||||
|
||||
| Tool | Description |
|
||||
|------|-------------|
|
||||
| `web_search` | Search the web using Tavily API |
|
||||
| `read_url` | Fetch and extract content from a URL |
|
||||
| `save_note` | Save a research finding with confidence level |
|
||||
| `synthesize_report` | Generate final report from collected notes |
|
||||
|
||||
### MCP Server
|
||||
|
||||
| Tool | Description |
|
||||
|------|-------------|
|
||||
| `research` | Full autonomous research with report generation |
|
||||
| `quick_search` | Fast web search without full research flow |
|
||||
| `read_url` | Extract content from a URL |
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
ResearchAgent
|
||||
└─> Agent (Pi core)
|
||||
├─> web_search (Tavily API)
|
||||
├─> read_url (URL fetcher + content extraction)
|
||||
├─> save_note (in-memory store)
|
||||
└─> synthesize_report (terminates agent)
|
||||
```
|
||||
|
||||
The agent autonomously decides when to search, read, and save findings. After collecting sufficient information, it calls `synthesize_report` to generate a comprehensive markdown report with executive summary, key findings, source citations, and confidence assessment.
|
||||
|
||||
## Configuration
|
||||
|
||||
### Environment Variables
|
||||
|
||||
| Variable | Required | Default | Description |
|
||||
|----------|----------|---------|-------------|
|
||||
| `TAVILY_API_KEY` | Yes | — | Tavily API key for web search |
|
||||
| `OPENROUTER_API_KEY` | Yes | — | OpenRouter API key for LLM |
|
||||
| `LLM_MODEL` | No | `minimax/minimax-m3` | LLM model to use |
|
||||
|
||||
### CLI Options
|
||||
|
||||
```
|
||||
pi-research [options] <topic>
|
||||
|
||||
Options:
|
||||
-h, --help Show help
|
||||
-v, --version Show version
|
||||
-p, --print Print mode (no TUI)
|
||||
-t, --max-turns <n> Max research turns (default: 15)
|
||||
-m, --model <model> Model (default: minimax/minimax-m3)
|
||||
-o, --output-dir <p> Output directory (default: ./research-output)
|
||||
--no-save Don't save to disk
|
||||
```
|
||||
|
||||
### Programmatic Options
|
||||
|
||||
```typescript
|
||||
interface ResearchAgentOptions {
|
||||
model?: Model; // LLM model
|
||||
thinkingLevel?: ThinkingLevel; // "off" | "minimal" | "low" | "medium" | "high" | "xhigh"
|
||||
maxTurns?: number; // Max research turns (default: 15)
|
||||
tavilyApiKey?: string; // Tavily API key
|
||||
llmApiKey?: string; // LLM provider API key
|
||||
skills?: ResearchSkill[]; // Custom research skills
|
||||
outputDir?: string; // Output directory (default: "./research-output")
|
||||
saveResults?: boolean; // Save to disk (default: true)
|
||||
}
|
||||
```
|
||||
|
||||
## Output Structure
|
||||
|
||||
Results are automatically saved to `./research-output/<timestamp>-<topic>/`:
|
||||
|
||||
```
|
||||
research-output/
|
||||
2026-06-04T14-30-45-quantum-computing-2026/
|
||||
report.md # Final research report (markdown)
|
||||
report.json # Full results (report + notes + metadata)
|
||||
notes.json # Collected research notes
|
||||
```
|
||||
|
||||
## Research Skills
|
||||
|
||||
Create custom skills in `~/.pi/skills/research/`:
|
||||
|
||||
```markdown
|
||||
---
|
||||
name: deep-dive
|
||||
description: Perform deep-dive research on technical topics
|
||||
max_turns: 15
|
||||
---
|
||||
|
||||
When researching technical topics:
|
||||
1. Start with official documentation
|
||||
2. Look for academic papers and whitepapers
|
||||
3. Check recent blog posts from industry experts
|
||||
4. Verify claims with multiple sources
|
||||
```
|
||||
|
||||
Skills are automatically loaded and appended to the system prompt.
|
||||
|
||||
## Examples
|
||||
|
||||
See [`examples/`](./examples) for working code:
|
||||
|
||||
- `basic.ts` — Basic programmatic usage
|
||||
- `custom-model.ts` — Using a different LLM model
|
||||
- `custom-skills.ts` — Custom research skills
|
||||
- `with-env.ts` — Environment variable configuration
|
||||
- `mcp-client.ts` — MCP client example
|
||||
- `mcp-config-claude.json` — Claude Desktop MCP config
|
||||
- `mcp-config-cursor.json` — Cursor MCP config
|
||||
|
||||
## Documentation
|
||||
|
||||
- [MCP Server Guide](./MCP_SERVER.md) — Detailed MCP setup instructions
|
||||
- [MCP Architecture](./MCP_ARCHITECTURE.md) — Architecture and flow diagrams
|
||||
- [Environment Setup](./ENV_SETUP.md) — Detailed env var configuration
|
||||
- [Changelog](./CHANGELOG.md) — Version history
|
||||
|
||||
## Development
|
||||
|
||||
```bash
|
||||
# Install dependencies
|
||||
npm install
|
||||
|
||||
# Build
|
||||
npm run build
|
||||
|
||||
# Test
|
||||
npm test
|
||||
|
||||
# Watch mode
|
||||
npm run dev
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
Contributions are welcome! Please see [CONTRIBUTING.md](../../CONTRIBUTING.md) for details.
|
||||
|
||||
## License
|
||||
|
||||
MIT © [Pi Research Team](https://github.com/earendil-works/pi)
|
||||
|
||||
## Related
|
||||
|
||||
- [Pi](https://github.com/earendil-works/pi) — The AI agent framework this is built on
|
||||
- [Model Context Protocol](https://modelcontextprotocol.io/) — Standard for AI tool integration
|
||||
- [Tavily](https://tavily.com/) — Web search API for AI applications
|
||||
- [OpenRouter](https://openrouter.ai/) — Unified API for LLMs
|
||||
@@ -0,0 +1,99 @@
|
||||
# Examples
|
||||
|
||||
This directory contains working examples of how to use Pi Research Agent.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Before running any example, make sure you have:
|
||||
|
||||
1. Built the project: `npm run build` (from the package root)
|
||||
2. Set up environment variables in a `.env` file or exported in your shell:
|
||||
```bash
|
||||
TAVILY_API_KEY=your-tavily-key
|
||||
OPENROUTER_API_KEY=your-openrouter-key
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
### [basic.ts](./basic.ts)
|
||||
|
||||
The simplest example — basic programmatic research.
|
||||
|
||||
```bash
|
||||
npx tsx examples/basic.ts
|
||||
```
|
||||
|
||||
### [custom-model.ts](./custom-model.ts)
|
||||
|
||||
Using a custom LLM model (GPT-4o instead of the default).
|
||||
|
||||
```bash
|
||||
npx tsx examples/custom-model.ts
|
||||
```
|
||||
|
||||
### [custom-skills.ts](./custom-skills.ts)
|
||||
|
||||
Adding custom research skills to specialize the agent's behavior.
|
||||
|
||||
```bash
|
||||
npx tsx examples/custom-skills.ts
|
||||
```
|
||||
|
||||
### [with-env.ts](./with-env.ts)
|
||||
|
||||
Loading API keys from a `.env` file automatically.
|
||||
|
||||
```bash
|
||||
npx tsx examples/with-env.ts
|
||||
```
|
||||
|
||||
### [mcp-client.ts](./mcp-client.ts)
|
||||
|
||||
Connecting to the MCP server programmatically from another agent.
|
||||
|
||||
```bash
|
||||
npx tsx examples/mcp-client.ts
|
||||
```
|
||||
|
||||
## MCP Configuration Files
|
||||
|
||||
### [mcp-config-claude.json](./mcp-config-claude.json)
|
||||
|
||||
Configuration for Claude Desktop. Copy to:
|
||||
- **macOS**: `~/Library/Application Support/Claude/claude_desktop_config.json`
|
||||
- **Windows**: `%APPDATA%\Claude\claude_desktop_config.json`
|
||||
|
||||
### [mcp-config-cursor.json](./mcp-config-cursor.json)
|
||||
|
||||
Configuration for Cursor. Copy to `.cursor/mcp.json` in your project root.
|
||||
|
||||
## Running Examples
|
||||
|
||||
All examples use `tsx` to run TypeScript files directly. You can run them with:
|
||||
|
||||
```bash
|
||||
# From the package root
|
||||
npx tsx examples/<example-name>.ts
|
||||
|
||||
# Or with a specific environment variable
|
||||
TAVILY_API_KEY=*** OPENROUTER_API_KEY=*** npx tsx examples/basic.ts
|
||||
```
|
||||
|
||||
## Output
|
||||
|
||||
Examples will:
|
||||
1. Connect to Tavily and OpenRouter APIs
|
||||
2. Run the research workflow
|
||||
3. Print progress to the console
|
||||
4. Save results to `./research-output/`
|
||||
5. Display the final report
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
If you get an error about missing API keys:
|
||||
- Make sure your `.env` file exists in the package root
|
||||
- Or export the variables in your shell before running
|
||||
|
||||
If you get a build error:
|
||||
- Run `npm run build` first
|
||||
- Make sure dependencies are installed: `npm install`
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Basic Research Agent Example
|
||||
*
|
||||
* Demonstrates how to use the ResearchAgent programmatically.
|
||||
*
|
||||
* Run with:
|
||||
* TAVILY_API_KEY=*** \
|
||||
* OPENROUTER_API_KEY=*** \
|
||||
* npx tsx examples/basic.ts
|
||||
*/
|
||||
|
||||
import { ResearchAgent } from "../src/index.ts";
|
||||
|
||||
const agent = new ResearchAgent({
|
||||
maxTurns: 10,
|
||||
});
|
||||
|
||||
agent.subscribe((event) => {
|
||||
switch (event.type) {
|
||||
case "tool_execution_start": {
|
||||
const toolName = event.toolName;
|
||||
const args = event.args as Record<string, unknown>;
|
||||
if (toolName === "web_search") {
|
||||
console.error(`[Searching: ${args.query}]`);
|
||||
} else if (toolName === "read_url") {
|
||||
console.error(`[Reading: ${args.url}]`);
|
||||
} else if (toolName === "save_note") {
|
||||
console.error(`[Saving note: ${args.topic}]`);
|
||||
} else if (toolName === "synthesize_report") {
|
||||
console.error("[Synthesizing report...]");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "message_update": {
|
||||
if (event.assistantMessageEvent.type === "text_delta") {
|
||||
process.stdout.write(event.assistantMessageEvent.delta);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const topic = "Latest developments in quantum error correction 2026";
|
||||
console.log(`Researching: ${topic}\n`);
|
||||
|
||||
const result = await agent.research(topic);
|
||||
|
||||
console.log("\n\n" + "=".repeat(60));
|
||||
console.log(`Research complete!`);
|
||||
console.log(`Turns used: ${result.turnsUsed}`);
|
||||
console.log(`Notes collected: ${result.notes.length}`);
|
||||
console.log("=".repeat(60) + "\n");
|
||||
|
||||
console.log(result.report);
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Custom Model Example
|
||||
*
|
||||
* Demonstrates how to use a custom model with the ResearchAgent.
|
||||
*
|
||||
* Run with:
|
||||
* TAVILY_API_KEY=*** \
|
||||
* OPENROUTER_API_KEY=*** \
|
||||
* npx tsx examples/custom-model.ts
|
||||
*/
|
||||
|
||||
import { getModels } from "@earendil-works/pi-ai";
|
||||
import { ResearchAgent } from "../src/index.ts";
|
||||
|
||||
const models = getModels("openrouter");
|
||||
const gpt4o = models.find((m) => m.id === "openai/gpt-4o");
|
||||
|
||||
if (!gpt4o) {
|
||||
console.error("GPT-4o model not found");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const agent = new ResearchAgent({
|
||||
model: gpt4o,
|
||||
thinkingLevel: "high",
|
||||
maxTurns: 15,
|
||||
});
|
||||
|
||||
agent.subscribe((event) => {
|
||||
if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") {
|
||||
process.stdout.write(event.assistantMessageEvent.delta);
|
||||
}
|
||||
});
|
||||
|
||||
const topic = "Comparison of renewable energy storage solutions";
|
||||
console.log(`Researching: ${topic}\n`);
|
||||
|
||||
const result = await agent.research(topic);
|
||||
|
||||
console.log("\n\n" + result.report);
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* Custom Skills Example
|
||||
*
|
||||
* Demonstrates how to use custom research skills with the ResearchAgent.
|
||||
*
|
||||
* Run with:
|
||||
* TAVILY_API_KEY=*** \
|
||||
* OPENROUTER_API_KEY=*** \
|
||||
* npx tsx examples/custom-skills.ts
|
||||
*/
|
||||
|
||||
import type { ResearchSkill } from "../src/index.ts";
|
||||
import { ResearchAgent } from "../src/index.ts";
|
||||
|
||||
const academicResearchSkill: ResearchSkill = {
|
||||
name: "academic-research",
|
||||
description: "Focus on academic sources and peer-reviewed papers",
|
||||
content: `When conducting research:
|
||||
1. Prioritize academic sources (Google Scholar, arXiv, PubMed)
|
||||
2. Look for peer-reviewed papers and citations
|
||||
3. Check publication dates and author credentials
|
||||
4. Note methodology and sample sizes
|
||||
5. Distinguish between correlation and causation`,
|
||||
maxTurns: 15,
|
||||
};
|
||||
|
||||
const factCheckingSkill: ResearchSkill = {
|
||||
name: "fact-checking",
|
||||
description: "Verify claims with multiple sources",
|
||||
content: `For each major claim:
|
||||
1. Find at least 3 independent sources
|
||||
2. Check for conflicting information
|
||||
3. Note the confidence level appropriately
|
||||
4. Flag any claims that cannot be verified`,
|
||||
};
|
||||
|
||||
const agent = new ResearchAgent({
|
||||
maxTurns: 12,
|
||||
skills: [academicResearchSkill, factCheckingSkill],
|
||||
});
|
||||
|
||||
agent.subscribe((event) => {
|
||||
if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") {
|
||||
process.stdout.write(event.assistantMessageEvent.delta);
|
||||
}
|
||||
});
|
||||
|
||||
const topic = "Effectiveness of cognitive behavioral therapy for anxiety";
|
||||
console.log(`Researching: ${topic}\n`);
|
||||
|
||||
const result = await agent.research(topic);
|
||||
|
||||
console.log("\n\n" + result.report);
|
||||
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* MCP Client Example
|
||||
*
|
||||
* Demonstrates how to call the research agent MCP server from another agent.
|
||||
*
|
||||
* Run with:
|
||||
* TAVILY_API_KEY=*** \
|
||||
* OPENROUTER_API_KEY=*** \
|
||||
* npx tsx examples/mcp-client.ts
|
||||
*/
|
||||
|
||||
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
||||
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
||||
import { join } from "node:path";
|
||||
|
||||
const mcpServerPath = join(import.meta.dirname, "../dist/mcp-cli.js");
|
||||
|
||||
const transport = new StdioClientTransport({
|
||||
command: "node",
|
||||
args: [mcpServerPath],
|
||||
env: {
|
||||
...process.env,
|
||||
},
|
||||
});
|
||||
|
||||
const client = new Client({
|
||||
name: "example-client",
|
||||
version: "1.0.0",
|
||||
});
|
||||
|
||||
await client.connect(transport);
|
||||
|
||||
console.log("Connected to research agent MCP server\n");
|
||||
|
||||
const toolsResult = await client.listTools();
|
||||
console.log("Available tools:");
|
||||
for (const tool of toolsResult.tools) {
|
||||
console.log(` - ${tool.name}: ${tool.description}`);
|
||||
}
|
||||
console.log();
|
||||
|
||||
console.log("Starting research on 'Rust async runtime'...\n");
|
||||
|
||||
const result = await client.callTool({
|
||||
name: "quick_search",
|
||||
arguments: {
|
||||
query: "Rust async runtime tokio vs async-std 2026",
|
||||
maxResults: 5,
|
||||
},
|
||||
});
|
||||
|
||||
console.log("Search results:");
|
||||
for (const content of result.content) {
|
||||
if (content.type === "text") {
|
||||
console.log(content.text);
|
||||
}
|
||||
}
|
||||
|
||||
await client.close();
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"mcpServers": {
|
||||
"research-agent": {
|
||||
"command": "node",
|
||||
"args": ["./packages/research-agent/dist/mcp-cli.js"],
|
||||
"env": {
|
||||
"TAVILY_API_KEY": "***",
|
||||
"OPENROUTER_API_KEY": "***",
|
||||
"LLM_MODEL": "minimax/minimax-m3"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"mcpServers": {
|
||||
"research-agent": {
|
||||
"command": "node",
|
||||
"args": ["./node_modules/@earendil-works/pi-research-agent/dist/mcp-cli.js"]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Environment Variables Example
|
||||
*
|
||||
* This example demonstrates how to use .env files with the research agent.
|
||||
*
|
||||
* Setup:
|
||||
* 1. Copy .env.example to .env:
|
||||
* cp packages/research-agent/.env.example packages/research-agent/.env
|
||||
*
|
||||
* 2. Edit .env with your API keys:
|
||||
* TAVILY_API_KEY=***
|
||||
* OPENROUTER_API_KEY=***
|
||||
*
|
||||
* 3. Run the example:
|
||||
* npx tsx examples/with-env.ts
|
||||
*
|
||||
* The agent will automatically load variables from .env file.
|
||||
* Environment variables take precedence over .env file values.
|
||||
*/
|
||||
|
||||
import { ResearchAgent } from "../src/index.ts";
|
||||
|
||||
// No need to manually read process.env - the CLI does this automatically
|
||||
// But when using the programmatic API, you can still pass keys explicitly
|
||||
const agent = new ResearchAgent({
|
||||
maxTurns: 5,
|
||||
// Keys will be read from process.env (loaded from .env by CLI)
|
||||
// Or you can pass them explicitly:
|
||||
// tavilyApiKey: process.env.TAVILY_API_KEY,
|
||||
// llmApiKey: process.env.OPENROUTER_API_KEY,
|
||||
});
|
||||
|
||||
agent.subscribe((event) => {
|
||||
if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") {
|
||||
process.stdout.write(event.assistantMessageEvent.delta);
|
||||
}
|
||||
});
|
||||
|
||||
console.log("Starting research with environment variables from .env file...\n");
|
||||
|
||||
const result = await agent.research("Benefits of morning exercise");
|
||||
|
||||
console.log("\n\n" + "=".repeat(60));
|
||||
console.log(`Research complete!`);
|
||||
console.log(`Turns used: ${result.turnsUsed}`);
|
||||
console.log(`Notes collected: ${result.notes.length}`);
|
||||
console.log("=".repeat(60));
|
||||
@@ -0,0 +1,77 @@
|
||||
{
|
||||
"name": "@earendil-works/pi-research-agent",
|
||||
"version": "0.78.0",
|
||||
"description": "Autonomous research agent with web search, source analysis, and report synthesis. MCP-compatible.",
|
||||
"type": "module",
|
||||
"bin": {
|
||||
"pi-research": "dist/cli.js",
|
||||
"pi-research-mcp": "dist/mcp-cli.js"
|
||||
},
|
||||
"main": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/index.js"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"README.md",
|
||||
"LICENSE",
|
||||
"CHANGELOG.md"
|
||||
],
|
||||
"scripts": {
|
||||
"clean": "shx rm -rf dist",
|
||||
"build": "tsgo -p tsconfig.build.json && shx chmod +x dist/cli.js dist/mcp-cli.js",
|
||||
"test": "vitest --run",
|
||||
"prepublishOnly": "npm run clean && npm run build"
|
||||
},
|
||||
"dependencies": {
|
||||
"@earendil-works/pi-agent-core": "^0.78.0",
|
||||
"@earendil-works/pi-ai": "^0.78.0",
|
||||
"@earendil-works/pi-tui": "^0.78.0",
|
||||
"@modelcontextprotocol/sdk": "1.29.0",
|
||||
"chalk": "5.6.2",
|
||||
"typebox": "1.1.38",
|
||||
"undici": "8.3.0",
|
||||
"yaml": "2.9.0",
|
||||
"zod": "3.25.67"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "24.12.4",
|
||||
"shx": "0.4.0",
|
||||
"typescript": "5.9.3",
|
||||
"vitest": "3.2.4"
|
||||
},
|
||||
"keywords": [
|
||||
"research-agent",
|
||||
"ai",
|
||||
"llm",
|
||||
"web-search",
|
||||
"autonomous",
|
||||
"agent",
|
||||
"mcp",
|
||||
"model-context-protocol",
|
||||
"deep-research",
|
||||
"report-generation",
|
||||
"tavily"
|
||||
],
|
||||
"author": {
|
||||
"name": "Pi Research Team",
|
||||
"url": "https://github.com/earendil-works/pi"
|
||||
},
|
||||
"license": "MIT",
|
||||
"homepage": "https://github.com/earendil-works/pi/tree/main/packages/research-agent#readme",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/earendil-works/pi.git",
|
||||
"directory": "packages/research-agent"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/earendil-works/pi/issues"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=22.19.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,404 @@
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { Agent, type AgentEvent, type AgentMessage } from "@earendil-works/pi-agent-core";
|
||||
import { type Api, getModels, type Message, type Model, streamSimple } from "@earendil-works/pi-ai";
|
||||
import {
|
||||
DEFAULT_MAX_TURNS,
|
||||
DEFAULT_MODEL_ID,
|
||||
DEFAULT_OUTPUT_DIR,
|
||||
DEFAULT_PROVIDER,
|
||||
ENV_LLM_MODEL,
|
||||
ENV_OPENROUTER_API_KEY,
|
||||
ENV_TAVILY_API_KEY,
|
||||
MIN_REPORT_LENGTH,
|
||||
} from "./config.ts";
|
||||
import { buildSystemPrompt } from "./system-prompt.ts";
|
||||
import { createNoteStore, createResearchTools } from "./tools/index.ts";
|
||||
import type { NoteStore, ResearchAgentOptions, ResearchResult } from "./types.ts";
|
||||
|
||||
export class ResearchAgent {
|
||||
private agent: Agent;
|
||||
private notes: NoteStore;
|
||||
private turnCount = 0;
|
||||
private maxTurns: number;
|
||||
private report = "";
|
||||
private forcedSynthesis = false;
|
||||
private synthesisRequested = false;
|
||||
private reportGenerationStopped = false;
|
||||
private turnsAfterSynthesis = 0;
|
||||
private topic = "";
|
||||
private outputDir: string;
|
||||
private saveResults: boolean;
|
||||
|
||||
constructor(options: ResearchAgentOptions = {}) {
|
||||
this.notes = createNoteStore();
|
||||
this.maxTurns = options.maxTurns ?? DEFAULT_MAX_TURNS;
|
||||
this.outputDir = options.outputDir ?? DEFAULT_OUTPUT_DIR;
|
||||
this.saveResults = options.saveResults ?? true;
|
||||
|
||||
const tavilyApiKey = options.tavilyApiKey ?? process.env[ENV_TAVILY_API_KEY];
|
||||
if (!tavilyApiKey) {
|
||||
throw new Error(
|
||||
`Tavily API key is required. Set ${ENV_TAVILY_API_KEY} environment variable or pass tavilyApiKey option.`,
|
||||
);
|
||||
}
|
||||
|
||||
const llmApiKey = options.llmApiKey ?? process.env[ENV_OPENROUTER_API_KEY];
|
||||
if (!llmApiKey) {
|
||||
throw new Error(
|
||||
`LLM API key is required. Set ${ENV_OPENROUTER_API_KEY} environment variable or pass llmApiKey option.`,
|
||||
);
|
||||
}
|
||||
|
||||
const model = options.model ?? this.resolveModel();
|
||||
const tools = createResearchTools(this.notes, tavilyApiKey);
|
||||
const systemPrompt = buildSystemPrompt(options.skills);
|
||||
|
||||
this.agent = new Agent({
|
||||
initialState: {
|
||||
systemPrompt,
|
||||
model,
|
||||
thinkingLevel: options.thinkingLevel ?? "medium",
|
||||
tools,
|
||||
},
|
||||
streamFn: async (streamModel, context, opts) => {
|
||||
return streamSimple(streamModel, context, {
|
||||
...opts,
|
||||
apiKey: llmApiKey,
|
||||
});
|
||||
},
|
||||
convertToLlm: (messages: AgentMessage[]): Message[] => {
|
||||
return messages.filter(
|
||||
(m) => m.role === "user" || m.role === "assistant" || m.role === "toolResult",
|
||||
) as Message[];
|
||||
},
|
||||
});
|
||||
|
||||
this.agent.subscribe((event: AgentEvent) => {
|
||||
// Log assistant message generation for debugging
|
||||
if (event.type === "message_end" && event.message.role === "assistant") {
|
||||
const textContent = event.message.content.find((c) => c.type === "text");
|
||||
if (textContent && textContent.type === "text" && textContent.text.length > 0) {
|
||||
console.error(`[Assistant message generated, length: ${textContent.text.length}]`);
|
||||
}
|
||||
}
|
||||
|
||||
// Track when synthesize_report tool completes successfully
|
||||
if (event.type === "tool_execution_end" && event.toolName === "synthesize_report") {
|
||||
if (event.result?.details && event.result.details.synthesisRequested === true) {
|
||||
this.synthesisRequested = true;
|
||||
this.turnsAfterSynthesis = 0;
|
||||
console.error(`\n[synthesize_report called, waiting for report generation...]`);
|
||||
}
|
||||
}
|
||||
|
||||
if (event.type === "turn_end") {
|
||||
this.turnCount++;
|
||||
|
||||
// After synthesize_report was called, track turns and check for report
|
||||
if (this.synthesisRequested && !this.reportGenerationStopped) {
|
||||
this.turnsAfterSynthesis++;
|
||||
|
||||
const lastMsg = this.getLastAssistantMessage();
|
||||
const reportLength = lastMsg ? this.getTextLength(lastMsg) : 0;
|
||||
const hasReport = reportLength >= MIN_REPORT_LENGTH;
|
||||
|
||||
// Stop after 2 turns OR if we have a substantial report after at least 1 turn
|
||||
if (this.turnsAfterSynthesis >= 2 || (hasReport && this.turnsAfterSynthesis >= 1)) {
|
||||
this.reportGenerationStopped = true;
|
||||
console.error(`\n[Report generation complete. Stopping agent.]`);
|
||||
this.agent.abort();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// When reaching max turns, inject a steering message to force synthesis
|
||||
if (this.turnCount >= this.maxTurns && !this.forcedSynthesis && !this.synthesisRequested) {
|
||||
this.forcedSynthesis = true;
|
||||
console.error(`\n[Max turns (${this.maxTurns}) reached. Forcing report synthesis.]`);
|
||||
this.agent.steer({
|
||||
role: "user",
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: `You have reached the maximum number of research turns (${this.maxTurns}). You MUST now call the synthesize_report tool immediately with all the notes you have collected. Do not search or read more - synthesize what you have now.`,
|
||||
},
|
||||
],
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private resolveModel(): Model<Api> {
|
||||
const modelId = process.env[ENV_LLM_MODEL] ?? DEFAULT_MODEL_ID;
|
||||
const models = getModels(DEFAULT_PROVIDER);
|
||||
const model = models.find((m) => m.id === modelId);
|
||||
if (!model) {
|
||||
throw new Error(`Model not found: ${DEFAULT_PROVIDER}/${modelId}`);
|
||||
}
|
||||
return model;
|
||||
}
|
||||
|
||||
subscribe(listener: (event: AgentEvent, signal: AbortSignal) => Promise<void> | void): () => void {
|
||||
return this.agent.subscribe(listener);
|
||||
}
|
||||
|
||||
get state() {
|
||||
return this.agent.state;
|
||||
}
|
||||
|
||||
getNoteCount(): number {
|
||||
return this.notes.count();
|
||||
}
|
||||
|
||||
getTurnCount(): number {
|
||||
return this.turnCount;
|
||||
}
|
||||
|
||||
async research(topic: string): Promise<ResearchResult> {
|
||||
this.turnCount = 0;
|
||||
this.notes.clear();
|
||||
this.report = "";
|
||||
this.forcedSynthesis = false;
|
||||
this.synthesisRequested = false;
|
||||
this.reportGenerationStopped = false;
|
||||
this.turnsAfterSynthesis = 0;
|
||||
this.topic = topic;
|
||||
|
||||
await this.agent.prompt(topic);
|
||||
|
||||
this.extractReport();
|
||||
|
||||
// Fallback: if no report was generated, create one from notes
|
||||
if (!this.report || this.report.trim() === "") {
|
||||
this.report = this.generateFallbackReport();
|
||||
}
|
||||
|
||||
const result: ResearchResult = {
|
||||
topic,
|
||||
notes: this.notes.getAll(),
|
||||
report: this.report,
|
||||
turnsUsed: this.turnCount,
|
||||
};
|
||||
|
||||
if (this.saveResults) {
|
||||
await this.saveToDisk(result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private getLastAssistantMessage(): AgentMessage | undefined {
|
||||
const messages = this.agent.state.messages;
|
||||
for (let i = messages.length - 1; i >= 0; i--) {
|
||||
const msg = messages[i];
|
||||
if (msg.role === "assistant" && "content" in msg) {
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private getTextLength(msg: AgentMessage): number {
|
||||
if ("content" in msg && msg.content) {
|
||||
const textContent = (msg.content as Array<{ type: string; text?: string }>).find((c) => c.type === "text");
|
||||
if (textContent?.text) {
|
||||
return textContent.text.length;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private extractReport(): void {
|
||||
const messages = this.agent.state.messages;
|
||||
|
||||
console.error(`\n[Extracting report from ${messages.length} messages...]`);
|
||||
|
||||
// Strategy 1: Look for assistant message AFTER synthesize_report tool result (correct flow)
|
||||
for (let i = messages.length - 1; i >= 0; i--) {
|
||||
const msg = messages[i];
|
||||
|
||||
if (msg.role === "toolResult") {
|
||||
const content = msg.content;
|
||||
if (Array.isArray(content)) {
|
||||
const textContent = content.find((c: any) => c.type === "text");
|
||||
if (
|
||||
textContent &&
|
||||
textContent.type === "text" &&
|
||||
textContent.text.includes("Research synthesis requested for")
|
||||
) {
|
||||
console.error(`[Found synthesize_report at index ${i}]`);
|
||||
// Found synthesize_report result, look for assistant message after it
|
||||
for (let j = i + 1; j < messages.length; j++) {
|
||||
const nextMsg = messages[j];
|
||||
if (nextMsg.role === "assistant" && nextMsg.content) {
|
||||
const nextTextContent = nextMsg.content.find((c) => c.type === "text");
|
||||
if (nextTextContent && nextTextContent.type === "text" && nextTextContent.text.length > 200) {
|
||||
console.error(
|
||||
`[Extracted report from assistant message at index ${j}, length: ${nextTextContent.text.length}]`,
|
||||
);
|
||||
this.report = nextTextContent.text;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.error(`[No report found after synthesize_report, trying fallback strategies...]`);
|
||||
|
||||
// Strategy 2: Look for the longest assistant message in the last 5 messages (fallback)
|
||||
const lastMessages = messages.slice(-5);
|
||||
let longestText = "";
|
||||
for (const msg of lastMessages) {
|
||||
if (msg.role === "assistant" && msg.content) {
|
||||
const textContent = msg.content.find((c) => c.type === "text");
|
||||
if (textContent && textContent.type === "text" && textContent.text.length > longestText.length) {
|
||||
longestText = textContent.text;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (longestText.length > 200) {
|
||||
console.error(`[Extracted longest report from last 5 messages, length: ${longestText.length}]`);
|
||||
this.report = longestText;
|
||||
return;
|
||||
}
|
||||
|
||||
console.error(`[No substantial report found in last 5 messages, trying any long message...]`);
|
||||
|
||||
// Strategy 3: Look for any assistant message with substantial text
|
||||
for (let i = messages.length - 1; i >= 0; i--) {
|
||||
const msg = messages[i];
|
||||
if (msg.role === "assistant" && msg.content) {
|
||||
const textContent = msg.content.find((c) => c.type === "text");
|
||||
if (textContent && textContent.type === "text" && textContent.text.length > 100) {
|
||||
console.error(
|
||||
`[Extracted fallback report from assistant message at index ${i}, length: ${textContent.text.length}]`,
|
||||
);
|
||||
this.report = textContent.text;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.error(`[No report found in messages, trying longest message from all...]`);
|
||||
|
||||
// Strategy 4: Find the longest assistant message in ALL messages
|
||||
let longestMsg = "";
|
||||
let longestIndex = -1;
|
||||
for (let i = 0; i < messages.length; i++) {
|
||||
const msg = messages[i];
|
||||
if (msg.role === "assistant" && msg.content) {
|
||||
const textContent = msg.content.find((c) => c.type === "text");
|
||||
if (textContent && textContent.type === "text" && textContent.text.length > longestMsg.length) {
|
||||
longestMsg = textContent.text;
|
||||
longestIndex = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (longestMsg.length > MIN_REPORT_LENGTH) {
|
||||
console.error(
|
||||
`[Extracted longest report from all messages at index ${longestIndex}, length: ${longestMsg.length}]`,
|
||||
);
|
||||
this.report = longestMsg;
|
||||
return;
|
||||
}
|
||||
|
||||
console.error(`[No report found in messages]`);
|
||||
this.report = "";
|
||||
}
|
||||
|
||||
private generateFallbackReport(): string {
|
||||
const notes = this.notes.getAll();
|
||||
if (notes.length === 0) {
|
||||
return "Research completed but no notes were collected.";
|
||||
}
|
||||
|
||||
const lines = [
|
||||
`# Research Report: ${this.topic}`,
|
||||
"",
|
||||
"## Executive Summary",
|
||||
"",
|
||||
`This research collected ${notes.length} findings on "${this.topic}".`,
|
||||
"",
|
||||
"## Key Findings",
|
||||
"",
|
||||
];
|
||||
|
||||
// Group notes by confidence
|
||||
const highConfidence = notes.filter((n) => n.confidence === "high");
|
||||
const mediumConfidence = notes.filter((n) => n.confidence === "medium");
|
||||
const lowConfidence = notes.filter((n) => n.confidence === "low");
|
||||
|
||||
if (highConfidence.length > 0) {
|
||||
lines.push("### High Confidence Findings");
|
||||
lines.push("");
|
||||
for (const note of highConfidence) {
|
||||
lines.push(`- **${note.topic}**: ${note.finding}`);
|
||||
lines.push(` - Source: ${note.source_url}`);
|
||||
}
|
||||
lines.push("");
|
||||
}
|
||||
|
||||
if (mediumConfidence.length > 0) {
|
||||
lines.push("### Medium Confidence Findings");
|
||||
lines.push("");
|
||||
for (const note of mediumConfidence) {
|
||||
lines.push(`- **${note.topic}**: ${note.finding}`);
|
||||
lines.push(` - Source: ${note.source_url}`);
|
||||
}
|
||||
lines.push("");
|
||||
}
|
||||
|
||||
if (lowConfidence.length > 0) {
|
||||
lines.push("### Low Confidence Findings");
|
||||
lines.push("");
|
||||
for (const note of lowConfidence) {
|
||||
lines.push(`- **${note.topic}**: ${note.finding}`);
|
||||
lines.push(` - Source: ${note.source_url}`);
|
||||
}
|
||||
lines.push("");
|
||||
}
|
||||
|
||||
lines.push("## Notes");
|
||||
lines.push("");
|
||||
lines.push(
|
||||
"*This report was auto-generated from collected notes as the agent reached the turn limit before synthesizing a full report.*",
|
||||
);
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
private async saveToDisk(result: ResearchResult): Promise<void> {
|
||||
try {
|
||||
const timestamp = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
|
||||
const slug = result.topic
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-|-$/g, "")
|
||||
.slice(0, 50);
|
||||
const dir = join(this.outputDir, `${timestamp}-${slug}`);
|
||||
|
||||
await mkdir(dir, { recursive: true });
|
||||
await writeFile(join(dir, "report.md"), result.report, "utf-8");
|
||||
await writeFile(join(dir, "report.json"), JSON.stringify(result, null, 2), "utf-8");
|
||||
await writeFile(join(dir, "notes.json"), JSON.stringify(result.notes, null, 2), "utf-8");
|
||||
|
||||
console.error(`\n[Results saved to ${dir}]`);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`\n[Warning: Failed to save results to disk: ${error instanceof Error ? error.message : String(error)}]`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
abort(): void {
|
||||
this.agent.abort();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env node
|
||||
import { loadEnvFile } from "./utils/env.ts";
|
||||
|
||||
// Load .env file before anything else
|
||||
loadEnvFile();
|
||||
|
||||
import { APP_NAME } from "./config.ts";
|
||||
import { main } from "./main.ts";
|
||||
|
||||
process.title = APP_NAME;
|
||||
process.emitWarning = (() => {}) as typeof process.emitWarning;
|
||||
|
||||
main(process.argv.slice(2));
|
||||
@@ -0,0 +1,18 @@
|
||||
export const VERSION = "0.78.0";
|
||||
export const APP_NAME = "pi-research";
|
||||
|
||||
export const DEFAULT_MAX_TURNS = 15;
|
||||
export const DEFAULT_MAX_RESULTS = 10;
|
||||
export const DEFAULT_MAX_CONTENT_LENGTH = 10000;
|
||||
|
||||
export const ENV_TAVILY_API_KEY = "TAVILY_API_KEY";
|
||||
export const ENV_OPENROUTER_API_KEY = "OPENROUTER_API_KEY";
|
||||
export const ENV_LLM_MODEL = "LLM_MODEL";
|
||||
|
||||
export const DEFAULT_MODEL_ID = "minimax/minimax-m3";
|
||||
export const DEFAULT_PROVIDER = "openrouter";
|
||||
|
||||
export const TAVILY_API_URL = "https://api.tavily.com/search";
|
||||
|
||||
export const DEFAULT_OUTPUT_DIR = "./research-output";
|
||||
export const MIN_REPORT_LENGTH = 500;
|
||||
@@ -0,0 +1,8 @@
|
||||
export { ResearchAgent } from "./agent.ts";
|
||||
export { DEFAULT_MODEL_ID, DEFAULT_PROVIDER, VERSION } from "./config.ts";
|
||||
export { createResearchMcpServer } from "./mcp-server.ts";
|
||||
export { loadResearchSkills } from "./skills/loader.ts";
|
||||
export type { ResearchSkill } from "./skills/types.ts";
|
||||
export { BASE_SYSTEM_PROMPT, buildSystemPrompt } from "./system-prompt.ts";
|
||||
export { createNoteStore, createResearchTools } from "./tools/index.ts";
|
||||
export type { NoteStore, ResearchAgentOptions, ResearchNote, ResearchResult } from "./types.ts";
|
||||
@@ -0,0 +1,141 @@
|
||||
import chalk from "chalk";
|
||||
import { ResearchAgent } from "./agent.ts";
|
||||
import { APP_NAME, VERSION } from "./config.ts";
|
||||
import { runInteractiveMode, runPrintMode } from "./modes/index.ts";
|
||||
import { loadResearchSkills } from "./skills/loader.ts";
|
||||
|
||||
interface ParsedArgs {
|
||||
help: boolean;
|
||||
version: boolean;
|
||||
print: boolean;
|
||||
maxTurns?: number;
|
||||
model?: string;
|
||||
topic?: string;
|
||||
save: boolean;
|
||||
outputDir?: string;
|
||||
}
|
||||
|
||||
function parseArgs(args: string[]): ParsedArgs {
|
||||
const parsed: ParsedArgs = {
|
||||
help: false,
|
||||
version: false,
|
||||
print: false,
|
||||
save: true,
|
||||
};
|
||||
|
||||
const positional: string[] = [];
|
||||
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
const arg = args[i];
|
||||
|
||||
if (arg === "--help" || arg === "-h") {
|
||||
parsed.help = true;
|
||||
} else if (arg === "--version" || arg === "-v") {
|
||||
parsed.version = true;
|
||||
} else if (arg === "--print" || arg === "-p") {
|
||||
parsed.print = true;
|
||||
} else if (arg === "--max-turns" || arg === "-t") {
|
||||
const value = args[++i];
|
||||
if (value) {
|
||||
parsed.maxTurns = Number.parseInt(value, 10);
|
||||
}
|
||||
} else if (arg === "--model" || arg === "-m") {
|
||||
parsed.model = args[++i];
|
||||
} else if (arg === "--no-save") {
|
||||
parsed.save = false;
|
||||
} else if (arg === "--output-dir" || arg === "-o") {
|
||||
parsed.outputDir = args[++i];
|
||||
} else if (!arg.startsWith("-")) {
|
||||
positional.push(arg);
|
||||
}
|
||||
}
|
||||
|
||||
if (positional.length > 0) {
|
||||
parsed.topic = positional.join(" ");
|
||||
}
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function printHelp(): void {
|
||||
console.log(`
|
||||
${chalk.bold(APP_NAME)} - Autonomous Research Agent
|
||||
|
||||
${chalk.bold("Usage:")}
|
||||
${APP_NAME} [options] <topic>
|
||||
|
||||
${chalk.bold("Options:")}
|
||||
-h, --help Show this help message
|
||||
-v, --version Show version number
|
||||
-p, --print Use print mode (no interactive TUI)
|
||||
-t, --max-turns <n> Maximum research turns (default: 15)
|
||||
-m, --model <model> Model to use (default: minimax/minimax-m3)
|
||||
-o, --output-dir <p> Output directory for results (default: ./research-output)
|
||||
--no-save Do not save results to disk
|
||||
|
||||
${chalk.bold("Environment Variables:")}
|
||||
TAVILY_API_KEY Required - Tavily API key for web search
|
||||
OPENROUTER_API_KEY Required - OpenRouter API key for LLM
|
||||
LLM_MODEL Optional - Override default model
|
||||
|
||||
${chalk.bold("Examples:")}
|
||||
${APP_NAME} "quantum computing breakthroughs 2026"
|
||||
${APP_NAME} --print "AI safety research"
|
||||
${APP_NAME} --max-turns 15 "climate change solutions"
|
||||
${APP_NAME} -m openai/gpt-4o "renewable energy trends"
|
||||
${APP_NAME} --no-save "quick research topic"
|
||||
${APP_NAME} -o ./my-reports "saved to custom directory"
|
||||
|
||||
${chalk.bold("Output:")}
|
||||
Results are automatically saved to <output-dir>/<timestamp>-<topic>/
|
||||
- report.md - Final research report in markdown
|
||||
- report.json - Full results (report + notes + metadata)
|
||||
- notes.json - Collected research notes
|
||||
|
||||
${chalk.bold("Skills:")}
|
||||
Research skills are loaded from ~/.pi/skills/research/ directory.
|
||||
Create .md files with YAML frontmatter to customize research behavior.
|
||||
`);
|
||||
}
|
||||
|
||||
export async function main(args: string[]): Promise<void> {
|
||||
const parsed = parseArgs(args);
|
||||
|
||||
if (parsed.help) {
|
||||
printHelp();
|
||||
return;
|
||||
}
|
||||
|
||||
if (parsed.version) {
|
||||
console.log(`${APP_NAME} ${VERSION}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!parsed.topic) {
|
||||
console.error(chalk.red("Error: Research topic is required."));
|
||||
console.error(chalk.dim(`Run '${APP_NAME} --help' for usage information.`));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const skills = await loadResearchSkills();
|
||||
|
||||
let agent: ResearchAgent;
|
||||
try {
|
||||
agent = new ResearchAgent({
|
||||
maxTurns: parsed.maxTurns,
|
||||
skills,
|
||||
saveResults: parsed.save,
|
||||
outputDir: parsed.outputDir,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(chalk.red(`Error: ${error instanceof Error ? error.message : String(error)}`));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const mode = parsed.print || !process.stdin.isTTY ? "print" : "interactive";
|
||||
|
||||
const exitCode =
|
||||
mode === "interactive" ? await runInteractiveMode(agent, parsed.topic) : await runPrintMode(agent, parsed.topic);
|
||||
|
||||
process.exit(exitCode);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env node
|
||||
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
||||
import { createResearchMcpServer } from "./mcp-server.ts";
|
||||
import { loadEnvFile } from "./utils/env.ts";
|
||||
|
||||
loadEnvFile();
|
||||
|
||||
async function main() {
|
||||
const server = createResearchMcpServer();
|
||||
const transport = new StdioServerTransport();
|
||||
await server.connect(transport);
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error("Fatal error:", error);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -0,0 +1,162 @@
|
||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||
import { z } from "zod";
|
||||
import { ResearchAgent } from "./agent.ts";
|
||||
import { VERSION } from "./config.ts";
|
||||
|
||||
const researchSchema = {
|
||||
topic: z.string().describe("The research topic to investigate"),
|
||||
maxTurns: z
|
||||
.number()
|
||||
.optional()
|
||||
.default(15)
|
||||
.describe("Maximum number of research turns (default: 15). More turns = deeper research."),
|
||||
format: z
|
||||
.enum(["markdown", "json", "bullet"])
|
||||
.optional()
|
||||
.default("markdown")
|
||||
.describe("Output format for the report (default: markdown)"),
|
||||
};
|
||||
|
||||
const quickSearchSchema = {
|
||||
query: z.string().describe("Search query"),
|
||||
maxResults: z.number().optional().default(10).describe("Maximum number of results (default: 10)"),
|
||||
};
|
||||
|
||||
const readUrlSchema = {
|
||||
url: z.string().describe("URL to read"),
|
||||
maxLength: z.number().optional().default(10000).describe("Maximum characters to extract (default: 10000)"),
|
||||
};
|
||||
|
||||
export function createResearchMcpServer(): McpServer {
|
||||
const server = new McpServer({
|
||||
name: "pi-research-agent",
|
||||
version: VERSION,
|
||||
});
|
||||
|
||||
server.registerTool(
|
||||
"research",
|
||||
{
|
||||
description:
|
||||
"Perform deep autonomous research on a topic. Searches the web, reads sources, saves findings, and generates a comprehensive report with citations.",
|
||||
inputSchema: researchSchema as any,
|
||||
},
|
||||
async (params: any) => {
|
||||
const { topic, maxTurns = 15, format = "markdown" } = params;
|
||||
const agent = new ResearchAgent({
|
||||
maxTurns,
|
||||
saveResults: false,
|
||||
});
|
||||
|
||||
// Suppress console output during MCP execution
|
||||
const originalError = console.error;
|
||||
console.error = () => {};
|
||||
|
||||
try {
|
||||
const result = await agent.research(topic);
|
||||
|
||||
console.error = originalError;
|
||||
|
||||
const reportWithMeta = [
|
||||
`# Research Report: ${topic}`,
|
||||
"",
|
||||
`**Turns used:** ${result.turnsUsed}`,
|
||||
`**Notes collected:** ${result.notes.length}`,
|
||||
`**Format:** ${format}`,
|
||||
"",
|
||||
"---",
|
||||
"",
|
||||
result.report,
|
||||
"",
|
||||
"---",
|
||||
"",
|
||||
"## Raw Research Notes",
|
||||
"",
|
||||
...result.notes.flatMap((note) => [
|
||||
`### ${note.topic}`,
|
||||
`- **Confidence:** ${note.confidence}`,
|
||||
`- **Source:** ${note.source_url}`,
|
||||
`- **Finding:** ${note.finding}`,
|
||||
"",
|
||||
]),
|
||||
].join("\n");
|
||||
|
||||
return {
|
||||
content: [{ type: "text" as const, text: reportWithMeta }],
|
||||
};
|
||||
} catch (error) {
|
||||
console.error = originalError;
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
return {
|
||||
content: [{ type: "text" as const, text: `Research failed: ${message}` }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
"quick_search",
|
||||
{
|
||||
description:
|
||||
"Quick web search on a topic. Returns search results without full research flow. Use for fast information lookup.",
|
||||
inputSchema: quickSearchSchema as any,
|
||||
},
|
||||
async (params: any) => {
|
||||
const { query, maxResults = 10 } = params;
|
||||
try {
|
||||
const { tavilySearch } = await import("./utils/tavily.ts");
|
||||
const { formatSearchResults } = await import("./utils/formatter.ts");
|
||||
|
||||
const apiKey = process.env.TAVILY_API_KEY;
|
||||
if (!apiKey) {
|
||||
return {
|
||||
content: [{ type: "text" as const, text: "Error: TAVILY_API_KEY environment variable is not set" }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
const results = await tavilySearch(query, {
|
||||
apiKey,
|
||||
maxResults,
|
||||
});
|
||||
|
||||
return {
|
||||
content: [{ type: "text" as const, text: formatSearchResults(results.results) }],
|
||||
};
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
return {
|
||||
content: [{ type: "text" as const, text: `Search failed: ${message}` }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
"read_url",
|
||||
{
|
||||
description: "Fetch and extract content from a URL. Useful for reading specific articles or pages.",
|
||||
inputSchema: readUrlSchema as any,
|
||||
},
|
||||
async (params: any) => {
|
||||
const { url, maxLength = 10000 } = params;
|
||||
try {
|
||||
const { fetchAndExtract } = await import("./utils/fetcher.ts");
|
||||
const content = await fetchAndExtract(url, maxLength);
|
||||
|
||||
return {
|
||||
content: [{ type: "text" as const, text: `Content from ${url}:\n\n${content}` }],
|
||||
};
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
return {
|
||||
content: [{ type: "text" as const, text: `Failed to read URL: ${message}` }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
return server;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export { runInteractiveMode } from "./interactive.ts";
|
||||
export { runPrintMode } from "./print.ts";
|
||||
@@ -0,0 +1,87 @@
|
||||
import chalk from "chalk";
|
||||
import type { ResearchAgent } from "../agent.ts";
|
||||
|
||||
export async function runInteractiveMode(agent: ResearchAgent, topic: string): Promise<number> {
|
||||
console.log(chalk.bold.cyan("\n╔══════════════════════════════════════════════════════════╗"));
|
||||
console.log(chalk.bold.cyan("║ Pi Research Agent - Interactive ║"));
|
||||
console.log(chalk.bold.cyan("╚══════════════════════════════════════════════════════════╝"));
|
||||
console.log();
|
||||
|
||||
console.log(chalk.bold(`Topic: ${topic}\n`));
|
||||
|
||||
let currentPhase = "";
|
||||
|
||||
agent.subscribe((event) => {
|
||||
switch (event.type) {
|
||||
case "turn_start": {
|
||||
const turnNum = agent.getTurnCount() + 1;
|
||||
console.log(chalk.bold.yellow(`\n┌─── Turn ${turnNum} ───────────────────────────────────`));
|
||||
break;
|
||||
}
|
||||
case "tool_execution_start": {
|
||||
const toolName = event.toolName;
|
||||
const args = event.args as Record<string, unknown>;
|
||||
if (toolName === "web_search") {
|
||||
currentPhase = "search";
|
||||
console.log(chalk.cyan(` 🔍 Searching: "${args.query}"`));
|
||||
} else if (toolName === "read_url") {
|
||||
currentPhase = "read";
|
||||
console.log(chalk.blue(` 📖 Reading: ${args.url}`));
|
||||
} else if (toolName === "save_note") {
|
||||
currentPhase = "note";
|
||||
console.log(chalk.green(` 💾 Saving note: "${args.topic}" (${args.confidence} confidence)`));
|
||||
} else if (toolName === "synthesize_report") {
|
||||
currentPhase = "synthesize";
|
||||
console.log(chalk.magenta(" 📊 Synthesizing final report..."));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "tool_execution_end": {
|
||||
if (event.isError) {
|
||||
console.log(chalk.red(` ⚠️ Error: ${event.toolName}`));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "turn_end": {
|
||||
console.log(chalk.dim(` └── Notes: ${agent.getNoteCount()} | Turn ${agent.getTurnCount()} complete`));
|
||||
break;
|
||||
}
|
||||
case "message_start": {
|
||||
if (currentPhase !== "synthesize") {
|
||||
console.log(chalk.dim(" 🤔 Thinking..."));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "message_update": {
|
||||
if (event.assistantMessageEvent.type === "text_delta") {
|
||||
process.stdout.write(chalk.white(event.assistantMessageEvent.delta));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "message_end": {
|
||||
process.stdout.write("\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await agent.research(topic);
|
||||
|
||||
console.log(chalk.bold.green("\n\n╔══════════════════════════════════════════════════════════╗"));
|
||||
console.log(chalk.bold.green("║ Research Complete! ║"));
|
||||
console.log(chalk.bold.green("╚══════════════════════════════════════════════════════════╝"));
|
||||
console.log();
|
||||
|
||||
console.log(chalk.dim(` Turns used: ${result.turnsUsed}`));
|
||||
console.log(chalk.dim(` Notes collected: ${result.notes.length}`));
|
||||
console.log(chalk.dim(`\n${"─".repeat(60)}\n`));
|
||||
|
||||
console.log(result.report);
|
||||
|
||||
return 0;
|
||||
} catch (error) {
|
||||
console.error(chalk.red(`\n\nResearch failed: ${error instanceof Error ? error.message : String(error)}`));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import chalk from "chalk";
|
||||
import type { ResearchAgent } from "../agent.ts";
|
||||
|
||||
export async function runPrintMode(agent: ResearchAgent, topic: string): Promise<number> {
|
||||
console.log(chalk.bold(`\nResearching: ${topic}\n`));
|
||||
console.log(chalk.dim("─".repeat(60)));
|
||||
|
||||
agent.subscribe((event) => {
|
||||
switch (event.type) {
|
||||
case "tool_execution_start": {
|
||||
const toolName = event.toolName;
|
||||
const args = event.args as Record<string, unknown>;
|
||||
if (toolName === "web_search") {
|
||||
console.error(chalk.cyan(`\n[Searching: ${args.query}]`));
|
||||
} else if (toolName === "read_url") {
|
||||
console.error(chalk.blue(`\n[Reading: ${args.url}]`));
|
||||
} else if (toolName === "save_note") {
|
||||
console.error(chalk.green(`\n[Saving note: ${args.topic}]`));
|
||||
} else if (toolName === "synthesize_report") {
|
||||
console.error(chalk.magenta("\n[Synthesizing report...]"));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "turn_end": {
|
||||
console.error(
|
||||
chalk.dim(`\n[Turn ${agent.getTurnCount()} complete, ${agent.getNoteCount()} notes collected]`),
|
||||
);
|
||||
break;
|
||||
}
|
||||
case "message_update": {
|
||||
if (event.assistantMessageEvent.type === "text_delta") {
|
||||
process.stdout.write(event.assistantMessageEvent.delta);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await agent.research(topic);
|
||||
|
||||
console.log("\n");
|
||||
console.log(chalk.dim("─".repeat(60)));
|
||||
console.log(chalk.bold.green("\nResearch Complete!"));
|
||||
console.log(chalk.dim(`Turns used: ${result.turnsUsed}`));
|
||||
console.log(chalk.dim(`Notes collected: ${result.notes.length}`));
|
||||
console.log(chalk.dim("─".repeat(60)));
|
||||
console.log(`\n${result.report}`);
|
||||
|
||||
return 0;
|
||||
} catch (error) {
|
||||
console.error(chalk.red(`\nResearch failed: ${error instanceof Error ? error.message : String(error)}`));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import { existsSync } from "node:fs";
|
||||
import { readdir, readFile } from "node:fs/promises";
|
||||
import { homedir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { parse as parseYaml } from "yaml";
|
||||
import type { ResearchSkill } from "./types.ts";
|
||||
|
||||
export type { ResearchSkill };
|
||||
|
||||
export async function loadResearchSkills(skillsDir?: string): Promise<ResearchSkill[]> {
|
||||
const dir = skillsDir ?? join(homedir(), ".pi", "skills", "research");
|
||||
|
||||
if (!existsSync(dir)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
let files: string[];
|
||||
try {
|
||||
files = await readdir(dir);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
|
||||
const skills: ResearchSkill[] = [];
|
||||
|
||||
for (const file of files) {
|
||||
if (!file.endsWith(".md")) continue;
|
||||
|
||||
try {
|
||||
const content = await readFile(join(dir, file), "utf-8");
|
||||
const parsed = parseSkillFrontmatter(content);
|
||||
if (parsed) {
|
||||
skills.push(parsed);
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
||||
return skills;
|
||||
}
|
||||
|
||||
function parseSkillFrontmatter(content: string): ResearchSkill | null {
|
||||
const match = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);
|
||||
if (!match) return null;
|
||||
|
||||
try {
|
||||
const frontmatter = parseYaml(match[1]) as Record<string, unknown>;
|
||||
return {
|
||||
name: (frontmatter.name as string) ?? "unnamed",
|
||||
description: (frontmatter.description as string) ?? "",
|
||||
content: match[2].trim(),
|
||||
maxTurns: frontmatter.max_turns as number | undefined,
|
||||
tools: frontmatter.tools as string[] | undefined,
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
export interface ResearchSkill {
|
||||
name: string;
|
||||
description: string;
|
||||
content: string;
|
||||
maxTurns?: number;
|
||||
tools?: string[];
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
export const BASE_SYSTEM_PROMPT = `You are an autonomous research agent. Your task is to thoroughly research topics by searching the web, reading sources, and synthesizing findings into comprehensive reports.
|
||||
|
||||
## Research Methodology
|
||||
|
||||
1. **Break down the topic** into key sub-questions that need answering
|
||||
2. **Search systematically** using web_search for each sub-question
|
||||
3. **Read relevant sources** using read_url to get detailed information from promising URLs
|
||||
4. **Save important findings** using save_note with appropriate confidence levels
|
||||
5. **Identify gaps** in your knowledge and search for additional information
|
||||
6. **Cross-reference** findings from multiple sources to verify accuracy
|
||||
7. **Synthesize** when you have enough information using synthesize_report
|
||||
|
||||
## Available Tools
|
||||
|
||||
- **web_search**: Search the web using Tavily. Use specific, targeted queries.
|
||||
- **read_url**: Fetch and read content from a URL. Use for sources found in search results.
|
||||
- **save_note**: Save a research finding with topic, content, source URL, and confidence level.
|
||||
- **synthesize_report**: Prepare all collected notes for report generation. Call this tool when you have gathered sufficient information. After calling this tool, you MUST generate the final comprehensive report in your next response.
|
||||
|
||||
## Guidelines
|
||||
|
||||
- Start with 2-3 broad searches to understand the topic landscape
|
||||
- Then narrow down with specific queries for each sub-question
|
||||
- Read at least 3-5 primary sources for comprehensive coverage
|
||||
- **CRITICAL**: You MUST call save_note for every significant finding from each source you read
|
||||
- Save at least 5-10 notes before calling synthesize_report
|
||||
- Use confidence levels appropriately:
|
||||
- **high**: well-established facts from authoritative sources (academic papers, official reports)
|
||||
- **medium**: recent information or facts from reputable but less authoritative sources
|
||||
- **low**: speculative claims, single-source information, or unverified data
|
||||
- **IMPORTANT**: Do NOT call synthesize_report until you have saved at least 5 notes with save_note
|
||||
- Call synthesize_report only after you have gathered sufficient information and saved multiple notes (typically after 5-10 searches and reading key sources)
|
||||
|
||||
## Report Generation Process
|
||||
|
||||
**CRITICAL**: Follow this exact sequence:
|
||||
|
||||
1. **Research phase**: Search, read, and save notes
|
||||
2. **Call synthesize_report**: When ready, call the tool
|
||||
3. **Generate report IMMEDIATELY**: In your NEXT response, generate the full report
|
||||
- Do NOT call any more tools
|
||||
- Do NOT search or read more sources
|
||||
- Generate the complete markdown report (2000+ words)
|
||||
- Include executive summary, findings, citations, confidence, gaps
|
||||
|
||||
**WRONG**: Calling synthesize_report, then searching more
|
||||
**RIGHT**: Calling synthesize_report, then generating the report immediately
|
||||
|
||||
The synthesize_report tool will return all your saved notes in a structured format. Use this information to write your final report.
|
||||
|
||||
## Output Format
|
||||
|
||||
Your final report should include:
|
||||
- Executive summary (2-3 paragraphs)
|
||||
- Key findings organized by theme
|
||||
- Source citations with URLs
|
||||
- Confidence assessment of the overall research
|
||||
- Identified gaps or areas for further research
|
||||
|
||||
Be thorough but efficient. Do not repeat searches for the same information.`;
|
||||
|
||||
export function buildSystemPrompt(skills?: Array<{ name: string; content: string }>): string {
|
||||
let prompt = BASE_SYSTEM_PROMPT;
|
||||
|
||||
if (skills && skills.length > 0) {
|
||||
prompt += "\n\n## Additional Research Skills\n\n";
|
||||
for (const skill of skills) {
|
||||
prompt += `### ${skill.name}\n${skill.content}\n\n`;
|
||||
}
|
||||
}
|
||||
|
||||
return prompt;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { AgentTool } from "@earendil-works/pi-agent-core";
|
||||
import type { NoteStore } from "../types.ts";
|
||||
import { createReadUrlTool } from "./read-url.ts";
|
||||
import { createSaveNoteTool } from "./save-note.ts";
|
||||
import { createSynthesizeTool } from "./synthesize.ts";
|
||||
import { createWebSearchTool } from "./web-search.ts";
|
||||
|
||||
export type { NoteStore, ResearchNote } from "../types.ts";
|
||||
export { createNoteStore } from "./save-note.ts";
|
||||
|
||||
export function createResearchTools(notes: NoteStore, tavilyApiKey: string): AgentTool[] {
|
||||
return [
|
||||
createWebSearchTool(tavilyApiKey),
|
||||
createReadUrlTool(),
|
||||
createSaveNoteTool(notes),
|
||||
createSynthesizeTool(notes),
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import type { AgentTool } from "@earendil-works/pi-agent-core";
|
||||
import { type Static, Type } from "typebox";
|
||||
import { DEFAULT_MAX_CONTENT_LENGTH } from "../config.ts";
|
||||
import { fetchAndExtract } from "../utils/fetcher.ts";
|
||||
|
||||
const readUrlSchema = Type.Object({
|
||||
url: Type.String({ description: "URL of the web page to read" }),
|
||||
max_length: Type.Optional(
|
||||
Type.Number({
|
||||
description: `Maximum characters to extract (default: ${DEFAULT_MAX_CONTENT_LENGTH})`,
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
export type ReadUrlInput = Static<typeof readUrlSchema>;
|
||||
|
||||
interface ReadUrlDetails {
|
||||
url: string;
|
||||
length: number;
|
||||
}
|
||||
|
||||
export function createReadUrlTool(): AgentTool<typeof readUrlSchema, ReadUrlDetails> {
|
||||
return {
|
||||
name: "read_url",
|
||||
label: "Read URL",
|
||||
description: "Fetch and read the content of a web page. Extracts main text content from HTML pages.",
|
||||
parameters: readUrlSchema,
|
||||
execute: async (_toolCallId, params: ReadUrlInput) => {
|
||||
const maxLength = params.max_length ?? DEFAULT_MAX_CONTENT_LENGTH;
|
||||
|
||||
// Try with default User-Agent, then retry with alternative if it fails
|
||||
let content: string;
|
||||
let lastError: Error | null = null;
|
||||
|
||||
try {
|
||||
content = await fetchAndExtract(params.url, maxLength);
|
||||
} catch (error) {
|
||||
lastError = error instanceof Error ? error : new Error(String(error));
|
||||
|
||||
// Retry with alternative User-Agent
|
||||
try {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait 1 second
|
||||
content = await fetchAndExtract(params.url, maxLength, true); // true = use alternative UA
|
||||
} catch (_retryError) {
|
||||
throw new Error(`Failed to fetch ${params.url} after 2 attempts: ${lastError.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text" as const,
|
||||
text: `Content from ${params.url}:\n\n${content}`,
|
||||
},
|
||||
],
|
||||
details: { url: params.url, length: content.length },
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import type { AgentTool } from "@earendil-works/pi-agent-core";
|
||||
import { type Static, Type } from "typebox";
|
||||
import type { NoteStore, ResearchNote } from "../types.ts";
|
||||
|
||||
const saveNoteSchema = Type.Object({
|
||||
topic: Type.String({ description: "Topic or sub-question this note addresses" }),
|
||||
finding: Type.String({ description: "The key finding or information extracted" }),
|
||||
source_url: Type.String({ description: "URL of the source where this finding came from" }),
|
||||
confidence: Type.Union([Type.Literal("high"), Type.Literal("medium"), Type.Literal("low")], {
|
||||
description:
|
||||
"Confidence level: high for well-established facts, medium for recent info, low for speculative claims",
|
||||
}),
|
||||
});
|
||||
|
||||
export type SaveNoteInput = Static<typeof saveNoteSchema>;
|
||||
|
||||
export function createNoteStore(): NoteStore {
|
||||
const notes: ResearchNote[] = [];
|
||||
return {
|
||||
add(note: ResearchNote) {
|
||||
notes.push(note);
|
||||
},
|
||||
getAll() {
|
||||
return notes.slice();
|
||||
},
|
||||
clear() {
|
||||
notes.length = 0;
|
||||
},
|
||||
count() {
|
||||
return notes.length;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function createSaveNoteTool(notes: NoteStore): AgentTool<typeof saveNoteSchema, ResearchNote> {
|
||||
return {
|
||||
name: "save_note",
|
||||
label: "Save Note",
|
||||
description: "Save a research note or finding for later synthesis into the final report",
|
||||
parameters: saveNoteSchema,
|
||||
execute: async (_toolCallId, params: SaveNoteInput) => {
|
||||
const note: ResearchNote = {
|
||||
topic: params.topic,
|
||||
finding: params.finding,
|
||||
source_url: params.source_url,
|
||||
confidence: params.confidence,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
notes.add(note);
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text" as const,
|
||||
text: `Note saved successfully. Topic: "${params.topic}". Total notes collected: ${notes.count()}`,
|
||||
},
|
||||
],
|
||||
details: note,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
import type { AgentTool } from "@earendil-works/pi-agent-core";
|
||||
import { type Static, Type } from "typebox";
|
||||
import type { NoteStore } from "../types.ts";
|
||||
import { formatNotes } from "../utils/formatter.ts";
|
||||
|
||||
const synthesizeSchema = Type.Object({
|
||||
topic: Type.String({ description: "The main research topic to synthesize" }),
|
||||
format: Type.Optional(
|
||||
Type.Union([Type.Literal("markdown"), Type.Literal("json"), Type.Literal("bullet")], {
|
||||
description: "Output format for the report (default: markdown)",
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
export type SynthesizeInput = Static<typeof synthesizeSchema>;
|
||||
|
||||
interface SynthesizeDetails {
|
||||
notes_count: number;
|
||||
topic: string;
|
||||
format: string;
|
||||
synthesisRequested: boolean;
|
||||
}
|
||||
|
||||
export function createSynthesizeTool(notes: NoteStore): AgentTool<typeof synthesizeSchema, SynthesizeDetails> {
|
||||
return {
|
||||
name: "synthesize_report",
|
||||
label: "Synthesize Report",
|
||||
description:
|
||||
"Synthesize all collected research notes into a structured report. Call this when you have gathered sufficient information to produce the final research report.",
|
||||
parameters: synthesizeSchema,
|
||||
execute: async (_toolCallId, params: SynthesizeInput) => {
|
||||
const allNotes = notes.getAll();
|
||||
const format = params.format ?? "markdown";
|
||||
|
||||
// Warn if too few notes were collected
|
||||
if (allNotes.length === 0) {
|
||||
const warning = [
|
||||
`WARNING: No research notes were collected for topic "${params.topic}".`,
|
||||
"",
|
||||
"You must use the save_note tool to save findings from your research before calling synthesize_report.",
|
||||
"Please go back and:",
|
||||
"1. Read sources using read_url",
|
||||
"2. Save key findings using save_note with topic, finding, source_url, and confidence",
|
||||
"3. Call synthesize_report again after saving at least 5 notes",
|
||||
].join("\n");
|
||||
|
||||
return {
|
||||
content: [{ type: "text" as const, text: warning }],
|
||||
details: {
|
||||
notes_count: 0,
|
||||
topic: params.topic,
|
||||
format,
|
||||
synthesisRequested: false,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (allNotes.length < 3) {
|
||||
const warning = [
|
||||
`WARNING: Only ${allNotes.length} note(s) collected for topic "${params.topic}". This is insufficient for a comprehensive report.`,
|
||||
"",
|
||||
"Please continue researching and save more notes using save_note before calling synthesize_report again.",
|
||||
"Aim for at least 5-10 notes covering different aspects of the topic.",
|
||||
"",
|
||||
"=== COLLECTED RESEARCH NOTES ===",
|
||||
"",
|
||||
formatNotes(allNotes),
|
||||
"",
|
||||
"=== END OF NOTES ===",
|
||||
].join("\n");
|
||||
|
||||
return {
|
||||
content: [{ type: "text" as const, text: warning }],
|
||||
details: {
|
||||
notes_count: allNotes.length,
|
||||
topic: params.topic,
|
||||
format,
|
||||
synthesisRequested: false,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const summary = [
|
||||
`Research synthesis requested for: "${params.topic}"`,
|
||||
`Format: ${format}`,
|
||||
`Notes collected: ${allNotes.length}`,
|
||||
"",
|
||||
"=== COLLECTED RESEARCH NOTES ===",
|
||||
"",
|
||||
formatNotes(allNotes),
|
||||
"",
|
||||
"=== END OF NOTES ===",
|
||||
"",
|
||||
"IMPORTANT: You MUST now generate the complete research report in your NEXT response.",
|
||||
"Do NOT call any more tools. Do NOT search or read more sources.",
|
||||
"Generate the full comprehensive report with:",
|
||||
"- Executive summary (2-3 paragraphs)",
|
||||
"- Key findings organized by theme",
|
||||
"- Source citations with URLs",
|
||||
"- Confidence assessment",
|
||||
"- Identified gaps for further research",
|
||||
"",
|
||||
"The report should be at least 2000 words and include all details from the notes above.",
|
||||
].join("\n");
|
||||
|
||||
return {
|
||||
content: [{ type: "text" as const, text: summary }],
|
||||
details: {
|
||||
notes_count: allNotes.length,
|
||||
topic: params.topic,
|
||||
format,
|
||||
synthesisRequested: true,
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import type { AgentTool } from "@earendil-works/pi-agent-core";
|
||||
import { type Static, Type } from "typebox";
|
||||
import { DEFAULT_MAX_RESULTS } from "../config.ts";
|
||||
import type { TavilySearchResponse } from "../types.ts";
|
||||
import { formatSearchResults } from "../utils/formatter.ts";
|
||||
import { tavilySearch } from "../utils/tavily.ts";
|
||||
|
||||
const webSearchSchema = Type.Object({
|
||||
query: Type.String({ description: "Search query to find information on the web" }),
|
||||
max_results: Type.Optional(
|
||||
Type.Number({
|
||||
description: `Maximum number of results to return (default: ${DEFAULT_MAX_RESULTS})`,
|
||||
}),
|
||||
),
|
||||
search_depth: Type.Optional(
|
||||
Type.Union([Type.Literal("basic"), Type.Literal("advanced")], {
|
||||
description: "Search depth: basic for quick overview, advanced for comprehensive results",
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
export type WebSearchInput = Static<typeof webSearchSchema>;
|
||||
|
||||
export function createWebSearchTool(apiKey: string): AgentTool<typeof webSearchSchema, TavilySearchResponse> {
|
||||
return {
|
||||
name: "web_search",
|
||||
label: "Web Search",
|
||||
description:
|
||||
"Search the web for information on a given query using Tavily. Returns titles, URLs, and content snippets.",
|
||||
parameters: webSearchSchema,
|
||||
execute: async (_toolCallId, params: WebSearchInput) => {
|
||||
const results = await tavilySearch(params.query, {
|
||||
apiKey,
|
||||
maxResults: params.max_results ?? DEFAULT_MAX_RESULTS,
|
||||
searchDepth: params.search_depth ?? "basic",
|
||||
});
|
||||
|
||||
return {
|
||||
content: [{ type: "text" as const, text: formatSearchResults(results.results) }],
|
||||
details: results,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
export interface ResearchNote {
|
||||
topic: string;
|
||||
finding: string;
|
||||
source_url: string;
|
||||
confidence: "high" | "medium" | "low";
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
export interface NoteStore {
|
||||
add(note: ResearchNote): void;
|
||||
getAll(): ResearchNote[];
|
||||
clear(): void;
|
||||
count(): number;
|
||||
}
|
||||
|
||||
export interface SearchResult {
|
||||
title: string;
|
||||
url: string;
|
||||
content: string;
|
||||
score: number;
|
||||
}
|
||||
|
||||
export interface TavilySearchResponse {
|
||||
query: string;
|
||||
results: SearchResult[];
|
||||
answer?: string;
|
||||
}
|
||||
|
||||
export interface ResearchResult {
|
||||
topic: string;
|
||||
notes: ResearchNote[];
|
||||
report: string;
|
||||
turnsUsed: number;
|
||||
}
|
||||
|
||||
export interface ResearchAgentOptions {
|
||||
model?: import("@earendil-works/pi-ai").Model<any>;
|
||||
thinkingLevel?: import("@earendil-works/pi-agent-core").ThinkingLevel;
|
||||
maxTurns?: number;
|
||||
tavilyApiKey?: string;
|
||||
llmApiKey?: string;
|
||||
skills?: import("./skills/types.ts").ResearchSkill[];
|
||||
outputDir?: string;
|
||||
saveResults?: boolean;
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
import { existsSync, readFileSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
/**
|
||||
* Simple .env file parser
|
||||
* Supports:
|
||||
* - KEY=VALUE
|
||||
* - KEY="VALUE" (double quotes)
|
||||
* - KEY='VALUE' (single quotes)
|
||||
* - # comments
|
||||
* - Empty lines
|
||||
* - Multiline values with quotes
|
||||
*/
|
||||
export function parseEnvFile(content: string): Record<string, string> {
|
||||
const result: Record<string, string> = {};
|
||||
const lines = content.split("\n");
|
||||
let currentKey = "";
|
||||
let currentValue = "";
|
||||
let inMultiline = false;
|
||||
let quoteChar = "";
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
|
||||
// Handle multiline values
|
||||
if (inMultiline) {
|
||||
if (line.endsWith(quoteChar)) {
|
||||
currentValue += `\n${line.slice(0, -1)}`;
|
||||
result[currentKey] = currentValue;
|
||||
inMultiline = false;
|
||||
currentKey = "";
|
||||
currentValue = "";
|
||||
} else {
|
||||
currentValue += `\n${line}`;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip empty lines and comments
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || trimmed.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Parse KEY=VALUE
|
||||
const eqIndex = trimmed.indexOf("=");
|
||||
if (eqIndex === -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const key = trimmed.slice(0, eqIndex).trim();
|
||||
let value = trimmed.slice(eqIndex + 1).trim();
|
||||
|
||||
// Handle quoted values
|
||||
if (value.startsWith('"') || value.startsWith("'")) {
|
||||
quoteChar = value[0];
|
||||
value = value.slice(1);
|
||||
|
||||
// Check if it's a multiline value
|
||||
if (!value.endsWith(quoteChar)) {
|
||||
currentKey = key;
|
||||
currentValue = value;
|
||||
inMultiline = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Single-line quoted value
|
||||
value = value.slice(0, -1);
|
||||
}
|
||||
|
||||
// Unescape common escape sequences
|
||||
value = value
|
||||
.replace(/\\n/g, "\n")
|
||||
.replace(/\\r/g, "\r")
|
||||
.replace(/\\t/g, "\t")
|
||||
.replace(/\\"/g, '"')
|
||||
.replace(/\\'/g, "'");
|
||||
|
||||
result[key] = value;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load .env file and set variables in process.env
|
||||
* Only sets variables that are not already defined in process.env
|
||||
* (environment variables take precedence)
|
||||
*/
|
||||
export function loadEnvFile(envPath?: string): void {
|
||||
const paths: string[] = [];
|
||||
|
||||
if (envPath) {
|
||||
paths.push(envPath);
|
||||
} else {
|
||||
// Try current working directory
|
||||
paths.push(join(process.cwd(), ".env"));
|
||||
paths.push(join(process.cwd(), "packages/research-agent/.env"));
|
||||
|
||||
// Try relative to this script's location
|
||||
try {
|
||||
const currentFile = fileURLToPath(import.meta.url);
|
||||
const scriptDir = dirname(currentFile);
|
||||
|
||||
// Go up from dist/utils to research-agent root
|
||||
const researchAgentDir = dirname(scriptDir);
|
||||
paths.push(join(researchAgentDir, ".env"));
|
||||
|
||||
// Go up to project root (pi-research)
|
||||
const projectRoot = dirname(researchAgentDir);
|
||||
paths.push(join(projectRoot, ".env"));
|
||||
} catch {
|
||||
// Ignore errors if import.meta.url is not available
|
||||
}
|
||||
}
|
||||
|
||||
for (const path of paths) {
|
||||
if (!existsSync(path)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const content = readFileSync(path, "utf-8");
|
||||
const vars = parseEnvFile(content);
|
||||
|
||||
for (const [key, value] of Object.entries(vars)) {
|
||||
// Don't override existing environment variables
|
||||
if (process.env[key] === undefined) {
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Successfully loaded from first found file
|
||||
return;
|
||||
} catch (error) {
|
||||
console.warn(`Warning: Failed to load .env file from ${path}:`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import { fetch } from "undici";
|
||||
|
||||
const USER_AGENT =
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";
|
||||
|
||||
const ALTERNATIVE_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0";
|
||||
|
||||
export async function fetchAndExtract(url: string, maxLength: number, useAlternativeUA = false): Promise<string> {
|
||||
const userAgent = useAlternativeUA ? ALTERNATIVE_USER_AGENT : USER_AGENT;
|
||||
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
"User-Agent": userAgent,
|
||||
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||
"Accept-Language": "en-US,en;q=0.9",
|
||||
},
|
||||
signal: AbortSignal.timeout(30000),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const contentType = response.headers.get("content-type") ?? "";
|
||||
const html = await response.text();
|
||||
|
||||
let text: string;
|
||||
if (contentType.includes("text/html")) {
|
||||
text = extractTextFromHtml(html);
|
||||
} else {
|
||||
text = html;
|
||||
}
|
||||
|
||||
text = collapseWhitespace(text);
|
||||
|
||||
if (text.length > maxLength) {
|
||||
text = `${text.slice(0, maxLength)}\n\n[... content truncated ...]`;
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
function extractTextFromHtml(html: string): string {
|
||||
let text = html;
|
||||
|
||||
text = text.replace(/<script[\s\S]*?<\/script>/gi, "");
|
||||
text = text.replace(/<style[\s\S]*?<\/style>/gi, "");
|
||||
text = text.replace(/<nav[\s\S]*?<\/nav>/gi, "");
|
||||
text = text.replace(/<footer[\s\S]*?<\/footer>/gi, "");
|
||||
text = text.replace(/<header[\s\S]*?<\/header>/gi, "");
|
||||
|
||||
const articleMatch = text.match(/<article[^>]*>([\s\S]*?)<\/article>/i);
|
||||
const mainMatch = text.match(/<main[^>]*>([\s\S]*?)<\/main>/i);
|
||||
|
||||
if (articleMatch) {
|
||||
text = articleMatch[1];
|
||||
} else if (mainMatch) {
|
||||
text = mainMatch[1];
|
||||
}
|
||||
|
||||
text = text.replace(/<[^>]+>/g, " ");
|
||||
|
||||
text = decodeHtmlEntities(text);
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
function decodeHtmlEntities(text: string): string {
|
||||
return text
|
||||
.replace(/ /g, " ")
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, "'")
|
||||
.replace(/&#(\d+);/g, (_, code) => String.fromCharCode(Number.parseInt(code, 10)));
|
||||
}
|
||||
|
||||
function collapseWhitespace(text: string): string {
|
||||
return text
|
||||
.split("\n")
|
||||
.map((line) => line.replace(/\s+/g, " ").trim())
|
||||
.filter((line) => line.length > 0)
|
||||
.join("\n");
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import type { ResearchNote, SearchResult } from "../types.ts";
|
||||
|
||||
export function formatSearchResults(results: SearchResult[]): string {
|
||||
if (results.length === 0) {
|
||||
return "No results found.";
|
||||
}
|
||||
|
||||
const lines: string[] = [`Found ${results.length} results:\n`];
|
||||
|
||||
for (let i = 0; i < results.length; i++) {
|
||||
const r = results[i];
|
||||
lines.push(`[${i + 1}] ${r.title}`);
|
||||
lines.push(` URL: ${r.url}`);
|
||||
lines.push(` Relevance: ${(r.score * 100).toFixed(0)}%`);
|
||||
lines.push(` Snippet: ${r.content.slice(0, 500)}${r.content.length > 500 ? "..." : ""}`);
|
||||
lines.push("");
|
||||
}
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
export function formatNotes(notes: ResearchNote[]): string {
|
||||
if (notes.length === 0) {
|
||||
return "No notes collected.";
|
||||
}
|
||||
|
||||
const lines: string[] = [`Collected ${notes.length} research notes:\n`];
|
||||
|
||||
for (let i = 0; i < notes.length; i++) {
|
||||
const note = notes[i];
|
||||
lines.push(`--- Note ${i + 1} ---`);
|
||||
lines.push(`Topic: ${note.topic}`);
|
||||
lines.push(`Confidence: ${note.confidence}`);
|
||||
lines.push(`Source: ${note.source_url}`);
|
||||
lines.push(`Finding: ${note.finding}`);
|
||||
lines.push("");
|
||||
}
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { fetch } from "undici";
|
||||
import { TAVILY_API_URL } from "../config.ts";
|
||||
import type { SearchResult, TavilySearchResponse } from "../types.ts";
|
||||
|
||||
export interface TavilySearchOptions {
|
||||
apiKey: string;
|
||||
maxResults?: number;
|
||||
searchDepth?: "basic" | "advanced";
|
||||
includeAnswer?: boolean;
|
||||
}
|
||||
|
||||
export async function tavilySearch(query: string, options: TavilySearchOptions): Promise<TavilySearchResponse> {
|
||||
const response = await fetch(TAVILY_API_URL, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
api_key: options.apiKey,
|
||||
query,
|
||||
max_results: options.maxResults ?? 10,
|
||||
search_depth: options.searchDepth ?? "basic",
|
||||
include_answer: options.includeAnswer ?? false,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(`Tavily API error (${response.status}): ${errorText}`);
|
||||
}
|
||||
|
||||
const data = (await response.json()) as {
|
||||
query: string;
|
||||
results: Array<{
|
||||
title: string;
|
||||
url: string;
|
||||
content: string;
|
||||
score: number;
|
||||
}>;
|
||||
answer?: string;
|
||||
};
|
||||
|
||||
return {
|
||||
query: data.query,
|
||||
results: data.results.map(
|
||||
(r): SearchResult => ({
|
||||
title: r.title,
|
||||
url: r.url,
|
||||
content: r.content,
|
||||
score: r.score,
|
||||
}),
|
||||
),
|
||||
answer: data.answer,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"paths": {
|
||||
"@earendil-works/pi-ai": ["../ai/dist/index.d.ts"],
|
||||
"@earendil-works/pi-ai/*": ["../ai/dist/*.d.ts", "../ai/dist/providers/*.d.ts"],
|
||||
"@earendil-works/pi-agent-core": ["../agent/dist/index.d.ts"],
|
||||
"@earendil-works/pi-tui": ["../tui/dist/index.d.ts"]
|
||||
},
|
||||
"rootDir": "./src"
|
||||
},
|
||||
"include": ["src/**/*.ts"],
|
||||
"exclude": ["node_modules", "dist", "**/*.d.ts", "src/**/*.d.ts"]
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { homedir, tmpdir } from "node:os";
|
||||
import { join, resolve } from "node:path";
|
||||
import { spawn } from "node:child_process";
|
||||
import { openBrowser } from "../packages/coding-agent/src/utils/open-browser.ts";
|
||||
|
||||
interface TextContent { type: "text"; text: string }
|
||||
interface ImageContent { type: "image"; data: string; mimeType?: string }
|
||||
@@ -229,4 +229,4 @@ const html = `<!doctype html>
|
||||
mkdirSync(resolve(output, ".."), { recursive: true });
|
||||
writeFileSync(output, html);
|
||||
console.log(`Wrote ${output}`);
|
||||
spawn(process.platform === "darwin" ? "open" : process.platform === "win32" ? "cmd" : "xdg-open", process.platform === "win32" ? ["/c", "start", output] : [output], { detached: true, stdio: "ignore" }).unref();
|
||||
openBrowser(output);
|
||||
|
||||
Reference in New Issue
Block a user