agentenkit 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +82 -15
- package/dist/adapters/brave.d.ts +21 -0
- package/dist/adapters/brave.d.ts.map +1 -0
- package/dist/adapters/brave.js +52 -0
- package/dist/adapters/brave.js.map +1 -0
- package/dist/adapters/computesdk.d.ts +71 -0
- package/dist/adapters/computesdk.d.ts.map +1 -0
- package/dist/adapters/computesdk.js +163 -0
- package/dist/adapters/computesdk.js.map +1 -0
- package/dist/adapters/docker.d.ts +35 -0
- package/dist/adapters/docker.d.ts.map +1 -0
- package/dist/adapters/docker.js +246 -0
- package/dist/adapters/docker.js.map +1 -0
- package/dist/adapters/e2b.d.ts +44 -0
- package/dist/adapters/e2b.d.ts.map +1 -0
- package/dist/adapters/e2b.js +349 -0
- package/dist/adapters/e2b.js.map +1 -0
- package/dist/adapters/inline.d.ts +19 -5
- package/dist/adapters/inline.d.ts.map +1 -1
- package/dist/adapters/inline.js +87 -14
- package/dist/adapters/inline.js.map +1 -1
- package/dist/adapters/jina.d.ts +34 -0
- package/dist/adapters/jina.d.ts.map +1 -0
- package/dist/adapters/jina.js +99 -0
- package/dist/adapters/jina.js.map +1 -0
- package/dist/adapters/local-sandbox.d.ts +32 -0
- package/dist/adapters/local-sandbox.d.ts.map +1 -0
- package/dist/adapters/local-sandbox.js +257 -0
- package/dist/adapters/local-sandbox.js.map +1 -0
- package/dist/adapters/memory.d.ts +146 -3
- package/dist/adapters/memory.d.ts.map +1 -1
- package/dist/adapters/memory.js +474 -3
- package/dist/adapters/memory.js.map +1 -1
- package/dist/adapters/page-reader.d.ts +39 -0
- package/dist/adapters/page-reader.d.ts.map +1 -0
- package/dist/adapters/page-reader.js +165 -0
- package/dist/adapters/page-reader.js.map +1 -0
- package/dist/adapters/prisma.d.ts +125 -18
- package/dist/adapters/prisma.d.ts.map +1 -1
- package/dist/adapters/prisma.js +308 -67
- package/dist/adapters/prisma.js.map +1 -1
- package/dist/adapters/qstash.d.ts +15 -2
- package/dist/adapters/qstash.d.ts.map +1 -1
- package/dist/adapters/qstash.js +31 -14
- package/dist/adapters/qstash.js.map +1 -1
- package/dist/adapters/redis.d.ts +40 -4
- package/dist/adapters/redis.d.ts.map +1 -1
- package/dist/adapters/redis.js +265 -20
- package/dist/adapters/redis.js.map +1 -1
- package/dist/adapters/sandbox-shell.d.ts +60 -0
- package/dist/adapters/sandbox-shell.d.ts.map +1 -0
- package/dist/adapters/sandbox-shell.js +129 -0
- package/dist/adapters/sandbox-shell.js.map +1 -0
- package/dist/adapters/sqlite.d.ts +88 -2
- package/dist/adapters/sqlite.d.ts.map +1 -1
- package/dist/adapters/sqlite.js +443 -37
- package/dist/adapters/sqlite.js.map +1 -1
- package/dist/adapters/stream-scripts.d.ts +49 -0
- package/dist/adapters/stream-scripts.d.ts.map +1 -0
- package/dist/adapters/stream-scripts.js +150 -0
- package/dist/adapters/stream-scripts.js.map +1 -0
- package/dist/adapters/upstash.d.ts +20 -0
- package/dist/adapters/upstash.d.ts.map +1 -1
- package/dist/adapters/upstash.js +29 -0
- package/dist/adapters/upstash.js.map +1 -1
- package/dist/adapters/web-search-shared.d.ts +11 -0
- package/dist/adapters/web-search-shared.d.ts.map +1 -0
- package/dist/adapters/web-search-shared.js +40 -0
- package/dist/adapters/web-search-shared.js.map +1 -0
- package/dist/admin/memory.d.ts +11 -1
- package/dist/admin/memory.d.ts.map +1 -1
- package/dist/admin/memory.js +83 -14
- package/dist/admin/memory.js.map +1 -1
- package/dist/admin/migrations/postgres/0002-run-settled-at.d.ts +5 -0
- package/dist/admin/migrations/postgres/0002-run-settled-at.d.ts.map +1 -0
- package/dist/admin/migrations/postgres/0002-run-settled-at.js +11 -0
- package/dist/admin/migrations/postgres/0002-run-settled-at.js.map +1 -0
- package/dist/admin/migrations/postgres/0003-run-enqueued-at.d.ts +5 -0
- package/dist/admin/migrations/postgres/0003-run-enqueued-at.d.ts.map +1 -0
- package/dist/admin/migrations/postgres/0003-run-enqueued-at.js +15 -0
- package/dist/admin/migrations/postgres/0003-run-enqueued-at.js.map +1 -0
- package/dist/admin/migrations/postgres/0004-run-settle-claim.d.ts +5 -0
- package/dist/admin/migrations/postgres/0004-run-settle-claim.d.ts.map +1 -0
- package/dist/admin/migrations/postgres/0004-run-settle-claim.js +15 -0
- package/dist/admin/migrations/postgres/0004-run-settle-claim.js.map +1 -0
- package/dist/admin/migrations/postgres/0005-bigint-counters.d.ts +5 -0
- package/dist/admin/migrations/postgres/0005-bigint-counters.d.ts.map +1 -0
- package/dist/admin/migrations/postgres/0005-bigint-counters.js +12 -0
- package/dist/admin/migrations/postgres/0005-bigint-counters.js.map +1 -0
- package/dist/admin/migrations/postgres/index.d.ts.map +1 -1
- package/dist/admin/migrations/postgres/index.js +16 -1
- package/dist/admin/migrations/postgres/index.js.map +1 -1
- package/dist/admin/migrations/runner.d.ts +21 -1
- package/dist/admin/migrations/runner.d.ts.map +1 -1
- package/dist/admin/migrations/runner.js +41 -4
- package/dist/admin/migrations/runner.js.map +1 -1
- package/dist/admin/migrations/sqlite/0002-run-settled-at.d.ts +5 -0
- package/dist/admin/migrations/sqlite/0002-run-settled-at.d.ts.map +1 -0
- package/dist/admin/migrations/sqlite/0002-run-settled-at.js +11 -0
- package/dist/admin/migrations/sqlite/0002-run-settled-at.js.map +1 -0
- package/dist/admin/migrations/sqlite/0003-run-enqueued-at.d.ts +5 -0
- package/dist/admin/migrations/sqlite/0003-run-enqueued-at.d.ts.map +1 -0
- package/dist/admin/migrations/sqlite/0003-run-enqueued-at.js +15 -0
- package/dist/admin/migrations/sqlite/0003-run-enqueued-at.js.map +1 -0
- package/dist/admin/migrations/sqlite/0004-run-settle-claim.d.ts +5 -0
- package/dist/admin/migrations/sqlite/0004-run-settle-claim.d.ts.map +1 -0
- package/dist/admin/migrations/sqlite/0004-run-settle-claim.js +15 -0
- package/dist/admin/migrations/sqlite/0004-run-settle-claim.js.map +1 -0
- package/dist/admin/migrations/sqlite/index.d.ts +2 -2
- package/dist/admin/migrations/sqlite/index.d.ts.map +1 -1
- package/dist/admin/migrations/sqlite/index.js +17 -3
- package/dist/admin/migrations/sqlite/index.js.map +1 -1
- package/dist/admin/postgres.d.ts +14 -4
- package/dist/admin/postgres.d.ts.map +1 -1
- package/dist/admin/postgres.js +131 -36
- package/dist/admin/postgres.js.map +1 -1
- package/dist/admin/shared.d.ts +19 -0
- package/dist/admin/shared.d.ts.map +1 -0
- package/dist/admin/shared.js +28 -0
- package/dist/admin/shared.js.map +1 -0
- package/dist/admin/sqlite.d.ts +9 -1
- package/dist/admin/sqlite.d.ts.map +1 -1
- package/dist/admin/sqlite.js +112 -29
- package/dist/admin/sqlite.js.map +1 -1
- package/dist/core/admin.d.ts +28 -6
- package/dist/core/admin.d.ts.map +1 -1
- package/dist/core/admin.js +92 -23
- package/dist/core/admin.js.map +1 -1
- package/dist/core/agent.d.ts +7 -4
- package/dist/core/agent.d.ts.map +1 -1
- package/dist/core/agent.js +15 -11
- package/dist/core/agent.js.map +1 -1
- package/dist/core/agui.d.ts +17 -0
- package/dist/core/agui.d.ts.map +1 -0
- package/dist/core/agui.js +69 -0
- package/dist/core/agui.js.map +1 -0
- package/dist/core/builtin/definitions.d.ts +12 -0
- package/dist/core/builtin/definitions.d.ts.map +1 -0
- package/dist/core/builtin/definitions.js +149 -0
- package/dist/core/builtin/definitions.js.map +1 -0
- package/dist/core/builtin/html.d.ts +13 -0
- package/dist/core/builtin/html.d.ts.map +1 -0
- package/dist/core/builtin/html.js +96 -0
- package/dist/core/builtin/html.js.map +1 -0
- package/dist/core/builtin/index.d.ts +21 -0
- package/dist/core/builtin/index.d.ts.map +1 -0
- package/dist/core/builtin/index.js +55 -0
- package/dist/core/builtin/index.js.map +1 -0
- package/dist/core/builtin/run.d.ts +37 -0
- package/dist/core/builtin/run.d.ts.map +1 -0
- package/dist/core/builtin/run.js +55 -0
- package/dist/core/builtin/run.js.map +1 -0
- package/dist/core/builtin/sandbox-tools.d.ts +69 -0
- package/dist/core/builtin/sandbox-tools.d.ts.map +1 -0
- package/dist/core/builtin/sandbox-tools.js +389 -0
- package/dist/core/builtin/sandbox-tools.js.map +1 -0
- package/dist/core/builtin/sandbox.d.ts +52 -0
- package/dist/core/builtin/sandbox.d.ts.map +1 -0
- package/dist/core/builtin/sandbox.js +174 -0
- package/dist/core/builtin/sandbox.js.map +1 -0
- package/dist/core/builtin/web.d.ts +54 -0
- package/dist/core/builtin/web.d.ts.map +1 -0
- package/dist/core/builtin/web.js +167 -0
- package/dist/core/builtin/web.js.map +1 -0
- package/dist/core/context.d.ts +13 -1
- package/dist/core/context.d.ts.map +1 -1
- package/dist/core/context.js +70 -19
- package/dist/core/context.js.map +1 -1
- package/dist/core/deleteThread.d.ts.map +1 -1
- package/dist/core/deleteThread.js +36 -4
- package/dist/core/deleteThread.js.map +1 -1
- package/dist/core/engine.d.ts +41 -11
- package/dist/core/engine.d.ts.map +1 -1
- package/dist/core/engine.js +880 -353
- package/dist/core/engine.js.map +1 -1
- package/dist/core/follow.d.ts +63 -1
- package/dist/core/follow.d.ts.map +1 -1
- package/dist/core/follow.js +293 -16
- package/dist/core/follow.js.map +1 -1
- package/dist/core/hitl.d.ts +43 -0
- package/dist/core/hitl.d.ts.map +1 -1
- package/dist/core/hitl.js +163 -35
- package/dist/core/hitl.js.map +1 -1
- package/dist/core/keys.d.ts +22 -4
- package/dist/core/keys.d.ts.map +1 -1
- package/dist/core/keys.js +23 -5
- package/dist/core/keys.js.map +1 -1
- package/dist/core/lease.d.ts +46 -0
- package/dist/core/lease.d.ts.map +1 -0
- package/dist/core/lease.js +142 -0
- package/dist/core/lease.js.map +1 -0
- package/dist/core/loop.d.ts +27 -7
- package/dist/core/loop.d.ts.map +1 -1
- package/dist/core/loop.js +140 -55
- package/dist/core/loop.js.map +1 -1
- package/dist/core/messages.d.ts +21 -2
- package/dist/core/messages.d.ts.map +1 -1
- package/dist/core/messages.js +61 -2
- package/dist/core/messages.js.map +1 -1
- package/dist/core/permanent.d.ts +11 -0
- package/dist/core/permanent.d.ts.map +1 -0
- package/dist/core/permanent.js +37 -0
- package/dist/core/permanent.js.map +1 -0
- package/dist/core/prune.d.ts +24 -0
- package/dist/core/prune.d.ts.map +1 -0
- package/dist/core/prune.js +43 -0
- package/dist/core/prune.js.map +1 -0
- package/dist/core/publish.d.ts +38 -10
- package/dist/core/publish.d.ts.map +1 -1
- package/dist/core/publish.js +126 -12
- package/dist/core/publish.js.map +1 -1
- package/dist/core/reclaim.d.ts +8 -1
- package/dist/core/reclaim.d.ts.map +1 -1
- package/dist/core/reclaim.js +149 -18
- package/dist/core/reclaim.js.map +1 -1
- package/dist/core/run.d.ts +7 -3
- package/dist/core/run.d.ts.map +1 -1
- package/dist/core/run.js +95 -35
- package/dist/core/run.js.map +1 -1
- package/dist/core/segment.d.ts +62 -0
- package/dist/core/segment.d.ts.map +1 -0
- package/dist/core/segment.js +318 -0
- package/dist/core/segment.js.map +1 -0
- package/dist/core/settle.d.ts +57 -0
- package/dist/core/settle.d.ts.map +1 -0
- package/dist/core/settle.js +181 -0
- package/dist/core/settle.js.map +1 -0
- package/dist/core/snapshot.d.ts +36 -0
- package/dist/core/snapshot.d.ts.map +1 -0
- package/dist/core/snapshot.js +87 -0
- package/dist/core/snapshot.js.map +1 -0
- package/dist/core/stop.d.ts +16 -1
- package/dist/core/stop.d.ts.map +1 -1
- package/dist/core/stop.js +69 -15
- package/dist/core/stop.js.map +1 -1
- package/dist/core/stream-events.d.ts +184 -0
- package/dist/core/stream-events.d.ts.map +1 -0
- package/dist/core/stream-events.js +26 -0
- package/dist/core/stream-events.js.map +1 -0
- package/dist/core/stream.d.ts +29 -0
- package/dist/core/stream.d.ts.map +1 -1
- package/dist/core/stream.js +79 -0
- package/dist/core/stream.js.map +1 -1
- package/dist/core/subagent.d.ts +36 -5
- package/dist/core/subagent.d.ts.map +1 -1
- package/dist/core/subagent.js +130 -25
- package/dist/core/subagent.js.map +1 -1
- package/dist/core/tools.d.ts +9 -3
- package/dist/core/tools.d.ts.map +1 -1
- package/dist/core/tools.js.map +1 -1
- package/dist/core/types.d.ts +212 -13
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js +29 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/usage.d.ts +53 -5
- package/dist/core/usage.d.ts.map +1 -1
- package/dist/core/usage.js +170 -47
- package/dist/core/usage.js.map +1 -1
- package/dist/index.d.ts +29 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -7
- package/dist/index.js.map +1 -1
- package/dist/ports/admin.d.ts +59 -0
- package/dist/ports/admin.d.ts.map +1 -1
- package/dist/ports/kv.d.ts +17 -0
- package/dist/ports/kv.d.ts.map +1 -1
- package/dist/ports/queue.d.ts +87 -7
- package/dist/ports/queue.d.ts.map +1 -1
- package/dist/ports/queue.js +36 -1
- package/dist/ports/queue.js.map +1 -1
- package/dist/ports/runtime.d.ts +188 -20
- package/dist/ports/runtime.d.ts.map +1 -1
- package/dist/ports/sandbox.d.ts +159 -0
- package/dist/ports/sandbox.d.ts.map +1 -0
- package/dist/ports/sandbox.js +44 -0
- package/dist/ports/sandbox.js.map +1 -0
- package/dist/ports/storage.d.ts +26 -5
- package/dist/ports/storage.d.ts.map +1 -1
- package/dist/ports/streams.d.ts +60 -0
- package/dist/ports/streams.d.ts.map +1 -0
- package/dist/ports/streams.js +20 -0
- package/dist/ports/streams.js.map +1 -0
- package/dist/ports/tools.d.ts +60 -0
- package/dist/ports/tools.d.ts.map +1 -0
- package/dist/ports/tools.js +5 -0
- package/dist/ports/tools.js.map +1 -0
- package/dist/pricing.d.ts +29 -0
- package/dist/pricing.d.ts.map +1 -1
- package/dist/pricing.js +64 -10
- package/dist/pricing.js.map +1 -1
- package/dist/runtime.d.ts +6 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +147 -46
- package/dist/runtime.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import type { AgentRunState } from '../core/state.js';
|
|
2
|
+
/** The sandbox port: a place apart from the app where an agent's commands run
|
|
3
|
+
* and its files live. A container, a hosted machine, or a folder on this
|
|
4
|
+
* machine in development.
|
|
5
|
+
*
|
|
6
|
+
* The shape and names follow ComputeSDK's Sandbox, so an adapter over it is
|
|
7
|
+
* thin, with three additions: `readFileBytes` (charts and images need it),
|
|
8
|
+
* `setTimeout` (how an idle sandbox ends by itself) and `SandboxInfo.workdir`.
|
|
9
|
+
* The Go runtime has the same port (ports/sandbox.go). */
|
|
10
|
+
/** How long a command may run when the caller does not say. */
|
|
11
|
+
export declare const DEFAULT_COMMAND_TIMEOUT_MS = 120000;
|
|
12
|
+
/** The most output kept from one command, per stream. Past it the rest is
|
|
13
|
+
* dropped and the result says `truncated: true`, so a command that prints
|
|
14
|
+
* without end cannot fill the app's memory. */
|
|
15
|
+
export declare const MAX_COMMAND_OUTPUT_BYTES: number;
|
|
16
|
+
/** The exit code of a command stopped at its time limit, as `timeout(1)`
|
|
17
|
+
* gives it. */
|
|
18
|
+
export declare const TIMED_OUT_EXIT_CODE = 124;
|
|
19
|
+
/** What the sandbox may reach on the network. `none` is the default. */
|
|
20
|
+
export type SandboxNetwork = 'none' | 'all' | {
|
|
21
|
+
allow: string[];
|
|
22
|
+
};
|
|
23
|
+
export interface CreateSandboxOptions {
|
|
24
|
+
/** How long the sandbox lives unless `setTimeout` pushes it back. It then
|
|
25
|
+
* shuts itself down, even when the app that made it is gone. */
|
|
26
|
+
timeoutMs: number;
|
|
27
|
+
/** What the sandbox is for. Adapters keep it as labels or metadata where
|
|
28
|
+
* the provider has them, and a multi-tenant adapter can read `state` to
|
|
29
|
+
* pick a template or key per tenant (§2.10). */
|
|
30
|
+
metadata: {
|
|
31
|
+
threadId: string;
|
|
32
|
+
runId?: string;
|
|
33
|
+
state?: AgentRunState;
|
|
34
|
+
};
|
|
35
|
+
/** Environment variables every command sees. */
|
|
36
|
+
envs?: Record<string, string>;
|
|
37
|
+
/** A provider template (E2B) or image (Docker). Adapters have their own
|
|
38
|
+
* default. */
|
|
39
|
+
template?: string;
|
|
40
|
+
image?: string;
|
|
41
|
+
network?: SandboxNetwork;
|
|
42
|
+
resources?: {
|
|
43
|
+
cpu?: number;
|
|
44
|
+
memoryMiB?: number;
|
|
45
|
+
diskMiB?: number;
|
|
46
|
+
};
|
|
47
|
+
/** Provider-only settings, passed through as they are. */
|
|
48
|
+
extra?: Record<string, unknown>;
|
|
49
|
+
}
|
|
50
|
+
/** Makes sandboxes and finds them again. One per app, passed to
|
|
51
|
+
* `setupAgentCore({ tools: { sandbox } })`. */
|
|
52
|
+
export interface SandboxProvider {
|
|
53
|
+
/** The adapter's name: 'docker', 'e2b', 'local'. */
|
|
54
|
+
readonly name: string;
|
|
55
|
+
create(options: CreateSandboxOptions): Promise<Sandbox>;
|
|
56
|
+
/** Finds a sandbox made earlier, by this process or another. Throws
|
|
57
|
+
* SandboxGoneError when it no longer exists. */
|
|
58
|
+
connect(sandboxId: string): Promise<Sandbox>;
|
|
59
|
+
}
|
|
60
|
+
export interface RunCommandOptions {
|
|
61
|
+
/** Where the command runs. A relative path is taken from the sandbox's
|
|
62
|
+
* work folder, which is also the default. */
|
|
63
|
+
cwd?: string;
|
|
64
|
+
env?: Record<string, string>;
|
|
65
|
+
/** Stops the command after this long: the result then has `timedOut: true`
|
|
66
|
+
* and exit code 124, with the output it printed so far. Default 120 s. */
|
|
67
|
+
timeoutMs?: number;
|
|
68
|
+
/** Starts the command and returns at once, leaving it running (a dev
|
|
69
|
+
* server, say). Its output is not kept. */
|
|
70
|
+
background?: boolean;
|
|
71
|
+
/** Output as it comes, decoded as UTF-8. */
|
|
72
|
+
onStdout?: (chunk: string) => void;
|
|
73
|
+
onStderr?: (chunk: string) => void;
|
|
74
|
+
/** Stops the command and rejects with the signal's reason. */
|
|
75
|
+
signal?: AbortSignal;
|
|
76
|
+
}
|
|
77
|
+
export interface CommandResult {
|
|
78
|
+
stdout: string;
|
|
79
|
+
stderr: string;
|
|
80
|
+
exitCode: number;
|
|
81
|
+
durationMs: number;
|
|
82
|
+
/** True when the command was stopped at its time limit. */
|
|
83
|
+
timedOut?: boolean;
|
|
84
|
+
/** True when output past MAX_COMMAND_OUTPUT_BYTES was dropped. */
|
|
85
|
+
truncated?: boolean;
|
|
86
|
+
}
|
|
87
|
+
export interface FileEntry {
|
|
88
|
+
name: string;
|
|
89
|
+
type: 'file' | 'directory';
|
|
90
|
+
size?: number;
|
|
91
|
+
}
|
|
92
|
+
/** The sandbox's files. A relative path is taken from the work folder. */
|
|
93
|
+
export interface SandboxFileSystem {
|
|
94
|
+
/** Throws SandboxFileNotFoundError when there is no such file. */
|
|
95
|
+
readFile(path: string): Promise<string>;
|
|
96
|
+
/** Not in ComputeSDK: charts and images are bytes. */
|
|
97
|
+
readFileBytes(path: string): Promise<Uint8Array>;
|
|
98
|
+
/** Makes the parent folders it needs, and replaces a file already there. */
|
|
99
|
+
writeFile(path: string, content: string | Uint8Array): Promise<void>;
|
|
100
|
+
/** The folder's entries, sorted by name. Throws SandboxFileNotFoundError
|
|
101
|
+
* when there is no such folder. */
|
|
102
|
+
readdir(path: string): Promise<FileEntry[]>;
|
|
103
|
+
/** Makes the folder and its parents; fine when it is there already. */
|
|
104
|
+
mkdir(path: string): Promise<void>;
|
|
105
|
+
exists(path: string): Promise<boolean>;
|
|
106
|
+
/** Removes a file, or a folder with everything in it; fine when it is not
|
|
107
|
+
* there. */
|
|
108
|
+
remove(path: string): Promise<void>;
|
|
109
|
+
}
|
|
110
|
+
export interface SandboxInfo {
|
|
111
|
+
id: string;
|
|
112
|
+
provider: string;
|
|
113
|
+
status: 'running' | 'stopped' | 'error';
|
|
114
|
+
createdAt: Date;
|
|
115
|
+
/** When it will shut itself down, when the provider says. */
|
|
116
|
+
expiresAt?: Date;
|
|
117
|
+
/** The folder commands start in and relative paths are taken from. */
|
|
118
|
+
workdir: string;
|
|
119
|
+
metadata?: Record<string, string>;
|
|
120
|
+
}
|
|
121
|
+
export interface Sandbox {
|
|
122
|
+
readonly sandboxId: string;
|
|
123
|
+
readonly provider: string;
|
|
124
|
+
readonly filesystem: SandboxFileSystem;
|
|
125
|
+
runCommand(command: string, options?: RunCommandOptions): Promise<CommandResult>;
|
|
126
|
+
/** A URL for a port the sandbox serves on. Throws SandboxUnsupportedError
|
|
127
|
+
* where the adapter cannot give one. */
|
|
128
|
+
getUrl(options: {
|
|
129
|
+
port: number;
|
|
130
|
+
protocol?: 'http' | 'https';
|
|
131
|
+
}): Promise<string>;
|
|
132
|
+
getInfo(): Promise<SandboxInfo>;
|
|
133
|
+
/** Not in ComputeSDK: the sandbox now shuts itself down `timeoutMs` from
|
|
134
|
+
* now. The runtime calls it as a thread uses its sandbox, so one left idle
|
|
135
|
+
* ends by itself. Throws SandboxUnsupportedError where the provider
|
|
136
|
+
* cannot change it. */
|
|
137
|
+
setTimeout(timeoutMs: number): Promise<void>;
|
|
138
|
+
/** Ends the sandbox and its files. Fine when it is already gone. */
|
|
139
|
+
destroy(): Promise<void>;
|
|
140
|
+
}
|
|
141
|
+
/** The sandbox no longer exists: it timed out, was destroyed, or its
|
|
142
|
+
* provider lost it. */
|
|
143
|
+
export declare class SandboxGoneError extends Error {
|
|
144
|
+
readonly sandboxId: string;
|
|
145
|
+
readonly name = "SandboxGoneError";
|
|
146
|
+
constructor(sandboxId: string, why?: string);
|
|
147
|
+
}
|
|
148
|
+
/** There is no file or folder at this path. */
|
|
149
|
+
export declare class SandboxFileNotFoundError extends Error {
|
|
150
|
+
readonly path: string;
|
|
151
|
+
readonly name = "SandboxFileNotFoundError";
|
|
152
|
+
constructor(path: string);
|
|
153
|
+
}
|
|
154
|
+
/** The adapter cannot do this. */
|
|
155
|
+
export declare class SandboxUnsupportedError extends Error {
|
|
156
|
+
readonly name = "SandboxUnsupportedError";
|
|
157
|
+
constructor(provider: string, what: string);
|
|
158
|
+
}
|
|
159
|
+
//# sourceMappingURL=sandbox.d.ts.map
|
|
@@ -0,0 +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AC,CAAC;IAChC;;;4BAGwB;IACxB,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7C,oEAAoE;IACpE,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1B;AAED;wBACwB;AACxB,qBAAa,gBAAiB,SAAQ,KAAK;IAE7B,QAAQ,CAAC,SAAS,EAAE,MAAM;IADtC,SAAkB,IAAI,sBAAsB;gBACvB,SAAS,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM;CAGrD;AAED,+CAA+C;AAC/C,qBAAa,wBAAyB,SAAQ,KAAK;IAErC,QAAQ,CAAC,IAAI,EAAE,MAAM;IADjC,SAAkB,IAAI,8BAA8B;gBAC/B,IAAI,EAAE,MAAM;CAGlC;AAED,kCAAkC;AAClC,qBAAa,uBAAwB,SAAQ,KAAK;IAChD,SAAkB,IAAI,6BAA6B;gBACvC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;CAG3C"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** The sandbox port: a place apart from the app where an agent's commands run
|
|
2
|
+
* and its files live. A container, a hosted machine, or a folder on this
|
|
3
|
+
* machine in development.
|
|
4
|
+
*
|
|
5
|
+
* The shape and names follow ComputeSDK's Sandbox, so an adapter over it is
|
|
6
|
+
* thin, with three additions: `readFileBytes` (charts and images need it),
|
|
7
|
+
* `setTimeout` (how an idle sandbox ends by itself) and `SandboxInfo.workdir`.
|
|
8
|
+
* The Go runtime has the same port (ports/sandbox.go). */
|
|
9
|
+
/** How long a command may run when the caller does not say. */
|
|
10
|
+
export const DEFAULT_COMMAND_TIMEOUT_MS = 120_000;
|
|
11
|
+
/** The most output kept from one command, per stream. Past it the rest is
|
|
12
|
+
* dropped and the result says `truncated: true`, so a command that prints
|
|
13
|
+
* without end cannot fill the app's memory. */
|
|
14
|
+
export const MAX_COMMAND_OUTPUT_BYTES = 4 * 1024 * 1024;
|
|
15
|
+
/** The exit code of a command stopped at its time limit, as `timeout(1)`
|
|
16
|
+
* gives it. */
|
|
17
|
+
export const TIMED_OUT_EXIT_CODE = 124;
|
|
18
|
+
/** The sandbox no longer exists: it timed out, was destroyed, or its
|
|
19
|
+
* provider lost it. */
|
|
20
|
+
export class SandboxGoneError extends Error {
|
|
21
|
+
sandboxId;
|
|
22
|
+
name = 'SandboxGoneError';
|
|
23
|
+
constructor(sandboxId, why) {
|
|
24
|
+
super(`sandbox ${sandboxId} is gone${why ? `: ${why}` : ''}`);
|
|
25
|
+
this.sandboxId = sandboxId;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** There is no file or folder at this path. */
|
|
29
|
+
export class SandboxFileNotFoundError extends Error {
|
|
30
|
+
path;
|
|
31
|
+
name = 'SandboxFileNotFoundError';
|
|
32
|
+
constructor(path) {
|
|
33
|
+
super(`sandbox: no such file or folder: ${path}`);
|
|
34
|
+
this.path = path;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** The adapter cannot do this. */
|
|
38
|
+
export class SandboxUnsupportedError extends Error {
|
|
39
|
+
name = 'SandboxUnsupportedError';
|
|
40
|
+
constructor(provider, what) {
|
|
41
|
+
super(`${provider}: ${what} is not supported`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=sandbox.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sandbox.js","sourceRoot":"","sources":["../../src/ports/sandbox.ts"],"names":[],"mappings":"AAEA;;;;;;;2DAO2D;AAE3D,+DAA+D;AAC/D,MAAM,CAAC,MAAM,0BAA0B,GAAG,OAAO,CAAC;AAElD;;gDAEgD;AAChD,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;AAExD;gBACgB;AAChB,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAwHvC;wBACwB;AACxB,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAEpB;IADH,IAAI,GAAG,kBAAkB,CAAC;IAC5C,YAAqB,SAAiB,EAAE,GAAY;QAClD,KAAK,CAAC,WAAW,SAAS,WAAW,GAAG,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QAD3C,cAAS,GAAT,SAAS,CAAQ;IAEtC,CAAC;CACF;AAED,+CAA+C;AAC/C,MAAM,OAAO,wBAAyB,SAAQ,KAAK;IAE5B;IADH,IAAI,GAAG,0BAA0B,CAAC;IACpD,YAAqB,IAAY;QAC/B,KAAK,CAAC,oCAAoC,IAAI,EAAE,CAAC,CAAC;QAD/B,SAAI,GAAJ,IAAI,CAAQ;IAEjC,CAAC;CACF;AAED,kCAAkC;AAClC,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IAC9B,IAAI,GAAG,yBAAyB,CAAC;IACnD,YAAY,QAAgB,EAAE,IAAY;QACxC,KAAK,CAAC,GAAG,QAAQ,KAAK,IAAI,mBAAmB,CAAC,CAAC;IACjD,CAAC;CACF"}
|
package/dist/ports/storage.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { StorageContext } from '../core/state.js';
|
|
2
|
-
import type { AgentEvent, ExecutionState, MessageDTO, NewMessage, NewUsage, UsageFilter, UsageTotals, ThreadDTO } from '../core/types.js';
|
|
2
|
+
import type { AgentEvent, ExecutionState, MessageDTO, NewMessage, NewThreadEvent, ThreadEventFilter, NewUsage, UsageFilter, UsageTotals, ThreadDTO, ThreadTransition } from '../core/types.js';
|
|
3
3
|
/** Persistence port for a caller's OWN data (§3.2): threads, messages, events,
|
|
4
4
|
* usage. DTOs cross the boundary; ORM/entity types never leak into core.
|
|
5
5
|
* Implement it once for MongoDB, MySQL, DynamoDB — or use the shipped
|
|
@@ -29,6 +29,11 @@ export interface Storage {
|
|
|
29
29
|
* Backs HITL reclamation + double-dispatch protection (§2.5, §2.8).
|
|
30
30
|
* Must be atomic — a single conditional UPDATE or equivalent. */
|
|
31
31
|
claimState(threadId: string, from: ExecutionState, to: ExecutionState, ctx: StorageContext): Promise<boolean>;
|
|
32
|
+
/** The compare-and-set every run state change goes through: true iff THIS
|
|
33
|
+
* caller moved the thread (see ThreadTransition). The adapter keeps the
|
|
34
|
+
* thread's current run beside its state. Atomic, like `claimState`: one
|
|
35
|
+
* conditional UPDATE or equivalent. */
|
|
36
|
+
transition(threadId: string, t: ThreadTransition, ctx: StorageContext): Promise<boolean>;
|
|
32
37
|
};
|
|
33
38
|
messages: {
|
|
34
39
|
append(threadId: string, message: NewMessage, ctx: StorageContext): Promise<MessageDTO>;
|
|
@@ -51,16 +56,32 @@ export interface Storage {
|
|
|
51
56
|
* provider accepts. `run()` enforces that; adapters just delete. */
|
|
52
57
|
deleteFrom(threadId: string, messageId: string, ctx: StorageContext): Promise<number>;
|
|
53
58
|
};
|
|
59
|
+
/** The thread record: the few events that must outlive a run — parks and
|
|
60
|
+
* their answers, refusals, budget stops, compaction, each segment's start
|
|
61
|
+
* and end, and an app's own events published durable. A run's live
|
|
62
|
+
* events go to its run stream, never here, so this grows with runs, not
|
|
63
|
+
* with tokens. */
|
|
54
64
|
events: {
|
|
55
|
-
|
|
56
|
-
|
|
65
|
+
/** Store an entry and mint its seq: the thread's next, one higher than
|
|
66
|
+
* any it holds. Two appends on one thread never get the same seq. */
|
|
67
|
+
append(threadId: string, event: NewThreadEvent, ctx: StorageContext): Promise<AgentEvent>;
|
|
68
|
+
/** Entries by filter, ascending by seq. */
|
|
69
|
+
list(threadId: string, filter: ThreadEventFilter, ctx: StorageContext): Promise<AgentEvent[]>;
|
|
70
|
+
/** All events after the cursor, ascending by seq. */
|
|
57
71
|
listSince(threadId: string, sinceSeq: number, ctx: StorageContext): Promise<AgentEvent[]>;
|
|
58
72
|
/** Most recent event of a type — the HITL pending check (§2.5) */
|
|
59
73
|
latest(threadId: string, type: string, ctx: StorageContext): Promise<AgentEvent | null>;
|
|
60
74
|
/** Every event of a type, ascending by seq. The open-approval set (§2.7)
|
|
61
|
-
* is derived from these
|
|
62
|
-
* CHUNK on the thread through memory. */
|
|
75
|
+
* is derived from these. */
|
|
63
76
|
listByType(threadId: string, type: string, ctx: StorageContext): Promise<AgentEvent[]>;
|
|
77
|
+
/** Optional: one batch of `runtime.pruneEvents`. Deletes up to `limit`
|
|
78
|
+
* entries of these types, on every thread, and says how many of each
|
|
79
|
+
* went. With `dryRun` it deletes nothing and counts every entry of
|
|
80
|
+
* these types instead. */
|
|
81
|
+
prune?(types: string[], opts: {
|
|
82
|
+
limit: number;
|
|
83
|
+
dryRun?: boolean;
|
|
84
|
+
}): Promise<Record<string, number>>;
|
|
64
85
|
};
|
|
65
86
|
/** One row per model call (§4).
|
|
66
87
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../src/ports/storage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,KAAK,EACV,UAAU,EACV,cAAc,EACd,UAAU,EACV,UAAU,EACV,QAAQ,EACR,WAAW,EACX,WAAW,EACX,SAAS,
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../src/ports/storage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,KAAK,EACV,UAAU,EACV,cAAc,EACd,UAAU,EACV,UAAU,EACV,cAAc,EACd,iBAAiB,EACjB,QAAQ,EACR,WAAW,EACX,WAAW,EACX,SAAS,EACT,gBAAgB,EACjB,MAAM,kBAAkB,CAAC;AAE1B;;;;;;;;;;;6DAW6D;AAC7D,MAAM,WAAW,OAAO;IACtB,OAAO,EAAE;QACP,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,cAAc,GAAG,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC,CAAC;QACtE,MAAM,CAAC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,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,EAAE,cAAc,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;KACzF,CAAC;CACH"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { StreamEnd, StreamEvent, StreamItem } from '../core/stream-events.js';
|
|
2
|
+
export type { StreamEnd, StreamEvent, StreamItem } from '../core/stream-events.js';
|
|
3
|
+
/** Who a stream belongs to. */
|
|
4
|
+
export interface StreamMeta {
|
|
5
|
+
threadId: string;
|
|
6
|
+
runId: string;
|
|
7
|
+
}
|
|
8
|
+
/** A stream as it stands now. */
|
|
9
|
+
export interface StreamSnapshot {
|
|
10
|
+
meta: StreamMeta;
|
|
11
|
+
items: StreamItem[];
|
|
12
|
+
/** The end item when the stream is closed; it is also the last of `items`. */
|
|
13
|
+
end: StreamEnd | null;
|
|
14
|
+
}
|
|
15
|
+
/** The stream does not exist: never opened, past its grace window, or
|
|
16
|
+
* deleted. A reader takes a fresh snapshot of the thread instead. */
|
|
17
|
+
export declare class StreamGoneError extends Error {
|
|
18
|
+
readonly streamId: string;
|
|
19
|
+
constructor(streamId: string);
|
|
20
|
+
}
|
|
21
|
+
/** An append to a stream that is already closed. */
|
|
22
|
+
export declare class StreamClosedError extends Error {
|
|
23
|
+
readonly streamId: string;
|
|
24
|
+
constructor(streamId: string);
|
|
25
|
+
}
|
|
26
|
+
/** Short-lived logs, one per run segment. Delivery lives here, not state:
|
|
27
|
+
* a stream is closed when its segment ends and deleted after a grace
|
|
28
|
+
* window. What must outlive the run is in the messages and the thread
|
|
29
|
+
* record.
|
|
30
|
+
*
|
|
31
|
+
* Every adapter keeps these rules:
|
|
32
|
+
* - Offsets are opaque strings. Only the store compares them; a client
|
|
33
|
+
* echoes the last one back and nothing else.
|
|
34
|
+
* - Offsets strictly increase within a stream. One writer per stream: the
|
|
35
|
+
* run lock already makes sure of that.
|
|
36
|
+
* - `close` writes the end as the last item, so a reader that sees it
|
|
37
|
+
* stops, and one that comes late still gets it.
|
|
38
|
+
* - A missed wake-up only delays a reader, never drops an event. */
|
|
39
|
+
export interface RunStreams {
|
|
40
|
+
/** Open a stream. Opening one that exists is a no-op. `ttlMs` deletes it
|
|
41
|
+
* even if it is never closed. */
|
|
42
|
+
open(streamId: string, meta: StreamMeta, ttlMs: number): Promise<void>;
|
|
43
|
+
/** Append in order; one offset per event. Throws StreamClosedError once
|
|
44
|
+
* closed and StreamGoneError when the stream does not exist. */
|
|
45
|
+
append(streamId: string, events: StreamEvent[]): Promise<string[]>;
|
|
46
|
+
/** The items after `after` (null: from the start), then live ones as they
|
|
47
|
+
* come. Ends after the end item, or when the signal aborts. Throws
|
|
48
|
+
* StreamGoneError when the stream does not exist, or goes while read. */
|
|
49
|
+
read(streamId: string, after: string | null, signal?: AbortSignal): AsyncIterable<StreamItem>;
|
|
50
|
+
/** The items after `after` now, without waiting. Null when the stream does
|
|
51
|
+
* not exist. */
|
|
52
|
+
snapshot(streamId: string, after?: string | null): Promise<StreamSnapshot | null>;
|
|
53
|
+
/** Write the end item and start the grace window: the stream is deleted
|
|
54
|
+
* `graceMs` later. Closing a closed stream is a no-op; closing one that
|
|
55
|
+
* does not exist throws StreamGoneError. */
|
|
56
|
+
close(streamId: string, end: StreamEnd, graceMs: number): Promise<void>;
|
|
57
|
+
/** Remove a stream now (thread delete). A missing one is a no-op. */
|
|
58
|
+
delete(streamId: string): Promise<void>;
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=streams.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streams.d.ts","sourceRoot":"","sources":["../../src/ports/streams.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAEnF,YAAY,EAAE,SAAS,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAEnF,+BAA+B;AAC/B,MAAM,WAAW,UAAU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AqE;IACrE,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACzC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** The stream does not exist: never opened, past its grace window, or
|
|
2
|
+
* deleted. A reader takes a fresh snapshot of the thread instead. */
|
|
3
|
+
export class StreamGoneError extends Error {
|
|
4
|
+
streamId;
|
|
5
|
+
constructor(streamId) {
|
|
6
|
+
super(`run stream ${streamId} is gone`);
|
|
7
|
+
this.streamId = streamId;
|
|
8
|
+
this.name = 'StreamGoneError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/** An append to a stream that is already closed. */
|
|
12
|
+
export class StreamClosedError extends Error {
|
|
13
|
+
streamId;
|
|
14
|
+
constructor(streamId) {
|
|
15
|
+
super(`run stream ${streamId} is closed`);
|
|
16
|
+
this.streamId = streamId;
|
|
17
|
+
this.name = 'StreamClosedError';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=streams.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streams.js","sourceRoot":"","sources":["../../src/ports/streams.ts"],"names":[],"mappings":"AAkBA;sEACsE;AACtE,MAAM,OAAO,eAAgB,SAAQ,KAAK;IACnB;IAArB,YAAqB,QAAgB;QACnC,KAAK,CAAC,cAAc,QAAQ,UAAU,CAAC,CAAC;QADrB,aAAQ,GAAR,QAAQ,CAAQ;QAEnC,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CACF;AAED,oDAAoD;AACpD,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IACrB;IAArB,YAAqB,QAAgB;QACnC,KAAK,CAAC,cAAc,QAAQ,YAAY,CAAC,CAAC;QADvB,aAAQ,GAAR,QAAQ,CAAQ;QAEnC,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IAClC,CAAC;CACF"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/** The ports behind the built-in tools. Each tool has one name and one input
|
|
2
|
+
* shape in every runtime; what does the work is an adapter you plug in here,
|
|
3
|
+
* like storage or the queue. */
|
|
4
|
+
/** How far back a search may look. */
|
|
5
|
+
export type SearchRecency = 'day' | 'week' | 'month' | 'year';
|
|
6
|
+
export interface SearchOptions {
|
|
7
|
+
/** 1 to 10. */
|
|
8
|
+
maxResults: number;
|
|
9
|
+
/** Only results from these domains (and their subdomains). */
|
|
10
|
+
allowedDomains?: string[];
|
|
11
|
+
/** Never results from these domains (and their subdomains). */
|
|
12
|
+
blockedDomains?: string[];
|
|
13
|
+
recency?: SearchRecency;
|
|
14
|
+
signal?: AbortSignal;
|
|
15
|
+
}
|
|
16
|
+
export interface SearchHit {
|
|
17
|
+
title: string;
|
|
18
|
+
url: string;
|
|
19
|
+
/** A short piece of the page that matched: what the model reads to decide
|
|
20
|
+
* whether to open it. */
|
|
21
|
+
snippet: string;
|
|
22
|
+
/** ISO date, when the engine knows it. */
|
|
23
|
+
publishedAt?: string;
|
|
24
|
+
}
|
|
25
|
+
/** A web search engine: Brave, Jina, Tavily… */
|
|
26
|
+
export interface Search {
|
|
27
|
+
/** The adapter's name, as usage rows and prices know it: 'brave'. */
|
|
28
|
+
readonly name: string;
|
|
29
|
+
search(query: string, options: SearchOptions): Promise<SearchHit[]>;
|
|
30
|
+
}
|
|
31
|
+
export interface FetchOptions {
|
|
32
|
+
/** Stop reading the page after this many bytes. */
|
|
33
|
+
maxBytes: number;
|
|
34
|
+
format: 'markdown' | 'text';
|
|
35
|
+
signal?: AbortSignal;
|
|
36
|
+
}
|
|
37
|
+
export interface FetchedPage {
|
|
38
|
+
/** Where the page ended up, after redirects. */
|
|
39
|
+
url: string;
|
|
40
|
+
title: string;
|
|
41
|
+
content: string;
|
|
42
|
+
/** True when the page was cut at `maxBytes`. */
|
|
43
|
+
truncated: boolean;
|
|
44
|
+
}
|
|
45
|
+
/** Reads one web page as clean text: our PageReader, Jina Reader… */
|
|
46
|
+
export interface Fetcher {
|
|
47
|
+
readonly name: string;
|
|
48
|
+
fetch(url: string, options: FetchOptions): Promise<FetchedPage>;
|
|
49
|
+
}
|
|
50
|
+
import type { SandboxProvider } from './sandbox.js';
|
|
51
|
+
/** The adapters the built-in tools use. A tool whose port is missing cannot
|
|
52
|
+
* be asked for: `builtinTools` throws at startup rather than a run failing
|
|
53
|
+
* later. */
|
|
54
|
+
export interface BuiltinToolPorts {
|
|
55
|
+
search?: Search;
|
|
56
|
+
fetcher?: Fetcher;
|
|
57
|
+
/** Where commands run and files live: one sandbox per thread. */
|
|
58
|
+
sandbox?: SandboxProvider;
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../src/ports/tools.ts"],"names":[],"mappings":"AAAA;;iCAEiC;AAEjC,sCAAsC;AACtC,MAAM,MAAM,aAAa,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,CAAC;AAE9D,MAAM,WAAW,aAAa;IAC5B,eAAe;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,8DAA8D;IAC9D,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,+DAA+D;IAC/D,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,OAAO,CAAC,EAAE,aAAa,CAAC;IACxB,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ;8BAC0B;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,0CAA0C;IAC1C,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,gDAAgD;AAChD,MAAM,WAAW,MAAM;IACrB,qEAAqE;IACrE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC;CACrE;AAED,MAAM,WAAW,YAAY;IAC3B,mDAAmD;IACnD,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC;IAC5B,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,MAAM,WAAW,WAAW;IAC1B,gDAAgD;IAChD,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,gDAAgD;IAChD,SAAS,EAAE,OAAO,CAAC;CACpB;AAED,qEAAqE;AACrE,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;CACjE;AAED,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAEpD;;aAEa;AACb,MAAM,WAAW,gBAAgB;IAC/B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,iEAAiE;IACjE,OAAO,CAAC,EAAE,eAAe,CAAC;CAC3B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.js","sourceRoot":"","sources":["../../src/ports/tools.ts"],"names":[],"mappings":"AAAA;;iCAEiC"}
|
package/dist/pricing.d.ts
CHANGED
|
@@ -25,11 +25,40 @@ export type PriceTable = Record<string, ModelPrice>;
|
|
|
25
25
|
* registry key without its '@variant' suffix. */
|
|
26
26
|
export declare function lookupPrice(prices: PriceTable, model?: string | null, modelId?: string | null): ModelPrice | undefined;
|
|
27
27
|
/** A price-list pricer (§4).
|
|
28
|
+
*
|
|
29
|
+
* A cache rate that is missing (or 0) is taken as not given: those tokens
|
|
30
|
+
* are priced at `inputPerMillion`, and a warning is logged once per model.
|
|
31
|
+
* Cache tokens are never free, and a price list that forgot them must not
|
|
32
|
+
* bill them at 0.
|
|
28
33
|
*
|
|
29
34
|
* A model the table does not know is not priced. Its row is stored with no
|
|
30
35
|
* cost and `UsageTotals.unpriced` counts it, so a missing price shows up as
|
|
31
36
|
* a gap in the bill rather than as a silent zero. */
|
|
32
37
|
export declare function table(prices: PriceTable, currency?: string): Pricer;
|
|
38
|
+
/** What a paid tool service costs, in currency units: per use (a search, a
|
|
39
|
+
* page), per second of sandbox time (bash, code_execution), or both. */
|
|
40
|
+
export interface ToolPrice {
|
|
41
|
+
perUse?: number;
|
|
42
|
+
perSecond?: number;
|
|
43
|
+
}
|
|
44
|
+
/** Tool prices, keyed by adapter name ('brave', 'jina-search') or by tool
|
|
45
|
+
* name ('web_search'). The adapter wins: the same tool costs what the
|
|
46
|
+
* service behind it charges. */
|
|
47
|
+
export type ToolPriceTable = Record<string, ToolPrice>;
|
|
48
|
+
/** Prices the built-in tools' usage rows (`kind: 'tool'`, `model:
|
|
49
|
+
* 'tool:<name>'`), per use. Every other row is left to the next pricer, so
|
|
50
|
+
* chain it with the model table:
|
|
51
|
+
*
|
|
52
|
+
* ```ts
|
|
53
|
+
* pricer: pricing.chain(pricing.table(modelPrices), pricing.tools({ brave: { perUse: 0.005 } }))
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* Sandbox tools are priced by the sandbox adapter's name and the seconds
|
|
57
|
+
* their commands ran: `{ e2b: { perSecond: 0.000028 } }`.
|
|
58
|
+
*
|
|
59
|
+
* A tool's own model calls (reading a page with a question) carry the
|
|
60
|
+
* model's key, so the model table prices them. */
|
|
61
|
+
export declare function tools(prices: ToolPriceTable, currency?: string): Pricer;
|
|
33
62
|
/** Pulls a cost out of what the provider attached to the finish: a gateway
|
|
34
63
|
* receipt, a billing header, whatever your provider sends. Return null when
|
|
35
64
|
* this call carried no receipt, and the next pricer in a chain gets its
|
package/dist/pricing.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pricing.d.ts","sourceRoot":"","sources":["../src/pricing.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAEjD,kEAAkE;AAClE,eAAO,MAAM,GAAG,QAAQ,CAAC;AAEzB;;;oBAGoB;AACpB,MAAM,WAAW,UAAU;IACzB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;;kBAGc;IACd,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B;AAED;;;uEAGuE;AACvE,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AAEpD;kDACkD;AAClD,wBAAgB,WAAW,CACzB,MAAM,EAAE,UAAU,EAClB,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,EACrB,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,GACtB,UAAU,GAAG,SAAS,
|
|
1
|
+
{"version":3,"file":"pricing.d.ts","sourceRoot":"","sources":["../src/pricing.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAEjD,kEAAkE;AAClE,eAAO,MAAM,GAAG,QAAQ,CAAC;AAEzB;;;oBAGoB;AACpB,MAAM,WAAW,UAAU;IACzB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;;kBAGc;IACd,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B;AAED;;;uEAGuE;AACvE,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,UAAU,C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C;AAE3D;qBACqB;AACrB,eAAO,MAAM,MAAM,GAAI,GAAG,MAAM,EAAE,WAAU,MAAY,KAAG,MACpB,CAAC"}
|
package/dist/pricing.js
CHANGED
|
@@ -3,14 +3,27 @@ export const USD = 'USD';
|
|
|
3
3
|
/** Find a model's price, trying the registry key, the wire id, then the
|
|
4
4
|
* registry key without its '@variant' suffix. */
|
|
5
5
|
export function lookupPrice(prices, model, modelId) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return prices[modelId];
|
|
6
|
+
// Own keys only: a model named 'constructor' or 'toString' must not find
|
|
7
|
+
// something off the object's prototype.
|
|
8
|
+
const own = (k) => (k && Object.hasOwn(prices, k) ? prices[k] : undefined);
|
|
10
9
|
const base = model?.split('@')[0];
|
|
11
|
-
return
|
|
10
|
+
return own(model) ?? own(modelId) ?? (base !== model ? own(base) : undefined);
|
|
11
|
+
}
|
|
12
|
+
/** Warned once per model and rate, so a price list missing a cache rate says
|
|
13
|
+
* so without filling the log. */
|
|
14
|
+
const warned = new Set();
|
|
15
|
+
function warnOnce(key, msg, model) {
|
|
16
|
+
if (warned.has(key))
|
|
17
|
+
return;
|
|
18
|
+
warned.add(key);
|
|
19
|
+
console.warn(msg, { model });
|
|
12
20
|
}
|
|
13
21
|
/** A price-list pricer (§4).
|
|
22
|
+
*
|
|
23
|
+
* A cache rate that is missing (or 0) is taken as not given: those tokens
|
|
24
|
+
* are priced at `inputPerMillion`, and a warning is logged once per model.
|
|
25
|
+
* Cache tokens are never free, and a price list that forgot them must not
|
|
26
|
+
* bill them at 0.
|
|
14
27
|
*
|
|
15
28
|
* A model the table does not know is not priced. Its row is stored with no
|
|
16
29
|
* cost and `UsageTotals.unpriced` counts it, so a missing price shows up as
|
|
@@ -24,15 +37,54 @@ export function table(prices, currency = USD) {
|
|
|
24
37
|
// tokens / 1_000_000 × pricePerMillion is the cost in currency units,
|
|
25
38
|
// and micros is that × 1_000_000. The two cancel: tokens ×
|
|
26
39
|
// pricePerMillion IS the micro-unit cost, with no scaling in between.
|
|
40
|
+
let cacheRead = p.cacheReadPerMillion ?? 0;
|
|
41
|
+
let cacheWrite = p.cacheWritePerMillion ?? 0;
|
|
42
|
+
if (!cacheRead && u.cacheReadInputTokens > 0) {
|
|
43
|
+
cacheRead = p.inputPerMillion ?? 0;
|
|
44
|
+
warnOnce(`${u.model}\u0000read`, 'price table has no cache read rate; cache reads priced as input', u.model);
|
|
45
|
+
}
|
|
46
|
+
if (!cacheWrite && u.cacheWriteInputTokens > 0) {
|
|
47
|
+
cacheWrite = p.inputPerMillion ?? 0;
|
|
48
|
+
warnOnce(`${u.model}\u0000write`, 'price table has no cache write rate; cache writes priced as input', u.model);
|
|
49
|
+
}
|
|
27
50
|
const micros = u.inputTokens * (p.inputPerMillion ?? 0) +
|
|
28
|
-
u.cacheReadInputTokens *
|
|
29
|
-
u.cacheWriteInputTokens *
|
|
51
|
+
u.cacheReadInputTokens * cacheRead +
|
|
52
|
+
u.cacheWriteInputTokens * cacheWrite +
|
|
30
53
|
u.outputTokens * (p.outputPerMillion ?? 0) +
|
|
31
54
|
u.reasoningTokens * (p.reasoningPerMillion ?? 0);
|
|
32
55
|
return { micros: Math.round(micros), currency, source: 'table' };
|
|
33
56
|
},
|
|
34
57
|
};
|
|
35
58
|
}
|
|
59
|
+
/** Prices the built-in tools' usage rows (`kind: 'tool'`, `model:
|
|
60
|
+
* 'tool:<name>'`), per use. Every other row is left to the next pricer, so
|
|
61
|
+
* chain it with the model table:
|
|
62
|
+
*
|
|
63
|
+
* ```ts
|
|
64
|
+
* pricer: pricing.chain(pricing.table(modelPrices), pricing.tools({ brave: { perUse: 0.005 } }))
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* Sandbox tools are priced by the sandbox adapter's name and the seconds
|
|
68
|
+
* their commands ran: `{ e2b: { perSecond: 0.000028 } }`.
|
|
69
|
+
*
|
|
70
|
+
* A tool's own model calls (reading a page with a question) carry the
|
|
71
|
+
* model's key, so the model table prices them. */
|
|
72
|
+
export function tools(prices, currency = USD) {
|
|
73
|
+
return {
|
|
74
|
+
price(u) {
|
|
75
|
+
if (u.kind !== 'tool' || !u.model?.startsWith('tool:'))
|
|
76
|
+
return null;
|
|
77
|
+
const p = (u.modelId ? prices[u.modelId] : undefined) ?? prices[u.model.slice('tool:'.length)];
|
|
78
|
+
if (!p)
|
|
79
|
+
return null;
|
|
80
|
+
const meta = u.providerMetadata;
|
|
81
|
+
const uses = typeof meta?.uses === 'number' && meta.uses > 0 ? meta.uses : 1;
|
|
82
|
+
const secs = typeof meta?.seconds === 'number' && meta.seconds > 0 ? meta.seconds : 0;
|
|
83
|
+
const amount = (p.perUse ?? 0) * uses + (p.perSecond ?? 0) * secs;
|
|
84
|
+
return { micros: Math.round(amount * 1_000_000), currency, source: 'table' };
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
36
88
|
/** Price a call from the number the provider already computed.
|
|
37
89
|
*
|
|
38
90
|
* This is the most accurate pricer there is: no price list to keep in step
|
|
@@ -44,9 +96,11 @@ export function receipt(read, currency = USD) {
|
|
|
44
96
|
if (!u.providerMetadata)
|
|
45
97
|
return null;
|
|
46
98
|
const micros = read(u.providerMetadata);
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
99
|
+
// No receipt, or one that makes no sense (not a number, negative, not
|
|
100
|
+
// finite): not believed, and the call is left for the next pricer.
|
|
101
|
+
if (typeof micros !== 'number' || !Number.isFinite(micros) || micros < 0)
|
|
102
|
+
return null;
|
|
103
|
+
return { micros: Math.round(micros), currency, source: 'receipt' };
|
|
50
104
|
},
|
|
51
105
|
};
|
|
52
106
|
}
|
package/dist/pricing.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pricing.js","sourceRoot":"","sources":["../src/pricing.ts"],"names":[],"mappings":"AAgBA,kEAAkE;AAClE,MAAM,CAAC,MAAM,GAAG,GAAG,KAAK,CAAC;AAwBzB;kDACkD;AAClD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