@box/box-ai-content-answers 0.148.1 → 0.148.2

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.
@@ -1,65 +1,65 @@
1
- import { useAgentsDispatch as ls, ACTIONS as as } from "@box/box-ai-agent-selector";
2
- import ts from "lodash/camelCase";
3
- import t, { useMemo as is, useCallback as ns } from "react";
4
- import { A as R, C as us, R as J } from "../../../../../chunks/types.js";
5
- import { SET_NEW_ITEM as cs, UPDATE_QUESTION as Es, SET_MODAL_ERROR as fs, SET_MODAL_SUCCESS as rs, SET_IS_LOADING as V, SET_ANSWER_FEEDBACK as Cs, CLEAR_CONVERSATION_HISTORY as _s, SET_SUGGESTED_QUESTIONS_REQUEST_STATE as X, SET_SUGGESTED_QUESTIONS as Ts } from "../actions.js";
6
- import { QuestionRecord as Rs } from "../records.js";
7
- import { defaultFormatSuggestions as ds, defaultFormatCitations as Ns } from "../utils/format.js";
8
- import { getAnswerRequest as Os } from "../utils/request.js";
9
- import I from "../utils/stream.js";
10
- const ys = /Too Many Requests/i, Y = "Agent not found";
1
+ import { useAgentsDispatch as as, ACTIONS as ns } from "@box/box-ai-agent-selector";
2
+ import us from "lodash/camelCase";
3
+ import t, { useMemo as is, useCallback as cs } from "react";
4
+ import { A as R, C as Es, R as V } from "../../../../../chunks/types.js";
5
+ import { SET_NEW_ITEM as fs, UPDATE_QUESTION as rs, SET_MODAL_ERROR as Cs, SET_MODAL_SUCCESS as _s, SET_IS_LOADING as X, SET_ANSWER_FEEDBACK as Ts, CLEAR_CONVERSATION_HISTORY as Rs, SET_SUGGESTED_QUESTIONS_REQUEST_STATE as Y, SET_SUGGESTED_QUESTIONS as ds } from "../actions.js";
6
+ import { QuestionRecord as Ns } from "../records.js";
7
+ import { defaultFormatSuggestions as Os, defaultFormatCitations as ys } from "../utils/format.js";
8
+ import { getAnswerRequest as Ss } from "../utils/request.js";
9
+ import F from "../utils/stream.js";
10
+ const Ls = /Too Many Requests/i, z = "Agent not found";
11
11
  function Ms({
12
- contentAnswers: i,
12
+ contentAnswers: n,
13
13
  createSessionRequest: O,
14
14
  dispatchStateUpdate: o,
15
- enterpriseId: F,
15
+ enterpriseId: D,
16
16
  fetchTimeout: k,
17
- formatCitations: v = Ns,
18
- getAnswerStreaming: D,
19
- getAnswer: j,
20
- getSuggestedQuestions: m,
21
- isStopResponseEnabled: x,
22
- isStreamingEnabled: L,
23
- itemID: c,
24
- items: E,
25
- onSuggestedQuestionsFetched: K,
17
+ formatCitations: v = ys,
18
+ getAnswerStreaming: j,
19
+ getAnswer: x,
20
+ getSuggestedQuestions: I,
21
+ isStopResponseEnabled: K,
22
+ isStreamingEnabled: S,
23
+ itemID: E,
24
+ items: i,
25
+ onSuggestedQuestionsFetched: P,
26
26
  sendErrorLog: G,
27
27
  shouldPreinitSession: d = !0,
28
- userId: P
28
+ userId: W
29
29
  }) {
30
- const y = t.useRef(null), W = ls(), S = is(() => Array.isArray(E) && E.length > 1, [E]), $ = ns((s) => Array.isArray(s) ? v(s) : null, [v]);
30
+ const y = t.useRef(null), $ = as(), L = is(() => Array.isArray(i) && i.length > 1, [i]), B = cs((s) => Array.isArray(s) ? v(s) : null, [v]);
31
31
  t.useEffect(
32
32
  (s) => {
33
- c && c !== i.item.id && o({
34
- type: cs,
35
- itemID: c,
33
+ E && E !== n.item.id && o({
34
+ type: fs,
35
+ itemID: E,
36
36
  fileVersionID: s
37
37
  });
38
38
  },
39
39
  // eslint-disable-next-line react-hooks/exhaustive-deps
40
- [c]
40
+ [E]
41
41
  );
42
- const u = t.useCallback((s, e) => {
42
+ const c = t.useCallback((s, e) => {
43
43
  o({
44
- type: Es,
44
+ type: rs,
45
45
  newQuestion: s,
46
46
  session: e
47
47
  }), y.current = s;
48
- }, [o]), B = t.useCallback((s) => {
48
+ }, [o]), H = t.useCallback((s) => {
49
49
  o({
50
- type: fs,
50
+ type: Cs,
51
51
  error: s
52
52
  });
53
53
  }, [o]), Q = t.useCallback((s, e, a) => {
54
54
  const {
55
- answer: n,
55
+ answer: u,
56
56
  error: f,
57
57
  citations: l = [],
58
58
  createdAt: r = null,
59
59
  encodedSession: C = void 0,
60
60
  contextSession: _ = void 0
61
- } = s, N = e.set("citations", $(l)).merge({
62
- answer: n,
61
+ } = s, N = e.set("citations", B(l)).merge({
62
+ answer: u,
63
63
  error: f,
64
64
  created_at: r,
65
65
  isLoading: !1,
@@ -70,68 +70,68 @@ function Ms({
70
70
  encodedSession: C
71
71
  }), _ && (T = {
72
72
  contextSession: _
73
- }), u(N, T);
74
- }, [u, $]), g = t.useCallback(
73
+ }), c(N, T);
74
+ }, [c, B]), g = t.useCallback(
75
75
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
76
76
  (s, e) => {
77
- var N, T, b;
78
- const a = (s == null ? void 0 : s.message) || "", n = ((N = s == null ? void 0 : s.response) == null ? void 0 : N.status) === 429 || ys.test(a), f = (s == null ? void 0 : s.code) === Y || (s == null ? void 0 : s.errorCode) === Y, l = ((T = s == null ? void 0 : s.response) == null ? void 0 : T.status) === 204, r = ((b = s == null ? void 0 : s.response) == null ? void 0 : b.status) === 412, C = l ? R.NO_CONTENT : n ? R.RATE_LIMITING : r ? R.PRECONDITION_FAILED : f ? R.AGENT_NOT_FOUND : R.GENERAL, _ = e.merge({
77
+ var N, T, m;
78
+ const a = (s == null ? void 0 : s.message) || "", u = ((N = s == null ? void 0 : s.response) == null ? void 0 : N.status) === 429 || Ls.test(a), f = (s == null ? void 0 : s.code) === z || (s == null ? void 0 : s.errorCode) === z, l = ((T = s == null ? void 0 : s.response) == null ? void 0 : T.status) === 204, r = ((m = s == null ? void 0 : s.response) == null ? void 0 : m.status) === 412, C = l ? R.NO_CONTENT : u ? R.RATE_LIMITING : r ? R.PRECONDITION_FAILED : f ? R.AGENT_NOT_FOUND : R.GENERAL, _ = e.merge({
79
79
  error: C,
80
80
  isLoading: !1,
81
81
  isCompleted: !0
82
82
  });
83
- u(_, void 0);
83
+ c(_, void 0);
84
84
  },
85
- [u]
85
+ [c]
86
86
  ), A = t.useCallback(async (s, e) => {
87
87
  try {
88
88
  const a = {
89
89
  prompt: s.prompt,
90
90
  aiAgent: e || void 0,
91
- userId: P,
92
- enterpriseId: F
93
- }, n = `content-answers-${s.id}`;
91
+ userId: W,
92
+ enterpriseId: D
93
+ }, u = `content-answers-${s.id}`;
94
94
  let f;
95
- L && (f = I.createAbortRequest(n));
96
- const l = await Os(a, L, n, c, E, i, j, D, f, k, G);
97
- L ? I.receiveStreamedResponse(n, l, (r) => {
95
+ S && (f = F.createAbortRequest(u));
96
+ const l = await Ss(a, S, u, E, i, n, x, j, f, k, G);
97
+ S ? F.receiveStreamedResponse(u, l, (r) => {
98
98
  const {
99
99
  code: C,
100
100
  data: _,
101
101
  error: N,
102
102
  citations: T,
103
- contextSession: b,
104
- isCompleted: ss,
105
- createdAt: es
106
- } = r, os = {
103
+ contextSession: m,
104
+ isCompleted: os,
105
+ createdAt: ts
106
+ } = r, ls = {
107
107
  answer: _,
108
108
  citations: T,
109
109
  error: N,
110
- contextSession: b,
111
- createdAt: es
110
+ contextSession: m,
111
+ createdAt: ts
112
112
  };
113
113
  l.ok && l.status === 200 ? Q({
114
- ...os
115
- }, s, ss) : g({
114
+ ...ls
115
+ }, s, os) : g({
116
116
  response: l,
117
117
  code: C
118
118
  }, s);
119
119
  }, void 0, k, G) : (Object.entries(l).forEach(([r, C]) => {
120
- const _ = ts(r);
120
+ const _ = us(r);
121
121
  delete l[r], l[_] = C;
122
122
  }), Q(l, s, !0));
123
123
  } catch (a) {
124
- if (x && a instanceof Error && a.name === "AbortError") {
125
- const n = s.set("error", []).merge({
124
+ if (K && a instanceof Error && a.name === "AbortError") {
125
+ const u = s.set("error", []).merge({
126
126
  error: R.RESPONSE_STOPPED,
127
127
  isLoading: !1,
128
128
  isCompleted: !0
129
129
  });
130
- u(n);
130
+ c(u);
131
131
  } else
132
132
  g(a, s);
133
133
  }
134
- }, [P, F, L, c, E, i, j, D, k, G, Q, g, x, u]), H = t.useCallback((s) => {
134
+ }, [W, D, S, E, i, n, x, j, k, G, Q, g, K, c]), J = t.useCallback((s) => {
135
135
  const {
136
136
  encoded_session: e,
137
137
  metadata: {
@@ -141,95 +141,102 @@ function Ms({
141
141
  }
142
142
  } = s;
143
143
  o({
144
- type: rs,
144
+ type: _s,
145
145
  encodedSession: e,
146
146
  isLargeFile: a
147
147
  });
148
- }, [o]), M = t.useCallback(async (s = !0, e = !0) => {
148
+ }, [o]), p = t.useCallback(async (s = !0, e = !0) => {
149
149
  try {
150
150
  s && o({
151
- type: V,
151
+ type: X,
152
152
  isLoading: !0
153
153
  });
154
154
  const a = await O({
155
- items: E
156
- }, c);
157
- H(a);
155
+ items: i
156
+ }, E);
157
+ J(a);
158
158
  } catch {
159
- if (e && B(us.GENERAL), !d && (o({
160
- type: V,
159
+ if (e && H(Es.GENERAL), !d && (o({
160
+ type: X,
161
161
  isLoading: !1
162
162
  }), y.current)) {
163
- const n = y.current.set("error", R.GENERAL).merge({
163
+ const u = y.current.set("error", R.GENERAL).merge({
164
164
  isLoading: !1,
165
165
  isCompleted: !0
166
166
  });
167
- u(n);
167
+ c(u);
168
168
  }
169
169
  }
170
- }, [O, E, c, H, o, d, B, u]), p = t.useCallback((s, e, a = !0) => {
171
- const n = !i.isLoading && !i.encodedSession && !S && O, f = (i == null ? void 0 : i.questions) || [];
172
- a && (s.id = `${f.length}`), !d && n && (s.error = R.GENERAL, s.isLoading = !1, s.isCompleted = !0);
173
- const l = new Rs(s);
174
- return u(l), d || i.encodedSession ? A(l, e) : !i.isLoading && !d && O && M(!0, !1), l;
175
- }, [i.encodedSession, i.isLoading, i == null ? void 0 : i.questions, M, O, S, A, d, u]), z = t.useCallback((s, e) => {
170
+ }, [O, i, E, J, o, d, H, c]), M = t.useCallback((s, e, a = !0) => {
171
+ const u = !n.isLoading && !n.encodedSession && !L && O, f = (n == null ? void 0 : n.questions) || [];
172
+ a && (s.id = `${f.length}`), !d && u && (s.error = R.GENERAL, s.isLoading = !1, s.isCompleted = !0);
173
+ const l = new Ns(s);
174
+ return c(l), d || n.encodedSession ? A(l, e) : !n.isLoading && !d && O && p(!0, !1), l;
175
+ }, [n.encodedSession, n.isLoading, n == null ? void 0 : n.questions, p, O, L, A, d, c]), Z = t.useCallback((s, e) => {
176
176
  o({
177
- type: Cs,
177
+ type: Ts,
178
178
  questionId: s,
179
179
  feedbackValue: e
180
180
  });
181
- }, [o]), Z = t.useCallback(() => {
181
+ }, [o]), q = t.useCallback(() => {
182
182
  o({
183
- type: _s
183
+ type: Rs
184
184
  });
185
- }, [o]), q = t.useCallback(async (s, e) => {
186
- p({
185
+ }, [o]), w = t.useCallback(async (s, e) => {
186
+ M({
187
187
  ...s.toJS(),
188
188
  answer: "",
189
189
  isLoading: !0,
190
190
  isCompleted: !1,
191
191
  error: null
192
192
  }, e, !1);
193
- }, [p]), w = t.useCallback((s) => {
193
+ }, [M]), U = t.useCallback((s) => {
194
194
  let e;
195
- s ? e = s.id : y.current && (e = y.current.id), e && I.abortRequest(`content-answers-${e}`, !0);
196
- }, []), U = t.useCallback(async (s) => {
197
- s && W({
198
- type: as.SET_ASK_CONFIG,
195
+ s ? e = s.id : y.current && (e = y.current.id), e && F.abortRequest(`content-answers-${e}`, !0);
196
+ }, []), h = t.useCallback(async (s) => {
197
+ s && $({
198
+ type: ns.SET_ASK_CONFIG,
199
199
  id: s.id,
200
200
  ask: s == null ? void 0 : s.ask
201
201
  });
202
- }, [W]), h = t.useCallback(async () => {
203
- if (!(!m || S)) {
202
+ }, [$]), [b, ss] = t.useState("");
203
+ t.useEffect(() => {
204
+ if (!i || i.length === 0)
205
+ return;
206
+ const s = i.map((e) => e.id).join(",");
207
+ s !== b && ss(s);
208
+ }, [i, b]);
209
+ const es = t.useCallback(async () => {
210
+ if (!(!b || !I || L)) {
204
211
  o({
205
- type: X,
206
- requestState: J.IN_PROGRESS
212
+ type: Y,
213
+ requestState: V.IN_PROGRESS
207
214
  });
208
215
  try {
209
- const s = ds(await m(c, E));
210
- K && K(s), o({
211
- type: Ts,
216
+ const s = Os(await I(E, i));
217
+ P && P(s), o({
218
+ type: ds,
212
219
  suggestedQuestions: s
213
220
  });
214
221
  } catch {
215
222
  o({
216
- type: X,
217
- requestState: J.ERROR
223
+ type: Y,
224
+ requestState: V.ERROR
218
225
  });
219
226
  }
220
227
  }
221
- }, [o, m, S, c, E]);
228
+ }, [o, I, L, E, b]);
222
229
  return {
223
- clearConversation: Z,
224
- createSession: M,
225
- fetchAgentConfig: U,
226
- fetchSuggestedQuestions: h,
227
- retryQuestion: q,
228
- sendQuestion: p,
230
+ clearConversation: q,
231
+ createSession: p,
232
+ fetchAgentConfig: h,
233
+ fetchSuggestedQuestions: es,
234
+ retryQuestion: w,
235
+ sendQuestion: M,
229
236
  sendQuestionAnswerRequest: A,
230
- setAnswerFeedback: z,
231
- stopQuestion: w,
232
- updateQuestionInState: u
237
+ setAnswerFeedback: Z,
238
+ stopQuestion: U,
239
+ updateQuestionInState: c
233
240
  };
234
241
  }
235
242
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@box/box-ai-content-answers",
3
- "version": "0.148.1",
3
+ "version": "0.148.2",
4
4
  "peerDependencies": {
5
5
  "@box/blueprint-web": "^7.8.0",
6
6
  "@box/blueprint-web-assets": "^4.56.1",
@@ -17,10 +17,10 @@
17
17
  },
18
18
  "devDependencies": {
19
19
  "@box/babel-plugin-target-attributes": "1.3.0",
20
- "@box/blueprint-web": "^12.64.0",
20
+ "@box/blueprint-web": "^12.64.1",
21
21
  "@box/blueprint-web-assets": "^4.64.8",
22
- "@box/box-ai-agent-selector": "^0.58.1",
23
- "@box/item-icon": "^0.23.1",
22
+ "@box/box-ai-agent-selector": "^0.58.2",
23
+ "@box/item-icon": "^0.23.2",
24
24
  "@box/storybook-utils": "^0.14.0",
25
25
  "@testing-library/react": "^15.0.6",
26
26
  "react": "^18.3.0",