@inkeep/agents-ui 0.15.9 → 0.15.11
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/color-mode/index.d.ts +0 -1
- package/dist/primitives/components/embedded-chat/use-inkeep-chat.cjs +2 -2
- package/dist/primitives/components/embedded-chat/use-inkeep-chat.d.ts +1 -0
- package/dist/primitives/components/embedded-chat/use-inkeep-chat.js +132 -122
- package/dist/primitives/components/embedded-chat/use-stream-processor.cjs +1 -1
- package/dist/primitives/components/embedded-chat/use-stream-processor.js +85 -82
- package/dist/primitives/components/embedded-chat.cjs +3 -3
- package/dist/primitives/components/embedded-chat.js +145 -145
- package/dist/primitives/hooks/use-instant-update.d.ts +0 -1
- package/dist/primitives/providers/base-events-provider.cjs +1 -1
- package/dist/primitives/providers/base-events-provider.js +1 -1
- package/dist/primitives/utils/split-props.d.ts +0 -1
- package/dist/react/chat-button.d.ts +0 -2
- package/dist/styled/components/data-summary-group.cjs +1 -1
- package/dist/styled/components/data-summary-group.js +70 -63
- package/dist/styled/components/message.cjs +1 -1
- package/dist/styled/components/message.js +253 -144
- package/dist/styled/inkeep.css.cjs +67 -0
- package/dist/styled/inkeep.css.js +67 -0
- package/dist/types/config/ai.d.ts +13 -15
- package/dist/types/config/base.d.ts +0 -20
- package/dist/types/config/components.d.ts +6 -0
- package/dist/types/config/search.d.ts +0 -2
- package/dist/types/config/settings/actions.d.ts +68 -2
- package/dist/types/sources.d.ts +1 -9
- package/package.json +1 -1
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const re=require("@ai-sdk/react"),ae=require("ai"),n=require("react"),oe=require("../../providers/config-provider.cjs"),ie=require("../../hooks/use-media-query.cjs"),U=require("../../utils/generate-uid.cjs"),ce=require("../../providers/base-events-provider.cjs"),ue=require("../../providers/chat-form-provider.cjs"),le=require("../../providers/widget-provider.cjs"),pe=require("@radix-ui/react-use-controllable-state"),de=require("../../hooks/use-streaming-events.cjs"),O=`Hmm..
|
|
2
2
|
|
|
3
|
-
It seems I might be having some issues right now. Please clear the chat and try again.`,
|
|
3
|
+
It seems I might be having some issues right now. Please clear the chat and try again.`,ge=()=>{const{baseSettings:C,aiChatSettings:l}=oe.useInkeepConfig(),[r="",I]=pe.useControllableState({prop:l.conversationId,defaultProp:l.conversationId??""}),{logEvent:o}=ce.useBaseEvents(),{setConversationId:B,emitToParent:p}=de.useStreamingEvents(),[i,d]=n.useState(""),H=e=>d(e.target.value),{filters:M}=C,{onInputMessageChange:W,filters:w,agentUrl:_,context:g,headers:m,apiKey:f}=l,z=e=>{switch(e.code){case 400:return e.message;case 403:return`There seems to be a configuration error. Please contact ${C.organizationDisplayName??"Administrator"}`;default:return O}},[K,h]=n.useState([]),[L,q]=n.useState(null),$=()=>({"x-inkeep-client-timezone":Intl.DateTimeFormat().resolvedOptions().timeZone,"x-inkeep-client-timestamp":new Date().toISOString()}),j=n.useMemo(()=>new ae.DefaultChatTransport({api:_,headers:{...f?{Authorization:`Bearer ${f}`}:{},...m},body:{requestContext:g}}),[_,f,m,g]),{messages:c,sendMessage:F,addToolApprovalResponse:G,status:x,setMessages:A,stop:Q,error:v}=re.useChat({transport:j,onData(e){p(e.type,e.data)},async onFinish(){p("completion",{conversationId:r}),await o({eventName:"assistant_message_received",properties:{conversationId:r}}),o({eventName:"assistant_answer_displayed",properties:{conversationId:r}})},onError(e){console.log("onError",e.message),o({eventName:"chat_error",properties:{conversationId:r,error:e.message}}),A(u=>{const s=[...u],t=s[s.length-1];if(t){const a=z(e);t.role==="user"?s.push({id:U.generateUid(16),role:"assistant",parts:[{type:"text",text:a}]}):t.parts=[{type:"text",text:a}]}return s})}}),P=x==="submitted",S=x==="streaming",J=n.useMemo(()=>{const e=a=>{if(!a||typeof a!="object")return!1;const N=a;return typeof N.type=="string"&&N.type.startsWith("tool-")},s=[...c??[]].reverse().find(a=>a.role==="assistant");if(!s)return!1;const t=s.parts?.at(-1);return!(!e(t)||t.state!=="output-available"||!t.approval?.id||S)},[c,S]),R=S||J,k=P||R,V=c.length===0,y=!i.trim()||k,Z=ie.useMediaQuery("(max-width: 768px)"),[X,E]=n.useState(null);n.useEffect(()=>{v&&E(v)},[v]);const Y=()=>E(null),D=n.useRef(null);n.useEffect(()=>{W?.(i)},[i]);const ee=e=>{e.key==="Enter"&&!e.shiftKey&&!y&&!e.nativeEvent.isComposing&&(e.preventDefault(),b())},b=async(e=i)=>{if(y&&(!e||e.trim().length===0))return;h([]),d(""),await o({eventName:"user_message_submitted",properties:{conversationId:r}});const u=M||w?JSON.stringify({...M,...w}):void 0,s={...m};u&&(s["inkeep-filters"]=u);let t=r;t||(t=`conv_${U.generateUid(16)}`,I(t)),B(t),F({text:e},{headers:{...$(),...s},body:{conversationId:t,requestContext:g}})},te=()=>{Q().then(()=>{p("aborted",{conversationId:r})})},T=()=>{Y(),A([]),I(""),h([]),q(null),o({eventName:"chat_clear_button_clicked",properties:{conversationId:r}})},{openForm:se}=ue.useChatForm(),ne=le.useWidget();return n.useImperativeHandle(l.chatFunctionsRef,()=>({submitMessage:b,updateInputMessage(e){d(e)},clearChat:T,openForm:e=>{ne?.setView("chat"),se(e,void 0)},focusInput:()=>{D.current?.focus()}})),{messages:c,sendMessage:F,addToolApprovalResponse:G,isLoading:P,isStreaming:R,isBusy:k,error:X,setError:E,isSubmitDisabled:y,input:i,handleInputChange:H,handleInputKeyDown:ee,handleSubmit:b,stop:te,clear:T,isEmpty:c.length===0,inputRef:D,isMobile:Z,messageAttachments:K,setMessageAttachments:h,selectedWorkflow:L,setSelectedWorkflow:q,isNewChat:V,conversationId:r}};exports.DEFAULT_ERROR_MESSAGE=O;exports.useInkeepChat=ge;
|
|
@@ -26,6 +26,7 @@ export declare const useInkeepChat: () => {
|
|
|
26
26
|
addToolApprovalResponse: ChatAddToolApproveResponseFunction;
|
|
27
27
|
isLoading: boolean;
|
|
28
28
|
isStreaming: boolean;
|
|
29
|
+
isBusy: boolean;
|
|
29
30
|
error: Error | null;
|
|
30
31
|
setError: Dispatch<SetStateAction<Error | null>>;
|
|
31
32
|
isSubmitDisabled: boolean;
|
|
@@ -1,194 +1,204 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useChat as
|
|
3
|
-
import { DefaultChatTransport as
|
|
4
|
-
import { useState as
|
|
5
|
-
import { useInkeepConfig as
|
|
6
|
-
import { useMediaQuery as
|
|
7
|
-
import { generateUid as
|
|
8
|
-
import { useBaseEvents as
|
|
9
|
-
import { useChatForm as
|
|
10
|
-
import { useWidget as
|
|
11
|
-
import { useControllableState as
|
|
12
|
-
import { useStreamingEvents as
|
|
13
|
-
const
|
|
2
|
+
import { useChat as re } from "@ai-sdk/react";
|
|
3
|
+
import { DefaultChatTransport as ae } from "ai";
|
|
4
|
+
import { useState as m, useMemo as H, useEffect as O, useRef as ie, useImperativeHandle as le } from "react";
|
|
5
|
+
import { useInkeepConfig as ce } from "../../providers/config-provider.js";
|
|
6
|
+
import { useMediaQuery as me } from "../../hooks/use-media-query.js";
|
|
7
|
+
import { generateUid as W } from "../../utils/generate-uid.js";
|
|
8
|
+
import { useBaseEvents as pe } from "../../providers/base-events-provider.js";
|
|
9
|
+
import { useChatForm as ue } from "../../providers/chat-form-provider.js";
|
|
10
|
+
import { useWidget as fe } from "../../providers/widget-provider.js";
|
|
11
|
+
import { useControllableState as ge } from "@radix-ui/react-use-controllable-state";
|
|
12
|
+
import { useStreamingEvents as de } from "../../hooks/use-streaming-events.js";
|
|
13
|
+
const he = `Hmm..
|
|
14
14
|
|
|
15
|
-
It seems I might be having some issues right now. Please clear the chat and try again.`,
|
|
16
|
-
const { baseSettings:
|
|
17
|
-
prop:
|
|
18
|
-
defaultProp:
|
|
19
|
-
}), { logEvent:
|
|
15
|
+
It seems I might be having some issues right now. Please clear the chat and try again.`, Fe = () => {
|
|
16
|
+
const { baseSettings: S, aiChatSettings: c } = ce(), [n = "", E] = ge({
|
|
17
|
+
prop: c.conversationId,
|
|
18
|
+
defaultProp: c.conversationId ?? ""
|
|
19
|
+
}), { logEvent: r } = pe(), { setConversationId: q, emitToParent: p } = de(), [a, u] = m(""), z = (e) => u(e.target.value), {
|
|
20
20
|
/* shouldBypassCaptcha, */
|
|
21
|
-
filters:
|
|
22
|
-
} =
|
|
23
|
-
onInputMessageChange:
|
|
24
|
-
filters:
|
|
25
|
-
agentUrl:
|
|
26
|
-
context:
|
|
27
|
-
headers:
|
|
21
|
+
filters: w
|
|
22
|
+
} = S, {
|
|
23
|
+
onInputMessageChange: K,
|
|
24
|
+
filters: M,
|
|
25
|
+
agentUrl: x,
|
|
26
|
+
context: f,
|
|
27
|
+
headers: g,
|
|
28
28
|
apiKey: d
|
|
29
|
-
} =
|
|
29
|
+
} = c, U = (e) => {
|
|
30
30
|
switch (e.code) {
|
|
31
31
|
case 400:
|
|
32
32
|
return e.message;
|
|
33
33
|
case 403:
|
|
34
|
-
return `There seems to be a configuration error. Please contact ${
|
|
34
|
+
return `There seems to be a configuration error. Please contact ${S.organizationDisplayName ?? "Administrator"}`;
|
|
35
35
|
default:
|
|
36
|
-
return
|
|
36
|
+
return he;
|
|
37
37
|
}
|
|
38
|
-
}, [
|
|
38
|
+
}, [$, h] = m([]), [L, _] = m(null), j = () => ({
|
|
39
39
|
"x-inkeep-client-timezone": Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
40
40
|
"x-inkeep-client-timestamp": (/* @__PURE__ */ new Date()).toISOString()
|
|
41
|
-
}),
|
|
42
|
-
() => new
|
|
43
|
-
api:
|
|
41
|
+
}), G = H(
|
|
42
|
+
() => new ae({
|
|
43
|
+
api: x,
|
|
44
44
|
headers: {
|
|
45
45
|
...d ? { Authorization: `Bearer ${d}` } : {},
|
|
46
|
-
...
|
|
46
|
+
...g
|
|
47
47
|
},
|
|
48
48
|
body: {
|
|
49
|
-
requestContext:
|
|
49
|
+
requestContext: f
|
|
50
50
|
}
|
|
51
51
|
}),
|
|
52
|
-
[
|
|
52
|
+
[x, d, g, f]
|
|
53
53
|
), {
|
|
54
|
-
messages:
|
|
55
|
-
sendMessage:
|
|
56
|
-
addToolApprovalResponse:
|
|
57
|
-
status:
|
|
58
|
-
setMessages:
|
|
59
|
-
stop:
|
|
60
|
-
error:
|
|
61
|
-
} =
|
|
62
|
-
transport:
|
|
54
|
+
messages: i,
|
|
55
|
+
sendMessage: F,
|
|
56
|
+
addToolApprovalResponse: J,
|
|
57
|
+
status: k,
|
|
58
|
+
setMessages: A,
|
|
59
|
+
stop: Q,
|
|
60
|
+
error: v
|
|
61
|
+
} = re({
|
|
62
|
+
transport: G,
|
|
63
63
|
onData(e) {
|
|
64
|
-
|
|
64
|
+
p(e.type, e.data);
|
|
65
65
|
},
|
|
66
66
|
async onFinish() {
|
|
67
|
-
|
|
67
|
+
p("completion", { conversationId: n }), await r({
|
|
68
68
|
eventName: "assistant_message_received",
|
|
69
69
|
properties: {
|
|
70
|
-
conversationId:
|
|
70
|
+
conversationId: n
|
|
71
71
|
}
|
|
72
|
-
}),
|
|
72
|
+
}), r({
|
|
73
73
|
eventName: "assistant_answer_displayed",
|
|
74
74
|
properties: {
|
|
75
|
-
conversationId:
|
|
75
|
+
conversationId: n
|
|
76
76
|
}
|
|
77
77
|
});
|
|
78
78
|
},
|
|
79
79
|
onError(e) {
|
|
80
|
-
console.log("onError", e.message),
|
|
80
|
+
console.log("onError", e.message), r({
|
|
81
81
|
eventName: "chat_error",
|
|
82
82
|
properties: {
|
|
83
|
-
conversationId:
|
|
83
|
+
conversationId: n,
|
|
84
84
|
error: e.message
|
|
85
85
|
}
|
|
86
|
-
}),
|
|
87
|
-
const
|
|
88
|
-
if (
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
id:
|
|
86
|
+
}), A((l) => {
|
|
87
|
+
const s = [...l], t = s[s.length - 1];
|
|
88
|
+
if (t) {
|
|
89
|
+
const o = U(e);
|
|
90
|
+
t.role === "user" ? s.push({
|
|
91
|
+
id: W(16),
|
|
92
92
|
role: "assistant",
|
|
93
|
-
parts: [{ type: "text", text:
|
|
94
|
-
}) :
|
|
93
|
+
parts: [{ type: "text", text: o }]
|
|
94
|
+
}) : t.parts = [{ type: "text", text: o }];
|
|
95
95
|
}
|
|
96
|
-
return
|
|
96
|
+
return s;
|
|
97
97
|
});
|
|
98
98
|
}
|
|
99
|
-
}), D =
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
99
|
+
}), D = k === "submitted", y = k === "streaming", V = H(() => {
|
|
100
|
+
const e = (o) => {
|
|
101
|
+
if (!o || typeof o != "object") return !1;
|
|
102
|
+
const B = o;
|
|
103
|
+
return typeof B.type == "string" && B.type.startsWith("tool-");
|
|
104
|
+
}, s = [...i ?? []].reverse().find((o) => o.role === "assistant");
|
|
105
|
+
if (!s) return !1;
|
|
106
|
+
const t = s.parts?.at(-1);
|
|
107
|
+
return !(!e(t) || t.state !== "output-available" || !t.approval?.id || y);
|
|
108
|
+
}, [i, y]), N = y || V, T = D || N, Z = i.length === 0, I = !a.trim() || T, X = me("(max-width: 768px)"), [Y, C] = m(null);
|
|
109
|
+
O(() => {
|
|
110
|
+
v && C(v);
|
|
111
|
+
}, [v]);
|
|
112
|
+
const ee = () => C(null), R = ie(null);
|
|
113
|
+
O(() => {
|
|
114
|
+
K?.(a);
|
|
115
|
+
}, [a]);
|
|
116
|
+
const te = (e) => {
|
|
117
|
+
e.key === "Enter" && !e.shiftKey && !I && !e.nativeEvent.isComposing && (e.preventDefault(), b());
|
|
118
|
+
}, b = async (e = a) => {
|
|
119
|
+
if (I && (!e || e.trim().length === 0)) return;
|
|
120
|
+
h([]), u(""), await r({
|
|
112
121
|
eventName: "user_message_submitted",
|
|
113
122
|
properties: {
|
|
114
|
-
conversationId:
|
|
123
|
+
conversationId: n
|
|
115
124
|
}
|
|
116
125
|
});
|
|
117
|
-
const
|
|
118
|
-
...
|
|
119
|
-
...
|
|
120
|
-
}) : void 0,
|
|
121
|
-
...
|
|
126
|
+
const l = w || M ? JSON.stringify({
|
|
127
|
+
...w,
|
|
128
|
+
...M
|
|
129
|
+
}) : void 0, s = {
|
|
130
|
+
...g
|
|
122
131
|
};
|
|
123
|
-
|
|
124
|
-
let
|
|
125
|
-
|
|
132
|
+
l && (s["inkeep-filters"] = l);
|
|
133
|
+
let t = n;
|
|
134
|
+
t || (t = `conv_${W(16)}`, E(t)), q(t), F(
|
|
126
135
|
{
|
|
127
136
|
text: e
|
|
128
137
|
},
|
|
129
138
|
{
|
|
130
139
|
headers: {
|
|
131
|
-
...
|
|
132
|
-
...
|
|
140
|
+
...j(),
|
|
141
|
+
...s
|
|
133
142
|
},
|
|
134
143
|
body: {
|
|
135
|
-
conversationId:
|
|
136
|
-
requestContext:
|
|
144
|
+
conversationId: t,
|
|
145
|
+
requestContext: f
|
|
137
146
|
}
|
|
138
147
|
}
|
|
139
148
|
);
|
|
140
|
-
},
|
|
141
|
-
|
|
142
|
-
|
|
149
|
+
}, se = () => {
|
|
150
|
+
Q().then(() => {
|
|
151
|
+
p("aborted", { conversationId: n });
|
|
143
152
|
});
|
|
144
|
-
},
|
|
145
|
-
|
|
153
|
+
}, P = () => {
|
|
154
|
+
ee(), A([]), E(""), h([]), _(null), r({
|
|
146
155
|
eventName: "chat_clear_button_clicked",
|
|
147
156
|
properties: {
|
|
148
|
-
conversationId:
|
|
157
|
+
conversationId: n
|
|
149
158
|
}
|
|
150
159
|
});
|
|
151
|
-
}, { openForm:
|
|
152
|
-
return
|
|
153
|
-
submitMessage:
|
|
160
|
+
}, { openForm: ne } = ue(), oe = fe();
|
|
161
|
+
return le(c.chatFunctionsRef, () => ({
|
|
162
|
+
submitMessage: b,
|
|
154
163
|
updateInputMessage(e) {
|
|
155
|
-
|
|
164
|
+
u(e);
|
|
156
165
|
},
|
|
157
|
-
clearChat:
|
|
166
|
+
clearChat: P,
|
|
158
167
|
openForm: (e) => {
|
|
159
|
-
|
|
168
|
+
oe?.setView("chat"), ne(e, void 0);
|
|
160
169
|
},
|
|
161
170
|
focusInput: () => {
|
|
162
|
-
|
|
171
|
+
R.current?.focus();
|
|
163
172
|
}
|
|
164
173
|
})), {
|
|
165
|
-
messages:
|
|
166
|
-
sendMessage:
|
|
167
|
-
addToolApprovalResponse:
|
|
174
|
+
messages: i,
|
|
175
|
+
sendMessage: F,
|
|
176
|
+
addToolApprovalResponse: J,
|
|
168
177
|
isLoading: D,
|
|
169
|
-
isStreaming:
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
178
|
+
isStreaming: N,
|
|
179
|
+
isBusy: T,
|
|
180
|
+
error: Y,
|
|
181
|
+
setError: C,
|
|
182
|
+
isSubmitDisabled: I,
|
|
183
|
+
input: a,
|
|
184
|
+
handleInputChange: z,
|
|
185
|
+
handleInputKeyDown: te,
|
|
186
|
+
handleSubmit: b,
|
|
187
|
+
stop: se,
|
|
188
|
+
clear: P,
|
|
189
|
+
isEmpty: i.length === 0,
|
|
190
|
+
inputRef: R,
|
|
191
|
+
isMobile: X,
|
|
182
192
|
// Additional state for attachments and workflow
|
|
183
|
-
messageAttachments:
|
|
184
|
-
setMessageAttachments:
|
|
185
|
-
selectedWorkflow:
|
|
186
|
-
setSelectedWorkflow:
|
|
187
|
-
isNewChat:
|
|
188
|
-
conversationId:
|
|
193
|
+
messageAttachments: $,
|
|
194
|
+
setMessageAttachments: h,
|
|
195
|
+
selectedWorkflow: L,
|
|
196
|
+
setSelectedWorkflow: _,
|
|
197
|
+
isNewChat: Z,
|
|
198
|
+
conversationId: n
|
|
189
199
|
};
|
|
190
200
|
};
|
|
191
201
|
export {
|
|
192
|
-
|
|
193
|
-
|
|
202
|
+
he as DEFAULT_ERROR_MESSAGE,
|
|
203
|
+
Fe as useInkeepChat
|
|
194
204
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("react"),L=require("./use-inkeep-chat.cjs");function M(l,u,C){const[p,k]=r.useState([]),[y,x]=r.useState(new Map),[w,A]=r.useState(Date.now()),[P,d]=r.useState(!1),S=r.useRef(0),D=r.useRef(0),R=r.useRef([]),m=r.useRef(y);m.current=y;const T=r.useCallback(()=>{const t=[];let e="",o=[],i=!1;l.length>S.current&&(A(Date.now()),S.current=l.length),D.current=0;const c=new Map,a=(n=!1)=>{if(o.length>0){const s=`group-${D.current++}`,h=m.current.get(s);h?c.set(s,{...h,isCompleted:n}):c.set(s,{isCompleted:n}),t.push({type:"summary-group",summaries:[...o],groupKey:s}),o=[]}};for(const n of l)if(n.type==="text")a(!0),e+=n.text||"";else if(n.type==="data-summary")e.trim()&&(t.push({type:"text",text:e}),e=""),o.push(n.data);else if(n.type==="data-operation"){e.trim()&&(t.push({type:"text",text:e}),e="");const s=n.data;if(s?.type){if((s.type==="tool_call"||s.type==="tool_result")&&s.details?.data?.inDelegatedAgent===!1)continue;switch(s.type){case"agent_initializing":break;case"completion":{const{type:f}=s;o.push({type:f,label:"Completed"}),a(!0);break}case"error":{const f=s.message||"Unknown error";if(console.warn("Data operation error:",f),!t.length)a(!0),C(new Error(f)),i||(t.push({type:"text",text:L.DEFAULT_ERROR_MESSAGE}),i=!0);else{const{type:g,label:E}=s;o.push({type:g,label:E})}break}default:{const{type:f,label:g}=s;o.push({type:f,label:g});break}}}}else if(n.type==="data-artifact")if(n.data?.type?.toLowerCase()==="citation"){const s=n.data,h=s.artifactSummary||{title:s.name};e+=` ^${h?.title||s.name}^`}else e.trim()&&(t.push({type:"text",text:e}),e=""),t.push(n);else n.type==="data-component"?(e.trim()&&(t.push({type:"text",text:e}),e=""),a(!0),t.push(n)):(e.trim()&&(t.push({type:"text",text:e}),e=""),a(!0),t.push(n));return a(!u),e.trim()&&t.push({type:"text",text:e}),{processed:t,newTimings:c}},[l,u,C]);r.useEffect(()=>{if(R.current===l)return;R.current=l;const{processed:t,newTimings:e}=T();k(t),(e.size!==m.current.size||Array.from(e.entries()).some(([i,c])=>{const a=m.current.get(i);return!a||a.isCompleted!==c.isCompleted}))&&x(e)},[T,l]),r.useEffect(()=>{u||x(t=>{const e=new Map(t);let o=!1;for(const[i,c]of e.entries())c.isCompleted||(e.set(i,{...c,isCompleted:!0}),o=!0);return o?e:t})},[u]);const b=r.useCallback(()=>{const t=Date.now()-w,e=Array.from(m.current.values()).some(i=>!i.isCompleted),o=t>1e3&&!e;d(o)},[w]);r.useEffect(()=>{if(!u){d(!1);return}if(!(p.length>0)){d(!1);return}const e=setInterval(b,200);return()=>clearInterval(e)},[u,b,p.length]);const v=r.useMemo(()=>!!(!(p.length>0)&&u),[p.length,u]);return{processedParts:p,summaryTimings:y,shouldShowInitialLoading:v,shouldShowStreamDelayLoading:P}}exports.useStreamProcessor=M;
|
|
@@ -1,101 +1,104 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useState as
|
|
2
|
+
import { useState as h, useRef as y, useCallback as R, useEffect as w, useMemo as z } from "react";
|
|
3
3
|
import { DEFAULT_ERROR_MESSAGE as G } from "./use-inkeep-chat.js";
|
|
4
|
-
function
|
|
5
|
-
const [p,
|
|
6
|
-
f.current =
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
let
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
if (
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
...
|
|
16
|
-
isCompleted:
|
|
17
|
-
}) :
|
|
18
|
-
isCompleted:
|
|
19
|
-
}),
|
|
4
|
+
function K(c, a, C) {
|
|
5
|
+
const [p, v] = h([]), [d, D] = h(/* @__PURE__ */ new Map()), [T, M] = h(Date.now()), [P, g] = h(!1), A = y(0), S = y(0), b = y([]), f = y(d);
|
|
6
|
+
f.current = d;
|
|
7
|
+
const k = R(() => {
|
|
8
|
+
const t = [];
|
|
9
|
+
let e = "", r = [], i = !1;
|
|
10
|
+
c.length > A.current && (M(Date.now()), A.current = c.length), S.current = 0;
|
|
11
|
+
const u = /* @__PURE__ */ new Map(), o = (n = !1) => {
|
|
12
|
+
if (r.length > 0) {
|
|
13
|
+
const s = `group-${S.current++}`, m = f.current.get(s);
|
|
14
|
+
m ? u.set(s, {
|
|
15
|
+
...m,
|
|
16
|
+
isCompleted: n
|
|
17
|
+
}) : u.set(s, {
|
|
18
|
+
isCompleted: n
|
|
19
|
+
}), t.push({
|
|
20
20
|
type: "summary-group",
|
|
21
|
-
summaries: [...
|
|
22
|
-
groupKey:
|
|
23
|
-
}),
|
|
21
|
+
summaries: [...r],
|
|
22
|
+
groupKey: s
|
|
23
|
+
}), r = [];
|
|
24
24
|
}
|
|
25
25
|
};
|
|
26
|
-
for (const
|
|
27
|
-
if (
|
|
28
|
-
|
|
29
|
-
else if (
|
|
30
|
-
|
|
31
|
-
else if (
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
if (
|
|
35
|
-
|
|
26
|
+
for (const n of c)
|
|
27
|
+
if (n.type === "text")
|
|
28
|
+
o(!0), e += n.text || "";
|
|
29
|
+
else if (n.type === "data-summary")
|
|
30
|
+
e.trim() && (t.push({ type: "text", text: e }), e = ""), r.push(n.data);
|
|
31
|
+
else if (n.type === "data-operation") {
|
|
32
|
+
e.trim() && (t.push({ type: "text", text: e }), e = "");
|
|
33
|
+
const s = n.data;
|
|
34
|
+
if (s?.type) {
|
|
35
|
+
if ((s.type === "tool_call" || s.type === "tool_result") && s.details?.data?.inDelegatedAgent === !1)
|
|
36
|
+
continue;
|
|
37
|
+
switch (s.type) {
|
|
36
38
|
case "agent_initializing":
|
|
37
39
|
break;
|
|
38
40
|
case "completion": {
|
|
39
|
-
const { type:
|
|
40
|
-
|
|
41
|
+
const { type: l } = s;
|
|
42
|
+
r.push({ type: l, label: "Completed" }), o(!0);
|
|
41
43
|
break;
|
|
42
44
|
}
|
|
43
45
|
case "error": {
|
|
44
|
-
const
|
|
45
|
-
if (console.warn("Data operation error:",
|
|
46
|
-
|
|
46
|
+
const l = s.message || "Unknown error";
|
|
47
|
+
if (console.warn("Data operation error:", l), !t.length)
|
|
48
|
+
o(!0), C(new Error(l)), i || (t.push({ type: "text", text: G }), i = !0);
|
|
47
49
|
else {
|
|
48
|
-
const { type:
|
|
49
|
-
|
|
50
|
+
const { type: x, label: _ } = s;
|
|
51
|
+
r.push({ type: x, label: _ });
|
|
50
52
|
}
|
|
51
53
|
break;
|
|
52
54
|
}
|
|
53
55
|
default: {
|
|
54
|
-
const { type:
|
|
55
|
-
|
|
56
|
+
const { type: l, label: x } = s;
|
|
57
|
+
r.push({ type: l, label: x });
|
|
56
58
|
break;
|
|
57
59
|
}
|
|
58
60
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
}
|
|
62
|
+
} else if (n.type === "data-artifact")
|
|
63
|
+
if (n.data?.type?.toLowerCase() === "citation") {
|
|
64
|
+
const s = n.data, m = s.artifactSummary || {
|
|
65
|
+
title: s.name
|
|
63
66
|
};
|
|
64
|
-
|
|
67
|
+
e += ` ^${m?.title || s.name}^`;
|
|
65
68
|
} else
|
|
66
|
-
|
|
67
|
-
else
|
|
68
|
-
return
|
|
69
|
-
}, [
|
|
70
|
-
|
|
71
|
-
if (
|
|
69
|
+
e.trim() && (t.push({ type: "text", text: e }), e = ""), t.push(n);
|
|
70
|
+
else n.type === "data-component" ? (e.trim() && (t.push({ type: "text", text: e }), e = ""), o(!0), t.push(n)) : (e.trim() && (t.push({ type: "text", text: e }), e = ""), o(!0), t.push(n));
|
|
71
|
+
return o(!a), e.trim() && t.push({ type: "text", text: e }), { processed: t, newTimings: u };
|
|
72
|
+
}, [c, a, C]);
|
|
73
|
+
w(() => {
|
|
74
|
+
if (b.current === c)
|
|
72
75
|
return;
|
|
73
|
-
|
|
74
|
-
const { processed:
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
return !
|
|
78
|
-
})) &&
|
|
79
|
-
}, [
|
|
80
|
-
|
|
81
|
-
const
|
|
82
|
-
let
|
|
83
|
-
for (const [
|
|
84
|
-
|
|
85
|
-
...
|
|
76
|
+
b.current = c;
|
|
77
|
+
const { processed: t, newTimings: e } = k();
|
|
78
|
+
v(t), (e.size !== f.current.size || Array.from(e.entries()).some(([i, u]) => {
|
|
79
|
+
const o = f.current.get(i);
|
|
80
|
+
return !o || o.isCompleted !== u.isCompleted;
|
|
81
|
+
})) && D(e);
|
|
82
|
+
}, [k, c]), w(() => {
|
|
83
|
+
a || D((t) => {
|
|
84
|
+
const e = new Map(t);
|
|
85
|
+
let r = !1;
|
|
86
|
+
for (const [i, u] of e.entries())
|
|
87
|
+
u.isCompleted || (e.set(i, {
|
|
88
|
+
...u,
|
|
86
89
|
isCompleted: !0
|
|
87
|
-
}),
|
|
88
|
-
return
|
|
90
|
+
}), r = !0);
|
|
91
|
+
return r ? e : t;
|
|
89
92
|
});
|
|
90
|
-
}, [
|
|
91
|
-
const
|
|
92
|
-
const
|
|
93
|
-
(
|
|
94
|
-
),
|
|
95
|
-
g(
|
|
96
|
-
}, [
|
|
97
|
-
|
|
98
|
-
if (!
|
|
93
|
+
}, [a]);
|
|
94
|
+
const L = R(() => {
|
|
95
|
+
const t = Date.now() - T, e = Array.from(f.current.values()).some(
|
|
96
|
+
(i) => !i.isCompleted
|
|
97
|
+
), r = t > 1e3 && !e;
|
|
98
|
+
g(r);
|
|
99
|
+
}, [T]);
|
|
100
|
+
w(() => {
|
|
101
|
+
if (!a) {
|
|
99
102
|
g(!1);
|
|
100
103
|
return;
|
|
101
104
|
}
|
|
@@ -103,17 +106,17 @@ function F(l, i, w) {
|
|
|
103
106
|
g(!1);
|
|
104
107
|
return;
|
|
105
108
|
}
|
|
106
|
-
const
|
|
107
|
-
return () => clearInterval(
|
|
108
|
-
}, [
|
|
109
|
-
const
|
|
109
|
+
const e = setInterval(L, 200);
|
|
110
|
+
return () => clearInterval(e);
|
|
111
|
+
}, [a, L, p.length]);
|
|
112
|
+
const E = z(() => !!(!(p.length > 0) && a), [p.length, a]);
|
|
110
113
|
return {
|
|
111
114
|
processedParts: p,
|
|
112
|
-
summaryTimings:
|
|
113
|
-
shouldShowInitialLoading:
|
|
114
|
-
shouldShowStreamDelayLoading:
|
|
115
|
+
summaryTimings: d,
|
|
116
|
+
shouldShowInitialLoading: E,
|
|
117
|
+
shouldShowStreamDelayLoading: P
|
|
115
118
|
};
|
|
116
119
|
}
|
|
117
120
|
export {
|
|
118
|
-
|
|
121
|
+
K as useStreamProcessor
|
|
119
122
|
};
|