@lunora/agent 0.0.0 → 1.0.0-alpha.10

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.
Files changed (53) hide show
  1. package/LICENSE.md +105 -0
  2. package/README.md +38 -31
  3. package/dist/channels.d.mts +74 -0
  4. package/dist/channels.d.ts +74 -0
  5. package/dist/channels.mjs +181 -0
  6. package/dist/component.d.mts +104 -0
  7. package/dist/component.d.ts +104 -0
  8. package/dist/component.mjs +418 -0
  9. package/dist/inbound.d.mts +34 -0
  10. package/dist/inbound.d.ts +34 -0
  11. package/dist/inbound.mjs +32 -0
  12. package/dist/index.d.mts +832 -0
  13. package/dist/index.d.ts +832 -0
  14. package/dist/index.mjs +18 -0
  15. package/dist/naming.d.mts +30 -0
  16. package/dist/naming.d.ts +30 -0
  17. package/dist/naming.mjs +8 -0
  18. package/dist/packem_shared/AGENT_MODULE-Dnt_-AAT.mjs +24 -0
  19. package/dist/packem_shared/VoiceSessionDO-BdwlLaXC.mjs +297 -0
  20. package/dist/packem_shared/adaptMcpResult-wtNMvLoP.mjs +65 -0
  21. package/dist/packem_shared/agentAsTool-CUHlWsmt.mjs +98 -0
  22. package/dist/packem_shared/base64-BVwtgRJV.mjs +18 -0
  23. package/dist/packem_shared/braintrustTelemetry-TP7Kwuuj.mjs +47 -0
  24. package/dist/packem_shared/buildModelMessages-BWFigaoo.mjs +69 -0
  25. package/dist/packem_shared/codeTool-CjgJOC9t.mjs +122 -0
  26. package/dist/packem_shared/collectAgenticMemoryTools-QrzpV-WX.mjs +97 -0
  27. package/dist/packem_shared/combineTelemetry-DCyaaWAI.mjs +43 -0
  28. package/dist/packem_shared/common-DQXayow6.mjs +89 -0
  29. package/dist/packem_shared/compileAgentWorkflow-DYFFyx7i.mjs +78 -0
  30. package/dist/packem_shared/consoleTelemetry--3sWfu1R.mjs +93 -0
  31. package/dist/packem_shared/createAgentContext-4xJGXNR4.mjs +50 -0
  32. package/dist/packem_shared/createAgentGenerate-DO7Z96zX.mjs +192 -0
  33. package/dist/packem_shared/createDispatchRunner-DSbp_dph-ZHTtxy3f.mjs +69 -0
  34. package/dist/packem_shared/defineAgent-DAwAZC9P.mjs +148 -0
  35. package/dist/packem_shared/defineSkill-Ctf_S-rz.mjs +22 -0
  36. package/dist/packem_shared/functionTool-D6lCa2jB.mjs +20 -0
  37. package/dist/packem_shared/graph-component-Bbaxxymp.mjs +217 -0
  38. package/dist/packem_shared/memory-D4FPcBsX.mjs +12 -0
  39. package/dist/packem_shared/normalizeEntityName-BouctxLC.mjs +3 -0
  40. package/dist/packem_shared/otlpTelemetry-DU5ZmoEV.mjs +177 -0
  41. package/dist/packem_shared/runAgentLoop-M8PKbtWT.mjs +493 -0
  42. package/dist/packem_shared/runVoiceTurn-LnqLvCRR.mjs +211 -0
  43. package/dist/packem_shared/sandboxComponent-DR3pTwBL.mjs +194 -0
  44. package/dist/packem_shared/sentryTelemetry-A4F5ndh9.mjs +36 -0
  45. package/dist/packem_shared/types.d-boAM2Yi1.d.mts +1114 -0
  46. package/dist/packem_shared/types.d-boAM2Yi1.d.ts +1114 -0
  47. package/dist/sandbox.d.mts +204 -0
  48. package/dist/sandbox.d.ts +204 -0
  49. package/dist/sandbox.mjs +113 -0
  50. package/dist/telemetry/index.d.mts +254 -0
  51. package/dist/telemetry/index.d.ts +254 -0
  52. package/dist/telemetry/index.mjs +5 -0
  53. package/package.json +88 -7
@@ -0,0 +1,204 @@
1
+ import { a as AgentToolContext, b as AgentToolDefinition } from "./packem_shared/types.d-boAM2Yi1.mjs";
2
+ import '@lunora/mail/inbound';
3
+ import 'ai';
4
+ /**
5
+ * The model-provided input to a {@link browserTool} call — a discriminated
6
+ * union on `op` so one tool exposes every headless-browser capability.
7
+ * @experimental
8
+ */
9
+ type BrowserToolInput = {
10
+ fullPage?: boolean;
11
+ op: "screenshot";
12
+ type?: "jpeg" | "png";
13
+ url: string;
14
+ } | {
15
+ op: "content";
16
+ url: string;
17
+ } | {
18
+ op: "pdf";
19
+ url: string;
20
+ } | {
21
+ op: "scrape";
22
+ selector?: string;
23
+ url: string;
24
+ };
25
+ /**
26
+ * The model-provided input to a {@link containerTool} call — a discriminated
27
+ * union on `op`. `fetch` sends an HTTP request to the container; `exec` asks it
28
+ * to run a command (routed as a POST to `/exec`, since the container surface
29
+ * exposes no first-class exec RPC — the container app must serve that route).
30
+ * @experimental
31
+ */
32
+ type ContainerToolInput = {
33
+ args?: string[];
34
+ command?: string;
35
+ op: "exec";
36
+ } | {
37
+ body?: string;
38
+ method?: string;
39
+ op: "fetch";
40
+ path: string;
41
+ };
42
+ /**
43
+ * Author-supplied config for `browserTool`.
44
+ * @experimental
45
+ */
46
+ interface BrowserToolOptions {
47
+ /** Override the model-facing description (what the tool does). */
48
+ description?: string;
49
+ /**
50
+ * Gate a call behind a human approval. Defaults to **unattended** (no gate),
51
+ * matching the AI SDK. `browserTool` fetches a model-chosen `url` with no
52
+ * allowlist, so it is an SSRF surface: a prompt-injected model can point it at
53
+ * an internal/link-local address and read the response back into context. Pass
54
+ * a boolean or a predicate to gate it (evaluated from replay-stable input, so
55
+ * keep it deterministic).
56
+ */
57
+ needsApproval?: ((input: BrowserToolInput) => boolean) | boolean;
58
+ }
59
+ /**
60
+ * The model-provided input to a {@link fsTool} call — a discriminated union on
61
+ * `op` over an R2-backed virtual filesystem. Paths are relative to the tool's
62
+ * pinned `root`; a `..` that escapes the root is rejected server-side.
63
+ * @experimental
64
+ */
65
+ type FsToolInput = {
66
+ op: "ls";
67
+ path?: string;
68
+ } | {
69
+ op: "read";
70
+ path: string;
71
+ } | {
72
+ op: "rm";
73
+ path: string;
74
+ } | {
75
+ op: "stat";
76
+ path: string;
77
+ } | {
78
+ content: string;
79
+ op: "write";
80
+ path: string;
81
+ };
82
+ /**
83
+ * Author-supplied config for `fsTool`.
84
+ * @experimental
85
+ */
86
+ interface FsToolOptions {
87
+ /** Override the model-facing description (what the tool does). */
88
+ description?: string;
89
+ /**
90
+ * Gate a call behind a human approval. Defaults to gating the WRITING ops
91
+ * (`write`, `rm`) — a prompt-injected model shouldn't silently overwrite or
92
+ * delete the sandbox — while `ls`/`read`/`stat` run unattended. Pass a boolean
93
+ * or a predicate (evaluated from replay-stable input, so keep it deterministic).
94
+ */
95
+ needsApproval?: ((input: FsToolInput) => boolean) | boolean;
96
+ /**
97
+ * The key prefix every path is scoped under, isolating this tool's files from
98
+ * the rest of the bucket. Either a fixed string (e.g. `"agents/support"`) or a
99
+ * function of the tool context — return `` `agents/${ctx.threadKey}` `` (or an
100
+ * owner-derived prefix) to give each run/user its OWN namespace in a
101
+ * multi-tenant agent. Default: the bucket root (shared — set a root for
102
+ * multi-tenant use). The server rejects any `..` that would escape it.
103
+ */
104
+ root?: ((context: AgentToolContext) => string) | string;
105
+ }
106
+ /**
107
+ * Author-supplied config for `containerTool`.
108
+ * @experimental
109
+ */
110
+ interface ContainerToolOptions {
111
+ /** Override the model-facing description (what the tool does). */
112
+ description?: string;
113
+ /**
114
+ * Gate a call behind a human approval. Defaults to gating any command
115
+ * execution: an `exec`, AND a `fetch` whose path resolves to the privileged
116
+ * `/exec` route (both reach the same command-execution path in the container,
117
+ * so gating on the `op` name alone would let a `fetch` to `/exec` run a
118
+ * command unattended). A plain `fetch` to any other route runs unattended.
119
+ * Pass a boolean or your own predicate to change that. Evaluated from
120
+ * replay-stable input, so keep it deterministic.
121
+ */
122
+ needsApproval?: ((input: ContainerToolInput) => boolean) | boolean;
123
+ }
124
+ /**
125
+ * A batteries-included agent tool that drives Cloudflare Browser Rendering. One
126
+ * tool exposes every browser op (screenshot / pdf / content / scrape); the model
127
+ * picks via `op`. The call dispatches to the auto-registered `sandbox:invoke`
128
+ * action — which runs on an action ctx carrying `ctx.browser`. Importing the tool
129
+ * makes codegen register the dispatcher and provision the `BROWSER` binding; like
130
+ * any `ctx.browser` user, the app still supplies a `config.browser` thunk
131
+ * (`createBrowser({ binding: env.BROWSER, launch })` with the optional
132
+ * `@cloudflare/playwright` `launch` peer) to `createShardDO()` — codegen never
133
+ * injects that peer, so the browser op throws a directed error until it is wired.
134
+ *
135
+ * Security: the model chooses the `url` with no allowlist, so this is an SSRF
136
+ * surface — pass `opts.needsApproval` to gate calls a prompt-injected model
137
+ * could aim at internal/link-local endpoints.
138
+ *
139
+ * ```ts
140
+ * import { browserTool, defineAgent } from "@lunora/agent/sandbox";
141
+ *
142
+ * export const researcher = defineAgent({
143
+ * model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",
144
+ * tools: { browser: browserTool() },
145
+ * });
146
+ * ```
147
+ * @experimental
148
+ */
149
+ declare const browserTool: (options?: BrowserToolOptions) => AgentToolDefinition<BrowserToolInput, string>;
150
+ /**
151
+ * A batteries-included agent tool that talks to a declared Cloudflare
152
+ * Container. `name` is the `ctx.containers.&lt;name>` key (the `lunora/containers.ts`
153
+ * export). One tool exposes `fetch` (HTTP request) and `exec` (run a command);
154
+ * the model picks via `op`. The call dispatches to the auto-registered
155
+ * `sandbox:invoke` action, which carries `ctx.containers`.
156
+ *
157
+ * By default a read-only (GET/HEAD/OPTIONS, or method-omitted) `fetch` runs
158
+ * unattended while everything else is gated behind a human approval — an
159
+ * `exec`; a `fetch` whose path resolves to the privileged `/exec` route (both
160
+ * reach the same command-execution path in the container, so gating on the
161
+ * `op` name alone would let a `fetch` to `/exec` run a command unattended);
162
+ * and a `fetch` using a non-idempotent method (POST/PUT/PATCH/DELETE) to ANY
163
+ * route, since a prompt-injected model could otherwise mutate container state
164
+ * through some other privileged route just by avoiding the literal `/exec`
165
+ * path. A `fetch` can still reach any *other read* route unattended, so scope
166
+ * the container's GET routes accordingly. Pass `opts.needsApproval` to widen
167
+ * or disable the gate.
168
+ *
169
+ * ```ts
170
+ * import { containerTool, defineAgent } from "@lunora/agent/sandbox";
171
+ *
172
+ * export const ops = defineAgent({
173
+ * model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",
174
+ * tools: { sandbox: containerTool("sandbox") },
175
+ * });
176
+ * ```
177
+ * @experimental
178
+ */
179
+ declare const containerTool: (name: string, options?: ContainerToolOptions) => AgentToolDefinition<ContainerToolInput, string>;
180
+ /**
181
+ * A batteries-included agent tool exposing a persistent, R2-backed virtual
182
+ * filesystem. `bucket` is the R2 binding name; every path is scoped under the
183
+ * pinned `opts.root` (a `..` that would escape it is rejected). One tool exposes
184
+ * `ls`/`read`/`write`/`rm`/`stat`; the model picks via `op`. The call dispatches
185
+ * to the auto-registered `sandbox:invoke` action, which reads the bucket from
186
+ * `ctx.env`. workerd has no real shell — this is object-store-backed file I/O.
187
+ *
188
+ * By default the writing ops (`write`/`rm`) pause for a human approval while
189
+ * reads run unattended; pass `opts.needsApproval` to change that. Importing the
190
+ * tool registers the dispatcher; the app must declare the `r2_bucket` binding in
191
+ * `wrangler.jsonc` (the fs op throws a directed error until it is wired).
192
+ *
193
+ * ```ts
194
+ * import { defineAgent, fsTool } from "@lunora/agent/sandbox";
195
+ *
196
+ * export const coder = defineAgent({
197
+ * model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",
198
+ * tools: { fs: fsTool("SANDBOX_BUCKET", { root: "agents/coder" }) },
199
+ * });
200
+ * ```
201
+ * @experimental
202
+ */
203
+ declare const fsTool: (bucket: string, options?: FsToolOptions) => AgentToolDefinition<FsToolInput>;
204
+ export { type BrowserToolInput, type BrowserToolOptions, type ContainerToolInput, type ContainerToolOptions, type FsToolInput, type FsToolOptions, browserTool, containerTool, fsTool };
@@ -0,0 +1,204 @@
1
+ import { a as AgentToolContext, b as AgentToolDefinition } from "./packem_shared/types.d-boAM2Yi1.js";
2
+ import '@lunora/mail/inbound';
3
+ import 'ai';
4
+ /**
5
+ * The model-provided input to a {@link browserTool} call — a discriminated
6
+ * union on `op` so one tool exposes every headless-browser capability.
7
+ * @experimental
8
+ */
9
+ type BrowserToolInput = {
10
+ fullPage?: boolean;
11
+ op: "screenshot";
12
+ type?: "jpeg" | "png";
13
+ url: string;
14
+ } | {
15
+ op: "content";
16
+ url: string;
17
+ } | {
18
+ op: "pdf";
19
+ url: string;
20
+ } | {
21
+ op: "scrape";
22
+ selector?: string;
23
+ url: string;
24
+ };
25
+ /**
26
+ * The model-provided input to a {@link containerTool} call — a discriminated
27
+ * union on `op`. `fetch` sends an HTTP request to the container; `exec` asks it
28
+ * to run a command (routed as a POST to `/exec`, since the container surface
29
+ * exposes no first-class exec RPC — the container app must serve that route).
30
+ * @experimental
31
+ */
32
+ type ContainerToolInput = {
33
+ args?: string[];
34
+ command?: string;
35
+ op: "exec";
36
+ } | {
37
+ body?: string;
38
+ method?: string;
39
+ op: "fetch";
40
+ path: string;
41
+ };
42
+ /**
43
+ * Author-supplied config for `browserTool`.
44
+ * @experimental
45
+ */
46
+ interface BrowserToolOptions {
47
+ /** Override the model-facing description (what the tool does). */
48
+ description?: string;
49
+ /**
50
+ * Gate a call behind a human approval. Defaults to **unattended** (no gate),
51
+ * matching the AI SDK. `browserTool` fetches a model-chosen `url` with no
52
+ * allowlist, so it is an SSRF surface: a prompt-injected model can point it at
53
+ * an internal/link-local address and read the response back into context. Pass
54
+ * a boolean or a predicate to gate it (evaluated from replay-stable input, so
55
+ * keep it deterministic).
56
+ */
57
+ needsApproval?: ((input: BrowserToolInput) => boolean) | boolean;
58
+ }
59
+ /**
60
+ * The model-provided input to a {@link fsTool} call — a discriminated union on
61
+ * `op` over an R2-backed virtual filesystem. Paths are relative to the tool's
62
+ * pinned `root`; a `..` that escapes the root is rejected server-side.
63
+ * @experimental
64
+ */
65
+ type FsToolInput = {
66
+ op: "ls";
67
+ path?: string;
68
+ } | {
69
+ op: "read";
70
+ path: string;
71
+ } | {
72
+ op: "rm";
73
+ path: string;
74
+ } | {
75
+ op: "stat";
76
+ path: string;
77
+ } | {
78
+ content: string;
79
+ op: "write";
80
+ path: string;
81
+ };
82
+ /**
83
+ * Author-supplied config for `fsTool`.
84
+ * @experimental
85
+ */
86
+ interface FsToolOptions {
87
+ /** Override the model-facing description (what the tool does). */
88
+ description?: string;
89
+ /**
90
+ * Gate a call behind a human approval. Defaults to gating the WRITING ops
91
+ * (`write`, `rm`) — a prompt-injected model shouldn't silently overwrite or
92
+ * delete the sandbox — while `ls`/`read`/`stat` run unattended. Pass a boolean
93
+ * or a predicate (evaluated from replay-stable input, so keep it deterministic).
94
+ */
95
+ needsApproval?: ((input: FsToolInput) => boolean) | boolean;
96
+ /**
97
+ * The key prefix every path is scoped under, isolating this tool's files from
98
+ * the rest of the bucket. Either a fixed string (e.g. `"agents/support"`) or a
99
+ * function of the tool context — return `` `agents/${ctx.threadKey}` `` (or an
100
+ * owner-derived prefix) to give each run/user its OWN namespace in a
101
+ * multi-tenant agent. Default: the bucket root (shared — set a root for
102
+ * multi-tenant use). The server rejects any `..` that would escape it.
103
+ */
104
+ root?: ((context: AgentToolContext) => string) | string;
105
+ }
106
+ /**
107
+ * Author-supplied config for `containerTool`.
108
+ * @experimental
109
+ */
110
+ interface ContainerToolOptions {
111
+ /** Override the model-facing description (what the tool does). */
112
+ description?: string;
113
+ /**
114
+ * Gate a call behind a human approval. Defaults to gating any command
115
+ * execution: an `exec`, AND a `fetch` whose path resolves to the privileged
116
+ * `/exec` route (both reach the same command-execution path in the container,
117
+ * so gating on the `op` name alone would let a `fetch` to `/exec` run a
118
+ * command unattended). A plain `fetch` to any other route runs unattended.
119
+ * Pass a boolean or your own predicate to change that. Evaluated from
120
+ * replay-stable input, so keep it deterministic.
121
+ */
122
+ needsApproval?: ((input: ContainerToolInput) => boolean) | boolean;
123
+ }
124
+ /**
125
+ * A batteries-included agent tool that drives Cloudflare Browser Rendering. One
126
+ * tool exposes every browser op (screenshot / pdf / content / scrape); the model
127
+ * picks via `op`. The call dispatches to the auto-registered `sandbox:invoke`
128
+ * action — which runs on an action ctx carrying `ctx.browser`. Importing the tool
129
+ * makes codegen register the dispatcher and provision the `BROWSER` binding; like
130
+ * any `ctx.browser` user, the app still supplies a `config.browser` thunk
131
+ * (`createBrowser({ binding: env.BROWSER, launch })` with the optional
132
+ * `@cloudflare/playwright` `launch` peer) to `createShardDO()` — codegen never
133
+ * injects that peer, so the browser op throws a directed error until it is wired.
134
+ *
135
+ * Security: the model chooses the `url` with no allowlist, so this is an SSRF
136
+ * surface — pass `opts.needsApproval` to gate calls a prompt-injected model
137
+ * could aim at internal/link-local endpoints.
138
+ *
139
+ * ```ts
140
+ * import { browserTool, defineAgent } from "@lunora/agent/sandbox";
141
+ *
142
+ * export const researcher = defineAgent({
143
+ * model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",
144
+ * tools: { browser: browserTool() },
145
+ * });
146
+ * ```
147
+ * @experimental
148
+ */
149
+ declare const browserTool: (options?: BrowserToolOptions) => AgentToolDefinition<BrowserToolInput, string>;
150
+ /**
151
+ * A batteries-included agent tool that talks to a declared Cloudflare
152
+ * Container. `name` is the `ctx.containers.&lt;name>` key (the `lunora/containers.ts`
153
+ * export). One tool exposes `fetch` (HTTP request) and `exec` (run a command);
154
+ * the model picks via `op`. The call dispatches to the auto-registered
155
+ * `sandbox:invoke` action, which carries `ctx.containers`.
156
+ *
157
+ * By default a read-only (GET/HEAD/OPTIONS, or method-omitted) `fetch` runs
158
+ * unattended while everything else is gated behind a human approval — an
159
+ * `exec`; a `fetch` whose path resolves to the privileged `/exec` route (both
160
+ * reach the same command-execution path in the container, so gating on the
161
+ * `op` name alone would let a `fetch` to `/exec` run a command unattended);
162
+ * and a `fetch` using a non-idempotent method (POST/PUT/PATCH/DELETE) to ANY
163
+ * route, since a prompt-injected model could otherwise mutate container state
164
+ * through some other privileged route just by avoiding the literal `/exec`
165
+ * path. A `fetch` can still reach any *other read* route unattended, so scope
166
+ * the container's GET routes accordingly. Pass `opts.needsApproval` to widen
167
+ * or disable the gate.
168
+ *
169
+ * ```ts
170
+ * import { containerTool, defineAgent } from "@lunora/agent/sandbox";
171
+ *
172
+ * export const ops = defineAgent({
173
+ * model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",
174
+ * tools: { sandbox: containerTool("sandbox") },
175
+ * });
176
+ * ```
177
+ * @experimental
178
+ */
179
+ declare const containerTool: (name: string, options?: ContainerToolOptions) => AgentToolDefinition<ContainerToolInput, string>;
180
+ /**
181
+ * A batteries-included agent tool exposing a persistent, R2-backed virtual
182
+ * filesystem. `bucket` is the R2 binding name; every path is scoped under the
183
+ * pinned `opts.root` (a `..` that would escape it is rejected). One tool exposes
184
+ * `ls`/`read`/`write`/`rm`/`stat`; the model picks via `op`. The call dispatches
185
+ * to the auto-registered `sandbox:invoke` action, which reads the bucket from
186
+ * `ctx.env`. workerd has no real shell — this is object-store-backed file I/O.
187
+ *
188
+ * By default the writing ops (`write`/`rm`) pause for a human approval while
189
+ * reads run unattended; pass `opts.needsApproval` to change that. Importing the
190
+ * tool registers the dispatcher; the app must declare the `r2_bucket` binding in
191
+ * `wrangler.jsonc` (the fs op throws a directed error until it is wired).
192
+ *
193
+ * ```ts
194
+ * import { defineAgent, fsTool } from "@lunora/agent/sandbox";
195
+ *
196
+ * export const coder = defineAgent({
197
+ * model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",
198
+ * tools: { fs: fsTool("SANDBOX_BUCKET", { root: "agents/coder" }) },
199
+ * });
200
+ * ```
201
+ * @experimental
202
+ */
203
+ declare const fsTool: (bucket: string, options?: FsToolOptions) => AgentToolDefinition<FsToolInput>;
204
+ export { type BrowserToolInput, type BrowserToolOptions, type ContainerToolInput, type ContainerToolOptions, type FsToolInput, type FsToolOptions, browserTool, containerTool, fsTool };
@@ -0,0 +1,113 @@
1
+ import { LunoraError } from '@lunora/errors';
2
+ import { jsonSchema } from 'ai';
3
+ import { toFunctionReference, SANDBOX_INVOKE_PATH } from './packem_shared/AGENT_MODULE-Dnt_-AAT.mjs';
4
+
5
+ const SANDBOX_REF = toFunctionReference(SANDBOX_INVOKE_PATH);
6
+ const DEFAULT_BROWSER_DESCRIPTION = 'Drive a headless browser: screenshot a page, render it to PDF, read its HTML content, or scrape it. Set `op` to "screenshot" | "pdf" | "content" | "scrape" and pass the target `url`.';
7
+ const DEFAULT_CONTAINER_DESCRIPTION = 'Talk to a sandboxed container: `fetch` an HTTP path on it, or `exec` a command inside it. Set `op` to "fetch" (with `path`) or "exec" (with `command`).';
8
+ const DEFAULT_FS_DESCRIPTION = 'Read and write files in a persistent sandbox filesystem. Set `op` to "ls" (list a directory), "read", "write" (with `content`), "rm", or "stat", and pass `path`.';
9
+ const defaultFsGate = (input) => input.op === "write" || input.op === "rm";
10
+ const FS_TOOL_SCHEMA = jsonSchema({
11
+ properties: {
12
+ content: { description: "write: the file contents.", type: "string" },
13
+ op: { description: "The filesystem operation to run.", enum: ["ls", "read", "write", "rm", "stat"], type: "string" },
14
+ path: { description: "The file or directory path, relative to the sandbox root.", type: "string" }
15
+ },
16
+ required: ["op"],
17
+ type: "object"
18
+ });
19
+ const BROWSER_TOOL_SCHEMA = jsonSchema({
20
+ properties: {
21
+ fullPage: { description: "screenshot: capture the full scrollable page rather than just the viewport.", type: "boolean" },
22
+ op: { description: "The browser operation to run.", enum: ["screenshot", "scrape", "pdf", "content"], type: "string" },
23
+ selector: { description: "scrape: optional CSS selector hint to narrow the extraction.", type: "string" },
24
+ type: { description: "screenshot: image encoding (default png).", enum: ["png", "jpeg"], type: "string" },
25
+ url: { description: "The URL to operate on.", type: "string" }
26
+ },
27
+ required: ["op", "url"],
28
+ type: "object"
29
+ });
30
+ const CONTAINER_EXEC_ROUTE = "/exec";
31
+ const CONTAINER_PATH_QUERY_SPLIT = /[?#]/u;
32
+ const normalizeContainerPath = (path) => {
33
+ const [raw = ""] = (path ?? "").split(CONTAINER_PATH_QUERY_SPLIT);
34
+ const segments = [];
35
+ for (const segment of raw.split("/")) {
36
+ if (segment === "" || segment === ".") {
37
+ continue;
38
+ }
39
+ if (segment === "..") {
40
+ segments.pop();
41
+ continue;
42
+ }
43
+ segments.push(segment);
44
+ }
45
+ return `/${segments.join("/")}`;
46
+ };
47
+ const NON_IDEMPOTENT_METHODS = /* @__PURE__ */ new Set(["DELETE", "PATCH", "POST", "PUT"]);
48
+ const defaultContainerGate = (input) => {
49
+ if (input.op === "exec") {
50
+ return true;
51
+ }
52
+ if (normalizeContainerPath(input.path) === CONTAINER_EXEC_ROUTE) {
53
+ return true;
54
+ }
55
+ return NON_IDEMPOTENT_METHODS.has((input.method ?? "GET").toUpperCase());
56
+ };
57
+ const CONTAINER_TOOL_SCHEMA = jsonSchema({
58
+ properties: {
59
+ args: { description: "exec: command arguments.", items: { type: "string" }, type: "array" },
60
+ body: { description: "fetch: request body.", type: "string" },
61
+ command: { description: "exec: the command to run.", type: "string" },
62
+ method: { description: "fetch: HTTP method (default GET).", type: "string" },
63
+ op: { description: "The container operation to run.", enum: ["fetch", "exec"], type: "string" },
64
+ path: { description: "fetch: request path on the container (e.g. /health).", type: "string" }
65
+ },
66
+ required: ["op"],
67
+ type: "object"
68
+ });
69
+ const browserTool = (options = {}) => {
70
+ return {
71
+ description: options.description ?? DEFAULT_BROWSER_DESCRIPTION,
72
+ // Pin `kind` LAST so out-of-schema model input can never override it.
73
+ execute: (input, context) => context.run(SANDBOX_REF, { ...input, kind: "browser" }),
74
+ inputSchema: BROWSER_TOOL_SCHEMA,
75
+ isLunoraAgentTool: true,
76
+ ...options.needsApproval === void 0 ? {} : { needsApproval: options.needsApproval }
77
+ };
78
+ };
79
+ const containerTool = (name, options = {}) => {
80
+ if (typeof name !== "string" || name.length === 0) {
81
+ throw new LunoraError("INTERNAL", "@lunora/agent: containerTool requires a container `name` (the ctx.containers.<name> key from lunora/containers.ts)");
82
+ }
83
+ const needsApproval = options.needsApproval ?? defaultContainerGate;
84
+ return {
85
+ description: options.description ?? DEFAULT_CONTAINER_DESCRIPTION,
86
+ // Pin `kind`/`name` LAST so out-of-schema model input can never override
87
+ // the authoritative container the author pinned.
88
+ execute: (input, context) => context.run(SANDBOX_REF, { ...input, kind: "container", name }),
89
+ inputSchema: CONTAINER_TOOL_SCHEMA,
90
+ isLunoraAgentTool: true,
91
+ needsApproval
92
+ };
93
+ };
94
+ const fsTool = (bucket, options = {}) => {
95
+ if (typeof bucket !== "string" || bucket.length === 0) {
96
+ throw new LunoraError("INTERNAL", "@lunora/agent: fsTool requires an R2 `bucket` binding name (the r2_bucket declared in wrangler.jsonc)");
97
+ }
98
+ return {
99
+ description: options.description ?? DEFAULT_FS_DESCRIPTION,
100
+ // Pin kind/bucket/root LAST so out-of-schema model input can never override
101
+ // the authoritative bucket + sandbox root the author pinned. `root` may be
102
+ // a function of the ctx (per-run/owner isolation), resolved here.
103
+ execute: (input, context) => {
104
+ const root = typeof options.root === "function" ? options.root(context) : options.root ?? "";
105
+ return context.run(SANDBOX_REF, { ...input, bucket, kind: "fs", root });
106
+ },
107
+ inputSchema: FS_TOOL_SCHEMA,
108
+ isLunoraAgentTool: true,
109
+ needsApproval: options.needsApproval ?? defaultFsGate
110
+ };
111
+ };
112
+
113
+ export { browserTool, containerTool, fsTool };