@alva-ai/toolkit 0.13.1 → 0.14.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
@@ -368,7 +368,8 @@ var ReleaseResource = class {
368
368
  cronjob_id: params.cronjob_id,
369
369
  view_json: params.view_json,
370
370
  description: params.description,
371
- changelog: params.changelog
371
+ changelog: params.changelog,
372
+ agent_type: params.agent_type
372
373
  }
373
374
  });
374
375
  }
@@ -554,8 +555,11 @@ var PlaybooksResource = class {
554
555
  "/api/v1/playbooks/trending",
555
556
  { query }
556
557
  );
558
+ const webOrigin = webOriginFromApiBase(this.client.baseUrl);
557
559
  return {
558
- playbooks: (raw.playbooks ?? []).map(toAgentPlaybook),
560
+ playbooks: (raw.playbooks ?? []).map(
561
+ (p) => toAgentPlaybook(p, webOrigin)
562
+ ),
559
563
  has_next: raw.has_next ?? false
560
564
  };
561
565
  }
@@ -584,10 +588,23 @@ var PlaybooksResource = class {
584
588
  );
585
589
  }
586
590
  };
587
- function toAgentPlaybook(raw) {
591
+ function webOriginFromApiBase(apiBaseUrl) {
592
+ try {
593
+ const host = new URL(apiBaseUrl).hostname;
594
+ const match = /^api-llm\.([a-z0-9-]+)\.alva\.ai$/i.exec(host);
595
+ if (match) {
596
+ const env = match[1].toLowerCase();
597
+ return env === "prd" ? "https://alva.ai" : `https://${env}.alva.ai`;
598
+ }
599
+ } catch {
600
+ }
601
+ return "https://alva.ai";
602
+ }
603
+ function toAgentPlaybook(raw, webOrigin) {
588
604
  const username = raw.creator?.name ?? "";
589
605
  const name = raw.name ?? "";
590
606
  const ref = username && name ? `${username}/${name}` : name;
607
+ const urlPath = username && name ? `/u/${username}/playbooks/${name}` : "";
591
608
  return {
592
609
  id: raw.id === void 0 ? "" : String(raw.id),
593
610
  ref,
@@ -597,7 +614,8 @@ function toAgentPlaybook(raw) {
597
614
  description: raw.description ?? "",
598
615
  tags: raw.tags ?? [],
599
616
  follow_count: raw.follow_count ?? 0,
600
- url_path: username && name ? `/${username}/playbooks/${name}` : "",
617
+ url_path: urlPath,
618
+ url: urlPath ? `${webOrigin}${urlPath}` : "",
601
619
  ...raw.readme ? { readme: raw.readme } : {},
602
620
  cursor: raw.cursor ?? ""
603
621
  };
@@ -2585,6 +2603,122 @@ function parseResultPayload(result) {
2585
2603
  }
2586
2604
  }
2587
2605
 
2606
+ // src/resources/credits.ts
2607
+ var CREDIT_WALLET_QUERY = `
2608
+ query ToolkitCreditWallet {
2609
+ viewer {
2610
+ creditWallet {
2611
+ balance
2612
+ totalRemaining
2613
+ todayUsed
2614
+ }
2615
+ }
2616
+ }
2617
+ `.trim();
2618
+ var CREDIT_WALLET_ITEMS_QUERY = `
2619
+ query ToolkitCreditWalletItems($input: CreditWalletItemConnectionInput!) {
2620
+ viewer {
2621
+ creditWallet {
2622
+ balance
2623
+ totalRemaining
2624
+ todayUsed
2625
+ items(input: $input) {
2626
+ pageInfo {
2627
+ hasNextPage
2628
+ hasPreviousPage
2629
+ startCursor
2630
+ endCursor
2631
+ }
2632
+ edges {
2633
+ cursor
2634
+ node {
2635
+ id
2636
+ sessionId
2637
+ playbookId
2638
+ feedId
2639
+ op
2640
+ source
2641
+ amount
2642
+ extras
2643
+ createdAtMs
2644
+ }
2645
+ }
2646
+ }
2647
+ }
2648
+ }
2649
+ }
2650
+ `.trim();
2651
+ function graphQLErrorMessage(errors) {
2652
+ const messages = errors.map(
2653
+ (error) => typeof error.message === "string" && error.message ? error.message : void 0
2654
+ ).filter((message) => Boolean(message));
2655
+ return messages.length > 0 ? messages.join("; ") : "GraphQL request failed";
2656
+ }
2657
+ var CreditsResource = class {
2658
+ constructor(client) {
2659
+ this.client = client;
2660
+ }
2661
+ client;
2662
+ async wallet() {
2663
+ this.client._requireAuth();
2664
+ const data = await this.graphql(CREDIT_WALLET_QUERY);
2665
+ const wallet = data.viewer?.creditWallet;
2666
+ if (!wallet) {
2667
+ throw new AlvaError(
2668
+ "GRAPHQL_EMPTY_RESPONSE",
2669
+ "GraphQL response did not include viewer.creditWallet",
2670
+ 502
2671
+ );
2672
+ }
2673
+ return wallet;
2674
+ }
2675
+ async items(params) {
2676
+ this.client._requireAuth();
2677
+ const data = await this.graphql(
2678
+ CREDIT_WALLET_ITEMS_QUERY,
2679
+ {
2680
+ input: {
2681
+ startAtMs: params.startAtMs,
2682
+ endAtMs: params.endAtMs,
2683
+ sessionId: params.sessionId,
2684
+ first: params.first,
2685
+ after: params.after
2686
+ }
2687
+ }
2688
+ );
2689
+ const wallet = data.viewer?.creditWallet;
2690
+ if (!wallet) {
2691
+ throw new AlvaError(
2692
+ "GRAPHQL_EMPTY_RESPONSE",
2693
+ "GraphQL response did not include viewer.creditWallet.items",
2694
+ 502
2695
+ );
2696
+ }
2697
+ return wallet;
2698
+ }
2699
+ async graphql(query, variables) {
2700
+ const response = await this.client._request("POST", "/query", {
2701
+ body: variables === void 0 ? { query } : { query, variables }
2702
+ });
2703
+ if (response.errors && response.errors.length > 0) {
2704
+ throw new AlvaError(
2705
+ "GRAPHQL_ERROR",
2706
+ graphQLErrorMessage(response.errors),
2707
+ 400,
2708
+ { errors: response.errors }
2709
+ );
2710
+ }
2711
+ if (!response.data) {
2712
+ throw new AlvaError(
2713
+ "GRAPHQL_EMPTY_RESPONSE",
2714
+ "GraphQL response did not include data",
2715
+ 502
2716
+ );
2717
+ }
2718
+ return response.data;
2719
+ }
2720
+ };
2721
+
2588
2722
  // src/client.ts
2589
2723
  var DEFAULT_BASE_URL = "https://api-llm.prd.alva.ai";
2590
2724
  var DEFAULT_ARRAYS_BASE_URL = "https://data-tools.prd.space.id";
@@ -2686,6 +2820,7 @@ var AlvaClient = class {
2686
2820
  _channelGroupSubscriptions;
2687
2821
  _feedback;
2688
2822
  _functions;
2823
+ _credits;
2689
2824
  constructor(config) {
2690
2825
  this.baseUrl = config.baseUrl ?? DEFAULT_BASE_URL;
2691
2826
  this.arraysBaseUrl = config.arraysBaseUrl ?? DEFAULT_ARRAYS_BASE_URL;
@@ -2767,6 +2902,9 @@ var AlvaClient = class {
2767
2902
  get functions() {
2768
2903
  return this._functions ??= new FunctionsResource(this);
2769
2904
  }
2905
+ get credits() {
2906
+ return this._credits ??= new CreditsResource(this);
2907
+ }
2770
2908
  _requireAuth() {
2771
2909
  if (!this.pbsvToken && !this.viewer_token && !this.apiKey) {
2772
2910
  throw new AlvaError(
@@ -2934,7 +3072,7 @@ var AlvaClient = class {
2934
3072
  return void 0;
2935
3073
  }
2936
3074
  const contentType = response.headers.get("content-type") ?? "";
2937
- if (contentType.includes("application/octet-stream") || contentType.includes("image/")) {
3075
+ if (contentType.includes("application/octet-stream") || contentType.startsWith("text/") || contentType.includes("image/")) {
2938
3076
  return response.arrayBuffer();
2939
3077
  }
2940
3078
  return response.json();
@@ -3368,7 +3506,7 @@ var installPlaybookRuntime = (targetWindow) => {
3368
3506
  };
3369
3507
 
3370
3508
  // src/index.ts
3371
- var VERSION = true ? "0.13.1" : "dev";
3509
+ var VERSION = true ? "0.14.0" : "dev";
3372
3510
  // Annotate the CommonJS export names for ESM import in node:
3373
3511
  0 && (module.exports = {
3374
3512
  AlvaClient,