@athenaintel/react 0.10.24 → 0.10.26
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 +586 -436
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +37 -1
- package/dist/index.js +587 -437
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx, Fragment as Fragment$1, jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { bindExternalStoreMessage, getExternalStoreMessages, INTERNAL, useAssistantTransportRuntime, useAuiState, useAssistantRuntime, useThread, useThreadList, unstable_useRemoteThreadListRuntime, Tools, useAui, AssistantRuntimeProvider, useComposerRuntime, useScrollLock, ActionBarPrimitive, AuiIf, ActionBarMorePrimitive,
|
|
2
|
+
import { bindExternalStoreMessage, getExternalStoreMessages, INTERNAL, useAssistantTransportRuntime, useAuiState, useAssistantRuntime, useThread, useThreadList, unstable_useRemoteThreadListRuntime, Tools, useAui, AssistantRuntimeProvider, useComposerRuntime, MessagePartPrimitive, MessagePrimitive, useScrollLock, ActionBarPrimitive, AuiIf, ActionBarMorePrimitive, ThreadPrimitive, ComposerPrimitive, ErrorPrimitive, ThreadListPrimitive, ThreadListItemPrimitive } from "@assistant-ui/react";
|
|
3
3
|
import * as React from "react";
|
|
4
4
|
import React__default, { useMemo, useState, useRef, useEffect, useLayoutEffect, useContext, createContext, useCallback, useDebugValue, forwardRef, createRef, memo, createElement, version as version$1, useImperativeHandle } from "react";
|
|
5
5
|
import { A as AthenaAuthContext } from "./AthenaAuthContext-DQsdayH2.js";
|
|
@@ -3896,407 +3896,6 @@ function cn(...inputs) {
|
|
|
3896
3896
|
function isRecord(value) {
|
|
3897
3897
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3898
3898
|
}
|
|
3899
|
-
const { fromThreadMessageLike, getAutoStatus } = INTERNAL;
|
|
3900
|
-
const joinExternalMessages = (messages) => {
|
|
3901
|
-
var _a2;
|
|
3902
|
-
const assistantMessage = {
|
|
3903
|
-
role: "assistant",
|
|
3904
|
-
content: []
|
|
3905
|
-
};
|
|
3906
|
-
let firstAssistantId;
|
|
3907
|
-
let lastAssistantId;
|
|
3908
|
-
let assistantMessageCount = 0;
|
|
3909
|
-
for (const output of messages) {
|
|
3910
|
-
if (!output || !("role" in output)) {
|
|
3911
|
-
continue;
|
|
3912
|
-
}
|
|
3913
|
-
if (output.role === "tool") {
|
|
3914
|
-
const toolCallIdx = assistantMessage.content.findIndex(
|
|
3915
|
-
(c) => c.type === "tool-call" && c.toolCallId === output.toolCallId
|
|
3916
|
-
);
|
|
3917
|
-
if (toolCallIdx !== -1) {
|
|
3918
|
-
const toolCall = assistantMessage.content[toolCallIdx];
|
|
3919
|
-
if (output.toolName != null) {
|
|
3920
|
-
if (toolCall.toolName !== output.toolName)
|
|
3921
|
-
console.error(
|
|
3922
|
-
Error(
|
|
3923
|
-
`Tool call name ${output.toolCallId} ${output.toolName} does not match existing tool call ${toolCall.toolName}`
|
|
3924
|
-
)
|
|
3925
|
-
);
|
|
3926
|
-
}
|
|
3927
|
-
const { messages: existingMessages, ...toolCallRest } = toolCall;
|
|
3928
|
-
const updatedToolCall = {
|
|
3929
|
-
...toolCallRest,
|
|
3930
|
-
result: output.result,
|
|
3931
|
-
artifact: output.artifact,
|
|
3932
|
-
isError: output.isError,
|
|
3933
|
-
...(output.messages ?? existingMessages) !== void 0 && {
|
|
3934
|
-
messages: output.messages ?? existingMessages
|
|
3935
|
-
}
|
|
3936
|
-
};
|
|
3937
|
-
bindExternalStoreMessage(updatedToolCall, [
|
|
3938
|
-
...getExternalStoreMessages(toolCallRest),
|
|
3939
|
-
output
|
|
3940
|
-
]);
|
|
3941
|
-
assistantMessage.content[toolCallIdx] = updatedToolCall;
|
|
3942
|
-
} else {
|
|
3943
|
-
console.warn(
|
|
3944
|
-
`Tool call ${output.toolCallId} ${output.toolName} not found in assistant message`
|
|
3945
|
-
);
|
|
3946
|
-
}
|
|
3947
|
-
} else {
|
|
3948
|
-
const role = output.role;
|
|
3949
|
-
const rawContent = typeof output.content === "string" ? [{ type: "text", text: output.content }] : Array.isArray(output.content) ? output.content : [];
|
|
3950
|
-
const content = rawContent.flatMap((c) => {
|
|
3951
|
-
if (!c || typeof c !== "object") {
|
|
3952
|
-
return [];
|
|
3953
|
-
}
|
|
3954
|
-
const mapped = { ...c };
|
|
3955
|
-
bindExternalStoreMessage(mapped, output);
|
|
3956
|
-
return [mapped];
|
|
3957
|
-
});
|
|
3958
|
-
switch (role) {
|
|
3959
|
-
case "system":
|
|
3960
|
-
case "user":
|
|
3961
|
-
return {
|
|
3962
|
-
...output,
|
|
3963
|
-
content
|
|
3964
|
-
};
|
|
3965
|
-
case "assistant":
|
|
3966
|
-
if (output.id) {
|
|
3967
|
-
lastAssistantId = output.id;
|
|
3968
|
-
assistantMessageCount++;
|
|
3969
|
-
}
|
|
3970
|
-
if (assistantMessage.content.length === 0) {
|
|
3971
|
-
firstAssistantId = output.id;
|
|
3972
|
-
assistantMessage.createdAt ?? (assistantMessage.createdAt = output.createdAt);
|
|
3973
|
-
assistantMessage.status ?? (assistantMessage.status = output.status);
|
|
3974
|
-
if (output.attachments) {
|
|
3975
|
-
assistantMessage.attachments = [
|
|
3976
|
-
...assistantMessage.attachments ?? [],
|
|
3977
|
-
...output.attachments
|
|
3978
|
-
];
|
|
3979
|
-
}
|
|
3980
|
-
if (output.metadata) {
|
|
3981
|
-
assistantMessage.metadata ?? (assistantMessage.metadata = {});
|
|
3982
|
-
if (output.metadata.unstable_state) {
|
|
3983
|
-
assistantMessage.metadata.unstable_state = output.metadata.unstable_state;
|
|
3984
|
-
}
|
|
3985
|
-
if (output.metadata.unstable_annotations) {
|
|
3986
|
-
assistantMessage.metadata.unstable_annotations = [
|
|
3987
|
-
...assistantMessage.metadata.unstable_annotations ?? [],
|
|
3988
|
-
...output.metadata.unstable_annotations
|
|
3989
|
-
];
|
|
3990
|
-
}
|
|
3991
|
-
if (output.metadata.unstable_data) {
|
|
3992
|
-
assistantMessage.metadata.unstable_data = [
|
|
3993
|
-
...assistantMessage.metadata.unstable_data ?? [],
|
|
3994
|
-
...output.metadata.unstable_data
|
|
3995
|
-
];
|
|
3996
|
-
}
|
|
3997
|
-
if (output.metadata.steps) {
|
|
3998
|
-
assistantMessage.metadata.steps = [
|
|
3999
|
-
...assistantMessage.metadata.steps ?? [],
|
|
4000
|
-
...output.metadata.steps
|
|
4001
|
-
];
|
|
4002
|
-
}
|
|
4003
|
-
if (output.metadata.custom) {
|
|
4004
|
-
assistantMessage.metadata.custom = {
|
|
4005
|
-
...assistantMessage.metadata.custom ?? {},
|
|
4006
|
-
...output.metadata.custom
|
|
4007
|
-
};
|
|
4008
|
-
}
|
|
4009
|
-
if (output.metadata.submittedFeedback) {
|
|
4010
|
-
assistantMessage.metadata.submittedFeedback = output.metadata.submittedFeedback;
|
|
4011
|
-
}
|
|
4012
|
-
}
|
|
4013
|
-
} else {
|
|
4014
|
-
if ((_a2 = output.metadata) == null ? void 0 : _a2.custom) {
|
|
4015
|
-
assistantMessage.metadata ?? (assistantMessage.metadata = {});
|
|
4016
|
-
assistantMessage.metadata.custom = {
|
|
4017
|
-
...assistantMessage.metadata.custom ?? {},
|
|
4018
|
-
...output.metadata.custom
|
|
4019
|
-
};
|
|
4020
|
-
}
|
|
4021
|
-
}
|
|
4022
|
-
assistantMessage.content.push(...content);
|
|
4023
|
-
break;
|
|
4024
|
-
default: {
|
|
4025
|
-
const unsupportedRole = role;
|
|
4026
|
-
throw new Error(`Unknown message role: ${unsupportedRole}`);
|
|
4027
|
-
}
|
|
4028
|
-
}
|
|
4029
|
-
}
|
|
4030
|
-
}
|
|
4031
|
-
assistantMessage.id = lastAssistantId ?? firstAssistantId;
|
|
4032
|
-
if (assistantMessageCount > 1) {
|
|
4033
|
-
assistantMessage.metadata ?? (assistantMessage.metadata = {});
|
|
4034
|
-
assistantMessage.metadata.custom = {
|
|
4035
|
-
...assistantMessage.metadata.custom ?? {},
|
|
4036
|
-
_firstMessageId: firstAssistantId,
|
|
4037
|
-
_lastMessageId: lastAssistantId,
|
|
4038
|
-
_joinedMessageCount: assistantMessageCount
|
|
4039
|
-
};
|
|
4040
|
-
}
|
|
4041
|
-
return assistantMessage;
|
|
4042
|
-
};
|
|
4043
|
-
const chunkExternalMessages = (callbackResults, _joinStrategy) => {
|
|
4044
|
-
var _a2;
|
|
4045
|
-
const results = [];
|
|
4046
|
-
let isAssistant = false;
|
|
4047
|
-
let pendingNone = false;
|
|
4048
|
-
let inputs = [];
|
|
4049
|
-
let outputs = [];
|
|
4050
|
-
const flush2 = () => {
|
|
4051
|
-
if (outputs.length) {
|
|
4052
|
-
results.push({
|
|
4053
|
-
inputs,
|
|
4054
|
-
outputs
|
|
4055
|
-
});
|
|
4056
|
-
}
|
|
4057
|
-
inputs = [];
|
|
4058
|
-
outputs = [];
|
|
4059
|
-
isAssistant = false;
|
|
4060
|
-
pendingNone = false;
|
|
4061
|
-
};
|
|
4062
|
-
for (const callbackResult of callbackResults) {
|
|
4063
|
-
for (const output of callbackResult.outputs) {
|
|
4064
|
-
if (!output || !("role" in output)) {
|
|
4065
|
-
continue;
|
|
4066
|
-
}
|
|
4067
|
-
if (pendingNone && output.role !== "tool" || !isAssistant || output.role === "user" || output.role === "system") {
|
|
4068
|
-
flush2();
|
|
4069
|
-
}
|
|
4070
|
-
isAssistant = output.role === "assistant" || output.role === "tool";
|
|
4071
|
-
if (inputs.at(-1) !== callbackResult.input) {
|
|
4072
|
-
inputs.push(callbackResult.input);
|
|
4073
|
-
}
|
|
4074
|
-
outputs.push(output);
|
|
4075
|
-
if (output.role === "assistant" && (((_a2 = output.convertConfig) == null ? void 0 : _a2.joinStrategy) === "none" || _joinStrategy === "none")) {
|
|
4076
|
-
pendingNone = true;
|
|
4077
|
-
}
|
|
4078
|
-
}
|
|
4079
|
-
}
|
|
4080
|
-
flush2();
|
|
4081
|
-
return results;
|
|
4082
|
-
};
|
|
4083
|
-
const convertExternalMessages = (messages, callback, isRunning, metadata) => {
|
|
4084
|
-
const callbackResults = [];
|
|
4085
|
-
for (const message of messages) {
|
|
4086
|
-
const output = callback(message, metadata);
|
|
4087
|
-
const rawOutputs = Array.isArray(output) ? output : [output];
|
|
4088
|
-
const outputs = rawOutputs.filter(
|
|
4089
|
-
(candidate) => candidate != null
|
|
4090
|
-
);
|
|
4091
|
-
if (outputs.length === 0) {
|
|
4092
|
-
continue;
|
|
4093
|
-
}
|
|
4094
|
-
const result = { input: message, outputs };
|
|
4095
|
-
callbackResults.push(result);
|
|
4096
|
-
}
|
|
4097
|
-
const chunks = chunkExternalMessages(callbackResults);
|
|
4098
|
-
return chunks.map((message, idx) => {
|
|
4099
|
-
const isLast = idx === chunks.length - 1;
|
|
4100
|
-
const joined = joinExternalMessages(message.outputs);
|
|
4101
|
-
const hasPendingToolCalls = typeof joined.content === "object" && joined.content.some((c) => c.type === "tool-call" && c.result === void 0);
|
|
4102
|
-
const autoStatus = getAutoStatus(isLast, isRunning, hasPendingToolCalls, hasPendingToolCalls);
|
|
4103
|
-
const newMessage = fromThreadMessageLike(joined, idx.toString(), autoStatus);
|
|
4104
|
-
bindExternalStoreMessage(newMessage, message.inputs);
|
|
4105
|
-
return newMessage;
|
|
4106
|
-
});
|
|
4107
|
-
};
|
|
4108
|
-
const warnedMessagePartTypes = /* @__PURE__ */ new Set();
|
|
4109
|
-
const warnForUnknownMessagePartType = (type) => {
|
|
4110
|
-
if (warnedMessagePartTypes.has(type)) return;
|
|
4111
|
-
warnedMessagePartTypes.add(type);
|
|
4112
|
-
console.warn(`Unknown message part type: ${type}`);
|
|
4113
|
-
};
|
|
4114
|
-
const contentToParts = (content) => {
|
|
4115
|
-
if (typeof content === "string") return [{ type: "text", text: content }];
|
|
4116
|
-
return content.flatMap((part) => {
|
|
4117
|
-
if (!part || typeof part !== "object" || !("type" in part)) {
|
|
4118
|
-
return [];
|
|
4119
|
-
}
|
|
4120
|
-
const type = part.type;
|
|
4121
|
-
switch (type) {
|
|
4122
|
-
case "text":
|
|
4123
|
-
return [{ type: "text", text: part.text }];
|
|
4124
|
-
case "text_delta":
|
|
4125
|
-
return [{ type: "text", text: part.text }];
|
|
4126
|
-
case "image_url":
|
|
4127
|
-
if (typeof part.image_url === "string") {
|
|
4128
|
-
return [{ type: "image", image: part.image_url }];
|
|
4129
|
-
}
|
|
4130
|
-
return [{
|
|
4131
|
-
type: "image",
|
|
4132
|
-
image: part.image_url.url
|
|
4133
|
-
}];
|
|
4134
|
-
case "thinking":
|
|
4135
|
-
return [{ type: "reasoning", text: part.thinking }];
|
|
4136
|
-
case "reasoning":
|
|
4137
|
-
return [{
|
|
4138
|
-
type: "reasoning",
|
|
4139
|
-
text: part.summary.map((s) => s.text).join("\n\n\n")
|
|
4140
|
-
}];
|
|
4141
|
-
case "tool_use":
|
|
4142
|
-
return [];
|
|
4143
|
-
case "input_json_delta":
|
|
4144
|
-
return [];
|
|
4145
|
-
default:
|
|
4146
|
-
warnForUnknownMessagePartType(type);
|
|
4147
|
-
return [];
|
|
4148
|
-
}
|
|
4149
|
-
});
|
|
4150
|
-
};
|
|
4151
|
-
const getNumberAtPath = (value, path) => {
|
|
4152
|
-
let current = value;
|
|
4153
|
-
for (const segment of path) {
|
|
4154
|
-
if (!isRecord(current)) {
|
|
4155
|
-
return void 0;
|
|
4156
|
-
}
|
|
4157
|
-
current = current[segment];
|
|
4158
|
-
}
|
|
4159
|
-
return typeof current === "number" && Number.isFinite(current) ? current : void 0;
|
|
4160
|
-
};
|
|
4161
|
-
const extractReasoningTokens = ({
|
|
4162
|
-
usageMetadata,
|
|
4163
|
-
responseMetadata
|
|
4164
|
-
}) => {
|
|
4165
|
-
const rawReasoningTokens = getNumberAtPath(usageMetadata, ["output_token_details", "reasoning"]) ?? getNumberAtPath(usageMetadata, ["output_token_details", "reasoning_tokens"]) ?? getNumberAtPath(usageMetadata, ["reasoning_tokens"]) ?? getNumberAtPath(responseMetadata, ["output_token_details", "reasoning"]) ?? getNumberAtPath(responseMetadata, ["output_token_details", "reasoning_tokens"]) ?? getNumberAtPath(responseMetadata, ["token_usage", "output_token_details", "reasoning"]) ?? getNumberAtPath(responseMetadata, ["token_usage", "output_token_details", "reasoning_tokens"]);
|
|
4166
|
-
return rawReasoningTokens !== void 0 && rawReasoningTokens > 0 ? rawReasoningTokens : void 0;
|
|
4167
|
-
};
|
|
4168
|
-
const buildCustomMetadata = ({
|
|
4169
|
-
additionalKwargs,
|
|
4170
|
-
usageMetadata,
|
|
4171
|
-
responseMetadata
|
|
4172
|
-
}) => {
|
|
4173
|
-
const customMetadata = additionalKwargs ? { ...additionalKwargs } : {};
|
|
4174
|
-
const reasoningTokens = extractReasoningTokens({ usageMetadata, responseMetadata });
|
|
4175
|
-
const existingAthenaMetadata = isRecord(customMetadata._athena) ? customMetadata._athena : void 0;
|
|
4176
|
-
const athenaMetadata = {
|
|
4177
|
-
...existingAthenaMetadata ?? {}
|
|
4178
|
-
};
|
|
4179
|
-
if (usageMetadata) {
|
|
4180
|
-
athenaMetadata.rawUsageMetadata = usageMetadata;
|
|
4181
|
-
}
|
|
4182
|
-
if (responseMetadata) {
|
|
4183
|
-
athenaMetadata.rawResponseMetadata = responseMetadata;
|
|
4184
|
-
}
|
|
4185
|
-
if (reasoningTokens !== void 0) {
|
|
4186
|
-
athenaMetadata.reasoningTokens = reasoningTokens;
|
|
4187
|
-
}
|
|
4188
|
-
if (Object.keys(athenaMetadata).length > 0) {
|
|
4189
|
-
customMetadata._athena = athenaMetadata;
|
|
4190
|
-
}
|
|
4191
|
-
return Object.keys(customMetadata).length > 0 ? customMetadata : void 0;
|
|
4192
|
-
};
|
|
4193
|
-
const convertLangChainMessage = (message) => {
|
|
4194
|
-
var _a2, _b, _c;
|
|
4195
|
-
switch (message.type) {
|
|
4196
|
-
case "system": {
|
|
4197
|
-
const customMetadata = buildCustomMetadata({
|
|
4198
|
-
additionalKwargs: message.additional_kwargs
|
|
4199
|
-
});
|
|
4200
|
-
return {
|
|
4201
|
-
role: "system",
|
|
4202
|
-
id: message.id,
|
|
4203
|
-
content: [{ type: "text", text: message.content }],
|
|
4204
|
-
...customMetadata && {
|
|
4205
|
-
metadata: {
|
|
4206
|
-
custom: customMetadata
|
|
4207
|
-
}
|
|
4208
|
-
}
|
|
4209
|
-
};
|
|
4210
|
-
}
|
|
4211
|
-
case "human": {
|
|
4212
|
-
const customMetadata = buildCustomMetadata({
|
|
4213
|
-
additionalKwargs: message.additional_kwargs
|
|
4214
|
-
});
|
|
4215
|
-
return {
|
|
4216
|
-
role: "user",
|
|
4217
|
-
id: message.id,
|
|
4218
|
-
content: contentToParts(message.content),
|
|
4219
|
-
...customMetadata && {
|
|
4220
|
-
metadata: {
|
|
4221
|
-
custom: customMetadata
|
|
4222
|
-
}
|
|
4223
|
-
}
|
|
4224
|
-
};
|
|
4225
|
-
}
|
|
4226
|
-
case "ai": {
|
|
4227
|
-
const customMetadata = buildCustomMetadata({
|
|
4228
|
-
additionalKwargs: message.additional_kwargs,
|
|
4229
|
-
usageMetadata: message.usage_metadata,
|
|
4230
|
-
responseMetadata: message.response_metadata
|
|
4231
|
-
});
|
|
4232
|
-
const toolCallParts = ((_a2 = message.tool_calls) == null ? void 0 : _a2.map((chunk) => {
|
|
4233
|
-
var _a3, _b2;
|
|
4234
|
-
let argsText;
|
|
4235
|
-
if (typeof chunk.args === "string") {
|
|
4236
|
-
argsText = chunk.args;
|
|
4237
|
-
} else {
|
|
4238
|
-
const rawString = chunk.partial_json ?? ((_b2 = (_a3 = message.tool_call_chunks) == null ? void 0 : _a3.find((c) => c.id === chunk.id)) == null ? void 0 : _b2.args) ?? JSON.stringify(chunk.args);
|
|
4239
|
-
argsText = rawString;
|
|
4240
|
-
}
|
|
4241
|
-
return {
|
|
4242
|
-
type: "tool-call",
|
|
4243
|
-
toolCallId: chunk.id,
|
|
4244
|
-
toolName: chunk.name,
|
|
4245
|
-
args: typeof chunk.args === "string" ? parsePartialJsonObject(argsText) ?? {} : chunk.args,
|
|
4246
|
-
argsText
|
|
4247
|
-
};
|
|
4248
|
-
})) ?? [];
|
|
4249
|
-
const normalizedContent = typeof message.content === "string" ? [{ type: "text", text: message.content }] : message.content;
|
|
4250
|
-
const allContent = [
|
|
4251
|
-
(_b = message.additional_kwargs) == null ? void 0 : _b.reasoning,
|
|
4252
|
-
...normalizedContent,
|
|
4253
|
-
...((_c = message.additional_kwargs) == null ? void 0 : _c.tool_outputs) ?? []
|
|
4254
|
-
].filter((c) => c != null);
|
|
4255
|
-
return {
|
|
4256
|
-
role: "assistant",
|
|
4257
|
-
id: message.id,
|
|
4258
|
-
content: [...contentToParts(allContent), ...toolCallParts],
|
|
4259
|
-
...message.status && { status: message.status },
|
|
4260
|
-
...customMetadata && {
|
|
4261
|
-
metadata: {
|
|
4262
|
-
custom: customMetadata
|
|
4263
|
-
}
|
|
4264
|
-
}
|
|
4265
|
-
};
|
|
4266
|
-
}
|
|
4267
|
-
case "tool": {
|
|
4268
|
-
return {
|
|
4269
|
-
role: "tool",
|
|
4270
|
-
toolName: message.name,
|
|
4271
|
-
toolCallId: message.tool_call_id,
|
|
4272
|
-
result: message.content,
|
|
4273
|
-
artifact: message.artifact,
|
|
4274
|
-
isError: message.status === "error"
|
|
4275
|
-
};
|
|
4276
|
-
}
|
|
4277
|
-
default:
|
|
4278
|
-
return null;
|
|
4279
|
-
}
|
|
4280
|
-
};
|
|
4281
|
-
const convertLangChainToThreadMessages = (messages, isRunning = false, metadata = {}) => {
|
|
4282
|
-
const validToolCallIds = /* @__PURE__ */ new Set();
|
|
4283
|
-
for (const message of messages) {
|
|
4284
|
-
if (message.type === "ai" && message.tool_calls) {
|
|
4285
|
-
for (const tc of message.tool_calls) {
|
|
4286
|
-
if (tc.id) {
|
|
4287
|
-
validToolCallIds.add(tc.id);
|
|
4288
|
-
}
|
|
4289
|
-
}
|
|
4290
|
-
}
|
|
4291
|
-
}
|
|
4292
|
-
const filteredMessages = messages.filter((message) => {
|
|
4293
|
-
if (message.type === "tool" && message.tool_call_id && !validToolCallIds.has(message.tool_call_id)) {
|
|
4294
|
-
return false;
|
|
4295
|
-
}
|
|
4296
|
-
return true;
|
|
4297
|
-
});
|
|
4298
|
-
return convertExternalMessages(filteredMessages, convertLangChainMessage, isRunning, metadata);
|
|
4299
|
-
};
|
|
4300
3899
|
function $constructor(name, initializer2, params) {
|
|
4301
3900
|
function init2(inst, def) {
|
|
4302
3901
|
if (!inst._zod) {
|
|
@@ -8464,6 +8063,451 @@ const parseLangGraphState = (state) => {
|
|
|
8464
8063
|
}
|
|
8465
8064
|
return result;
|
|
8466
8065
|
};
|
|
8066
|
+
const { fromThreadMessageLike, getAutoStatus } = INTERNAL;
|
|
8067
|
+
const joinExternalMessages = (messages) => {
|
|
8068
|
+
var _a2;
|
|
8069
|
+
const assistantMessage = {
|
|
8070
|
+
role: "assistant",
|
|
8071
|
+
content: []
|
|
8072
|
+
};
|
|
8073
|
+
let firstAssistantId;
|
|
8074
|
+
let lastAssistantId;
|
|
8075
|
+
let assistantMessageCount = 0;
|
|
8076
|
+
for (const output of messages) {
|
|
8077
|
+
if (!output || !("role" in output)) {
|
|
8078
|
+
continue;
|
|
8079
|
+
}
|
|
8080
|
+
if (output.role === "tool") {
|
|
8081
|
+
const toolCallIdx = assistantMessage.content.findIndex(
|
|
8082
|
+
(c) => c.type === "tool-call" && c.toolCallId === output.toolCallId
|
|
8083
|
+
);
|
|
8084
|
+
if (toolCallIdx !== -1) {
|
|
8085
|
+
const toolCall = assistantMessage.content[toolCallIdx];
|
|
8086
|
+
if (output.toolName != null) {
|
|
8087
|
+
if (toolCall.toolName !== output.toolName)
|
|
8088
|
+
console.error(
|
|
8089
|
+
Error(
|
|
8090
|
+
`Tool call name ${output.toolCallId} ${output.toolName} does not match existing tool call ${toolCall.toolName}`
|
|
8091
|
+
)
|
|
8092
|
+
);
|
|
8093
|
+
}
|
|
8094
|
+
const { messages: existingMessages, ...toolCallRest } = toolCall;
|
|
8095
|
+
const updatedToolCall = {
|
|
8096
|
+
...toolCallRest,
|
|
8097
|
+
result: output.result,
|
|
8098
|
+
artifact: output.artifact,
|
|
8099
|
+
isError: output.isError,
|
|
8100
|
+
...(output.messages ?? existingMessages) !== void 0 && {
|
|
8101
|
+
messages: output.messages ?? existingMessages
|
|
8102
|
+
}
|
|
8103
|
+
};
|
|
8104
|
+
bindExternalStoreMessage(updatedToolCall, [
|
|
8105
|
+
...getExternalStoreMessages(toolCallRest),
|
|
8106
|
+
output
|
|
8107
|
+
]);
|
|
8108
|
+
assistantMessage.content[toolCallIdx] = updatedToolCall;
|
|
8109
|
+
} else {
|
|
8110
|
+
console.warn(
|
|
8111
|
+
`Tool call ${output.toolCallId} ${output.toolName} not found in assistant message`
|
|
8112
|
+
);
|
|
8113
|
+
}
|
|
8114
|
+
} else {
|
|
8115
|
+
const role = output.role;
|
|
8116
|
+
const rawContent = typeof output.content === "string" ? [{ type: "text", text: output.content }] : Array.isArray(output.content) ? output.content : [];
|
|
8117
|
+
const content = rawContent.flatMap((c) => {
|
|
8118
|
+
if (!c || typeof c !== "object") {
|
|
8119
|
+
return [];
|
|
8120
|
+
}
|
|
8121
|
+
const mapped = { ...c };
|
|
8122
|
+
bindExternalStoreMessage(mapped, output);
|
|
8123
|
+
return [mapped];
|
|
8124
|
+
});
|
|
8125
|
+
switch (role) {
|
|
8126
|
+
case "system":
|
|
8127
|
+
case "user":
|
|
8128
|
+
return {
|
|
8129
|
+
...output,
|
|
8130
|
+
content
|
|
8131
|
+
};
|
|
8132
|
+
case "assistant":
|
|
8133
|
+
if (output.id) {
|
|
8134
|
+
lastAssistantId = output.id;
|
|
8135
|
+
assistantMessageCount++;
|
|
8136
|
+
}
|
|
8137
|
+
if (assistantMessage.content.length === 0) {
|
|
8138
|
+
firstAssistantId = output.id;
|
|
8139
|
+
assistantMessage.createdAt ?? (assistantMessage.createdAt = output.createdAt);
|
|
8140
|
+
assistantMessage.status ?? (assistantMessage.status = output.status);
|
|
8141
|
+
if (output.attachments) {
|
|
8142
|
+
assistantMessage.attachments = [
|
|
8143
|
+
...assistantMessage.attachments ?? [],
|
|
8144
|
+
...output.attachments
|
|
8145
|
+
];
|
|
8146
|
+
}
|
|
8147
|
+
if (output.metadata) {
|
|
8148
|
+
assistantMessage.metadata ?? (assistantMessage.metadata = {});
|
|
8149
|
+
if (output.metadata.unstable_state) {
|
|
8150
|
+
assistantMessage.metadata.unstable_state = output.metadata.unstable_state;
|
|
8151
|
+
}
|
|
8152
|
+
if (output.metadata.unstable_annotations) {
|
|
8153
|
+
assistantMessage.metadata.unstable_annotations = [
|
|
8154
|
+
...assistantMessage.metadata.unstable_annotations ?? [],
|
|
8155
|
+
...output.metadata.unstable_annotations
|
|
8156
|
+
];
|
|
8157
|
+
}
|
|
8158
|
+
if (output.metadata.unstable_data) {
|
|
8159
|
+
assistantMessage.metadata.unstable_data = [
|
|
8160
|
+
...assistantMessage.metadata.unstable_data ?? [],
|
|
8161
|
+
...output.metadata.unstable_data
|
|
8162
|
+
];
|
|
8163
|
+
}
|
|
8164
|
+
if (output.metadata.steps) {
|
|
8165
|
+
assistantMessage.metadata.steps = [
|
|
8166
|
+
...assistantMessage.metadata.steps ?? [],
|
|
8167
|
+
...output.metadata.steps
|
|
8168
|
+
];
|
|
8169
|
+
}
|
|
8170
|
+
if (output.metadata.custom) {
|
|
8171
|
+
assistantMessage.metadata.custom = {
|
|
8172
|
+
...assistantMessage.metadata.custom ?? {},
|
|
8173
|
+
...output.metadata.custom
|
|
8174
|
+
};
|
|
8175
|
+
}
|
|
8176
|
+
if (output.metadata.submittedFeedback) {
|
|
8177
|
+
assistantMessage.metadata.submittedFeedback = output.metadata.submittedFeedback;
|
|
8178
|
+
}
|
|
8179
|
+
}
|
|
8180
|
+
} else {
|
|
8181
|
+
if ((_a2 = output.metadata) == null ? void 0 : _a2.custom) {
|
|
8182
|
+
assistantMessage.metadata ?? (assistantMessage.metadata = {});
|
|
8183
|
+
assistantMessage.metadata.custom = {
|
|
8184
|
+
...assistantMessage.metadata.custom ?? {},
|
|
8185
|
+
...output.metadata.custom
|
|
8186
|
+
};
|
|
8187
|
+
}
|
|
8188
|
+
}
|
|
8189
|
+
assistantMessage.content.push(...content);
|
|
8190
|
+
break;
|
|
8191
|
+
default: {
|
|
8192
|
+
const unsupportedRole = role;
|
|
8193
|
+
throw new Error(`Unknown message role: ${unsupportedRole}`);
|
|
8194
|
+
}
|
|
8195
|
+
}
|
|
8196
|
+
}
|
|
8197
|
+
}
|
|
8198
|
+
assistantMessage.id = lastAssistantId ?? firstAssistantId;
|
|
8199
|
+
if (assistantMessageCount > 1) {
|
|
8200
|
+
assistantMessage.metadata ?? (assistantMessage.metadata = {});
|
|
8201
|
+
assistantMessage.metadata.custom = {
|
|
8202
|
+
...assistantMessage.metadata.custom ?? {},
|
|
8203
|
+
_firstMessageId: firstAssistantId,
|
|
8204
|
+
_lastMessageId: lastAssistantId,
|
|
8205
|
+
_joinedMessageCount: assistantMessageCount
|
|
8206
|
+
};
|
|
8207
|
+
}
|
|
8208
|
+
return assistantMessage;
|
|
8209
|
+
};
|
|
8210
|
+
const chunkExternalMessages = (callbackResults, _joinStrategy) => {
|
|
8211
|
+
var _a2;
|
|
8212
|
+
const results = [];
|
|
8213
|
+
let isAssistant = false;
|
|
8214
|
+
let pendingNone = false;
|
|
8215
|
+
let inputs = [];
|
|
8216
|
+
let outputs = [];
|
|
8217
|
+
const flush2 = () => {
|
|
8218
|
+
if (outputs.length) {
|
|
8219
|
+
results.push({
|
|
8220
|
+
inputs,
|
|
8221
|
+
outputs
|
|
8222
|
+
});
|
|
8223
|
+
}
|
|
8224
|
+
inputs = [];
|
|
8225
|
+
outputs = [];
|
|
8226
|
+
isAssistant = false;
|
|
8227
|
+
pendingNone = false;
|
|
8228
|
+
};
|
|
8229
|
+
for (const callbackResult of callbackResults) {
|
|
8230
|
+
for (const output of callbackResult.outputs) {
|
|
8231
|
+
if (!output || !("role" in output)) {
|
|
8232
|
+
continue;
|
|
8233
|
+
}
|
|
8234
|
+
if (pendingNone && output.role !== "tool" || !isAssistant || output.role === "user" || output.role === "system") {
|
|
8235
|
+
flush2();
|
|
8236
|
+
}
|
|
8237
|
+
isAssistant = output.role === "assistant" || output.role === "tool";
|
|
8238
|
+
if (inputs.at(-1) !== callbackResult.input) {
|
|
8239
|
+
inputs.push(callbackResult.input);
|
|
8240
|
+
}
|
|
8241
|
+
outputs.push(output);
|
|
8242
|
+
if (output.role === "assistant" && (((_a2 = output.convertConfig) == null ? void 0 : _a2.joinStrategy) === "none" || _joinStrategy === "none")) {
|
|
8243
|
+
pendingNone = true;
|
|
8244
|
+
}
|
|
8245
|
+
}
|
|
8246
|
+
}
|
|
8247
|
+
flush2();
|
|
8248
|
+
return results;
|
|
8249
|
+
};
|
|
8250
|
+
const convertExternalMessages = (messages, callback, isRunning, metadata) => {
|
|
8251
|
+
const callbackResults = [];
|
|
8252
|
+
for (const message of messages) {
|
|
8253
|
+
const output = callback(message, metadata);
|
|
8254
|
+
const rawOutputs = Array.isArray(output) ? output : [output];
|
|
8255
|
+
const outputs = rawOutputs.filter(
|
|
8256
|
+
(candidate) => candidate != null
|
|
8257
|
+
);
|
|
8258
|
+
if (outputs.length === 0) {
|
|
8259
|
+
continue;
|
|
8260
|
+
}
|
|
8261
|
+
const result = { input: message, outputs };
|
|
8262
|
+
callbackResults.push(result);
|
|
8263
|
+
}
|
|
8264
|
+
const chunks = chunkExternalMessages(callbackResults);
|
|
8265
|
+
return chunks.map((message, idx) => {
|
|
8266
|
+
const isLast = idx === chunks.length - 1;
|
|
8267
|
+
const joined = joinExternalMessages(message.outputs);
|
|
8268
|
+
const hasPendingToolCalls = typeof joined.content === "object" && joined.content.some((c) => c.type === "tool-call" && c.result === void 0);
|
|
8269
|
+
const autoStatus = getAutoStatus(isLast, isRunning, hasPendingToolCalls, hasPendingToolCalls);
|
|
8270
|
+
const newMessage = fromThreadMessageLike(joined, idx.toString(), autoStatus);
|
|
8271
|
+
bindExternalStoreMessage(newMessage, message.inputs);
|
|
8272
|
+
return newMessage;
|
|
8273
|
+
});
|
|
8274
|
+
};
|
|
8275
|
+
const PTC_SUBCALL_DID_NOT_COMPLETE_MESSAGE = "Sub-call did not complete before the parent tool returned.";
|
|
8276
|
+
const autoCloseInFlightSubgraphMessages = (msgs) => {
|
|
8277
|
+
const beginIds = /* @__PURE__ */ new Set();
|
|
8278
|
+
const endIds = /* @__PURE__ */ new Set();
|
|
8279
|
+
for (const message of msgs) {
|
|
8280
|
+
if (!isRecord(message)) continue;
|
|
8281
|
+
if (message.type === "ai" && Array.isArray(message.tool_calls)) {
|
|
8282
|
+
for (const toolCall of message.tool_calls) {
|
|
8283
|
+
if (!isRecord(toolCall)) continue;
|
|
8284
|
+
const id = toolCall.id;
|
|
8285
|
+
if (typeof id === "string") beginIds.add(id);
|
|
8286
|
+
}
|
|
8287
|
+
} else if (message.type === "tool" && typeof message.tool_call_id === "string") {
|
|
8288
|
+
endIds.add(message.tool_call_id);
|
|
8289
|
+
}
|
|
8290
|
+
}
|
|
8291
|
+
const unmatchedIds = [...beginIds].filter((id) => !endIds.has(id));
|
|
8292
|
+
if (unmatchedIds.length === 0) return [...msgs];
|
|
8293
|
+
return [
|
|
8294
|
+
...msgs,
|
|
8295
|
+
...unmatchedIds.map((toolCallId) => ({
|
|
8296
|
+
type: "tool",
|
|
8297
|
+
tool_call_id: toolCallId,
|
|
8298
|
+
content: PTC_SUBCALL_DID_NOT_COMPLETE_MESSAGE,
|
|
8299
|
+
status: "error"
|
|
8300
|
+
}))
|
|
8301
|
+
];
|
|
8302
|
+
};
|
|
8303
|
+
const warnedMessagePartTypes = /* @__PURE__ */ new Set();
|
|
8304
|
+
const warnForUnknownMessagePartType = (type) => {
|
|
8305
|
+
if (warnedMessagePartTypes.has(type)) return;
|
|
8306
|
+
warnedMessagePartTypes.add(type);
|
|
8307
|
+
console.warn(`Unknown message part type: ${type}`);
|
|
8308
|
+
};
|
|
8309
|
+
const contentToParts = (content) => {
|
|
8310
|
+
if (typeof content === "string") return [{ type: "text", text: content }];
|
|
8311
|
+
return content.flatMap((part) => {
|
|
8312
|
+
if (!part || typeof part !== "object" || !("type" in part)) {
|
|
8313
|
+
return [];
|
|
8314
|
+
}
|
|
8315
|
+
const type = part.type;
|
|
8316
|
+
switch (type) {
|
|
8317
|
+
case "text":
|
|
8318
|
+
return [{ type: "text", text: part.text }];
|
|
8319
|
+
case "text_delta":
|
|
8320
|
+
return [{ type: "text", text: part.text }];
|
|
8321
|
+
case "image_url":
|
|
8322
|
+
if (typeof part.image_url === "string") {
|
|
8323
|
+
return [{ type: "image", image: part.image_url }];
|
|
8324
|
+
}
|
|
8325
|
+
return [{
|
|
8326
|
+
type: "image",
|
|
8327
|
+
image: part.image_url.url
|
|
8328
|
+
}];
|
|
8329
|
+
case "thinking":
|
|
8330
|
+
return [{ type: "reasoning", text: part.thinking }];
|
|
8331
|
+
case "reasoning":
|
|
8332
|
+
return [{
|
|
8333
|
+
type: "reasoning",
|
|
8334
|
+
text: part.summary.map((s) => s.text).join("\n\n\n")
|
|
8335
|
+
}];
|
|
8336
|
+
case "tool_use":
|
|
8337
|
+
return [];
|
|
8338
|
+
case "input_json_delta":
|
|
8339
|
+
return [];
|
|
8340
|
+
default:
|
|
8341
|
+
warnForUnknownMessagePartType(type);
|
|
8342
|
+
return [];
|
|
8343
|
+
}
|
|
8344
|
+
});
|
|
8345
|
+
};
|
|
8346
|
+
const getNumberAtPath = (value, path) => {
|
|
8347
|
+
let current = value;
|
|
8348
|
+
for (const segment of path) {
|
|
8349
|
+
if (!isRecord(current)) {
|
|
8350
|
+
return void 0;
|
|
8351
|
+
}
|
|
8352
|
+
current = current[segment];
|
|
8353
|
+
}
|
|
8354
|
+
return typeof current === "number" && Number.isFinite(current) ? current : void 0;
|
|
8355
|
+
};
|
|
8356
|
+
const extractReasoningTokens = ({
|
|
8357
|
+
usageMetadata,
|
|
8358
|
+
responseMetadata
|
|
8359
|
+
}) => {
|
|
8360
|
+
const rawReasoningTokens = getNumberAtPath(usageMetadata, ["output_token_details", "reasoning"]) ?? getNumberAtPath(usageMetadata, ["output_token_details", "reasoning_tokens"]) ?? getNumberAtPath(usageMetadata, ["reasoning_tokens"]) ?? getNumberAtPath(responseMetadata, ["output_token_details", "reasoning"]) ?? getNumberAtPath(responseMetadata, ["output_token_details", "reasoning_tokens"]) ?? getNumberAtPath(responseMetadata, ["token_usage", "output_token_details", "reasoning"]) ?? getNumberAtPath(responseMetadata, ["token_usage", "output_token_details", "reasoning_tokens"]);
|
|
8361
|
+
return rawReasoningTokens !== void 0 && rawReasoningTokens > 0 ? rawReasoningTokens : void 0;
|
|
8362
|
+
};
|
|
8363
|
+
const buildCustomMetadata = ({
|
|
8364
|
+
additionalKwargs,
|
|
8365
|
+
usageMetadata,
|
|
8366
|
+
responseMetadata
|
|
8367
|
+
}) => {
|
|
8368
|
+
const customMetadata = additionalKwargs ? { ...additionalKwargs } : {};
|
|
8369
|
+
const reasoningTokens = extractReasoningTokens({ usageMetadata, responseMetadata });
|
|
8370
|
+
const existingAthenaMetadata = isRecord(customMetadata._athena) ? customMetadata._athena : void 0;
|
|
8371
|
+
const athenaMetadata = {
|
|
8372
|
+
...existingAthenaMetadata ?? {}
|
|
8373
|
+
};
|
|
8374
|
+
if (usageMetadata) {
|
|
8375
|
+
athenaMetadata.rawUsageMetadata = usageMetadata;
|
|
8376
|
+
}
|
|
8377
|
+
if (responseMetadata) {
|
|
8378
|
+
athenaMetadata.rawResponseMetadata = responseMetadata;
|
|
8379
|
+
}
|
|
8380
|
+
if (reasoningTokens !== void 0) {
|
|
8381
|
+
athenaMetadata.reasoningTokens = reasoningTokens;
|
|
8382
|
+
}
|
|
8383
|
+
if (Object.keys(athenaMetadata).length > 0) {
|
|
8384
|
+
customMetadata._athena = athenaMetadata;
|
|
8385
|
+
}
|
|
8386
|
+
return Object.keys(customMetadata).length > 0 ? customMetadata : void 0;
|
|
8387
|
+
};
|
|
8388
|
+
const getSubgraphMessages = (artifact) => {
|
|
8389
|
+
if (!isRecord(artifact)) return void 0;
|
|
8390
|
+
const subgraphState = artifact.subgraph_state;
|
|
8391
|
+
if (!isRecord(subgraphState)) return void 0;
|
|
8392
|
+
const messages = subgraphState.messages;
|
|
8393
|
+
return Array.isArray(messages) && messages.length > 0 ? messages : void 0;
|
|
8394
|
+
};
|
|
8395
|
+
const convertLangChainMessage = (message) => {
|
|
8396
|
+
var _a2, _b, _c;
|
|
8397
|
+
switch (message.type) {
|
|
8398
|
+
case "system": {
|
|
8399
|
+
const customMetadata = buildCustomMetadata({
|
|
8400
|
+
additionalKwargs: message.additional_kwargs
|
|
8401
|
+
});
|
|
8402
|
+
return {
|
|
8403
|
+
role: "system",
|
|
8404
|
+
id: message.id,
|
|
8405
|
+
content: [{ type: "text", text: message.content }],
|
|
8406
|
+
...customMetadata && {
|
|
8407
|
+
metadata: {
|
|
8408
|
+
custom: customMetadata
|
|
8409
|
+
}
|
|
8410
|
+
}
|
|
8411
|
+
};
|
|
8412
|
+
}
|
|
8413
|
+
case "human": {
|
|
8414
|
+
const customMetadata = buildCustomMetadata({
|
|
8415
|
+
additionalKwargs: message.additional_kwargs
|
|
8416
|
+
});
|
|
8417
|
+
return {
|
|
8418
|
+
role: "user",
|
|
8419
|
+
id: message.id,
|
|
8420
|
+
content: contentToParts(message.content),
|
|
8421
|
+
...customMetadata && {
|
|
8422
|
+
metadata: {
|
|
8423
|
+
custom: customMetadata
|
|
8424
|
+
}
|
|
8425
|
+
}
|
|
8426
|
+
};
|
|
8427
|
+
}
|
|
8428
|
+
case "ai": {
|
|
8429
|
+
const customMetadata = buildCustomMetadata({
|
|
8430
|
+
additionalKwargs: message.additional_kwargs,
|
|
8431
|
+
usageMetadata: message.usage_metadata,
|
|
8432
|
+
responseMetadata: message.response_metadata
|
|
8433
|
+
});
|
|
8434
|
+
const toolCallParts = ((_a2 = message.tool_calls) == null ? void 0 : _a2.map((chunk) => {
|
|
8435
|
+
var _a3, _b2;
|
|
8436
|
+
let argsText;
|
|
8437
|
+
if (typeof chunk.args === "string") {
|
|
8438
|
+
argsText = chunk.args;
|
|
8439
|
+
} else {
|
|
8440
|
+
const rawString = chunk.partial_json ?? ((_b2 = (_a3 = message.tool_call_chunks) == null ? void 0 : _a3.find((c) => c.id === chunk.id)) == null ? void 0 : _b2.args) ?? JSON.stringify(chunk.args);
|
|
8441
|
+
argsText = rawString;
|
|
8442
|
+
}
|
|
8443
|
+
return {
|
|
8444
|
+
type: "tool-call",
|
|
8445
|
+
toolCallId: chunk.id,
|
|
8446
|
+
toolName: chunk.name,
|
|
8447
|
+
args: typeof chunk.args === "string" ? parsePartialJsonObject(argsText) ?? {} : chunk.args,
|
|
8448
|
+
argsText
|
|
8449
|
+
};
|
|
8450
|
+
})) ?? [];
|
|
8451
|
+
const normalizedContent = typeof message.content === "string" ? [{ type: "text", text: message.content }] : message.content;
|
|
8452
|
+
const allContent = [
|
|
8453
|
+
(_b = message.additional_kwargs) == null ? void 0 : _b.reasoning,
|
|
8454
|
+
...normalizedContent,
|
|
8455
|
+
...((_c = message.additional_kwargs) == null ? void 0 : _c.tool_outputs) ?? []
|
|
8456
|
+
].filter((c) => c != null);
|
|
8457
|
+
return {
|
|
8458
|
+
role: "assistant",
|
|
8459
|
+
id: message.id,
|
|
8460
|
+
content: [...contentToParts(allContent), ...toolCallParts],
|
|
8461
|
+
...message.status && { status: message.status },
|
|
8462
|
+
...customMetadata && {
|
|
8463
|
+
metadata: {
|
|
8464
|
+
custom: customMetadata
|
|
8465
|
+
}
|
|
8466
|
+
}
|
|
8467
|
+
};
|
|
8468
|
+
}
|
|
8469
|
+
case "tool": {
|
|
8470
|
+
const subgraphMessages = getSubgraphMessages(message.artifact);
|
|
8471
|
+
const parentEnded = message.content !== void 0 && message.content !== null;
|
|
8472
|
+
const nestedMessages = subgraphMessages !== void 0 ? convertLangChainToThreadMessages(
|
|
8473
|
+
parseMessages(
|
|
8474
|
+
parentEnded ? autoCloseInFlightSubgraphMessages(subgraphMessages) : subgraphMessages
|
|
8475
|
+
),
|
|
8476
|
+
!parentEnded
|
|
8477
|
+
) : void 0;
|
|
8478
|
+
return {
|
|
8479
|
+
role: "tool",
|
|
8480
|
+
toolName: message.name,
|
|
8481
|
+
toolCallId: message.tool_call_id,
|
|
8482
|
+
result: message.content,
|
|
8483
|
+
artifact: message.artifact,
|
|
8484
|
+
isError: message.status === "error",
|
|
8485
|
+
...nestedMessages !== void 0 && { messages: nestedMessages }
|
|
8486
|
+
};
|
|
8487
|
+
}
|
|
8488
|
+
default:
|
|
8489
|
+
return null;
|
|
8490
|
+
}
|
|
8491
|
+
};
|
|
8492
|
+
const convertLangChainToThreadMessages = (messages, isRunning = false, metadata = {}) => {
|
|
8493
|
+
const validToolCallIds = /* @__PURE__ */ new Set();
|
|
8494
|
+
for (const message of messages) {
|
|
8495
|
+
if (message.type === "ai" && message.tool_calls) {
|
|
8496
|
+
for (const tc of message.tool_calls) {
|
|
8497
|
+
if (tc.id) {
|
|
8498
|
+
validToolCallIds.add(tc.id);
|
|
8499
|
+
}
|
|
8500
|
+
}
|
|
8501
|
+
}
|
|
8502
|
+
}
|
|
8503
|
+
const filteredMessages = messages.filter((message) => {
|
|
8504
|
+
if (message.type === "tool" && message.tool_call_id && !validToolCallIds.has(message.tool_call_id)) {
|
|
8505
|
+
return false;
|
|
8506
|
+
}
|
|
8507
|
+
return true;
|
|
8508
|
+
});
|
|
8509
|
+
return convertExternalMessages(filteredMessages, convertLangChainMessage, isRunning, metadata);
|
|
8510
|
+
};
|
|
8467
8511
|
function getAuthHeaders(auth) {
|
|
8468
8512
|
if (auth.token) {
|
|
8469
8513
|
return { Authorization: `Bearer ${auth.token}` };
|
|
@@ -8530,20 +8574,59 @@ function deserializeMessage(msg) {
|
|
|
8530
8574
|
}
|
|
8531
8575
|
return msg;
|
|
8532
8576
|
}
|
|
8577
|
+
function getAuthMode(auth) {
|
|
8578
|
+
if (auth.token) {
|
|
8579
|
+
return "bearer";
|
|
8580
|
+
}
|
|
8581
|
+
if (auth.apiKey) {
|
|
8582
|
+
return "api-key";
|
|
8583
|
+
}
|
|
8584
|
+
return "none";
|
|
8585
|
+
}
|
|
8586
|
+
function getResponseErrorBody(response) {
|
|
8587
|
+
return response.text().catch(() => "");
|
|
8588
|
+
}
|
|
8533
8589
|
async function getThreadState(backendUrl, auth, threadId) {
|
|
8534
8590
|
const base2 = getAthenaApiBaseUrl(backendUrl);
|
|
8535
|
-
const
|
|
8536
|
-
|
|
8537
|
-
|
|
8591
|
+
const authMode = getAuthMode(auth);
|
|
8592
|
+
const endpoint = `${base2}/api/conversations/threads/get`;
|
|
8593
|
+
console.info("[AthenaSDK] Loading thread state from conversations API:", {
|
|
8594
|
+
threadId,
|
|
8595
|
+
endpoint,
|
|
8596
|
+
authMode,
|
|
8597
|
+
hasToken: Boolean(auth.token),
|
|
8598
|
+
hasApiKey: Boolean(auth.apiKey)
|
|
8599
|
+
});
|
|
8600
|
+
const res = await fetch(endpoint, {
|
|
8601
|
+
method: "POST",
|
|
8602
|
+
headers: { "Content-Type": "application/json", ...getAuthHeaders(auth) },
|
|
8603
|
+
body: JSON.stringify({
|
|
8604
|
+
thread_id: threadId,
|
|
8605
|
+
skip_cache: true
|
|
8606
|
+
})
|
|
8538
8607
|
});
|
|
8539
8608
|
if (!res.ok) {
|
|
8540
|
-
|
|
8609
|
+
const body = await getResponseErrorBody(res);
|
|
8610
|
+
throw new Error(
|
|
8611
|
+
`[AthenaSDK] Failed to get thread state: ${res.status} (${authMode})${body ? `: ${body.slice(0, 300)}` : ""}`
|
|
8612
|
+
);
|
|
8541
8613
|
}
|
|
8542
8614
|
const data = await res.json();
|
|
8543
|
-
|
|
8544
|
-
|
|
8615
|
+
const thread = data.thread;
|
|
8616
|
+
if (data.thread_found === false || !thread) {
|
|
8617
|
+
return {
|
|
8618
|
+
thread_id: threadId,
|
|
8619
|
+
messages: []
|
|
8620
|
+
};
|
|
8545
8621
|
}
|
|
8546
|
-
|
|
8622
|
+
const channelValues = thread.channel_values ?? {};
|
|
8623
|
+
const rawMessages = Array.isArray(channelValues.messages) ? channelValues.messages : Array.isArray(thread.messages) ? thread.messages : [];
|
|
8624
|
+
return {
|
|
8625
|
+
...channelValues,
|
|
8626
|
+
thread_id: thread.thread_id ?? threadId,
|
|
8627
|
+
messages: rawMessages.map(deserializeMessage),
|
|
8628
|
+
...thread.state ? { status: thread.state } : {}
|
|
8629
|
+
};
|
|
8547
8630
|
}
|
|
8548
8631
|
async function archiveThread(backendUrl, auth, threadId) {
|
|
8549
8632
|
const base2 = getAthenaApiBaseUrl(backendUrl);
|
|
@@ -8938,7 +9021,7 @@ const useAthenaRuntime = (config2) => {
|
|
|
8938
9021
|
hasMessages: messageCount > 0
|
|
8939
9022
|
});
|
|
8940
9023
|
runtime.thread.importExternalState({
|
|
8941
|
-
|
|
9024
|
+
...state
|
|
8942
9025
|
});
|
|
8943
9026
|
console.log(
|
|
8944
9027
|
"[AthenaSDK] importExternalState completed, runtime messages:",
|
|
@@ -46865,7 +46948,12 @@ const ComposerKeybinds = Extension.create({
|
|
|
46865
46948
|
if (editor.isActive("bulletList") || editor.isActive("orderedList")) {
|
|
46866
46949
|
return false;
|
|
46867
46950
|
}
|
|
46868
|
-
this.options.onSubmit
|
|
46951
|
+
const submit = this.options.onSubmit;
|
|
46952
|
+
if (typeof window !== "undefined" && typeof window.requestAnimationFrame === "function") {
|
|
46953
|
+
window.requestAnimationFrame(() => submit());
|
|
46954
|
+
} else {
|
|
46955
|
+
submit();
|
|
46956
|
+
}
|
|
46869
46957
|
return true;
|
|
46870
46958
|
},
|
|
46871
46959
|
"Shift-Enter": ({ editor }) => {
|
|
@@ -49432,6 +49520,43 @@ function CollapsibleTrigger({ ...props }) {
|
|
|
49432
49520
|
function CollapsibleContent({ ...props }) {
|
|
49433
49521
|
return /* @__PURE__ */ jsx(CollapsibleContent$1, { "data-slot": "collapsible-content", ...props });
|
|
49434
49522
|
}
|
|
49523
|
+
const NestedPtcComponentsContext = createContext(null);
|
|
49524
|
+
const NestedPtcComponentsProvider = ({ components, children }) => /* @__PURE__ */ jsx(NestedPtcComponentsContext.Provider, { value: components, children });
|
|
49525
|
+
const useNestedPtcComponents = (fallback) => {
|
|
49526
|
+
const components = useContext(NestedPtcComponentsContext);
|
|
49527
|
+
return useMemo(() => {
|
|
49528
|
+
const standardComponents = components && "ChainOfThought" in components && components.ChainOfThought ? void 0 : components;
|
|
49529
|
+
return {
|
|
49530
|
+
...standardComponents ?? {},
|
|
49531
|
+
tools: {
|
|
49532
|
+
...(standardComponents == null ? void 0 : standardComponents.tools) ?? {},
|
|
49533
|
+
Fallback: fallback
|
|
49534
|
+
}
|
|
49535
|
+
};
|
|
49536
|
+
}, [components, fallback]);
|
|
49537
|
+
};
|
|
49538
|
+
const EmptyComponent = () => null;
|
|
49539
|
+
const NestedPtcMessages = ({
|
|
49540
|
+
components,
|
|
49541
|
+
messageClassName
|
|
49542
|
+
}) => {
|
|
49543
|
+
const propsRef = useRef({ components, messageClassName });
|
|
49544
|
+
propsRef.current = { components, messageClassName };
|
|
49545
|
+
const messageComponents = useMemo(() => {
|
|
49546
|
+
const NestedMessage = () => {
|
|
49547
|
+
const { components: components2, messageClassName: messageClassName2 } = propsRef.current;
|
|
49548
|
+
const parts = /* @__PURE__ */ jsx(
|
|
49549
|
+
MessagePrimitive.Parts,
|
|
49550
|
+
{
|
|
49551
|
+
components: { Empty: EmptyComponent, ...components2 }
|
|
49552
|
+
}
|
|
49553
|
+
);
|
|
49554
|
+
return messageClassName2 ? /* @__PURE__ */ jsx("div", { className: messageClassName2, children: parts }) : parts;
|
|
49555
|
+
};
|
|
49556
|
+
return { Message: NestedMessage };
|
|
49557
|
+
}, []);
|
|
49558
|
+
return /* @__PURE__ */ jsx(MessagePartPrimitive.Messages, { components: messageComponents });
|
|
49559
|
+
};
|
|
49435
49560
|
const ANIMATION_DURATION = 200;
|
|
49436
49561
|
function truncateLine(text2, max2 = 70) {
|
|
49437
49562
|
const oneLine = text2.replace(/\s+/g, " ").trim();
|
|
@@ -49980,16 +50105,20 @@ const ToolFallbackImpl = ({
|
|
|
49980
50105
|
status
|
|
49981
50106
|
}) => {
|
|
49982
50107
|
const isCancelled = (status == null ? void 0 : status.type) === "incomplete" && status.reason === "cancelled";
|
|
50108
|
+
const nestedComponents = useNestedPtcComponents(ToolFallback);
|
|
49983
50109
|
if (isAssetTool(toolName, result)) {
|
|
49984
|
-
return /* @__PURE__ */
|
|
49985
|
-
|
|
49986
|
-
|
|
49987
|
-
|
|
49988
|
-
|
|
49989
|
-
|
|
49990
|
-
|
|
49991
|
-
|
|
49992
|
-
|
|
50110
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [
|
|
50111
|
+
/* @__PURE__ */ jsx(
|
|
50112
|
+
AssetToolCard,
|
|
50113
|
+
{
|
|
50114
|
+
toolName,
|
|
50115
|
+
argsText,
|
|
50116
|
+
result,
|
|
50117
|
+
status
|
|
50118
|
+
}
|
|
50119
|
+
),
|
|
50120
|
+
/* @__PURE__ */ jsx(NestedPtcMessages, { components: nestedComponents })
|
|
50121
|
+
] });
|
|
49993
50122
|
}
|
|
49994
50123
|
const isComplete = (status == null ? void 0 : status.type) === "complete" || !status;
|
|
49995
50124
|
const resultAssetId = extractAssetId$1(result);
|
|
@@ -50014,6 +50143,7 @@ const ToolFallbackImpl = ({
|
|
|
50014
50143
|
}
|
|
50015
50144
|
),
|
|
50016
50145
|
!isCancelled && /* @__PURE__ */ jsx(ToolFallbackResult, { result }),
|
|
50146
|
+
/* @__PURE__ */ jsx(NestedPtcMessages, { components: nestedComponents }),
|
|
50017
50147
|
!isCancelled && /* @__PURE__ */ jsx("div", { className: "flex justify-end px-3 pt-1", children: /* @__PURE__ */ jsx(CopyToolSpec, { toolName, argsText, result }) })
|
|
50018
50148
|
] })
|
|
50019
50149
|
]
|
|
@@ -52906,14 +53036,14 @@ const useAthenaChatDefaultComponents = () => {
|
|
|
52906
53036
|
return value;
|
|
52907
53037
|
};
|
|
52908
53038
|
const AthenaDefaultAssistantMessage = () => {
|
|
52909
|
-
const { toolUIs, TextComponent, ReasoningComponent, EmptyComponent, ActionBarComponent } = useAthenaChatDefaultComponents();
|
|
53039
|
+
const { toolUIs, TextComponent, ReasoningComponent, EmptyComponent: EmptyComponent2, ActionBarComponent } = useAthenaChatDefaultComponents();
|
|
52910
53040
|
return /* @__PURE__ */ jsx(
|
|
52911
53041
|
AthenaAssistantMessage,
|
|
52912
53042
|
{
|
|
52913
53043
|
toolUIs,
|
|
52914
53044
|
TextComponent,
|
|
52915
53045
|
ReasoningComponent,
|
|
52916
|
-
EmptyComponent,
|
|
53046
|
+
EmptyComponent: EmptyComponent2,
|
|
52917
53047
|
ActionBarComponent
|
|
52918
53048
|
}
|
|
52919
53049
|
);
|
|
@@ -52938,6 +53068,16 @@ const getReasoningTokensFromMetadata = (metadata) => {
|
|
|
52938
53068
|
return typeof reasoningTokens === "number" && Number.isFinite(reasoningTokens) ? reasoningTokens : void 0;
|
|
52939
53069
|
};
|
|
52940
53070
|
const formatReasoningTokens = (reasoningTokens) => `${reasoningTokens.toLocaleString("en-US")} ${reasoningTokens === 1 ? "token" : "tokens"}`;
|
|
53071
|
+
const withNestedPtcMessages = (ToolUI) => {
|
|
53072
|
+
const ToolUIWithNestedPtcMessages = (props) => {
|
|
53073
|
+
const nestedComponents = useNestedPtcComponents(ToolFallback);
|
|
53074
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [
|
|
53075
|
+
/* @__PURE__ */ jsx(ToolUI, { ...props }),
|
|
53076
|
+
/* @__PURE__ */ jsx(NestedPtcMessages, { components: nestedComponents })
|
|
53077
|
+
] });
|
|
53078
|
+
};
|
|
53079
|
+
return ToolUIWithNestedPtcMessages;
|
|
53080
|
+
};
|
|
52941
53081
|
const SuggestionCard = ({
|
|
52942
53082
|
suggestion,
|
|
52943
53083
|
index: index2
|
|
@@ -53293,10 +53433,29 @@ const AthenaAssistantMessage = ({
|
|
|
53293
53433
|
toolUIs,
|
|
53294
53434
|
TextComponent = TiptapText,
|
|
53295
53435
|
ReasoningComponent,
|
|
53296
|
-
EmptyComponent = AthenaAssistantMessageEmpty,
|
|
53436
|
+
EmptyComponent: EmptyComponent2 = AthenaAssistantMessageEmpty,
|
|
53297
53437
|
ActionBarComponent = AthenaAssistantActionBar
|
|
53298
53438
|
}) => {
|
|
53299
53439
|
const effectiveReasoningComponent = ReasoningComponent ?? AthenaReasoningPart;
|
|
53440
|
+
const toolUIsWithNestedMessages = useMemo(() => {
|
|
53441
|
+
const wrappedToolUIs = {};
|
|
53442
|
+
for (const [toolName, ToolUI] of Object.entries(toolUIs)) {
|
|
53443
|
+
wrappedToolUIs[toolName] = withNestedPtcMessages(ToolUI);
|
|
53444
|
+
}
|
|
53445
|
+
return wrappedToolUIs;
|
|
53446
|
+
}, [toolUIs]);
|
|
53447
|
+
const partsComponents = useMemo(
|
|
53448
|
+
() => ({
|
|
53449
|
+
Text: TextComponent,
|
|
53450
|
+
Reasoning: effectiveReasoningComponent,
|
|
53451
|
+
tools: {
|
|
53452
|
+
Fallback: ToolFallback,
|
|
53453
|
+
by_name: toolUIsWithNestedMessages
|
|
53454
|
+
},
|
|
53455
|
+
Empty: EmptyComponent2
|
|
53456
|
+
}),
|
|
53457
|
+
[EmptyComponent2, TextComponent, effectiveReasoningComponent, toolUIsWithNestedMessages]
|
|
53458
|
+
);
|
|
53300
53459
|
return /* @__PURE__ */ jsxs(
|
|
53301
53460
|
MessagePrimitive.Root,
|
|
53302
53461
|
{
|
|
@@ -53304,20 +53463,7 @@ const AthenaAssistantMessage = ({
|
|
|
53304
53463
|
"data-role": "assistant",
|
|
53305
53464
|
children: [
|
|
53306
53465
|
/* @__PURE__ */ jsx(AthenaReasoningTextComponentContext.Provider, { value: TextComponent, children: /* @__PURE__ */ jsxs("div", { className: "aui-assistant-message-content wrap-break-word px-2 text-foreground leading-relaxed", children: [
|
|
53307
|
-
/* @__PURE__ */ jsx(
|
|
53308
|
-
MessagePrimitive.Parts,
|
|
53309
|
-
{
|
|
53310
|
-
components: {
|
|
53311
|
-
Text: TextComponent,
|
|
53312
|
-
Reasoning: effectiveReasoningComponent,
|
|
53313
|
-
tools: {
|
|
53314
|
-
Fallback: ToolFallback,
|
|
53315
|
-
by_name: toolUIs
|
|
53316
|
-
},
|
|
53317
|
-
Empty: EmptyComponent
|
|
53318
|
-
}
|
|
53319
|
-
}
|
|
53320
|
-
),
|
|
53466
|
+
/* @__PURE__ */ jsx(NestedPtcComponentsProvider, { components: partsComponents, children: /* @__PURE__ */ jsx(MessagePrimitive.Parts, { components: partsComponents }) }),
|
|
53321
53467
|
/* @__PURE__ */ jsx(MessageError, {})
|
|
53322
53468
|
] }) }),
|
|
53323
53469
|
/* @__PURE__ */ jsx("div", { className: "aui-assistant-message-footer mt-1 ml-2 flex", children: /* @__PURE__ */ jsx(ActionBarComponent, {}) })
|
|
@@ -53825,7 +53971,9 @@ export {
|
|
|
53825
53971
|
FileUploadButton,
|
|
53826
53972
|
GetDatabaseTableSchemaToolUI,
|
|
53827
53973
|
ListDatabaseTablesToolUI,
|
|
53974
|
+
NestedPtcMessages,
|
|
53828
53975
|
OpenAssetToolUI,
|
|
53976
|
+
PTC_SUBCALL_DID_NOT_COMPLETE_MESSAGE,
|
|
53829
53977
|
ReadAssetToolUI,
|
|
53830
53978
|
RunPythonCodeToolUI,
|
|
53831
53979
|
RunSqlToolUI,
|
|
@@ -53850,9 +53998,11 @@ export {
|
|
|
53850
53998
|
UpdateSheetRangeToolUI,
|
|
53851
53999
|
WebSearchToolUI,
|
|
53852
54000
|
archiveThread,
|
|
54001
|
+
autoCloseInFlightSubgraphMessages,
|
|
53853
54002
|
buttonVariants,
|
|
53854
54003
|
clearAutoOpenedAssets,
|
|
53855
54004
|
cn,
|
|
54005
|
+
convertLangChainToThreadMessages,
|
|
53856
54006
|
createAssetToolUI,
|
|
53857
54007
|
createThread,
|
|
53858
54008
|
formatToolName,
|