@sonzai-labs/agents 1.0.11 → 1.1.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/dist/index.cjs CHANGED
@@ -72,15 +72,18 @@ var HTTPClient = class {
72
72
  headers;
73
73
  timeout;
74
74
  maxRetries;
75
+ fetchFn;
75
76
  constructor(options) {
76
77
  this.baseUrl = options.baseUrl.replace(/\/$/, "");
77
78
  this.headers = {
78
79
  Authorization: `Bearer ${options.apiKey}`,
79
80
  "Content-Type": "application/json",
80
- "User-Agent": "sonzai-typescript/1.0.11"
81
+ "User-Agent": "sonzai-typescript/1.1.0",
82
+ ...options.defaultHeaders
81
83
  };
82
84
  this.timeout = options.timeout;
83
85
  this.maxRetries = options.maxRetries;
86
+ this.fetchFn = options.customFetch ?? fetch;
84
87
  }
85
88
  async request(method, path, options) {
86
89
  const url = this.buildUrl(path, options?.params);
@@ -90,7 +93,7 @@ var HTTPClient = class {
90
93
  const controller = new AbortController();
91
94
  const timer = setTimeout(() => controller.abort(), this.timeout);
92
95
  try {
93
- const response = await fetch(url, {
96
+ const response = await this.fetchFn(url, {
94
97
  method,
95
98
  headers: this.headers,
96
99
  body: options?.body ? JSON.stringify(options.body) : void 0,
@@ -144,12 +147,38 @@ var HTTPClient = class {
144
147
  async delete(path, params) {
145
148
  return this.request("DELETE", path, { params });
146
149
  }
150
+ async uploadFile(path, fileName, fileData, contentType) {
151
+ const url = this.buildUrl(path);
152
+ const formData = new FormData();
153
+ let blob;
154
+ if (fileData instanceof Blob) {
155
+ blob = fileData;
156
+ } else {
157
+ const data = fileData instanceof ArrayBuffer ? fileData : new Uint8Array(fileData).buffer;
158
+ blob = new Blob([data], { type: contentType });
159
+ }
160
+ formData.append("file", blob, fileName);
161
+ const response = await this.fetchFn(url, {
162
+ method: "POST",
163
+ headers: {
164
+ Authorization: this.headers["Authorization"],
165
+ "User-Agent": this.headers["User-Agent"]
166
+ },
167
+ body: formData
168
+ });
169
+ await this.throwOnError(response);
170
+ const contentTypeResp = response.headers.get("content-type") ?? "";
171
+ if (contentTypeResp.includes("application/json")) {
172
+ return await response.json();
173
+ }
174
+ return await response.text();
175
+ }
147
176
  async *streamSSE(method, path, body) {
148
177
  const url = this.buildUrl(path);
149
178
  const controller = new AbortController();
150
179
  const timer = setTimeout(() => controller.abort(), this.timeout * 10);
151
180
  try {
152
- const response = await fetch(url, {
181
+ const response = await this.fetchFn(url, {
153
182
  method,
154
183
  headers: {
155
184
  ...this.headers,
@@ -755,9 +784,14 @@ var Inventory = class {
755
784
  if (options.item_type) params.item_type = options.item_type;
756
785
  if (options.query) params.query = options.query;
757
786
  if (options.project_id) params.project_id = options.project_id;
787
+ if (options.filters) params.filters = options.filters;
788
+ if (options.sort_by) params.sort_by = options.sort_by;
789
+ if (options.sort_order) params.sort_order = options.sort_order;
758
790
  if (options.aggregations) params.aggregations = options.aggregations;
759
791
  if (options.group_by) params.group_by = options.group_by;
760
792
  if (options.limit) params.limit = String(options.limit);
793
+ if (options.offset) params.offset = String(options.offset);
794
+ if (options.cursor) params.cursor = options.cursor;
761
795
  if (options.instanceId) params.instance_id = options.instanceId;
762
796
  return this.http.get(
763
797
  `/api/v1/agents/${agentId}/users/${encodeURIComponent(userId)}/inventory`,
@@ -1181,8 +1215,7 @@ var Agents = class {
1181
1215
  body.tool_capabilities = options.toolCapabilities;
1182
1216
  if (options.language) body.language = options.language;
1183
1217
  if (options.seedMemories) body.seed_memories = options.seedMemories;
1184
- if (options.loreContext)
1185
- body.lore_generation_context = options.loreContext;
1218
+ if (options.loreContext) body.lore_generation_context = options.loreContext;
1186
1219
  if (options.generateOriginStory != null)
1187
1220
  body.generate_origin_story = options.generateOriginStory;
1188
1221
  if (options.generatePersonalizedMemories != null)
@@ -1344,10 +1377,7 @@ var Agents = class {
1344
1377
  if (options.config) body.config = options.config;
1345
1378
  if (options.model) body.model = options.model;
1346
1379
  if (options.configOverride) body.config_override = options.configOverride;
1347
- return this.http.post(
1348
- `/api/v1/agents/${agentId}/simulate`,
1349
- body
1350
- );
1380
+ return this.http.post(`/api/v1/agents/${agentId}/simulate`, body);
1351
1381
  }
1352
1382
  /** Run simulation + evaluation combined (two-step: POST to start, then stream SSE). */
1353
1383
  async *runEval(agentId, options) {
@@ -1373,10 +1403,7 @@ var Agents = class {
1373
1403
  if (options.adaptationTemplateId)
1374
1404
  body.adaptation_template_id = options.adaptationTemplateId;
1375
1405
  if (options.qualityOnly != null) body.quality_only = options.qualityOnly;
1376
- return this.http.post(
1377
- `/api/v1/agents/${agentId}/run-eval`,
1378
- body
1379
- );
1406
+ return this.http.post(`/api/v1/agents/${agentId}/run-eval`, body);
1380
1407
  }
1381
1408
  /** Re-evaluate an existing run (two-step: POST to start, then stream SSE). */
1382
1409
  async *evalOnly(agentId, options) {
@@ -1397,10 +1424,7 @@ var Agents = class {
1397
1424
  if (options.adaptationTemplateId)
1398
1425
  body.adaptation_template_id = options.adaptationTemplateId;
1399
1426
  if (options.qualityOnly != null) body.quality_only = options.qualityOnly;
1400
- return this.http.post(
1401
- `/api/v1/agents/${agentId}/eval-only`,
1402
- body
1403
- );
1427
+ return this.http.post(`/api/v1/agents/${agentId}/eval-only`, body);
1404
1428
  }
1405
1429
  // -- Context Engine convenience accessors --
1406
1430
  async getMood(agentId, options = {}) {
@@ -1444,10 +1468,7 @@ var Agents = class {
1444
1468
  if (options.description) body.description = options.description;
1445
1469
  if (options.displayName) body.display_name = options.displayName;
1446
1470
  if (options.strength != null) body.strength = options.strength;
1447
- return this.http.post(
1448
- `/api/v1/agents/${agentId}/habits`,
1449
- body
1450
- );
1471
+ return this.http.post(`/api/v1/agents/${agentId}/habits`, body);
1451
1472
  }
1452
1473
  /** Update an existing habit by name. */
1453
1474
  async updateHabit(agentId, habitName, options) {
@@ -1487,10 +1508,7 @@ var Agents = class {
1487
1508
  if (options.type) body.type = options.type;
1488
1509
  if (options.priority != null) body.priority = options.priority;
1489
1510
  if (options.relatedTraits) body.related_traits = options.relatedTraits;
1490
- return this.http.post(
1491
- `/api/v1/agents/${agentId}/goals`,
1492
- body
1493
- );
1511
+ return this.http.post(`/api/v1/agents/${agentId}/goals`, body);
1494
1512
  }
1495
1513
  /** Update an existing goal. Set userId for per-user goals. */
1496
1514
  async updateGoal(agentId, goalId, options) {
@@ -1510,10 +1528,7 @@ var Agents = class {
1510
1528
  async deleteGoal(agentId, goalId, options = {}) {
1511
1529
  const params = {};
1512
1530
  if (options.userId) params.user_id = options.userId;
1513
- await this.http.delete(
1514
- `/api/v1/agents/${agentId}/goals/${goalId}`,
1515
- params
1516
- );
1531
+ await this.http.delete(`/api/v1/agents/${agentId}/goals/${goalId}`, params);
1517
1532
  }
1518
1533
  async getInterests(agentId, options = {}) {
1519
1534
  return this.http.get(`/api/v1/agents/${agentId}/interests`, {
@@ -1530,6 +1545,12 @@ var Agents = class {
1530
1545
  async getUsers(agentId) {
1531
1546
  return this.http.get(`/api/v1/agents/${agentId}/users`);
1532
1547
  }
1548
+ async respondToToolCall(agentId, options) {
1549
+ return this.http.post(
1550
+ `/api/v1/agents/${agentId}/tools/respond`,
1551
+ options
1552
+ );
1553
+ }
1533
1554
  /** Get constellation data for an agent. */
1534
1555
  async getConstellation(agentId, options = {}) {
1535
1556
  return this.http.get(`/api/v1/agents/${agentId}/constellation`, {
@@ -1589,38 +1610,59 @@ var Agents = class {
1589
1610
  // -- Agent Status --
1590
1611
  /** Set the active status of an agent. */
1591
1612
  async setStatus(agentId, options) {
1592
- return this.http.patch(`/api/v1/agents/${agentId}/status`, options);
1613
+ return this.http.patch(
1614
+ `/api/v1/agents/${agentId}/status`,
1615
+ options
1616
+ );
1593
1617
  }
1594
1618
  // -- Project Association --
1595
1619
  /** Update an agent's project association. */
1596
1620
  async updateProject(agentId, options) {
1597
- return this.http.patch(`/api/v1/agents/${agentId}/project`, options);
1621
+ return this.http.patch(
1622
+ `/api/v1/agents/${agentId}/project`,
1623
+ options
1624
+ );
1598
1625
  }
1599
1626
  // -- Capabilities --
1600
1627
  /** Get an agent's capabilities. */
1601
1628
  async getCapabilities(agentId) {
1602
- return this.http.get(`/api/v1/agents/${agentId}/capabilities`);
1629
+ return this.http.get(
1630
+ `/api/v1/agents/${agentId}/capabilities`
1631
+ );
1603
1632
  }
1604
1633
  /** Update an agent's capabilities. */
1605
1634
  async updateCapabilities(agentId, options) {
1606
- return this.http.patch(`/api/v1/agents/${agentId}/capabilities`, options);
1635
+ return this.http.patch(
1636
+ `/api/v1/agents/${agentId}/capabilities`,
1637
+ options
1638
+ );
1607
1639
  }
1608
1640
  // -- Custom Tools --
1609
1641
  /** List custom tools for an agent. */
1610
1642
  async listCustomTools(agentId) {
1611
- return this.http.get(`/api/v1/agents/${agentId}/tools`);
1643
+ return this.http.get(
1644
+ `/api/v1/agents/${agentId}/tools`
1645
+ );
1612
1646
  }
1613
1647
  /** Create a custom tool for an agent. */
1614
1648
  async createCustomTool(agentId, options) {
1615
- return this.http.post(`/api/v1/agents/${agentId}/tools`, options);
1649
+ return this.http.post(
1650
+ `/api/v1/agents/${agentId}/tools`,
1651
+ options
1652
+ );
1616
1653
  }
1617
1654
  /** Update a custom tool for an agent. */
1618
1655
  async updateCustomTool(agentId, toolName, options) {
1619
- return this.http.put(`/api/v1/agents/${agentId}/tools/${toolName}`, options);
1656
+ return this.http.put(
1657
+ `/api/v1/agents/${agentId}/tools/${toolName}`,
1658
+ options
1659
+ );
1620
1660
  }
1621
1661
  /** Delete a custom tool from an agent. */
1622
1662
  async deleteCustomTool(agentId, toolName) {
1623
- return this.http.delete(`/api/v1/agents/${agentId}/tools/${toolName}`);
1663
+ return this.http.delete(
1664
+ `/api/v1/agents/${agentId}/tools/${toolName}`
1665
+ );
1624
1666
  }
1625
1667
  // -- Avatar Generation --
1626
1668
  /** Trigger avatar generation for an agent. */
@@ -1648,8 +1690,12 @@ var Agents = class {
1648
1690
  if (options.instanceId) body.instanceId = options.instanceId;
1649
1691
  if (options.provider) body.provider = options.provider;
1650
1692
  if (options.model) body.model = options.model;
1651
- if (options.includeExtractions) body.include_extractions = options.includeExtractions;
1652
- return this.http.post(`/api/v1/agents/${agentId}/process`, body);
1693
+ if (options.includeExtractions)
1694
+ body.include_extractions = options.includeExtractions;
1695
+ return this.http.post(
1696
+ `/api/v1/agents/${agentId}/process`,
1697
+ body
1698
+ );
1653
1699
  }
1654
1700
  /** Get available LLM providers and models for the /process endpoint. */
1655
1701
  async getModels(agentId) {
@@ -1678,14 +1724,20 @@ var Agents = class {
1678
1724
  // -- Consolidation --
1679
1725
  /** Trigger memory consolidation for an agent. */
1680
1726
  async consolidate(agentId, options) {
1681
- return this.http.post(`/api/v1/agents/${agentId}/consolidate`, options ?? {});
1727
+ return this.http.post(
1728
+ `/api/v1/agents/${agentId}/consolidate`,
1729
+ options ?? {}
1730
+ );
1682
1731
  }
1683
1732
  /** Get memory summaries for an agent. */
1684
1733
  async getSummaries(agentId, options) {
1685
1734
  const params = {};
1686
1735
  if (options?.period) params.period = options.period;
1687
1736
  if (options?.limit) params.limit = String(options.limit);
1688
- return this.http.get(`/api/v1/agents/${agentId}/summaries`, params);
1737
+ return this.http.get(
1738
+ `/api/v1/agents/${agentId}/summaries`,
1739
+ params
1740
+ );
1689
1741
  }
1690
1742
  // -- Time Machine --
1691
1743
  /** Get a point-in-time snapshot of an agent's personality and mood. */
@@ -1693,7 +1745,10 @@ var Agents = class {
1693
1745
  const params = { at: options.at };
1694
1746
  if (options.userId) params.user_id = options.userId;
1695
1747
  if (options.instanceId) params.instance_id = options.instanceId;
1696
- return this.http.get(`/api/v1/agents/${agentId}/timemachine`, params);
1748
+ return this.http.get(
1749
+ `/api/v1/agents/${agentId}/timemachine`,
1750
+ params
1751
+ );
1697
1752
  }
1698
1753
  // -- Knowledge Search (tool endpoint) --
1699
1754
  /** Search the knowledge base for an agent. */
@@ -1876,6 +1931,14 @@ var Knowledge = class {
1876
1931
  `/api/v1/projects/${projectId}/knowledge/documents/${docId}`
1877
1932
  );
1878
1933
  }
1934
+ async uploadDocument(projectId, fileName, fileData, contentType = "application/octet-stream") {
1935
+ return this.http.uploadFile(
1936
+ `/api/v1/projects/${projectId}/knowledge/documents`,
1937
+ fileName,
1938
+ fileData,
1939
+ contentType
1940
+ );
1941
+ }
1879
1942
  // -- Facts / Graph --
1880
1943
  /** Insert entities and relationships into the knowledge graph. */
1881
1944
  async insertFacts(projectId, options) {
@@ -1889,6 +1952,14 @@ var Knowledge = class {
1889
1952
  const params = {};
1890
1953
  if (options.type) params.type = options.type;
1891
1954
  if (options.limit) params.limit = String(options.limit);
1955
+ if (options.offset) params.offset = String(options.offset);
1956
+ if (options.sort_by) params.sort_by = options.sort_by;
1957
+ if (options.sort_order) params.sort_order = options.sort_order;
1958
+ if (options.properties) {
1959
+ for (const [k, v] of Object.entries(options.properties)) {
1960
+ params[`properties.${k}`] = v;
1961
+ }
1962
+ }
1892
1963
  return this.http.get(
1893
1964
  `/api/v1/projects/${projectId}/knowledge/nodes`,
1894
1965
  params
@@ -1926,6 +1997,7 @@ var Knowledge = class {
1926
1997
  if (options.includeHistory) params.history = "true";
1927
1998
  if (options.entityTypes) params.type = options.entityTypes;
1928
1999
  if (options.filters) params.filters = options.filters;
2000
+ if (options.hops) params.hops = String(options.hops);
1929
2001
  return this.http.get(
1930
2002
  `/api/v1/projects/${projectId}/knowledge/search`,
1931
2003
  params
@@ -2266,7 +2338,9 @@ var Sonzai = class {
2266
2338
  baseUrl,
2267
2339
  apiKey,
2268
2340
  timeout: config?.timeout ?? DEFAULT_TIMEOUT,
2269
- maxRetries: config?.maxRetries ?? DEFAULT_MAX_RETRIES
2341
+ maxRetries: config?.maxRetries ?? DEFAULT_MAX_RETRIES,
2342
+ defaultHeaders: config?.defaultHeaders,
2343
+ customFetch: config?.customFetch
2270
2344
  });
2271
2345
  this.agents = new Agents(this.http);
2272
2346
  this.knowledge = new Knowledge(this.http);