@copilotz/chat-adapter 0.9.45 → 0.9.47

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.js CHANGED
@@ -111,6 +111,9 @@ var getStreamEventPayload = (event) => {
111
111
  var getLlmAttemptId = (event) => {
112
112
  if (!isRecord(event)) return null;
113
113
  const metadata = isRecord(event.metadata) ? event.metadata : {};
114
+ if (typeof metadata.streamLlmAttemptId === "string" && metadata.streamLlmAttemptId.trim().length > 0) {
115
+ return metadata.streamLlmAttemptId.trim();
116
+ }
114
117
  if (typeof metadata.llmAttemptId === "string" && metadata.llmAttemptId.trim().length > 0) {
115
118
  return metadata.llmAttemptId.trim();
116
119
  }
@@ -119,6 +122,24 @@ var getLlmAttemptId = (event) => {
119
122
  }
120
123
  return null;
121
124
  };
125
+ var getRoutingMessageFromMetadata = (metadata) => {
126
+ if (!isRecord(metadata)) return null;
127
+ const routing = isRecord(metadata.routing) ? metadata.routing : null;
128
+ if (!routing) return null;
129
+ if (routing.source !== "model_control" || !["consult", "ask", "handoff"].includes(String(routing.action)) || typeof routing.targetId !== "string" || routing.targetId.trim().length === 0 || typeof routing.message !== "string" || routing.message.trim().length === 0) {
130
+ return null;
131
+ }
132
+ return routing.message.trim();
133
+ };
134
+ var getRoutingMessage = (event) => {
135
+ if (!isRecord(event)) return null;
136
+ return getRoutingMessageFromMetadata(event.metadata);
137
+ };
138
+ var getVisibleLlmResultAnswer = (event) => {
139
+ const payload = getStreamEventPayload(event);
140
+ const answer = isRecord(payload) && typeof payload.answer === "string" ? payload.answer : "";
141
+ return answer.trim().length > 0 ? answer : getRoutingMessage(event) ?? void 0;
142
+ };
122
143
  var isTerminalEmptyLlmResultEvent = (event) => {
123
144
  if (!isRecord(event) || event.type !== "LLM_RESULT") return false;
124
145
  const payload = getStreamEventPayload(event);
@@ -127,8 +148,7 @@ var isTerminalEmptyLlmResultEvent = (event) => {
127
148
  if (payload.finishReason === "tool_calls") return false;
128
149
  if (hasValues(payload.toolCalls)) return false;
129
150
  const metadata = isRecord(event.metadata) ? event.metadata : {};
130
- const routing = isRecord(metadata.routing) ? metadata.routing : {};
131
- if ((routing.action === "ask" || routing.action === "handoff") && typeof routing.targetId === "string" && routing.targetId.trim().length > 0) return false;
151
+ if (getRoutingMessageFromMetadata(metadata)) return false;
132
152
  if (hasValues(metadata.targetQueue)) return false;
133
153
  return true;
134
154
  };
@@ -1678,7 +1698,7 @@ var shouldRenderHydratedMessage = (msg) => {
1678
1698
  return false;
1679
1699
  }
1680
1700
  const text = expectStringValue(msg.content, "message.content").trim();
1681
- const hasText = text.length > 0;
1701
+ const hasText = text.length > 0 || getRoutingMessageFromMetadata(meta) !== null;
1682
1702
  const hasToolCalls = extractToolCallsFromServerMessage(msg).length > 0;
1683
1703
  const hasAttachments = extractAttachments(meta).length > 0;
1684
1704
  if (msg.senderType === "tool") {
@@ -1692,6 +1712,7 @@ var convertServerMessage = (msg, options = {}) => {
1692
1712
  const metadata = msg.metadata ?? void 0;
1693
1713
  const attachments = extractAttachments(metadata);
1694
1714
  const messageContent = expectStringValue(msg.content, "message.content");
1715
+ const routingMessage = getRoutingMessageFromMetadata(metadata);
1695
1716
  const role = roleBySender[msg.senderType];
1696
1717
  const parsedToolCalls = extractToolCallsFromServerMessage(msg);
1697
1718
  const shouldRenderToolCalls = msg.senderType !== "tool";
@@ -1705,7 +1726,7 @@ var convertServerMessage = (msg, options = {}) => {
1705
1726
  }));
1706
1727
  const hasToolCalls = shouldRenderToolCalls && mappedToolCalls.length > 0;
1707
1728
  const isToolSender = msg.senderType === "tool";
1708
- const content = isToolSender ? "" : messageContent;
1729
+ const content = isToolSender ? "" : messageContent.trim().length > 0 ? messageContent : routingMessage ?? "";
1709
1730
  const reasoning = typeof msg.reasoning === "string" && msg.reasoning.length > 0 ? msg.reasoning : void 0;
1710
1731
  const llmAttemptId = typeof metadata?.[LLM_ATTEMPT_ID_METADATA_KEY] === "string" ? metadata[LLM_ATTEMPT_ID_METADATA_KEY].trim() : "";
1711
1732
  const activityItems = [
@@ -1881,13 +1902,14 @@ var transitionLiveRun = (state, action, options) => {
1881
1902
  };
1882
1903
  }
1883
1904
  if (action.type === "attempt-result") {
1884
- const cursor = state.attemptsById.get(action.attemptId);
1905
+ const resolvedAttemptId = state.attemptsById.has(action.attemptId) ? action.attemptId : state.activeAttemptId ?? state.lastAttemptId;
1906
+ const cursor = resolvedAttemptId ? state.attemptsById.get(resolvedAttemptId) : void 0;
1885
1907
  if (!cursor) return { state, operations: [] };
1886
1908
  const next = copyState(state);
1887
- if (next.activeAttemptId === action.attemptId) {
1909
+ if (next.activeAttemptId === resolvedAttemptId) {
1888
1910
  next.activeAttemptId = null;
1889
1911
  }
1890
- next.lastAttemptId = action.attemptId;
1912
+ next.lastAttemptId = resolvedAttemptId;
1891
1913
  return {
1892
1914
  state: next,
1893
1915
  operations: [{
@@ -2804,7 +2826,7 @@ function useCopilotz({ userId, userName, userAvatar, assistantName, agentOptions
2804
2826
  return;
2805
2827
  }
2806
2828
  if (type === "LLM_RESULT") {
2807
- const finalAnswer = typeof payload?.answer === "string" ? payload.answer : void 0;
2829
+ const finalAnswer = getVisibleLlmResultAnswer(event);
2808
2830
  const attemptId = getLlmAttemptId(event) ?? liveRunState.activeAttemptId ?? liveRunState.lastAttemptId;
2809
2831
  if (attemptId) {
2810
2832
  dispatchLiveRunAction({