runling 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +63 -0
- package/bin/runling.js +26 -0
- package/bin/workflow-loader.js +10 -0
- package/dist/extensions/web-fetch.d.ts +8 -0
- package/dist/extensions/web-fetch.js +245 -0
- package/dist/src/agent.d.ts +78 -0
- package/dist/src/agent.js +450 -0
- package/dist/src/ansi.d.ts +1 -0
- package/dist/src/ansi.js +9 -0
- package/dist/src/cli.d.ts +8 -0
- package/dist/src/cli.js +46 -0
- package/dist/src/config-reloader.d.ts +20 -0
- package/dist/src/config-reloader.js +110 -0
- package/dist/src/events.d.ts +82 -0
- package/dist/src/events.js +21 -0
- package/dist/src/git.d.ts +9 -0
- package/dist/src/git.js +82 -0
- package/dist/src/id.d.ts +1 -0
- package/dist/src/id.js +7 -0
- package/dist/src/index.d.ts +15 -0
- package/dist/src/index.js +14 -0
- package/dist/src/input.d.ts +14 -0
- package/dist/src/input.js +49 -0
- package/dist/src/log.d.ts +35 -0
- package/dist/src/log.js +92 -0
- package/dist/src/markdown.d.ts +2 -0
- package/dist/src/markdown.js +32 -0
- package/dist/src/model.d.ts +5 -0
- package/dist/src/model.js +10 -0
- package/dist/src/paths.d.ts +10 -0
- package/dist/src/paths.js +18 -0
- package/dist/src/runner.d.ts +43 -0
- package/dist/src/runner.js +200 -0
- package/dist/src/runtime.d.ts +62 -0
- package/dist/src/runtime.js +44 -0
- package/dist/src/schema.d.ts +3 -0
- package/dist/src/schema.js +17 -0
- package/dist/src/shell.d.ts +47 -0
- package/dist/src/shell.js +170 -0
- package/dist/src/step.d.ts +5 -0
- package/dist/src/step.js +39 -0
- package/dist/src/system-prompt.d.ts +2 -0
- package/dist/src/system-prompt.js +23 -0
- package/dist/src/text.d.ts +2 -0
- package/dist/src/text.js +6 -0
- package/dist/src/tui.d.ts +46 -0
- package/dist/src/tui.js +477 -0
- package/dist/src/usage.d.ts +35 -0
- package/dist/src/usage.js +90 -0
- package/dist/src/utils.d.ts +2 -0
- package/dist/src/utils.js +16 -0
- package/dist/src/web-config.d.ts +16 -0
- package/dist/src/web-config.js +22 -0
- package/dist/src/web-server.d.ts +8 -0
- package/dist/src/web-server.js +91 -0
- package/dist/src/workflow.d.ts +11 -0
- package/dist/src/workflow.js +39 -0
- package/dist/web/client/_app/immutable/assets/0.DkxYLsNr.css +1 -0
- package/dist/web/client/_app/immutable/assets/0.DkxYLsNr.css.br +0 -0
- package/dist/web/client/_app/immutable/assets/0.DkxYLsNr.css.gz +0 -0
- package/dist/web/client/_app/immutable/assets/2.D0D6RatL.css +1 -0
- package/dist/web/client/_app/immutable/assets/2.D0D6RatL.css.br +0 -0
- package/dist/web/client/_app/immutable/assets/2.D0D6RatL.css.gz +0 -0
- package/dist/web/client/_app/immutable/assets/plus-jakarta-sans-latin-ext-wght-italic.DJWiFoht.woff2 +0 -0
- package/dist/web/client/_app/immutable/assets/plus-jakarta-sans-latin-ext-wght-normal.DmpS2jIq.woff2 +0 -0
- package/dist/web/client/_app/immutable/assets/plus-jakarta-sans-latin-wght-italic.DnD1KgkH.woff2 +0 -0
- package/dist/web/client/_app/immutable/assets/plus-jakarta-sans-latin-wght-normal.eXO_dkmS.woff2 +0 -0
- package/dist/web/client/_app/immutable/assets/plus-jakarta-sans-vietnamese-wght-italic.CPBsCcxN.woff2 +0 -0
- package/dist/web/client/_app/immutable/assets/plus-jakarta-sans-vietnamese-wght-normal.qRpaaN48.woff2 +0 -0
- package/dist/web/client/_app/immutable/chunks/DVu8M-Ny.js +1 -0
- package/dist/web/client/_app/immutable/chunks/DVu8M-Ny.js.br +0 -0
- package/dist/web/client/_app/immutable/chunks/DVu8M-Ny.js.gz +0 -0
- package/dist/web/client/_app/immutable/chunks/DZ8VO57n.js +2 -0
- package/dist/web/client/_app/immutable/chunks/DZ8VO57n.js.br +0 -0
- package/dist/web/client/_app/immutable/chunks/DZ8VO57n.js.gz +0 -0
- package/dist/web/client/_app/immutable/chunks/DcSF4jBT.js +2 -0
- package/dist/web/client/_app/immutable/chunks/DcSF4jBT.js.br +0 -0
- package/dist/web/client/_app/immutable/chunks/DcSF4jBT.js.gz +0 -0
- package/dist/web/client/_app/immutable/chunks/DhqrxzQW.js +1 -0
- package/dist/web/client/_app/immutable/chunks/DhqrxzQW.js.br +0 -0
- package/dist/web/client/_app/immutable/chunks/DhqrxzQW.js.gz +0 -0
- package/dist/web/client/_app/immutable/chunks/DqKc9n9r.js +1 -0
- package/dist/web/client/_app/immutable/chunks/DqKc9n9r.js.br +0 -0
- package/dist/web/client/_app/immutable/chunks/DqKc9n9r.js.gz +0 -0
- package/dist/web/client/_app/immutable/chunks/EYhLy6Dm.js +1 -0
- package/dist/web/client/_app/immutable/chunks/EYhLy6Dm.js.br +0 -0
- package/dist/web/client/_app/immutable/chunks/EYhLy6Dm.js.gz +0 -0
- package/dist/web/client/_app/immutable/chunks/Jt-Bo-2J.js +1 -0
- package/dist/web/client/_app/immutable/chunks/Jt-Bo-2J.js.br +0 -0
- package/dist/web/client/_app/immutable/chunks/Jt-Bo-2J.js.gz +0 -0
- package/dist/web/client/_app/immutable/chunks/N_quh2N-.js +1 -0
- package/dist/web/client/_app/immutable/chunks/N_quh2N-.js.br +0 -0
- package/dist/web/client/_app/immutable/chunks/N_quh2N-.js.gz +0 -0
- package/dist/web/client/_app/immutable/chunks/wbPk3Yxo.js +81 -0
- package/dist/web/client/_app/immutable/chunks/wbPk3Yxo.js.br +0 -0
- package/dist/web/client/_app/immutable/chunks/wbPk3Yxo.js.gz +0 -0
- package/dist/web/client/_app/immutable/entry/app.YNAAR_x0.js +2 -0
- package/dist/web/client/_app/immutable/entry/app.YNAAR_x0.js.br +0 -0
- package/dist/web/client/_app/immutable/entry/app.YNAAR_x0.js.gz +0 -0
- package/dist/web/client/_app/immutable/entry/start.BQmDILhe.js +1 -0
- package/dist/web/client/_app/immutable/entry/start.BQmDILhe.js.br +0 -0
- package/dist/web/client/_app/immutable/entry/start.BQmDILhe.js.gz +0 -0
- package/dist/web/client/_app/immutable/nodes/0.DNWnBVC3.js +1 -0
- package/dist/web/client/_app/immutable/nodes/0.DNWnBVC3.js.br +0 -0
- package/dist/web/client/_app/immutable/nodes/0.DNWnBVC3.js.gz +0 -0
- package/dist/web/client/_app/immutable/nodes/1.CZEgzXeT.js +1 -0
- package/dist/web/client/_app/immutable/nodes/1.CZEgzXeT.js.br +0 -0
- package/dist/web/client/_app/immutable/nodes/1.CZEgzXeT.js.gz +0 -0
- package/dist/web/client/_app/immutable/nodes/2.BSE8F3Ef.js +19 -0
- package/dist/web/client/_app/immutable/nodes/2.BSE8F3Ef.js.br +0 -0
- package/dist/web/client/_app/immutable/nodes/2.BSE8F3Ef.js.gz +0 -0
- package/dist/web/client/_app/version.json +1 -0
- package/dist/web/client/_app/version.json.br +0 -0
- package/dist/web/client/_app/version.json.gz +0 -0
- package/dist/web/env.js +70 -0
- package/dist/web/env.js.map +1 -0
- package/dist/web/handler.js +23 -0
- package/dist/web/handler.js.map +1 -0
- package/dist/web/index.js +146 -0
- package/dist/web/index.js.map +1 -0
- package/dist/web/server/chunks/chunks/exports.js-BZBK1HC9.js +161 -0
- package/dist/web/server/chunks/chunks/exports.js-BZBK1HC9.js.map +1 -0
- package/dist/web/server/chunks/chunks/internal.js-pcXelyRW.js +126 -0
- package/dist/web/server/chunks/chunks/internal.js-pcXelyRW.js.map +1 -0
- package/dist/web/server/chunks/chunks/root.js-HNgH7zyW.js +4349 -0
- package/dist/web/server/chunks/chunks/root.js-HNgH7zyW.js.map +1 -0
- package/dist/web/server/chunks/chunks/run-store.js-Dgbf5-M9.js +174 -0
- package/dist/web/server/chunks/chunks/run-store.js-Dgbf5-M9.js.map +1 -0
- package/dist/web/server/chunks/chunks/runs.js-vPPJr_ta.js +20 -0
- package/dist/web/server/chunks/chunks/runs.js-vPPJr_ta.js.map +1 -0
- package/dist/web/server/chunks/chunks/server.js-Bf8x1V_n.js +16 -0
- package/dist/web/server/chunks/chunks/server.js-Bf8x1V_n.js.map +1 -0
- package/dist/web/server/chunks/chunks/state.svelte.js-lAA5-aPf.js +12 -0
- package/dist/web/server/chunks/chunks/state.svelte.js-lAA5-aPf.js.map +1 -0
- package/dist/web/server/chunks/chunks/stream.js-CHB18nO5.js +51 -0
- package/dist/web/server/chunks/chunks/stream.js-CHB18nO5.js.map +1 -0
- package/dist/web/server/chunks/chunks/uneval.js-5Y6J9rDU.js +530 -0
- package/dist/web/server/chunks/chunks/uneval.js-5Y6J9rDU.js.map +1 -0
- package/dist/web/server/chunks/chunks/utils.js-BQzn9ikS.js +38 -0
- package/dist/web/server/chunks/chunks/utils.js-BQzn9ikS.js.map +1 -0
- package/dist/web/server/chunks/chunks/utils2.js-CPBdgO4h.js +1627 -0
- package/dist/web/server/chunks/chunks/utils2.js-CPBdgO4h.js.map +1 -0
- package/dist/web/server/chunks/chunks/web-config.js-CAwLL_g4.js +12 -0
- package/dist/web/server/chunks/chunks/web-config.js-CAwLL_g4.js.map +1 -0
- package/dist/web/server/chunks/chunks/webhook.js-nEb5r1eG.js +62 -0
- package/dist/web/server/chunks/chunks/webhook.js-nEb5r1eG.js.map +1 -0
- package/dist/web/server/chunks/entries/endpoints/api/config/events/_server.ts.js-DwgI16MG.js +20 -0
- package/dist/web/server/chunks/entries/endpoints/api/config/events/_server.ts.js-DwgI16MG.js.map +1 -0
- package/dist/web/server/chunks/entries/endpoints/api/runs/_id_/_server.ts.js-BSKmJv3K.js +14 -0
- package/dist/web/server/chunks/entries/endpoints/api/runs/_id_/_server.ts.js-BSKmJv3K.js.map +1 -0
- package/dist/web/server/chunks/entries/endpoints/api/runs/_id_/events/_server.ts.js-CWWee51l.js +23 -0
- package/dist/web/server/chunks/entries/endpoints/api/runs/_id_/events/_server.ts.js-CWWee51l.js.map +1 -0
- package/dist/web/server/chunks/entries/endpoints/api/runs/events/_server.ts.js-CY2mz9bH.js +28 -0
- package/dist/web/server/chunks/entries/endpoints/api/runs/events/_server.ts.js-CY2mz9bH.js.map +1 -0
- package/dist/web/server/chunks/entries/endpoints/api/runs/start/_name_/_server.ts.js-CD4RKiK6.js +30 -0
- package/dist/web/server/chunks/entries/endpoints/api/runs/start/_name_/_server.ts.js-CD4RKiK6.js.map +1 -0
- package/dist/web/server/chunks/entries/endpoints/api/webhooks/_name_/_server.ts.js-Dgh42cDJ.js +28 -0
- package/dist/web/server/chunks/entries/endpoints/api/webhooks/_name_/_server.ts.js-Dgh42cDJ.js.map +1 -0
- package/dist/web/server/chunks/entries/fallbacks/error.svelte.js-LEaHcOM2.js +27 -0
- package/dist/web/server/chunks/entries/fallbacks/error.svelte.js-LEaHcOM2.js.map +1 -0
- package/dist/web/server/chunks/entries/pages/_layout.svelte.js-D_wzPsk8.js +8 -0
- package/dist/web/server/chunks/entries/pages/_layout.svelte.js-D_wzPsk8.js.map +1 -0
- package/dist/web/server/chunks/entries/pages/_page.server.ts.js-oVQ0hBaN.js +27 -0
- package/dist/web/server/chunks/entries/pages/_page.server.ts.js-oVQ0hBaN.js.map +1 -0
- package/dist/web/server/chunks/entries/pages/_page.svelte.js-B7fSvh9U.js +141 -0
- package/dist/web/server/chunks/entries/pages/_page.svelte.js-B7fSvh9U.js.map +1 -0
- package/dist/web/server/chunks/handler-BpKA6eGq.js +1734 -0
- package/dist/web/server/chunks/handler-BpKA6eGq.js.map +1 -0
- package/dist/web/server/chunks/index.js-CVS44OGr.js +4157 -0
- package/dist/web/server/chunks/index.js-CVS44OGr.js.map +1 -0
- package/dist/web/server/chunks/manifest.js-CnB5rB3W.js +84 -0
- package/dist/web/server/chunks/manifest.js-CnB5rB3W.js.map +1 -0
- package/dist/web/server/chunks/nodes/0.js-BqA14fLU.js +9 -0
- package/dist/web/server/chunks/nodes/0.js-BqA14fLU.js.map +1 -0
- package/dist/web/server/chunks/nodes/1.js-DzvhRJCS.js +9 -0
- package/dist/web/server/chunks/nodes/1.js-DzvhRJCS.js.map +1 -0
- package/dist/web/server/chunks/nodes/2.js-DmujW_AW.js +20 -0
- package/dist/web/server/chunks/nodes/2.js-DmujW_AW.js.map +1 -0
- package/dist/web/shims.js +33 -0
- package/dist/web/shims.js.map +1 -0
- package/package.json +69 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hendrik Mans and contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Runling
|
|
2
|
+
|
|
3
|
+
Write TypeScript workflows for coding agents. Run them from the command line
|
|
4
|
+
or use the included web console to trigger runs, inspect activity, and track
|
|
5
|
+
token use and estimated cost.
|
|
6
|
+
|
|
7
|
+
Requires Node.js 22.18 or later. The package includes the TypeScript loader
|
|
8
|
+
and web UI; consumer projects do not need Bun, Vite, or SvelteKit.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
After the first release is published:
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm install runling
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Add `"runling": "runling"` to the `scripts` in your `package.json`.
|
|
19
|
+
|
|
20
|
+
## Write a workflow
|
|
21
|
+
|
|
22
|
+
Create `workflows/echo.ts`:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { Type, workflow } from "runling";
|
|
26
|
+
|
|
27
|
+
export default workflow(
|
|
28
|
+
{ name: "Echo", input: Type.String(), output: Type.String() },
|
|
29
|
+
(r, input) => r.step("Echo input", () => input),
|
|
30
|
+
);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Run it with `npm run runling -- workflows/echo.ts "hello"`.
|
|
34
|
+
|
|
35
|
+
## Start the web console
|
|
36
|
+
|
|
37
|
+
Create `runling.config.ts` in the project root:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { defineWebConfig } from "runling/web";
|
|
41
|
+
import echo from "./workflows/echo.ts";
|
|
42
|
+
|
|
43
|
+
export default defineWebConfig({ webhooks: { echo: { workflow: echo } } });
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Run `npm run runling`, then open `http://localhost:5173`.
|
|
47
|
+
Use the console to start a run or send a request:
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
curl http://localhost:5173/api/webhooks/echo \
|
|
51
|
+
-H 'content-type: application/json' -d '"hello"'
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Keep the console local: it does not provide authentication. Workflows can
|
|
55
|
+
execute commands and modify files with the server's permissions. Configure
|
|
56
|
+
model credentials before running workflows that use agents.
|
|
57
|
+
|
|
58
|
+
See the [repository documentation](https://github.com/hmans/runling#readme)
|
|
59
|
+
for agent sessions, configuration, and workflow examples.
|
|
60
|
+
|
|
61
|
+
## License
|
|
62
|
+
|
|
63
|
+
MIT. See [LICENSE](LICENSE).
|
package/bin/runling.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import "tsx/esm";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { registerHooks } from "node:module";
|
|
5
|
+
import { resolve } from "./workflow-loader.js";
|
|
6
|
+
|
|
7
|
+
registerHooks({ resolve });
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
if (existsSync(".env")) process.loadEnvFile(".env");
|
|
11
|
+
const args = process.argv.slice(2);
|
|
12
|
+
if (
|
|
13
|
+
args.length === 0 ||
|
|
14
|
+
args[0] === "web" ||
|
|
15
|
+
["--config", "--host", "--port", "--open", "--help", "-h"].includes(args[0].split("=")[0])
|
|
16
|
+
) {
|
|
17
|
+
const { runRunlingWeb } = await import("../dist/src/web-server.js");
|
|
18
|
+
await runRunlingWeb(args[0] === "web" ? args.slice(1) : args);
|
|
19
|
+
} else {
|
|
20
|
+
const { runRunling } = await import("../dist/src/runner.js");
|
|
21
|
+
await runRunling(args);
|
|
22
|
+
}
|
|
23
|
+
} catch (error) {
|
|
24
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
25
|
+
process.exitCode = 1;
|
|
26
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Workflow/config TypeScript uses ESM imports even in a CommonJS project.
|
|
2
|
+
// Let tsx perform the transform; do not transform installed dependencies.
|
|
3
|
+
export function resolve(specifier, context, nextResolve) {
|
|
4
|
+
const result = nextResolve(specifier, context);
|
|
5
|
+
const url = new URL(result.url);
|
|
6
|
+
if (url.protocol === "file:" && /\.(?:ts|tsx|mts)$/.test(url.pathname) && !url.pathname.includes("/node_modules/")) {
|
|
7
|
+
return { ...result, format: "module" };
|
|
8
|
+
}
|
|
9
|
+
return result;
|
|
10
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
interface WebFetchDependencies {
|
|
3
|
+
fetch(input: URL, init: RequestInit): Promise<Response>;
|
|
4
|
+
resolveAddresses(hostname: string): Promise<readonly string[]>;
|
|
5
|
+
}
|
|
6
|
+
export declare function createWebFetchExtension(dependencies?: WebFetchDependencies): (pi: ExtensionAPI) => void;
|
|
7
|
+
declare const webFetchExtension: (pi: ExtensionAPI) => void;
|
|
8
|
+
export default webFetchExtension;
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { lookup } from "node:dns/promises";
|
|
2
|
+
import { isIP } from "node:net";
|
|
3
|
+
import { Type } from "typebox";
|
|
4
|
+
const MAX_RESPONSE_BYTES = 100_000;
|
|
5
|
+
const REQUEST_TIMEOUT_MS = 30_000;
|
|
6
|
+
const MAX_REDIRECTS = 5;
|
|
7
|
+
const parameters = Type.Object({
|
|
8
|
+
url: Type.String({
|
|
9
|
+
description: "The absolute HTTP or HTTPS URL to fetch",
|
|
10
|
+
minLength: 1,
|
|
11
|
+
}),
|
|
12
|
+
});
|
|
13
|
+
const defaultDependencies = {
|
|
14
|
+
fetch: (input, init) => globalThis.fetch(input, init),
|
|
15
|
+
async resolveAddresses(hostname) {
|
|
16
|
+
if (isIP(hostname) !== 0)
|
|
17
|
+
return [hostname];
|
|
18
|
+
return (await lookup(hostname, { all: true, verbatim: true })).map(({ address }) => address);
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
export function createWebFetchExtension(dependencies = defaultDependencies) {
|
|
22
|
+
return function webFetchExtension(pi) {
|
|
23
|
+
pi.registerTool({
|
|
24
|
+
name: "web_fetch",
|
|
25
|
+
label: "Fetch URL",
|
|
26
|
+
description: "Fetch textual content from an HTTP or HTTPS URL. Returns the final URL, HTTP status, content type, and a size-limited response body.",
|
|
27
|
+
promptSnippet: "Fetch textual content from an HTTP or HTTPS URL",
|
|
28
|
+
parameters,
|
|
29
|
+
async execute(_toolCallId, { url }, signal) {
|
|
30
|
+
const parsedUrl = new URL(url);
|
|
31
|
+
if (parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:") {
|
|
32
|
+
throw new Error("web_fetch only supports HTTP and HTTPS URLs");
|
|
33
|
+
}
|
|
34
|
+
if (parsedUrl.username !== "" || parsedUrl.password !== "") {
|
|
35
|
+
throw new Error("web_fetch does not accept credentials in URLs");
|
|
36
|
+
}
|
|
37
|
+
const timeoutSignal = AbortSignal.timeout(REQUEST_TIMEOUT_MS);
|
|
38
|
+
const requestSignal = signal
|
|
39
|
+
? AbortSignal.any([signal, timeoutSignal])
|
|
40
|
+
: timeoutSignal;
|
|
41
|
+
const response = await fetchPublicUrl(parsedUrl, requestSignal, dependencies);
|
|
42
|
+
const contentType = response.headers.get("content-type") ?? "unknown";
|
|
43
|
+
if (!isTextualContentType(contentType)) {
|
|
44
|
+
await response.body?.cancel();
|
|
45
|
+
throw new Error(`web_fetch cannot return content type ${contentType}`);
|
|
46
|
+
}
|
|
47
|
+
const { text, truncated } = await readLimitedText(response.body, MAX_RESPONSE_BYTES);
|
|
48
|
+
const metadata = [
|
|
49
|
+
`URL: ${response.url}`,
|
|
50
|
+
`Status: ${response.status} ${response.statusText}`,
|
|
51
|
+
`Content-Type: ${contentType}`,
|
|
52
|
+
];
|
|
53
|
+
return {
|
|
54
|
+
content: [
|
|
55
|
+
{
|
|
56
|
+
type: "text",
|
|
57
|
+
text: [
|
|
58
|
+
...metadata,
|
|
59
|
+
"",
|
|
60
|
+
text,
|
|
61
|
+
...(truncated
|
|
62
|
+
? [`\n[Response truncated after ${MAX_RESPONSE_BYTES} bytes]`]
|
|
63
|
+
: []),
|
|
64
|
+
].join("\n"),
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
details: {
|
|
68
|
+
url: response.url,
|
|
69
|
+
status: response.status,
|
|
70
|
+
contentType,
|
|
71
|
+
truncated,
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
const webFetchExtension = createWebFetchExtension();
|
|
79
|
+
export default webFetchExtension;
|
|
80
|
+
async function fetchPublicUrl(initialUrl, signal, dependencies) {
|
|
81
|
+
let url = initialUrl;
|
|
82
|
+
for (let redirects = 0; redirects <= MAX_REDIRECTS; redirects++) {
|
|
83
|
+
await assertPublicDestination(url, dependencies.resolveAddresses);
|
|
84
|
+
const response = await dependencies.fetch(url, {
|
|
85
|
+
headers: {
|
|
86
|
+
accept: "text/plain, text/html, text/markdown, application/json, application/xml;q=0.9, text/xml;q=0.9",
|
|
87
|
+
"user-agent": "runling-web-fetch/1.0",
|
|
88
|
+
},
|
|
89
|
+
redirect: "manual",
|
|
90
|
+
signal,
|
|
91
|
+
});
|
|
92
|
+
if (!isRedirect(response.status))
|
|
93
|
+
return response;
|
|
94
|
+
const location = response.headers.get("location");
|
|
95
|
+
if (location === null)
|
|
96
|
+
return response;
|
|
97
|
+
await response.body?.cancel();
|
|
98
|
+
if (redirects === MAX_REDIRECTS) {
|
|
99
|
+
throw new Error(`web_fetch stopped after ${MAX_REDIRECTS} redirects`);
|
|
100
|
+
}
|
|
101
|
+
url = new URL(location, url);
|
|
102
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
103
|
+
throw new Error("web_fetch redirects must use HTTP or HTTPS");
|
|
104
|
+
}
|
|
105
|
+
if (url.username !== "" || url.password !== "") {
|
|
106
|
+
throw new Error("web_fetch redirects cannot contain credentials");
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
throw new Error("web_fetch redirect limit exceeded");
|
|
110
|
+
}
|
|
111
|
+
function isRedirect(status) {
|
|
112
|
+
return [301, 302, 303, 307, 308].includes(status);
|
|
113
|
+
}
|
|
114
|
+
async function assertPublicDestination(url, resolveAddresses) {
|
|
115
|
+
const hostname = url.hostname.replace(/^\[|\]$/g, "");
|
|
116
|
+
const addresses = await resolveAddresses(hostname);
|
|
117
|
+
if (addresses.length === 0) {
|
|
118
|
+
throw new Error(`web_fetch could not resolve ${url.hostname}`);
|
|
119
|
+
}
|
|
120
|
+
const blockedAddress = addresses.find((address) => !isPublicIpAddress(address));
|
|
121
|
+
if (blockedAddress !== undefined) {
|
|
122
|
+
throw new Error(`web_fetch blocked non-public destination ${url.hostname} (${blockedAddress})`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
function isPublicIpAddress(address) {
|
|
126
|
+
if (isIP(address) === 4)
|
|
127
|
+
return isPublicIpv4(parseIpv4(address));
|
|
128
|
+
if (isIP(address) !== 6)
|
|
129
|
+
return false;
|
|
130
|
+
const bytes = parseIpv6(address);
|
|
131
|
+
if (bytes === undefined)
|
|
132
|
+
return false;
|
|
133
|
+
// Unspecified, loopback, IPv4-compatible, and IPv4-mapped addresses.
|
|
134
|
+
if (bytes.slice(0, 12).every((byte) => byte === 0)) {
|
|
135
|
+
return isPublicIpv4(bytes.slice(12));
|
|
136
|
+
}
|
|
137
|
+
if (bytes.slice(0, 10).every((byte) => byte === 0) &&
|
|
138
|
+
bytes[10] === 0xff &&
|
|
139
|
+
bytes[11] === 0xff) {
|
|
140
|
+
return isPublicIpv4(bytes.slice(12));
|
|
141
|
+
}
|
|
142
|
+
// Only globally routable unicast space is eligible.
|
|
143
|
+
if ((bytes[0] & 0xe0) !== 0x20)
|
|
144
|
+
return false;
|
|
145
|
+
if (bytes[0] === 0x20 &&
|
|
146
|
+
bytes[1] === 0x01 &&
|
|
147
|
+
((bytes[2] === 0x0d && bytes[3] === 0xb8) || bytes[2] <= 0x01)) {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
// 6to4 and NAT64 can otherwise tunnel requests to blocked IPv4 targets.
|
|
151
|
+
if (bytes[0] === 0x20 && bytes[1] === 0x02)
|
|
152
|
+
return false;
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
function isPublicIpv4(bytes) {
|
|
156
|
+
const [a = 0, b = 0, c = 0] = bytes;
|
|
157
|
+
return !(a === 0 ||
|
|
158
|
+
a === 10 ||
|
|
159
|
+
(a === 100 && b >= 64 && b <= 127) ||
|
|
160
|
+
a === 127 ||
|
|
161
|
+
(a === 169 && b === 254) ||
|
|
162
|
+
(a === 172 && b >= 16 && b <= 31) ||
|
|
163
|
+
(a === 192 && b === 0 && (c === 0 || c === 2)) ||
|
|
164
|
+
(a === 192 && b === 88 && c === 99) ||
|
|
165
|
+
(a === 192 && b === 168) ||
|
|
166
|
+
(a === 198 && (b === 18 || b === 19)) ||
|
|
167
|
+
(a === 198 && b === 51 && c === 100) ||
|
|
168
|
+
(a === 203 && b === 0 && c === 113) ||
|
|
169
|
+
a >= 224);
|
|
170
|
+
}
|
|
171
|
+
function parseIpv4(address) {
|
|
172
|
+
return address.split(".").map(Number);
|
|
173
|
+
}
|
|
174
|
+
function parseIpv6(address) {
|
|
175
|
+
const sections = address.toLowerCase().split("::");
|
|
176
|
+
if (sections.length > 2)
|
|
177
|
+
return undefined;
|
|
178
|
+
const parseSection = (section) => {
|
|
179
|
+
if (section === "")
|
|
180
|
+
return [];
|
|
181
|
+
const groups = [];
|
|
182
|
+
for (const part of section.split(":")) {
|
|
183
|
+
if (part.includes(".")) {
|
|
184
|
+
const ipv4 = parseIpv4(part);
|
|
185
|
+
if (ipv4.length !== 4)
|
|
186
|
+
return undefined;
|
|
187
|
+
groups.push((ipv4[0] << 8) | ipv4[1], (ipv4[2] << 8) | ipv4[3]);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
const group = Number.parseInt(part, 16);
|
|
191
|
+
if (!Number.isInteger(group) || group < 0 || group > 0xffff) {
|
|
192
|
+
return undefined;
|
|
193
|
+
}
|
|
194
|
+
groups.push(group);
|
|
195
|
+
}
|
|
196
|
+
return groups;
|
|
197
|
+
};
|
|
198
|
+
const left = parseSection(sections[0]);
|
|
199
|
+
const right = parseSection(sections[1] ?? "");
|
|
200
|
+
if (left === undefined || right === undefined)
|
|
201
|
+
return undefined;
|
|
202
|
+
const missing = 8 - left.length - right.length;
|
|
203
|
+
if ((sections.length === 1 && missing !== 0) || missing < 0)
|
|
204
|
+
return undefined;
|
|
205
|
+
const groups = [...left, ...Array(missing).fill(0), ...right];
|
|
206
|
+
if (groups.length !== 8)
|
|
207
|
+
return undefined;
|
|
208
|
+
return groups.flatMap((group) => [group >> 8, group & 0xff]);
|
|
209
|
+
}
|
|
210
|
+
function isTextualContentType(contentType) {
|
|
211
|
+
const mediaType = contentType.split(";", 1)[0]?.trim().toLowerCase();
|
|
212
|
+
return (mediaType?.startsWith("text/") === true ||
|
|
213
|
+
mediaType?.endsWith("+json") === true ||
|
|
214
|
+
mediaType?.endsWith("+xml") === true ||
|
|
215
|
+
mediaType === "application/json" ||
|
|
216
|
+
mediaType === "application/xml" ||
|
|
217
|
+
mediaType === "application/javascript" ||
|
|
218
|
+
mediaType === "application/x-www-form-urlencoded");
|
|
219
|
+
}
|
|
220
|
+
async function readLimitedText(body, maxBytes) {
|
|
221
|
+
if (body === null)
|
|
222
|
+
return { text: "", truncated: false };
|
|
223
|
+
const reader = body.getReader();
|
|
224
|
+
const decoder = new TextDecoder();
|
|
225
|
+
let bytesRead = 0;
|
|
226
|
+
let text = "";
|
|
227
|
+
try {
|
|
228
|
+
while (true) {
|
|
229
|
+
const { done, value } = await reader.read();
|
|
230
|
+
if (done)
|
|
231
|
+
return { text: text + decoder.decode(), truncated: false };
|
|
232
|
+
const remaining = maxBytes - bytesRead;
|
|
233
|
+
if (value.byteLength > remaining) {
|
|
234
|
+
text += decoder.decode(value.subarray(0, remaining), { stream: true });
|
|
235
|
+
await reader.cancel();
|
|
236
|
+
return { text: text + decoder.decode(), truncated: true };
|
|
237
|
+
}
|
|
238
|
+
bytesRead += value.byteLength;
|
|
239
|
+
text += decoder.decode(value, { stream: true });
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
finally {
|
|
243
|
+
reader.releaseLock();
|
|
244
|
+
}
|
|
245
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { type AgentSessionEvent, type ExtensionAPI, type InlineExtension } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Type, type Static } from "typebox";
|
|
3
|
+
import { type TokenUsage } from "./usage.ts";
|
|
4
|
+
declare const reportSchema: Type.TObject<{
|
|
5
|
+
outcome: Type.TUnion<[Type.TLiteral<"completed">, Type.TLiteral<"blocked">, Type.TLiteral<"failed">]>;
|
|
6
|
+
summary: Type.TString;
|
|
7
|
+
details: Type.TOptional<Type.TString>;
|
|
8
|
+
}>;
|
|
9
|
+
export type AgentReport = Static<typeof reportSchema>;
|
|
10
|
+
/** A report enriched with the token usage of the agent interaction. */
|
|
11
|
+
export type AgentResult = AgentReport & {
|
|
12
|
+
usage: TokenUsage;
|
|
13
|
+
};
|
|
14
|
+
export type CompletedAgentReport = AgentResult & {
|
|
15
|
+
outcome: "completed";
|
|
16
|
+
};
|
|
17
|
+
/** A named or anonymous pi extension instantiated for one agent session. */
|
|
18
|
+
export type AgentExtension = InlineExtension;
|
|
19
|
+
/** The API available while configuring an agent-local extension. */
|
|
20
|
+
export type AgentExtensionAPI = ExtensionAPI;
|
|
21
|
+
/** Preserve contextual typing when declaring a reusable agent extension. */
|
|
22
|
+
export declare function defineAgentExtension(extension: AgentExtension): AgentExtension;
|
|
23
|
+
export interface AgentResourceOptions {
|
|
24
|
+
/** Directory containing global pi configuration and resources. */
|
|
25
|
+
agentDir?: string;
|
|
26
|
+
extensions?: boolean;
|
|
27
|
+
skills?: boolean;
|
|
28
|
+
promptTemplates?: boolean;
|
|
29
|
+
themes?: boolean;
|
|
30
|
+
contextFiles?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Reasoning effort for models that support extended thinking. "xhigh" and
|
|
34
|
+
* "max" are only supported by selected model families.
|
|
35
|
+
*/
|
|
36
|
+
export type ThinkingLevel = "off" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max";
|
|
37
|
+
export interface RunAgentOptions {
|
|
38
|
+
model: string;
|
|
39
|
+
/** Reasoning effort for the model. Defaults to pi's own settings default. */
|
|
40
|
+
thinkingLevel?: ThinkingLevel;
|
|
41
|
+
instructions?: readonly string[];
|
|
42
|
+
cwd?: string;
|
|
43
|
+
/** Built-in and extension tools to expose. `report_outcome` is always added. */
|
|
44
|
+
tools?: readonly string[];
|
|
45
|
+
resources?: AgentResourceOptions;
|
|
46
|
+
/** Extensions instantiated only for this agent session. */
|
|
47
|
+
extensions?: readonly AgentExtension[];
|
|
48
|
+
/** Observe the raw pi event stream for this agent session. */
|
|
49
|
+
onEvent?: (event: AgentSessionEvent) => void;
|
|
50
|
+
/** Abort an active model turn. */
|
|
51
|
+
signal?: AbortSignal;
|
|
52
|
+
}
|
|
53
|
+
export type AgentOptions = Omit<RunAgentOptions, "signal">;
|
|
54
|
+
export interface AgentRunOptions {
|
|
55
|
+
/** Abort this model turn without disposing the agent. */
|
|
56
|
+
signal?: AbortSignal;
|
|
57
|
+
}
|
|
58
|
+
export interface RunlingAgent extends AsyncDisposable {
|
|
59
|
+
/** Human-friendly ID used to prefix this agent's log lines. */
|
|
60
|
+
readonly id: string;
|
|
61
|
+
/** Run one turn and require it to complete successfully. */
|
|
62
|
+
run(prompt: string, options?: AgentRunOptions): Promise<CompletedAgentReport>;
|
|
63
|
+
/** Run one turn and return any reported outcome. */
|
|
64
|
+
runOutcome(prompt: string, options?: AgentRunOptions): Promise<AgentResult>;
|
|
65
|
+
/** Create an independent in-memory agent with a copy of this conversation. */
|
|
66
|
+
fork(): Promise<RunlingAgent>;
|
|
67
|
+
/** Release the underlying in-memory session. */
|
|
68
|
+
dispose(): void;
|
|
69
|
+
}
|
|
70
|
+
export declare class AgentOutcomeError extends Error {
|
|
71
|
+
readonly report: AgentResult;
|
|
72
|
+
readonly name = "AgentOutcomeError";
|
|
73
|
+
constructor(report: AgentResult);
|
|
74
|
+
}
|
|
75
|
+
export declare function describeTool(name: string, args: Record<string, unknown>): string;
|
|
76
|
+
export declare function runAgent(prompt: string, options: RunAgentOptions): Promise<AgentResult>;
|
|
77
|
+
export declare function agent(options: AgentOptions): Promise<RunlingAgent>;
|
|
78
|
+
export {};
|