@botpress/cognitive 0.3.18 → 0.4.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.d.ts CHANGED
@@ -1415,6 +1415,10 @@ interface CreateConversationResponse {
1415
1415
  tags: {
1416
1416
  [k: string]: string;
1417
1417
  };
1418
+ /**
1419
+ * Number of messages in the conversation
1420
+ */
1421
+ messageCount: number;
1418
1422
  };
1419
1423
  }
1420
1424
 
@@ -1468,6 +1472,10 @@ interface GetConversationResponse {
1468
1472
  tags: {
1469
1473
  [k: string]: string;
1470
1474
  };
1475
+ /**
1476
+ * Number of messages in the conversation
1477
+ */
1478
+ messageCount: number;
1471
1479
  };
1472
1480
  }
1473
1481
 
@@ -1528,6 +1536,10 @@ interface ListConversationsResponse {
1528
1536
  tags: {
1529
1537
  [k: string]: string;
1530
1538
  };
1539
+ /**
1540
+ * Number of messages in the conversation
1541
+ */
1542
+ messageCount: number;
1531
1543
  }[];
1532
1544
  meta: {
1533
1545
  /**
@@ -1605,6 +1617,10 @@ interface GetOrCreateConversationResponse {
1605
1617
  tags: {
1606
1618
  [k: string]: string;
1607
1619
  };
1620
+ /**
1621
+ * Number of messages in the conversation
1622
+ */
1623
+ messageCount: number;
1608
1624
  };
1609
1625
  meta: {
1610
1626
  created: boolean;
@@ -1672,6 +1688,10 @@ interface UpdateConversationResponse {
1672
1688
  tags: {
1673
1689
  [k: string]: string;
1674
1690
  };
1691
+ /**
1692
+ * Number of messages in the conversation
1693
+ */
1694
+ messageCount: number;
1675
1695
  };
1676
1696
  }
1677
1697
 
@@ -1727,6 +1747,12 @@ interface ListParticipantsResponse {
1727
1747
  * Picture URL of the [User](#schema_user)
1728
1748
  */
1729
1749
  pictureUrl?: string;
1750
+ /**
1751
+ * Optional properties
1752
+ */
1753
+ properties?: {
1754
+ [k: string]: string;
1755
+ };
1730
1756
  /**
1731
1757
  * Optional attributes
1732
1758
  */
@@ -1787,6 +1813,12 @@ interface AddParticipantResponse {
1787
1813
  * Picture URL of the [User](#schema_user)
1788
1814
  */
1789
1815
  pictureUrl?: string;
1816
+ /**
1817
+ * Optional properties
1818
+ */
1819
+ properties?: {
1820
+ [k: string]: string;
1821
+ };
1790
1822
  /**
1791
1823
  * Optional attributes
1792
1824
  */
@@ -1838,6 +1870,12 @@ interface GetParticipantResponse {
1838
1870
  * Picture URL of the [User](#schema_user)
1839
1871
  */
1840
1872
  pictureUrl?: string;
1873
+ /**
1874
+ * Optional properties
1875
+ */
1876
+ properties?: {
1877
+ [k: string]: string;
1878
+ };
1841
1879
  /**
1842
1880
  * Optional attributes
1843
1881
  */
@@ -2189,7 +2227,7 @@ interface GetOrCreateMessageRequestBody {
2189
2227
  /**
2190
2228
  * Payload is the content type of the message. Accepted payload options: Text, Image, Choice, Dropdown, Card, Carousel, File, Audio, Video, Location
2191
2229
  */
2192
- payload: {
2230
+ payload?: {
2193
2231
  [k: string]: any;
2194
2232
  };
2195
2233
  /**
@@ -2280,6 +2318,9 @@ interface GetOrCreateMessageResponse {
2280
2318
  */
2281
2319
  origin?: "synthetic";
2282
2320
  };
2321
+ meta: {
2322
+ created: boolean;
2323
+ };
2283
2324
  }
2284
2325
 
2285
2326
  interface GetMessageRequestHeaders {
@@ -2531,7 +2572,14 @@ interface InitializeIncomingMessageRequestBody {
2531
2572
  */
2532
2573
  pictureUrl?: string;
2533
2574
  /**
2534
- * EXPERIMENTAL - Optional shared attributes that can be accessed and modified by both the bot and any of its integrations.
2575
+ * EXPERIMENTAL - Optional shared properties that can be accessed and modified by both the bot and any of its integrations.
2576
+ */
2577
+ properties?: {
2578
+ [k: string]: string;
2579
+ };
2580
+ /**
2581
+ * @deprecated
2582
+ * DEPRECATED - Use properties instead.
2535
2583
  */
2536
2584
  attributes?: {
2537
2585
  [k: string]: string;
@@ -2619,6 +2667,12 @@ interface InitializeIncomingMessageResponse {
2619
2667
  * Picture URL of the [User](#schema_user)
2620
2668
  */
2621
2669
  pictureUrl?: string;
2670
+ /**
2671
+ * Optional properties
2672
+ */
2673
+ properties?: {
2674
+ [k: string]: string;
2675
+ };
2622
2676
  /**
2623
2677
  * Optional attributes
2624
2678
  */
@@ -2665,6 +2719,10 @@ interface InitializeIncomingMessageResponse {
2665
2719
  tags: {
2666
2720
  [k: string]: string;
2667
2721
  };
2722
+ /**
2723
+ * Number of messages in the conversation
2724
+ */
2725
+ messageCount: number;
2668
2726
  };
2669
2727
  /**
2670
2728
  * The Message object represents a message in a [Conversation](#schema_conversation) for a specific [User](#schema_user).
@@ -2839,7 +2897,14 @@ interface CreateUserRequestBody {
2839
2897
  */
2840
2898
  pictureUrl?: string;
2841
2899
  /**
2842
- * EXPERIMENTAL - Optional shared attributes that can be accessed and modified by both the bot and any of its integrations.
2900
+ * EXPERIMENTAL - Optional shared properties that can be accessed and modified by both the bot and any of its integrations.
2901
+ */
2902
+ properties?: {
2903
+ [k: string]: string;
2904
+ };
2905
+ /**
2906
+ * @deprecated
2907
+ * DEPRECATED - Use properties instead.
2843
2908
  */
2844
2909
  attributes?: {
2845
2910
  [k: string]: string;
@@ -2877,6 +2942,12 @@ interface CreateUserResponse {
2877
2942
  * Picture URL of the [User](#schema_user)
2878
2943
  */
2879
2944
  pictureUrl?: string;
2945
+ /**
2946
+ * Optional properties
2947
+ */
2948
+ properties?: {
2949
+ [k: string]: string;
2950
+ };
2880
2951
  /**
2881
2952
  * Optional attributes
2882
2953
  */
@@ -2927,6 +2998,12 @@ interface GetUserResponse {
2927
2998
  * Picture URL of the [User](#schema_user)
2928
2999
  */
2929
3000
  pictureUrl?: string;
3001
+ /**
3002
+ * Optional properties
3003
+ */
3004
+ properties?: {
3005
+ [k: string]: string;
3006
+ };
2930
3007
  /**
2931
3008
  * Optional attributes
2932
3009
  */
@@ -2983,6 +3060,12 @@ interface ListUsersResponse {
2983
3060
  * Picture URL of the [User](#schema_user)
2984
3061
  */
2985
3062
  pictureUrl?: string;
3063
+ /**
3064
+ * Optional properties
3065
+ */
3066
+ properties?: {
3067
+ [k: string]: string;
3068
+ };
2986
3069
  /**
2987
3070
  * Optional attributes
2988
3071
  */
@@ -3025,7 +3108,14 @@ interface GetOrCreateUserRequestBody {
3025
3108
  */
3026
3109
  pictureUrl?: string;
3027
3110
  /**
3028
- * EXPERIMENTAL - Optional shared attributes that can be accessed and modified by both the bot and any of its integrations.
3111
+ * EXPERIMENTAL - Optional shared properties that can be accessed and modified by both the bot and any of its integrations.
3112
+ */
3113
+ properties?: {
3114
+ [k: string]: string;
3115
+ };
3116
+ /**
3117
+ * @deprecated
3118
+ * DEPRECATED - Use properties instead.
3029
3119
  */
3030
3120
  attributes?: {
3031
3121
  [k: string]: string;
@@ -3067,6 +3157,12 @@ interface GetOrCreateUserResponse {
3067
3157
  * Picture URL of the [User](#schema_user)
3068
3158
  */
3069
3159
  pictureUrl?: string;
3160
+ /**
3161
+ * Optional properties
3162
+ */
3163
+ properties?: {
3164
+ [k: string]: string;
3165
+ };
3070
3166
  /**
3071
3167
  * Optional attributes
3072
3168
  */
@@ -3074,6 +3170,9 @@ interface GetOrCreateUserResponse {
3074
3170
  [k: string]: string;
3075
3171
  };
3076
3172
  };
3173
+ meta: {
3174
+ created: boolean;
3175
+ };
3077
3176
  }
3078
3177
 
3079
3178
  interface UpdateUserRequestHeaders {
@@ -3099,11 +3198,18 @@ interface UpdateUserRequestBody {
3099
3198
  */
3100
3199
  pictureUrl?: string | null;
3101
3200
  /**
3102
- * EXPERIMENTAL - Optional shared attributes that can be accessed and modified by both the bot and any of its integrations. Set individual attributes to null to remove them.
3201
+ * @deprecated
3202
+ * DEPRECATED - Use properties instead.
3103
3203
  */
3104
3204
  attributes?: {
3105
3205
  [k: string]: string | null;
3106
3206
  };
3207
+ /**
3208
+ * EXPERIMENTAL - Optional shared properties that can be accessed and modified by both the bot and any of its integrations. Set individual properties to null to remove them.
3209
+ */
3210
+ properties?: {
3211
+ [k: string]: string | null;
3212
+ };
3107
3213
  }
3108
3214
  type UpdateUserInput = UpdateUserRequestBody & UpdateUserRequestHeaders & UpdateUserRequestQuery & UpdateUserRequestParams;
3109
3215
  interface UpdateUserResponse {
@@ -3137,6 +3243,12 @@ interface UpdateUserResponse {
3137
3243
  * Picture URL of the [User](#schema_user)
3138
3244
  */
3139
3245
  pictureUrl?: string;
3246
+ /**
3247
+ * Optional properties
3248
+ */
3249
+ properties?: {
3250
+ [k: string]: string;
3251
+ };
3140
3252
  /**
3141
3253
  * Optional attributes
3142
3254
  */
@@ -18761,6 +18873,7 @@ declare class Client extends Client$1 implements IClient {
18761
18873
  tags: {
18762
18874
  [k: string]: string;
18763
18875
  };
18876
+ messageCount: number;
18764
18877
  }>;
18765
18878
  participants: (props: {
18766
18879
  id: string;
@@ -18773,6 +18886,9 @@ declare class Client extends Client$1 implements IClient {
18773
18886
  };
18774
18887
  name?: string;
18775
18888
  pictureUrl?: string;
18889
+ properties?: {
18890
+ [k: string]: string;
18891
+ };
18776
18892
  attributes?: {
18777
18893
  [k: string]: string;
18778
18894
  };
@@ -18839,6 +18955,9 @@ declare class Client extends Client$1 implements IClient {
18839
18955
  };
18840
18956
  name?: string;
18841
18957
  pictureUrl?: string;
18958
+ properties?: {
18959
+ [k: string]: string;
18960
+ };
18842
18961
  attributes?: {
18843
18962
  [k: string]: string;
18844
18963
  };
@@ -19669,6 +19788,9 @@ declare class Cognitive {
19669
19788
  protected _downtimes: ModelPreferences['downtimes'];
19670
19789
  protected _useBeta: boolean;
19671
19790
  protected _debug: boolean;
19791
+ private _remoteModelCache;
19792
+ private _remoteModelCacheTime;
19793
+ private _remoteModelCachePending;
19672
19794
  private _events;
19673
19795
  constructor(props: CognitiveProps);
19674
19796
  get client(): ExtendedClient;
@@ -19679,8 +19801,11 @@ declare class Cognitive {
19679
19801
  setPreferences(preferences: ModelPreferences, save?: boolean): Promise<void>;
19680
19802
  private _cleanupOldDowntimes;
19681
19803
  private _selectModel;
19804
+ fetchRemoteModels(): Promise<Map<string, Model$1>>;
19805
+ private _doFetchRemoteModels;
19682
19806
  getModelDetails(model: string): Promise<Model$1>;
19683
19807
  generateContent(input: InputProps): Promise<Response>;
19808
+ private _generateContentV2;
19684
19809
  private _generateContent;
19685
19810
  }
19686
19811
 
@@ -19895,6 +20020,7 @@ declare class CognitiveBeta {
19895
20020
  private _isRetryableServerError;
19896
20021
  private _withServerRetry;
19897
20022
  }
20023
+ declare const isKnownV2Model: (model: string | undefined) => boolean;
19898
20024
  declare const getCognitiveV2Model: (model: string) => Model | undefined;
19899
20025
 
19900
- export { type BetaEvents, type BotpressClientLike, Cognitive, CognitiveBeta, type CognitiveRequest, type CognitiveResponse, type CognitiveStreamChunk, type Events, type GenerateContentInput, type GenerateContentOutput, type Model$1 as Model, type ModelPreferences, ModelProvider, type Models, RemoteModelProvider, getCognitiveV2Model };
20026
+ export { type BetaEvents, type BotpressClientLike, Cognitive, CognitiveBeta, type CognitiveRequest, type CognitiveResponse, type CognitiveStreamChunk, type Events, type GenerateContentInput, type GenerateContentOutput, type Model$1 as Model, type ModelPreferences, ModelProvider, type Models, RemoteModelProvider, getCognitiveV2Model, isKnownV2Model };
package/dist/index.mjs CHANGED
@@ -2332,6 +2332,23 @@ var CognitiveBeta = class _CognitiveBeta {
2332
2332
  );
2333
2333
  }
2334
2334
  };
2335
+ var COGNITIVE_V2_PROVIDERS = /* @__PURE__ */ new Set([
2336
+ "openai",
2337
+ "anthropic",
2338
+ "google-ai",
2339
+ "groq",
2340
+ "cerebras",
2341
+ "fireworks-ai",
2342
+ "xai",
2343
+ "openrouter"
2344
+ ]);
2345
+ var isKnownV2Model = (model) => {
2346
+ if (!model || ["auto", "best", "fast"].includes(model)) {
2347
+ return true;
2348
+ }
2349
+ const provider = model.split(":")[0];
2350
+ return !!provider && COGNITIVE_V2_PROVIDERS.has(provider);
2351
+ };
2335
2352
  var getCognitiveV2Model = (model) => {
2336
2353
  if (models[model]) {
2337
2354
  return models[model];
@@ -2603,6 +2620,9 @@ var Cognitive = class _Cognitive {
2603
2620
  _downtimes = [];
2604
2621
  _useBeta = false;
2605
2622
  _debug = false;
2623
+ _remoteModelCache = /* @__PURE__ */ new Map();
2624
+ _remoteModelCacheTime = 0;
2625
+ _remoteModelCachePending = null;
2606
2626
  _events = createNanoEvents();
2607
2627
  constructor(props) {
2608
2628
  this._client = getExtendedClient(props.client);
@@ -2626,6 +2646,9 @@ var Cognitive = class _Cognitive {
2626
2646
  copy._models = [...this._models];
2627
2647
  copy._preferences = this._preferences ? { ...this._preferences } : null;
2628
2648
  copy._downtimes = [...this._downtimes];
2649
+ copy._remoteModelCache = new Map(this._remoteModelCache);
2650
+ copy._remoteModelCacheTime = this._remoteModelCacheTime;
2651
+ copy._remoteModelCachePending = null;
2629
2652
  copy.interceptors.request = this.interceptors.request;
2630
2653
  copy.interceptors.response = this.interceptors.response;
2631
2654
  return copy;
@@ -2688,12 +2711,50 @@ var Cognitive = class _Cognitive {
2688
2711
  }
2689
2712
  return parseRef(pickModel([ref, ...preferences.best, ...preferences.fast], downtimes));
2690
2713
  }
2714
+ async fetchRemoteModels() {
2715
+ if (this._remoteModelCacheTime > 0 && Date.now() - this._remoteModelCacheTime < 60 * 60 * 1e3) {
2716
+ return this._remoteModelCache;
2717
+ }
2718
+ if (this._remoteModelCachePending !== null) {
2719
+ return this._remoteModelCachePending;
2720
+ }
2721
+ this._remoteModelCachePending = this._doFetchRemoteModels().finally(() => {
2722
+ this._remoteModelCachePending = null;
2723
+ });
2724
+ return this._remoteModelCachePending;
2725
+ }
2726
+ async _doFetchRemoteModels() {
2727
+ const betaClient = new CognitiveBeta(this._client.config);
2728
+ const remoteModels = await betaClient.listModels();
2729
+ this._remoteModelCache.clear();
2730
+ this._remoteModelCacheTime = Date.now();
2731
+ for (const m of remoteModels) {
2732
+ const converted = { ...m, ref: m.id, integration: "cognitive-v2" };
2733
+ this._remoteModelCache.set(m.id, converted);
2734
+ if (m.aliases) {
2735
+ for (const alias of m.aliases) {
2736
+ this._remoteModelCache.set(alias, converted);
2737
+ }
2738
+ }
2739
+ }
2740
+ return this._remoteModelCache;
2741
+ }
2691
2742
  async getModelDetails(model) {
2692
2743
  if (this._useBeta) {
2693
2744
  const resolvedModel = getCognitiveV2Model(model);
2694
2745
  if (resolvedModel) {
2695
2746
  return { ...resolvedModel, ref: resolvedModel.id, integration: "cognitive-v2" };
2696
2747
  }
2748
+ if (isKnownV2Model(model)) {
2749
+ try {
2750
+ const remoteModels = await this.fetchRemoteModels();
2751
+ const found = remoteModels.get(model);
2752
+ if (found) {
2753
+ return found;
2754
+ }
2755
+ } catch {
2756
+ }
2757
+ }
2697
2758
  }
2698
2759
  await this.fetchInstalledModels();
2699
2760
  const { integration, model: modelName } = await this._selectModel(model);
@@ -2704,12 +2765,23 @@ var Cognitive = class _Cognitive {
2704
2765
  return def;
2705
2766
  }
2706
2767
  async generateContent(input) {
2707
- if (!this._useBeta || !input.model || !getCognitiveV2Model(input.model)) {
2768
+ if (!this._useBeta || !isKnownV2Model(input.model)) {
2769
+ return this._generateContent(input);
2770
+ }
2771
+ try {
2772
+ return await this._generateContentV2(input);
2773
+ } catch (err) {
2774
+ if (input.signal?.aborted) {
2775
+ throw err;
2776
+ }
2708
2777
  return this._generateContent(input);
2709
2778
  }
2710
- if (input.systemPrompt) {
2711
- input.messages.unshift({ role: "system", content: input.systemPrompt });
2712
- delete input.systemPrompt;
2779
+ }
2780
+ async _generateContentV2(input) {
2781
+ const v2Input = { ...input, messages: [...input.messages] };
2782
+ if (v2Input.systemPrompt) {
2783
+ v2Input.messages.unshift({ role: "system", content: v2Input.systemPrompt });
2784
+ delete v2Input.systemPrompt;
2713
2785
  }
2714
2786
  const betaClient = new CognitiveBeta(this._client.config);
2715
2787
  const props = { input };
@@ -2722,7 +2794,7 @@ var Cognitive = class _Cognitive {
2722
2794
  betaClient.on("retry", (_req, error) => {
2723
2795
  this._events.emit("retry", props, error);
2724
2796
  });
2725
- const response = await betaClient.generateText(input, {
2797
+ const response = await betaClient.generateText(v2Input, {
2726
2798
  signal: input.signal,
2727
2799
  timeout: this._timeoutMs
2728
2800
  });
@@ -2737,7 +2809,7 @@ var Cognitive = class _Cognitive {
2737
2809
  content: response.output,
2738
2810
  role: "assistant",
2739
2811
  index: 0,
2740
- stopReason: response.metadata.stopReason
2812
+ stopReason: response.metadata.stopReason ?? "stop"
2741
2813
  }
2742
2814
  ],
2743
2815
  usage: {
@@ -2843,6 +2915,7 @@ export {
2843
2915
  CognitiveBeta,
2844
2916
  ModelProvider,
2845
2917
  RemoteModelProvider,
2846
- getCognitiveV2Model
2918
+ getCognitiveV2Model,
2919
+ isKnownV2Model
2847
2920
  };
2848
2921
  //# sourceMappingURL=index.mjs.map