notte-sdk 0.0.1 → 0.0.3

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.mjs CHANGED
@@ -1585,6 +1585,16 @@ var Session = class {
1585
1585
 
1586
1586
  // src/agent.ts
1587
1587
  import WebSocket from "ws";
1588
+ import { z as z2 } from "zod";
1589
+ function prepareAgentRequest(data) {
1590
+ const apiData = { ...data };
1591
+ const rf = data.response_format;
1592
+ if (rf && typeof rf.parse === "function" && typeof rf.safeParse === "function") {
1593
+ apiData.response_format = z2.toJSONSchema(rf);
1594
+ return { apiData, zodSchema: rf };
1595
+ }
1596
+ return { apiData, zodSchema: null };
1597
+ }
1588
1598
  function validatePersonaOption(persona, persona_id) {
1589
1599
  if (persona) {
1590
1600
  try {
@@ -1707,9 +1717,6 @@ var Agent = class {
1707
1717
  throw new Error("Either session (for running a new agent) or agent_id (for accessing an existing agent) have to be provided");
1708
1718
  }
1709
1719
  }
1710
- /**
1711
- * Start an agent task (non-blocking) - mirrors Python start method
1712
- */
1713
1720
  async start(data) {
1714
1721
  if (this.existingAgent) {
1715
1722
  throw new Error("You cannot call start() on an agent instantiated from agent id");
@@ -1718,9 +1725,10 @@ var Agent = class {
1718
1725
  throw new Error("Agent not properly initialized with session");
1719
1726
  }
1720
1727
  try {
1728
+ const { apiData } = prepareAgentRequest(data);
1721
1729
  const requestBody = {
1722
1730
  ...this.request,
1723
- ...data
1731
+ ...apiData
1724
1732
  };
1725
1733
  const response = await agentStart({
1726
1734
  client: this.client.getClient(),
@@ -1735,17 +1743,28 @@ var Agent = class {
1735
1743
  throw new Error(`Failed to start agent: ${error instanceof Error ? error.message : String(error)}`);
1736
1744
  }
1737
1745
  }
1738
- /**
1739
- * Run agent task (blocking) - mirrors Python run method behavior
1740
- * This is the main method that mirrors the Python SDK's run method
1741
- */
1742
1746
  async run(data) {
1743
1747
  if (this.existingAgent) {
1744
1748
  throw new Error("You cannot call run() on an agent instantiated from agent id");
1745
1749
  }
1746
- this.response = await this.start(data);
1750
+ const { apiData, zodSchema } = prepareAgentRequest(data);
1751
+ this.response = await this.start(apiData);
1747
1752
  console.log(`[Agent] ${this.agentId} started`);
1748
- return await this.watchLogsAndWait(true, data.updateHandler || null);
1753
+ const result = await this.watchLogsAndWait(true, data.updateHandler || null);
1754
+ if (zodSchema && result && result.answer !== void 0 && result.answer !== null) {
1755
+ let candidate;
1756
+ try {
1757
+ candidate = typeof result.answer === "string" ? JSON.parse(result.answer) : result.answer;
1758
+ } catch {
1759
+ throw new Error(`Agent answer is not valid JSON and cannot be validated against the schema`);
1760
+ }
1761
+ try {
1762
+ result.answer = zodSchema.parse(candidate);
1763
+ } catch (error) {
1764
+ throw new Error(`Agent answer schema validation failed: ${error instanceof Error ? error.message : String(error)}`);
1765
+ }
1766
+ }
1767
+ return result;
1749
1768
  }
1750
1769
  /**
1751
1770
  * Watch logs and wait for completion - mirrors Python watch_logs_and_wait
@@ -2463,7 +2482,7 @@ var NottePersona = class {
2463
2482
  };
2464
2483
 
2465
2484
  // src/version.ts
2466
- var SDK_VERSION = true ? "0.0.1" : "dev";
2485
+ var SDK_VERSION = true ? "0.0.3" : "dev";
2467
2486
 
2468
2487
  // src/functions.ts
2469
2488
  var NotteFunction = class {
@@ -2540,7 +2559,7 @@ var NotteFunction = class {
2540
2559
  };
2541
2560
 
2542
2561
  // src/client.ts
2543
- import { z as z2 } from "zod";
2562
+ import { z as z3 } from "zod";
2544
2563
  var NotteClient = class {
2545
2564
  constructor(config = {}) {
2546
2565
  const apiKey = config.apiKey || process.env.NOTTE_API_KEY;
@@ -2549,12 +2568,13 @@ var NotteClient = class {
2549
2568
  throw new Error("API key is required. Provide it via config.apiKey or set the NOTTE_API_KEY environment variable.");
2550
2569
  }
2551
2570
  this.config = {
2552
- baseUrl: config.baseUrl || "https://api.notte.cc",
2571
+ baseUrl: config.baseUrl || process.env.NOTTE_API_URL || "https://api.notte.cc",
2553
2572
  apiKey,
2554
2573
  ...config
2555
2574
  };
2556
2575
  client.setConfig({
2557
- baseUrl: this.config.baseUrl
2576
+ baseUrl: this.config.baseUrl,
2577
+ redirect: "manual"
2558
2578
  });
2559
2579
  client.interceptors.request.use((request) => {
2560
2580
  request.headers.set("Authorization", `Bearer ${this.config.apiKey}`);
@@ -2562,6 +2582,33 @@ var NotteClient = class {
2562
2582
  request.headers.set("x-notte-sdk-version", SDK_VERSION);
2563
2583
  return request;
2564
2584
  });
2585
+ client.interceptors.response.use(async (response, request, opts) => {
2586
+ if ((response.status === 307 || response.status === 308) && response.headers.get("location")) {
2587
+ const location = response.headers.get("location");
2588
+ const headers = new Headers(request.headers);
2589
+ if (new URL(location).origin !== new URL(request.url).origin) {
2590
+ headers.delete("Authorization");
2591
+ }
2592
+ const headerRecord = {};
2593
+ headers.forEach((v, k) => {
2594
+ headerRecord[k] = v;
2595
+ });
2596
+ const redirected = await fetch(location, {
2597
+ method: request.method,
2598
+ headers: headerRecord,
2599
+ body: opts.serializedBody ?? void 0
2600
+ });
2601
+ return redirected;
2602
+ }
2603
+ const ct = response.headers.get("content-type") ?? "";
2604
+ if (ct.split(";")[0]?.trim() === "text/plain") {
2605
+ const body = await response.arrayBuffer();
2606
+ const headers = new Headers(response.headers);
2607
+ headers.set("content-type", "application/json");
2608
+ return new Response(body, { status: response.status, statusText: response.statusText, headers });
2609
+ }
2610
+ return response;
2611
+ });
2565
2612
  }
2566
2613
  /**
2567
2614
  * Create a new session
@@ -2622,7 +2669,7 @@ var NotteClient = class {
2622
2669
  if (options.json_schema) {
2623
2670
  apiOptions.response_format = options.json_schema;
2624
2671
  } else {
2625
- apiOptions.response_format = z2.toJSONSchema(options.response_format);
2672
+ apiOptions.response_format = z3.toJSONSchema(options.response_format);
2626
2673
  }
2627
2674
  if (!apiOptions.instructions) {
2628
2675
  const schemaStr = JSON.stringify(apiOptions.response_format);