@airtop/sdk 1.0.0-alpha2.45 → 1.0.0-alpha2.46

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
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } function _optionalChainDelete(ops) { const result = _optionalChain(ops); return result == null ? true : result; }var __getOwnPropNames = Object.getOwnPropertyNames;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } function _optionalChainDelete(ops) { const result = _optionalChain(ops); return result == null ? true : result; }var __getOwnPropNames = Object.getOwnPropertyNames;
2
2
  var __commonJS = (cb, mod) => function __require() {
3
3
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
4
4
  };
@@ -9,7 +9,7 @@ var require_package = __commonJS({
9
9
  module.exports = {
10
10
  name: "@airtop/sdk",
11
11
  description: "Airtop SDK for TypeScript",
12
- version: "1.0.0-alpha2.45",
12
+ version: "1.0.0-alpha2.46",
13
13
  type: "module",
14
14
  main: "./dist/index.cjs",
15
15
  module: "./dist/index.js",
@@ -198,7 +198,7 @@ var AirtopBase = class {
198
198
  * @internal
199
199
  */
200
200
  convertToJsonSchema(input) {
201
- if (input && _optionalChain([this, 'access', _ => _.outputJsonAdapter, 'optionalAccess', _2 => _2.shouldRunConversion, 'call', _3 => _3(input)])) {
201
+ if (input && _optionalChain([this, 'access', _2 => _2.outputJsonAdapter, 'optionalAccess', _3 => _3.shouldRunConversion, 'call', _4 => _4(input)])) {
202
202
  return JSON.stringify(this.outputJsonAdapter.toJsonSchema(input));
203
203
  }
204
204
  if (typeof input === "object") {
@@ -350,7 +350,7 @@ var processLogMessage = (log, logLevel, ...args) => {
350
350
  }
351
351
  let finalMessage = messages.join(" ").trim();
352
352
  if (finalMessage === "\u2A2F" && error) {
353
- finalMessage = _optionalChain([error, 'optionalAccess', _4 => _4.message]) || "";
353
+ finalMessage = _optionalChain([error, 'optionalAccess', _5 => _5.message]) || "";
354
354
  }
355
355
  if (error && hasData && messages.length > 0) {
356
356
  log.withError(error).withMetadata(data)[logLevel](finalMessage);
@@ -370,6 +370,8 @@ var processLogMessage = (log, logLevel, ...args) => {
370
370
  };
371
371
 
372
372
  // src/session/AirtopSessionClient.ts
373
+ var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
374
+ var _path = require('path'); var _path2 = _interopRequireDefault(_path);
373
375
 
374
376
 
375
377
  // src/async-utils.ts
@@ -392,7 +394,7 @@ ${JSON.stringify(metadata)}` : errorMessage;
392
394
  // src/async-utils.ts
393
395
  async function waitForRequestCompletion(client, requestId, requestOptions) {
394
396
  const startTime = Date.now();
395
- const timeoutMs = _datefns.secondsToMilliseconds.call(void 0, _optionalChain([requestOptions, 'optionalAccess', _5 => _5.timeoutInSeconds]) || TIMEOUT_SECONDS_DEFAULT_VALUE);
397
+ const timeoutMs = _datefns.secondsToMilliseconds.call(void 0, _optionalChain([requestOptions, 'optionalAccess', _6 => _6.timeoutInSeconds]) || TIMEOUT_SECONDS_DEFAULT_VALUE);
396
398
  while (Date.now() - startTime < timeoutMs) {
397
399
  const apiResponse = await client.requests.getRequestStatusV2(requestId, requestOptions);
398
400
  if (apiResponse.status === "completed") {
@@ -410,7 +412,7 @@ async function withRequestCompletionPolling(client, fn, requestOptions) {
410
412
  const response = await fn();
411
413
  return waitForRequestCompletion(client, response.requestId, requestOptions);
412
414
  } catch (thrownError) {
413
- if (_optionalChain([thrownError, 'access', _6 => _6.error, 'optionalAccess', _7 => _7.errors])) {
415
+ if (_optionalChain([thrownError, 'access', _7 => _7.error, 'optionalAccess', _8 => _8.errors])) {
414
416
  throw new AirtopError(thrownError.error.errors, {
415
417
  requestId: thrownError.error.requestId
416
418
  });
@@ -772,7 +774,7 @@ var AirtopWindowClient = class extends AirtopBase {
772
774
  */
773
775
  async monitor(condition, config = {}, requestOptions = {}) {
774
776
  this.log.withMetadata().info("Monitoring window");
775
- if (!_optionalChain([config, 'optionalAccess', _8 => _8.configuration, 'optionalAccess', _9 => _9.interval, 'optionalAccess', _10 => _10.timeoutSeconds])) {
777
+ if (!_optionalChain([config, 'optionalAccess', _9 => _9.configuration, 'optionalAccess', _10 => _10.interval, 'optionalAccess', _11 => _11.timeoutSeconds])) {
776
778
  if (!config.configuration) {
777
779
  config.configuration = {};
778
780
  }
@@ -781,7 +783,7 @@ var AirtopWindowClient = class extends AirtopBase {
781
783
  }
782
784
  config.configuration.interval.timeoutSeconds = this.defaultTimeoutInSeconds;
783
785
  }
784
- if (!_optionalChain([config, 'optionalAccess', _11 => _11.timeThresholdSeconds])) {
786
+ if (!_optionalChain([config, 'optionalAccess', _12 => _12.timeThresholdSeconds])) {
785
787
  config.timeThresholdSeconds = this.defaultTimeoutInSeconds;
786
788
  }
787
789
  return withRequestCompletionPolling(
@@ -812,7 +814,7 @@ var AirtopWindowClient = class extends AirtopBase {
812
814
  prompt
813
815
  }).info("Performing a page query");
814
816
  const newConfig = config;
815
- if (_optionalChain([config, 'optionalAccess', _12 => _12.configuration, 'access', _13 => _13.outputSchema])) {
817
+ if (_optionalChain([config, 'optionalAccess', _13 => _13.configuration, 'access', _14 => _14.outputSchema])) {
816
818
  newConfig.configuration.outputSchema = this.convertToJsonSchema(config.configuration.outputSchema);
817
819
  }
818
820
  return withRequestCompletionPolling(
@@ -845,7 +847,7 @@ var AirtopWindowClient = class extends AirtopBase {
845
847
  prompt
846
848
  }).info("Performing a paginated extraction");
847
849
  const newConfig = config;
848
- if (_optionalChain([config, 'optionalAccess', _14 => _14.configuration, 'access', _15 => _15.outputSchema])) {
850
+ if (_optionalChain([config, 'optionalAccess', _15 => _15.configuration, 'access', _16 => _16.outputSchema])) {
849
851
  newConfig.configuration.outputSchema = this.convertToJsonSchema(config.configuration.outputSchema);
850
852
  }
851
853
  return withRequestCompletionPolling(
@@ -1014,7 +1016,7 @@ var AirtopWindowClient = class extends AirtopBase {
1014
1016
  ...config || {},
1015
1017
  includeWebContext: true,
1016
1018
  // Always include web context for window.llm() calls
1017
- outputSchema: _optionalChain([config, 'optionalAccess', _16 => _16.outputSchema]) ? this.convertToJsonSchema(config.outputSchema) : void 0
1019
+ outputSchema: _optionalChain([config, 'optionalAccess', _17 => _17.outputSchema]) ? this.convertToJsonSchema(config.outputSchema) : void 0
1018
1020
  },
1019
1021
  {
1020
1022
  ...this.resolveRequestOptions(requestOptions)
@@ -1137,7 +1139,7 @@ var AirtopWindowClient = class extends AirtopBase {
1137
1139
  {
1138
1140
  sessionId: this.sessionId,
1139
1141
  ...config || {},
1140
- automationId: _optionalChain([config, 'optionalAccess', _17 => _17.automationId]) || "auto",
1142
+ automationId: _optionalChain([config, 'optionalAccess', _18 => _18.automationId]) || "auto",
1141
1143
  parameters: {
1142
1144
  customData: typeof formData === "string" ? formData : JSON.stringify(formData)
1143
1145
  // Will be interpreted by the LLM
@@ -1174,6 +1176,41 @@ var AirtopWindowClient = class extends AirtopBase {
1174
1176
  requestOptions
1175
1177
  );
1176
1178
  }
1179
+ /**
1180
+ * fileInput fills out a file input field in the window, using a file from the session
1181
+ * @param fileId - The ID of the file to use
1182
+ * @param config - Configuration options for the file input
1183
+ * @param requestOptions - Request options
1184
+ * @returns Promise resolving to the file input operation result
1185
+ */
1186
+ async fileInput(fileId, config, requestOptions = {}) {
1187
+ return await withRequestCompletionPolling(
1188
+ this.client,
1189
+ () => this.client.windows.fileInputAsync(
1190
+ this.getWindowId(),
1191
+ { sessionId: this.sessionId, ...config || {}, fileId },
1192
+ { ...this.resolveRequestOptions(requestOptions) }
1193
+ ),
1194
+ requestOptions
1195
+ );
1196
+ }
1197
+ /**
1198
+ * uploadFileAndSelectInput uploads a file to the session, gets it ready to upload,
1199
+ * and selects the file in the indicated input field
1200
+ * @param filePath - The path to the file to upload
1201
+ * @param config - Configuration options for the upload file and select input operation
1202
+ * @param requestOptions - Request options
1203
+ * @returns Promise resolving to the upload file and select input operation result
1204
+ */
1205
+ async uploadFileAndSelectInput({ filePath }, config, requestOptions = {}) {
1206
+ const sessionClient = new AirtopSessionClient(this.getCommonConfig(), this.sessionId);
1207
+ const file = await sessionClient.uploadFile(filePath, { pushToSession: true }, requestOptions);
1208
+ return await this.fileInput(
1209
+ file.id,
1210
+ { ...config, fileId: file.id },
1211
+ { ...this.resolveRequestOptions(requestOptions) }
1212
+ );
1213
+ }
1177
1214
  };
1178
1215
 
1179
1216
  // src/window/AirtopWindow.ts
@@ -1393,7 +1430,7 @@ var AirtopSessionClient = class extends AirtopBase {
1393
1430
  ...config || {},
1394
1431
  includeWebContext: false,
1395
1432
  // Do not include web context for session.llm() calls
1396
- outputSchema: _optionalChain([config, 'optionalAccess', _18 => _18.outputSchema]) ? this.convertToJsonSchema(config.outputSchema) : void 0
1433
+ outputSchema: _optionalChain([config, 'optionalAccess', _19 => _19.outputSchema]) ? this.convertToJsonSchema(config.outputSchema) : void 0
1397
1434
  },
1398
1435
  {
1399
1436
  ...this.resolveRequestOptions(requestOptions)
@@ -1463,7 +1500,7 @@ var AirtopSessionClient = class extends AirtopBase {
1463
1500
  const { lookbackSeconds = 5, fileType } = configuration || {};
1464
1501
  this.log.info(`waiting for file to be available on session: ${this.sessionId}`);
1465
1502
  const startTime = /* @__PURE__ */ new Date();
1466
- const timeoutSeconds = _optionalChain([requestOptions, 'optionalAccess', _19 => _19.timeoutInSeconds]) || 120;
1503
+ const timeoutSeconds = _optionalChain([requestOptions, 'optionalAccess', _20 => _20.timeoutInSeconds]) || 120;
1467
1504
  const timeoutPromise = new Promise((resolve) => {
1468
1505
  setTimeout(() => {
1469
1506
  this.log.info(`waiting for file timed out after ${timeoutSeconds} seconds`);
@@ -1532,6 +1569,102 @@ ${JSON.stringify(event, null, 2)}`);
1532
1569
  })();
1533
1570
  return Promise.race([timeoutPromise, processEventsPromise]);
1534
1571
  }
1572
+ /**
1573
+ * Uploads a file to the session and waits for it to be available.
1574
+ * @param filePath - The path to the file to upload
1575
+ * @param options - Additional configuration options for uploading the file,
1576
+ * @param options.fileName - The name of the file to upload, defaults to the basename of the file path
1577
+ * @param options.fileType - The type of the file to upload, defaults to "customer_upload"
1578
+ * @param options.sessionIds - The IDs of the sessions to upload the file to, defaults to the current session id
1579
+ * @param options.waitForUpload - Whether to wait for the upload to complete, defaults to true
1580
+ * @param options.pushToSession - Whether to push the file to the session, defaults to true, waits for the upload to complete regardless of waitForUpload setting
1581
+ * @param requestOptions - Request options
1582
+ * @returns the uploaded file result
1583
+ */
1584
+ async uploadFile(filePath, options = {}, requestOptions = {}) {
1585
+ this.log.info(`uploading file ${filePath}`);
1586
+ const fileName = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _21 => _21.fileName]), () => ( _path2.default.basename(filePath)));
1587
+ const fileType = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _22 => _22.fileType]), () => ( "customer_upload"));
1588
+ const sessionIds = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _23 => _23.sessionIds]), () => ( [this.sessionId]));
1589
+ const waitForUpload = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _24 => _24.waitForUpload]), () => ( true));
1590
+ const pushToSession = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _25 => _25.pushToSession]), () => ( true));
1591
+ const file = await this.client.files.createFile(
1592
+ {
1593
+ sessionIds,
1594
+ fileName,
1595
+ fileType
1596
+ },
1597
+ this.resolveRequestOptions(requestOptions)
1598
+ );
1599
+ const fileContent = _fs2.default.readFileSync(filePath);
1600
+ if (waitForUpload || pushToSession) {
1601
+ const uploadWaitPromise = this.waitForUpload(file.data.id, "uploaded");
1602
+ fetch(file.data.uploadUrl, {
1603
+ method: "PUT",
1604
+ body: fileContent,
1605
+ headers: {
1606
+ "Content-Type": "application/octet-stream"
1607
+ }
1608
+ });
1609
+ await uploadWaitPromise;
1610
+ }
1611
+ if (pushToSession) {
1612
+ const pushWaitPromise = this.waitForUpload(file.data.id, "pushed");
1613
+ this.client.files.push(file.data.id, {
1614
+ sessionIds
1615
+ });
1616
+ await pushWaitPromise;
1617
+ }
1618
+ const fileDataResponse = await this.client.files.get(file.data.id);
1619
+ const fileData = fileDataResponse.data;
1620
+ return {
1621
+ id: file.data.id,
1622
+ downloadUrl: fileData.downloadUrl,
1623
+ fileName: file.data.fileName,
1624
+ fileType: file.data.fileType
1625
+ };
1626
+ }
1627
+ /**
1628
+ * Waits for a file to be uploaded, pushed to a session, then reach 'available' status. Throws an error if the file upload fails or times out.
1629
+ * @param fileId - The ID of the file to wait for
1630
+ * @param waitType - The type of wait to perform, "uploaded": just the file being uploaded to Airtop, "pushed": the file being pushed to a session
1631
+ * @param timeoutSeconds - The timeout in seconds, defaults to 300
1632
+ * @returns void
1633
+ */
1634
+ async waitForUpload(fileId, waitType = "uploaded", timeoutSeconds = 300) {
1635
+ const waitEvent = waitType === "pushed" ? "file_upload_status" : "file_status";
1636
+ const timeoutPromise = new Promise((_, reject) => {
1637
+ setTimeout(() => {
1638
+ reject(new Error(`waiting for file upload timed out after ${timeoutSeconds} seconds`));
1639
+ }, timeoutSeconds * 1e3);
1640
+ });
1641
+ const processEventsPromise = (async () => {
1642
+ const newEvents = await this.client.sessions.getEvents(
1643
+ this.sessionId,
1644
+ { all: false, reverse: false },
1645
+ { timeout: timeoutSeconds * 1e3 }
1646
+ );
1647
+ for await (const event of newEvents) {
1648
+ const e = event;
1649
+ this.log.info(`waitForUpload processing event:
1650
+ ${JSON.stringify(event, null, 2)}`);
1651
+ if (e.event === waitEvent) {
1652
+ if (e.fileId === fileId) {
1653
+ if (e.status === "available") {
1654
+ this.log.info(`uploaded file is now available on session: ${fileId}`);
1655
+ return;
1656
+ }
1657
+ if (e.status === "upload_failed") {
1658
+ this.log.info(`upload failed: ${this.sessionId} ${fileId}`);
1659
+ throw new Error(`upload failed: ${e.eventData.error}`);
1660
+ }
1661
+ }
1662
+ }
1663
+ }
1664
+ return null;
1665
+ })();
1666
+ return Promise.race([timeoutPromise, processEventsPromise]);
1667
+ }
1535
1668
  };
1536
1669
 
1537
1670
  // src/session/AirtopSession.ts
@@ -1590,20 +1723,20 @@ var AirtopClient = class extends AirtopBase {
1590
1723
  */
1591
1724
  constructor(config) {
1592
1725
  super({
1593
- logLevel: _optionalChain([config, 'optionalAccess', _20 => _20.logLevel]),
1726
+ logLevel: _optionalChain([config, 'optionalAccess', _26 => _26.logLevel]),
1594
1727
  client: new (0, _core.Airtop)({
1595
1728
  maxRetries: 0,
1596
1729
  timeout: _secondsToMilliseconds.secondsToMilliseconds.call(void 0, _nullishCoalesce(config.defaultTimeoutInSeconds, () => ( TIMEOUT_SECONDS_DEFAULT_VALUE))),
1597
1730
  apiKey: config.apiKey,
1598
- baseURL: _optionalChain([config, 'optionalAccess', _21 => _21.airtopUrl]),
1599
- logLevel: _optionalChain([config, 'optionalAccess', _22 => _22.logLevel]) || "off",
1731
+ baseURL: _optionalChain([config, 'optionalAccess', _27 => _27.airtopUrl]),
1732
+ logLevel: _optionalChain([config, 'optionalAccess', _28 => _28.logLevel]) || "off",
1600
1733
  defaultHeaders: {
1601
1734
  "x-airtop-sdk-source": "typescript",
1602
1735
  "x-airtop-sdk-version": version,
1603
- "x-airtop-sdk-environment": _nullishCoalesce(_optionalChain([config, 'optionalAccess', _23 => _23.environment]), () => ( "default"))
1736
+ "x-airtop-sdk-environment": _nullishCoalesce(_optionalChain([config, 'optionalAccess', _29 => _29.environment]), () => ( "default"))
1604
1737
  }
1605
1738
  }),
1606
- log: _optionalChain([config, 'optionalAccess', _24 => _24.logger]) || new (0, _loglayer.LogLayer)({
1739
+ log: _optionalChain([config, 'optionalAccess', _30 => _30.logger]) || new (0, _loglayer.LogLayer)({
1607
1740
  errorSerializer: _serializeerror.serializeError,
1608
1741
  transport: new (0, _loglayer.ConsoleTransport)({
1609
1742
  logger: console,
@@ -1643,8 +1776,8 @@ var AirtopClient = class extends AirtopBase {
1643
1776
  * @returns A new AirtopSession instance
1644
1777
  */
1645
1778
  async createSession(config, options = {}) {
1646
- const skipWaitSessionReady = _nullishCoalesce(_optionalChain([config, 'optionalAccess', _25 => _25.skipWaitSessionReady]), () => ( false));
1647
- _optionalChainDelete([config, 'optionalAccess', _26 => delete _26.skipWaitSessionReady]);
1779
+ const skipWaitSessionReady = _nullishCoalesce(_optionalChain([config, 'optionalAccess', _31 => _31.skipWaitSessionReady]), () => ( false));
1780
+ _optionalChainDelete([config, 'optionalAccess', _32 => delete _32.skipWaitSessionReady]);
1648
1781
  const sessionResponse = await this.client.sessions.create(
1649
1782
  {
1650
1783
  configuration: config
@@ -1900,19 +2033,19 @@ var AirtopAgentClient = class extends AirtopBase {
1900
2033
  */
1901
2034
  constructor(config) {
1902
2035
  super({
1903
- logLevel: _optionalChain([config, 'optionalAccess', _27 => _27.logLevel]),
2036
+ logLevel: _optionalChain([config, 'optionalAccess', _33 => _33.logLevel]),
1904
2037
  client: new (0, _core.Airtop)({
1905
2038
  maxRetries: 0,
1906
2039
  timeout: _datefns.minutesToMilliseconds.call(void 0, 1),
1907
2040
  apiKey: config.apiKey,
1908
- baseURL: _optionalChain([config, 'optionalAccess', _28 => _28.airtopUrl]),
1909
- logLevel: _optionalChain([config, 'optionalAccess', _29 => _29.logLevel]) || "off",
2041
+ baseURL: _optionalChain([config, 'optionalAccess', _34 => _34.airtopUrl]),
2042
+ logLevel: _optionalChain([config, 'optionalAccess', _35 => _35.logLevel]) || "off",
1910
2043
  defaultHeaders: {
1911
2044
  "x-airtop-sdk-source": "typescript",
1912
2045
  "x-airtop-sdk-version": _process.version
1913
2046
  }
1914
2047
  }),
1915
- log: _optionalChain([config, 'optionalAccess', _30 => _30.logger]) || new (0, _loglayer.LogLayer)({
2048
+ log: _optionalChain([config, 'optionalAccess', _36 => _36.logger]) || new (0, _loglayer.LogLayer)({
1916
2049
  errorSerializer: _serializeerror.serializeError,
1917
2050
  transport: new (0, _loglayer.ConsoleTransport)({
1918
2051
  logger: console,