sarvam-agents-client 0.2.0 → 0.2.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.
@@ -17,8 +17,11 @@ export interface AgentOptions {
17
17
  name: string;
18
18
  instructions?: string;
19
19
  model?: string;
20
+ /** Catalog endpoint to serve `model` from (agent.json `endpoint`). */
21
+ endpoint?: string;
20
22
  modelSettings?: ModelSettings;
21
23
  fallbackModels?: string[];
24
+ /** Exact list of built-ins to keep on; omit to keep the server default (all on). */
22
25
  tools?: BuiltinTool[];
23
26
  limits?: Limits;
24
27
  metadata?: Record<string, string>;
@@ -27,9 +30,10 @@ export declare class Agent {
27
30
  readonly name: string;
28
31
  readonly instructions: string;
29
32
  readonly model?: string;
33
+ readonly endpoint?: string;
30
34
  readonly modelSettings?: ModelSettings;
31
35
  readonly fallbackModels?: string[];
32
- readonly tools: readonly BuiltinTool[];
36
+ readonly tools?: readonly BuiltinTool[];
33
37
  readonly limits?: Limits;
34
38
  readonly metadata?: Record<string, string>;
35
39
  constructor(rawOpts: AgentOptions);
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Agent = void 0;
4
4
  const errors_js_1 = require("./errors.js");
5
5
  const fingerprint_js_1 = require("./fingerprint.js");
6
+ const builtins_js_1 = require("./builtins.js");
6
7
  const SLUG = /^[a-z0-9][a-z0-9-]{0,63}$/;
7
8
  const REASONING = ["off", "none", "low", "medium", "high", "xhigh", "max"];
8
9
  const TOOL_CHOICES = ["auto", "none"];
@@ -74,7 +75,7 @@ const LIMIT_KEYS = [
74
75
  ];
75
76
  class Agent {
76
77
  constructor(rawOpts) {
77
- var _a, _b, _c;
78
+ var _a, _b;
78
79
  const opts = normalize(rawOpts);
79
80
  for (const tool of (_a = opts.tools) !== null && _a !== void 0 ? _a : []) {
80
81
  if (tool instanceof Agent) {
@@ -85,18 +86,28 @@ class Agent {
85
86
  this.name = opts.name;
86
87
  this.instructions = (_b = opts.instructions) !== null && _b !== void 0 ? _b : "";
87
88
  this.model = opts.model;
89
+ this.endpoint = opts.endpoint;
88
90
  this.modelSettings = clone(opts.modelSettings);
89
91
  this.fallbackModels = clone(opts.fallbackModels);
90
- this.tools = clone((_c = opts.tools) !== null && _c !== void 0 ? _c : []);
92
+ this.tools = clone(opts.tools);
91
93
  this.limits = clone(opts.limits);
92
94
  this.metadata = clone(opts.metadata);
93
95
  this.check();
94
96
  deepFreeze(this);
95
97
  }
96
98
  check() {
99
+ var _a, _b;
97
100
  if (typeof this.name !== "string" || !SLUG.test(this.name)) {
98
101
  throw new errors_js_1.UserError(`name: ${JSON.stringify(this.name)} must match ${SLUG.source}`);
99
102
  }
103
+ for (const [key, value] of [["model", this.model], ["endpoint", this.endpoint]]) {
104
+ if (value !== undefined && (typeof value !== "string" || value === "")) {
105
+ throw new errors_js_1.UserError(`${key}: must be a non-empty string`);
106
+ }
107
+ }
108
+ if (this.endpoint !== undefined && this.model === undefined) {
109
+ throw new errors_js_1.UserError("endpoint: needs model; set the model this endpoint serves");
110
+ }
100
111
  const ms = this.modelSettings;
101
112
  if (ms !== undefined) {
102
113
  if (ms.reasoning !== undefined && REASONING.indexOf(ms.reasoning.effort) === -1) {
@@ -129,16 +140,33 @@ class Agent {
129
140
  throw new errors_js_1.UserError(`metadata: keys starting with 'sdk_' are reserved (${bad.join(", ")})`);
130
141
  }
131
142
  }
132
- const names = this.tools.map((t) => t.name);
143
+ const names = ((_a = this.tools) !== null && _a !== void 0 ? _a : []).map((t) => t.name);
144
+ for (const n of names) {
145
+ if (builtins_js_1.BUILTIN_NAMES.indexOf(n) === -1) {
146
+ throw new errors_js_1.UserError(`tools: unknown built-in tool ${JSON.stringify(n)} (known: ${builtins_js_1.BUILTIN_NAMES.join(", ")})`);
147
+ }
148
+ }
133
149
  const dupes = Array.from(new Set(names.filter((n, i) => names.indexOf(n) !== i))).sort();
134
150
  if (dupes.length > 0) {
135
151
  throw new errors_js_1.UserError(`tools: duplicate built-in tool(s): ${dupes.join(", ")}`);
136
152
  }
153
+ for (const t of (_b = this.tools) !== null && _b !== void 0 ? _b : []) {
154
+ if (t.options === undefined || Object.keys(t.options).length === 0)
155
+ continue;
156
+ if (t.name !== "web_search")
157
+ throw new errors_js_1.UserError(`tools: ${t.name} takes no options`);
158
+ (0, builtins_js_1.checkWebSearchOptions)(t.options);
159
+ }
160
+ if (names.indexOf("code_mode") !== -1 && names.indexOf("sandbox") === -1) {
161
+ throw new errors_js_1.UserError("tools: codeMode needs sandbox; add sandbox() to tools");
162
+ }
137
163
  }
138
164
  baseSettings() {
139
165
  const out = { version: 1 };
140
166
  if (this.model !== undefined)
141
167
  out["model"] = this.model;
168
+ if (this.endpoint !== undefined)
169
+ out["endpoint"] = this.endpoint;
142
170
  const ms = this.modelSettings;
143
171
  if (ms !== undefined) {
144
172
  if (ms.reasoning !== undefined)
@@ -165,10 +193,13 @@ class Agent {
165
193
  if (Object.keys(limits).length > 0)
166
194
  out["limits"] = limits;
167
195
  }
168
- if (this.tools.length > 0) {
196
+ if (this.tools !== undefined) {
197
+ // Built-ins are on by default, so an exact list writes every unlisted one as off.
169
198
  const tools = {};
170
- for (const t of this.tools) {
171
- tools[t.name] = t.options !== undefined && Object.keys(t.options).length > 0 ? clone(t.options) : true;
199
+ for (const n of builtins_js_1.BUILTIN_NAMES) {
200
+ const t = this.tools.find((x) => x.name === n);
201
+ tools[n] = t === undefined ? false
202
+ : t.options !== undefined && Object.keys(t.options).length > 0 ? clone(t.options) : true;
172
203
  }
173
204
  out["builtin_tools"] = tools;
174
205
  }
@@ -0,0 +1,4 @@
1
+ /** The agent.json builtin_tools switches, in schema order. */
2
+ export declare const BUILTIN_NAMES: readonly ["web_search", "sandbox", "code_mode", "todo", "schedules", "clarify", "subagents"];
3
+ /** The server's web search rules, for options built by hand (`{ name: "web_search", options }`). */
4
+ export declare function checkWebSearchOptions(options: Record<string, unknown>): void;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BUILTIN_NAMES = void 0;
4
+ exports.checkWebSearchOptions = checkWebSearchOptions;
5
+ /** Internal: built-in tool names and checks shared by tools.ts and agent.ts. Not re-exported from index.ts. */
6
+ const errors_js_1 = require("./errors.js");
7
+ /** The agent.json builtin_tools switches, in schema order. */
8
+ exports.BUILTIN_NAMES = ["web_search", "sandbox", "code_mode", "todo", "schedules", "clarify", "subagents"];
9
+ /** The server's web search rules, for options built by hand (`{ name: "web_search", options }`). */
10
+ function checkWebSearchOptions(options) {
11
+ const keys = Object.keys(options);
12
+ for (const k of keys) {
13
+ if (k !== "allowed_domains" && k !== "blocked_domains")
14
+ throw new errors_js_1.UserError(`tools.web_search: unknown option ${JSON.stringify(k)}`);
15
+ const v = options[k];
16
+ if (!Array.isArray(v) || v.length === 0 || v.some((d) => typeof d !== "string")) {
17
+ throw new errors_js_1.UserError(`tools.web_search.${k}: must be a non-empty list of domains`);
18
+ }
19
+ }
20
+ if (keys.length > 1)
21
+ throw new errors_js_1.UserError("tools.web_search: give allowed_domains or blocked_domains, not both");
22
+ }
@@ -1,8 +1,8 @@
1
- /** Built-in (server-side) tools. Listing a tool enables it; unlisted tools keep the server default. */
2
1
  export interface BuiltinTool {
3
2
  readonly name: string;
4
3
  readonly options?: Record<string, unknown>;
5
4
  }
5
+ /** Give at most one of `filters.allowedDomains` and `blockedDomains`, and never an empty list. */
6
6
  export declare function webSearchTool(opts?: {
7
7
  filters?: {
8
8
  allowedDomains?: string[] | null;
@@ -1,16 +1,31 @@
1
1
  "use strict";
2
- /** Built-in (server-side) tools. Listing a tool enables it; unlisted tools keep the server default. */
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
3
  exports.subagents = exports.clarify = exports.schedules = exports.todo = exports.codeMode = exports.sandbox = void 0;
5
4
  exports.webSearchTool = webSearchTool;
5
+ /**
6
+ * Built-in (server-side) tools. Every built-in is on by default. Passing `tools` to `Agent` makes the list exact:
7
+ * listed built-ins stay on and the rest are turned off. Leave `tools` out to keep the server default.
8
+ */
9
+ const errors_js_1 = require("./errors.js");
10
+ /** Give at most one of `filters.allowedDomains` and `blockedDomains`, and never an empty list. */
6
11
  function webSearchTool(opts = {}) {
7
- var _a;
12
+ var _a, _b;
8
13
  const options = {};
9
14
  const allowed = (_a = opts.filters) === null || _a === void 0 ? void 0 : _a.allowedDomains;
15
+ const blocked = (_b = opts.blockedDomains) !== null && _b !== void 0 ? _b : undefined;
16
+ if (allowed !== undefined && allowed !== null && blocked !== undefined) {
17
+ throw new errors_js_1.UserError("webSearchTool: give filters.allowedDomains or blockedDomains, not both");
18
+ }
19
+ if (allowed !== undefined && allowed !== null && allowed.length === 0) {
20
+ throw new errors_js_1.UserError("webSearchTool.filters.allowedDomains: must not be empty (leave it out for no restriction)");
21
+ }
22
+ if (blocked !== undefined && blocked.length === 0) {
23
+ throw new errors_js_1.UserError("webSearchTool.blockedDomains: must not be empty (leave it out for no restriction)");
24
+ }
10
25
  if (allowed !== undefined && allowed !== null)
11
26
  options["allowed_domains"] = allowed.slice();
12
- if (opts.blockedDomains !== undefined)
13
- options["blocked_domains"] = opts.blockedDomains.slice();
27
+ if (blocked !== undefined)
28
+ options["blocked_domains"] = blocked.slice();
14
29
  const tool = Object.keys(options).length > 0 ? { name: "web_search", options } : { name: "web_search" };
15
30
  return tool;
16
31
  }
@@ -17,8 +17,11 @@ export interface AgentOptions {
17
17
  name: string;
18
18
  instructions?: string;
19
19
  model?: string;
20
+ /** Catalog endpoint to serve `model` from (agent.json `endpoint`). */
21
+ endpoint?: string;
20
22
  modelSettings?: ModelSettings;
21
23
  fallbackModels?: string[];
24
+ /** Exact list of built-ins to keep on; omit to keep the server default (all on). */
22
25
  tools?: BuiltinTool[];
23
26
  limits?: Limits;
24
27
  metadata?: Record<string, string>;
@@ -27,9 +30,10 @@ export declare class Agent {
27
30
  readonly name: string;
28
31
  readonly instructions: string;
29
32
  readonly model?: string;
33
+ readonly endpoint?: string;
30
34
  readonly modelSettings?: ModelSettings;
31
35
  readonly fallbackModels?: string[];
32
- readonly tools: readonly BuiltinTool[];
36
+ readonly tools?: readonly BuiltinTool[];
33
37
  readonly limits?: Limits;
34
38
  readonly metadata?: Record<string, string>;
35
39
  constructor(rawOpts: AgentOptions);
@@ -1,5 +1,6 @@
1
1
  import { NotSupportedError, UserError } from "./errors.js";
2
2
  import { settingsFingerprint } from "./fingerprint.js";
3
+ import { BUILTIN_NAMES, checkWebSearchOptions } from "./builtins.js";
3
4
  const SLUG = /^[a-z0-9][a-z0-9-]{0,63}$/;
4
5
  const REASONING = ["off", "none", "low", "medium", "high", "xhigh", "max"];
5
6
  const TOOL_CHOICES = ["auto", "none"];
@@ -71,7 +72,7 @@ const LIMIT_KEYS = [
71
72
  ];
72
73
  export class Agent {
73
74
  constructor(rawOpts) {
74
- var _a, _b, _c;
75
+ var _a, _b;
75
76
  const opts = normalize(rawOpts);
76
77
  for (const tool of (_a = opts.tools) !== null && _a !== void 0 ? _a : []) {
77
78
  if (tool instanceof Agent) {
@@ -82,18 +83,28 @@ export class Agent {
82
83
  this.name = opts.name;
83
84
  this.instructions = (_b = opts.instructions) !== null && _b !== void 0 ? _b : "";
84
85
  this.model = opts.model;
86
+ this.endpoint = opts.endpoint;
85
87
  this.modelSettings = clone(opts.modelSettings);
86
88
  this.fallbackModels = clone(opts.fallbackModels);
87
- this.tools = clone((_c = opts.tools) !== null && _c !== void 0 ? _c : []);
89
+ this.tools = clone(opts.tools);
88
90
  this.limits = clone(opts.limits);
89
91
  this.metadata = clone(opts.metadata);
90
92
  this.check();
91
93
  deepFreeze(this);
92
94
  }
93
95
  check() {
96
+ var _a, _b;
94
97
  if (typeof this.name !== "string" || !SLUG.test(this.name)) {
95
98
  throw new UserError(`name: ${JSON.stringify(this.name)} must match ${SLUG.source}`);
96
99
  }
100
+ for (const [key, value] of [["model", this.model], ["endpoint", this.endpoint]]) {
101
+ if (value !== undefined && (typeof value !== "string" || value === "")) {
102
+ throw new UserError(`${key}: must be a non-empty string`);
103
+ }
104
+ }
105
+ if (this.endpoint !== undefined && this.model === undefined) {
106
+ throw new UserError("endpoint: needs model; set the model this endpoint serves");
107
+ }
97
108
  const ms = this.modelSettings;
98
109
  if (ms !== undefined) {
99
110
  if (ms.reasoning !== undefined && REASONING.indexOf(ms.reasoning.effort) === -1) {
@@ -126,16 +137,33 @@ export class Agent {
126
137
  throw new UserError(`metadata: keys starting with 'sdk_' are reserved (${bad.join(", ")})`);
127
138
  }
128
139
  }
129
- const names = this.tools.map((t) => t.name);
140
+ const names = ((_a = this.tools) !== null && _a !== void 0 ? _a : []).map((t) => t.name);
141
+ for (const n of names) {
142
+ if (BUILTIN_NAMES.indexOf(n) === -1) {
143
+ throw new UserError(`tools: unknown built-in tool ${JSON.stringify(n)} (known: ${BUILTIN_NAMES.join(", ")})`);
144
+ }
145
+ }
130
146
  const dupes = Array.from(new Set(names.filter((n, i) => names.indexOf(n) !== i))).sort();
131
147
  if (dupes.length > 0) {
132
148
  throw new UserError(`tools: duplicate built-in tool(s): ${dupes.join(", ")}`);
133
149
  }
150
+ for (const t of (_b = this.tools) !== null && _b !== void 0 ? _b : []) {
151
+ if (t.options === undefined || Object.keys(t.options).length === 0)
152
+ continue;
153
+ if (t.name !== "web_search")
154
+ throw new UserError(`tools: ${t.name} takes no options`);
155
+ checkWebSearchOptions(t.options);
156
+ }
157
+ if (names.indexOf("code_mode") !== -1 && names.indexOf("sandbox") === -1) {
158
+ throw new UserError("tools: codeMode needs sandbox; add sandbox() to tools");
159
+ }
134
160
  }
135
161
  baseSettings() {
136
162
  const out = { version: 1 };
137
163
  if (this.model !== undefined)
138
164
  out["model"] = this.model;
165
+ if (this.endpoint !== undefined)
166
+ out["endpoint"] = this.endpoint;
139
167
  const ms = this.modelSettings;
140
168
  if (ms !== undefined) {
141
169
  if (ms.reasoning !== undefined)
@@ -162,10 +190,13 @@ export class Agent {
162
190
  if (Object.keys(limits).length > 0)
163
191
  out["limits"] = limits;
164
192
  }
165
- if (this.tools.length > 0) {
193
+ if (this.tools !== undefined) {
194
+ // Built-ins are on by default, so an exact list writes every unlisted one as off.
166
195
  const tools = {};
167
- for (const t of this.tools) {
168
- tools[t.name] = t.options !== undefined && Object.keys(t.options).length > 0 ? clone(t.options) : true;
196
+ for (const n of BUILTIN_NAMES) {
197
+ const t = this.tools.find((x) => x.name === n);
198
+ tools[n] = t === undefined ? false
199
+ : t.options !== undefined && Object.keys(t.options).length > 0 ? clone(t.options) : true;
169
200
  }
170
201
  out["builtin_tools"] = tools;
171
202
  }
@@ -0,0 +1,4 @@
1
+ /** The agent.json builtin_tools switches, in schema order. */
2
+ export declare const BUILTIN_NAMES: readonly ["web_search", "sandbox", "code_mode", "todo", "schedules", "clarify", "subagents"];
3
+ /** The server's web search rules, for options built by hand (`{ name: "web_search", options }`). */
4
+ export declare function checkWebSearchOptions(options: Record<string, unknown>): void;
@@ -0,0 +1,18 @@
1
+ /** Internal: built-in tool names and checks shared by tools.ts and agent.ts. Not re-exported from index.ts. */
2
+ import { UserError } from "./errors.js";
3
+ /** The agent.json builtin_tools switches, in schema order. */
4
+ export const BUILTIN_NAMES = ["web_search", "sandbox", "code_mode", "todo", "schedules", "clarify", "subagents"];
5
+ /** The server's web search rules, for options built by hand (`{ name: "web_search", options }`). */
6
+ export function checkWebSearchOptions(options) {
7
+ const keys = Object.keys(options);
8
+ for (const k of keys) {
9
+ if (k !== "allowed_domains" && k !== "blocked_domains")
10
+ throw new UserError(`tools.web_search: unknown option ${JSON.stringify(k)}`);
11
+ const v = options[k];
12
+ if (!Array.isArray(v) || v.length === 0 || v.some((d) => typeof d !== "string")) {
13
+ throw new UserError(`tools.web_search.${k}: must be a non-empty list of domains`);
14
+ }
15
+ }
16
+ if (keys.length > 1)
17
+ throw new UserError("tools.web_search: give allowed_domains or blocked_domains, not both");
18
+ }
@@ -1,8 +1,8 @@
1
- /** Built-in (server-side) tools. Listing a tool enables it; unlisted tools keep the server default. */
2
1
  export interface BuiltinTool {
3
2
  readonly name: string;
4
3
  readonly options?: Record<string, unknown>;
5
4
  }
5
+ /** Give at most one of `filters.allowedDomains` and `blockedDomains`, and never an empty list. */
6
6
  export declare function webSearchTool(opts?: {
7
7
  filters?: {
8
8
  allowedDomains?: string[] | null;
@@ -1,12 +1,27 @@
1
- /** Built-in (server-side) tools. Listing a tool enables it; unlisted tools keep the server default. */
1
+ /**
2
+ * Built-in (server-side) tools. Every built-in is on by default. Passing `tools` to `Agent` makes the list exact:
3
+ * listed built-ins stay on and the rest are turned off. Leave `tools` out to keep the server default.
4
+ */
5
+ import { UserError } from "./errors.js";
6
+ /** Give at most one of `filters.allowedDomains` and `blockedDomains`, and never an empty list. */
2
7
  export function webSearchTool(opts = {}) {
3
- var _a;
8
+ var _a, _b;
4
9
  const options = {};
5
10
  const allowed = (_a = opts.filters) === null || _a === void 0 ? void 0 : _a.allowedDomains;
11
+ const blocked = (_b = opts.blockedDomains) !== null && _b !== void 0 ? _b : undefined;
12
+ if (allowed !== undefined && allowed !== null && blocked !== undefined) {
13
+ throw new UserError("webSearchTool: give filters.allowedDomains or blockedDomains, not both");
14
+ }
15
+ if (allowed !== undefined && allowed !== null && allowed.length === 0) {
16
+ throw new UserError("webSearchTool.filters.allowedDomains: must not be empty (leave it out for no restriction)");
17
+ }
18
+ if (blocked !== undefined && blocked.length === 0) {
19
+ throw new UserError("webSearchTool.blockedDomains: must not be empty (leave it out for no restriction)");
20
+ }
6
21
  if (allowed !== undefined && allowed !== null)
7
22
  options["allowed_domains"] = allowed.slice();
8
- if (opts.blockedDomains !== undefined)
9
- options["blocked_domains"] = opts.blockedDomains.slice();
23
+ if (blocked !== undefined)
24
+ options["blocked_domains"] = blocked.slice();
10
25
  const tool = Object.keys(options).length > 0 ? { name: "web_search", options } : { name: "web_search" };
11
26
  return tool;
12
27
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sarvam-agents-client",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Client for the Sarvam Agents API: chat with an agent, exchange files with it, and set it up from your own code.",
5
5
  "author": "Sarvam AI <support@sarvam.ai>",
6
6
  "license": "MIT",
@@ -1,5 +1,6 @@
1
1
  import { NotSupportedError, UserError } from "./errors.js";
2
2
  import { settingsFingerprint } from "./fingerprint.js";
3
+ import { BUILTIN_NAMES, checkWebSearchOptions } from "./builtins.js";
3
4
  import { BuiltinTool } from "./tools.js";
4
5
 
5
6
  const SLUG = /^[a-z0-9][a-z0-9-]{0,63}$/;
@@ -24,8 +25,11 @@ export interface AgentOptions {
24
25
  name: string;
25
26
  instructions?: string;
26
27
  model?: string;
28
+ /** Catalog endpoint to serve `model` from (agent.json `endpoint`). */
29
+ endpoint?: string;
27
30
  modelSettings?: ModelSettings;
28
31
  fallbackModels?: string[];
32
+ /** Exact list of built-ins to keep on; omit to keep the server default (all on). */
29
33
  tools?: BuiltinTool[];
30
34
  limits?: Limits;
31
35
  metadata?: Record<string, string>;
@@ -104,9 +108,10 @@ export class Agent {
104
108
  readonly name: string;
105
109
  readonly instructions: string;
106
110
  readonly model?: string;
111
+ readonly endpoint?: string;
107
112
  readonly modelSettings?: ModelSettings;
108
113
  readonly fallbackModels?: string[];
109
- readonly tools: readonly BuiltinTool[];
114
+ readonly tools?: readonly BuiltinTool[];
110
115
  readonly limits?: Limits;
111
116
  readonly metadata?: Record<string, string>;
112
117
 
@@ -121,9 +126,10 @@ export class Agent {
121
126
  this.name = opts.name;
122
127
  this.instructions = opts.instructions ?? "";
123
128
  this.model = opts.model;
129
+ this.endpoint = opts.endpoint;
124
130
  this.modelSettings = clone(opts.modelSettings);
125
131
  this.fallbackModels = clone(opts.fallbackModels);
126
- this.tools = clone(opts.tools ?? []);
132
+ this.tools = clone(opts.tools);
127
133
  this.limits = clone(opts.limits);
128
134
  this.metadata = clone(opts.metadata);
129
135
  this.check();
@@ -134,6 +140,14 @@ export class Agent {
134
140
  if (typeof this.name !== "string" || !SLUG.test(this.name)) {
135
141
  throw new UserError(`name: ${JSON.stringify(this.name)} must match ${SLUG.source}`);
136
142
  }
143
+ for (const [key, value] of [["model", this.model], ["endpoint", this.endpoint]] as [string, unknown][]) {
144
+ if (value !== undefined && (typeof value !== "string" || value === "")) {
145
+ throw new UserError(`${key}: must be a non-empty string`);
146
+ }
147
+ }
148
+ if (this.endpoint !== undefined && this.model === undefined) {
149
+ throw new UserError("endpoint: needs model; set the model this endpoint serves");
150
+ }
137
151
  const ms = this.modelSettings;
138
152
  if (ms !== undefined) {
139
153
  if (ms.reasoning !== undefined && REASONING.indexOf(ms.reasoning.effort) === -1) {
@@ -168,16 +182,30 @@ export class Agent {
168
182
  throw new UserError(`metadata: keys starting with 'sdk_' are reserved (${bad.join(", ")})`);
169
183
  }
170
184
  }
171
- const names = this.tools.map((t) => t.name);
185
+ const names = (this.tools ?? []).map((t) => t.name);
186
+ for (const n of names) {
187
+ if ((BUILTIN_NAMES as readonly string[]).indexOf(n) === -1) {
188
+ throw new UserError(`tools: unknown built-in tool ${JSON.stringify(n)} (known: ${BUILTIN_NAMES.join(", ")})`);
189
+ }
190
+ }
172
191
  const dupes = Array.from(new Set(names.filter((n, i) => names.indexOf(n) !== i))).sort();
173
192
  if (dupes.length > 0) {
174
193
  throw new UserError(`tools: duplicate built-in tool(s): ${dupes.join(", ")}`);
175
194
  }
195
+ for (const t of this.tools ?? []) {
196
+ if (t.options === undefined || Object.keys(t.options).length === 0) continue;
197
+ if (t.name !== "web_search") throw new UserError(`tools: ${t.name} takes no options`);
198
+ checkWebSearchOptions(t.options);
199
+ }
200
+ if (names.indexOf("code_mode") !== -1 && names.indexOf("sandbox") === -1) {
201
+ throw new UserError("tools: codeMode needs sandbox; add sandbox() to tools");
202
+ }
176
203
  }
177
204
 
178
205
  private baseSettings(): Record<string, unknown> {
179
206
  const out: Record<string, unknown> = { version: 1 };
180
207
  if (this.model !== undefined) out["model"] = this.model;
208
+ if (this.endpoint !== undefined) out["endpoint"] = this.endpoint;
181
209
  const ms = this.modelSettings;
182
210
  if (ms !== undefined) {
183
211
  if (ms.reasoning !== undefined) out["reasoning"] = ms.reasoning.effort;
@@ -197,10 +225,13 @@ export class Agent {
197
225
  }
198
226
  if (Object.keys(limits).length > 0) out["limits"] = limits;
199
227
  }
200
- if (this.tools.length > 0) {
228
+ if (this.tools !== undefined) {
229
+ // Built-ins are on by default, so an exact list writes every unlisted one as off.
201
230
  const tools: Record<string, unknown> = {};
202
- for (const t of this.tools) {
203
- tools[t.name] = t.options !== undefined && Object.keys(t.options).length > 0 ? clone(t.options) : true;
231
+ for (const n of BUILTIN_NAMES) {
232
+ const t = this.tools.find((x) => x.name === n);
233
+ tools[n] = t === undefined ? false
234
+ : t.options !== undefined && Object.keys(t.options).length > 0 ? clone(t.options) : true;
204
235
  }
205
236
  out["builtin_tools"] = tools;
206
237
  }
@@ -0,0 +1,19 @@
1
+ /** Internal: built-in tool names and checks shared by tools.ts and agent.ts. Not re-exported from index.ts. */
2
+ import { UserError } from "./errors.js";
3
+
4
+ /** The agent.json builtin_tools switches, in schema order. */
5
+ export const BUILTIN_NAMES = ["web_search", "sandbox", "code_mode", "todo", "schedules", "clarify", "subagents"] as const;
6
+
7
+ /** The server's web search rules, for options built by hand (`{ name: "web_search", options }`). */
8
+ export function checkWebSearchOptions(options: Record<string, unknown>): void {
9
+ const keys = Object.keys(options);
10
+ for (const k of keys) {
11
+ if (k !== "allowed_domains" && k !== "blocked_domains") throw new UserError(`tools.web_search: unknown option ${JSON.stringify(k)}`);
12
+ const v = options[k];
13
+ if (!Array.isArray(v) || v.length === 0 || v.some((d) => typeof d !== "string")) {
14
+ throw new UserError(`tools.web_search.${k}: must be a non-empty list of domains`);
15
+ }
16
+ }
17
+ if (keys.length > 1) throw new UserError("tools.web_search: give allowed_domains or blocked_domains, not both");
18
+ }
19
+
@@ -1,18 +1,33 @@
1
- /** Built-in (server-side) tools. Listing a tool enables it; unlisted tools keep the server default. */
1
+ /**
2
+ * Built-in (server-side) tools. Every built-in is on by default. Passing `tools` to `Agent` makes the list exact:
3
+ * listed built-ins stay on and the rest are turned off. Leave `tools` out to keep the server default.
4
+ */
5
+ import { UserError } from "./errors.js";
2
6
 
3
7
  export interface BuiltinTool {
4
8
  readonly name: string;
5
9
  readonly options?: Record<string, unknown>;
6
10
  }
7
11
 
12
+ /** Give at most one of `filters.allowedDomains` and `blockedDomains`, and never an empty list. */
8
13
  export function webSearchTool(opts: {
9
14
  filters?: { allowedDomains?: string[] | null };
10
15
  blockedDomains?: string[];
11
16
  } = {}): BuiltinTool {
12
17
  const options: Record<string, unknown> = {};
13
18
  const allowed = opts.filters?.allowedDomains;
19
+ const blocked = opts.blockedDomains ?? undefined;
20
+ if (allowed !== undefined && allowed !== null && blocked !== undefined) {
21
+ throw new UserError("webSearchTool: give filters.allowedDomains or blockedDomains, not both");
22
+ }
23
+ if (allowed !== undefined && allowed !== null && allowed.length === 0) {
24
+ throw new UserError("webSearchTool.filters.allowedDomains: must not be empty (leave it out for no restriction)");
25
+ }
26
+ if (blocked !== undefined && blocked.length === 0) {
27
+ throw new UserError("webSearchTool.blockedDomains: must not be empty (leave it out for no restriction)");
28
+ }
14
29
  if (allowed !== undefined && allowed !== null) options["allowed_domains"] = allowed.slice();
15
- if (opts.blockedDomains !== undefined) options["blocked_domains"] = opts.blockedDomains.slice();
30
+ if (blocked !== undefined) options["blocked_domains"] = blocked.slice();
16
31
  const tool: BuiltinTool = Object.keys(options).length > 0 ? { name: "web_search", options } : { name: "web_search" };
17
32
  return tool;
18
33
  }