@nexface/chat 0.1.21-alpha.6 → 0.1.21-alpha.8
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/client.js +85 -47
- package/dist/default-chat.js +20544 -20571
- package/dist/react.js +0 -81
- package/dist/shared/errors.d.ts +1 -1
- package/dist/shared/errors.js +1 -1
- package/dist/shared/runtime-execution.d.ts +6 -0
- package/dist/shared/runtime-execution.js +3 -0
- package/dist/styles.css +3 -0
- package/dist/tool-confirmation-card.d.ts +1 -2
- package/dist/tool-confirmation-card.js +7 -27
- package/dist/tool-confirmation.d.ts +1 -23
- package/dist/tool-confirmation.js +1 -9
- package/package.json +3 -3
package/dist/client.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { decodeToolProxy } from "./shared/tool-proxy.js";
|
|
2
|
-
import { pageDecisionSymbol } from "./shared/runtime-execution.js";
|
|
2
|
+
import { executionStartSymbol, executionStartedSymbol, hostConfirmationSymbol, pageDecisionSymbol, } from "./shared/runtime-execution.js";
|
|
3
3
|
import { createAgent, } from "@nexface/agent";
|
|
4
4
|
import { decodeAgentResponse, } from "./protocol-response.js";
|
|
5
5
|
import { isToolRuntimeError } from "@nexface/tools";
|
|
@@ -86,6 +86,8 @@ class ChatClientImplementation {
|
|
|
86
86
|
this.agent = options.agent
|
|
87
87
|
? createAgent({ ...options.agent, model: this.#model, toolRuntime: this.runtime })
|
|
88
88
|
: undefined;
|
|
89
|
+
if (this.agent)
|
|
90
|
+
agentController(this.agent).setHostConfirmation((approval, tool, signal) => this.#wait(approval, tool, signal, "callback"));
|
|
89
91
|
}
|
|
90
92
|
getMessages = () => this.#messages;
|
|
91
93
|
getStatus = () => this.#status;
|
|
@@ -204,8 +206,8 @@ class ChatClientImplementation {
|
|
|
204
206
|
/* Incomplete calls have no local diagnostic. */
|
|
205
207
|
}
|
|
206
208
|
const presentation = toolError(event.messageId, event.toolCallId, event.content, event.metadata?.nexface?.parentErrorId, false, name);
|
|
207
|
-
const
|
|
208
|
-
if (presentation.toolState === "cancelled" &&
|
|
209
|
+
const runtimeFailure = presentation.toolState && name ? this.#runtimeCauses.get(name)?.shift() : undefined;
|
|
210
|
+
if (presentation.toolState === "cancelled" && runtimeFailure?.executionStarted) {
|
|
209
211
|
// Cancellation after execution began cannot prove that effects were undone.
|
|
210
212
|
event = {
|
|
211
213
|
...event,
|
|
@@ -220,7 +222,7 @@ class ChatClientImplementation {
|
|
|
220
222
|
};
|
|
221
223
|
}
|
|
222
224
|
if (presentation.error && !presentation.error.parentErrorId)
|
|
223
|
-
this.#report(presentation.error, this.#toolCauses.get(event.toolCallId) ??
|
|
225
|
+
this.#report(presentation.error, this.#toolCauses.get(event.toolCallId) ?? runtimeFailure?.error ?? presentation.cause);
|
|
224
226
|
this.#toolCauses.delete(event.toolCallId);
|
|
225
227
|
}
|
|
226
228
|
if (event.type === EventType.RUN_STARTED || event.type === EventType.RUN_FINISHED)
|
|
@@ -243,7 +245,7 @@ class ChatClientImplementation {
|
|
|
243
245
|
const diagnostic = toolError(receipt.id, part.toolCallId, receipt.content, undefined, false, part.name)
|
|
244
246
|
.cause ?? receipt.error;
|
|
245
247
|
this.#report(part.error, this.#toolCauses.get(part.toolCallId) ??
|
|
246
|
-
this.#runtimeCauses.get(part.name)?.shift() ??
|
|
248
|
+
this.#runtimeCauses.get(part.name)?.shift()?.error ??
|
|
247
249
|
diagnostic);
|
|
248
250
|
this.#toolCauses.delete(part.toolCallId);
|
|
249
251
|
}
|
|
@@ -403,10 +405,12 @@ class ChatClientImplementation {
|
|
|
403
405
|
this.cancel();
|
|
404
406
|
});
|
|
405
407
|
const unsubscribeExecution = this.runtime.on("toolexecution", (event) => {
|
|
406
|
-
if (event.status !== "error")
|
|
408
|
+
if (event.operationId !== operationId || event.status !== "error")
|
|
407
409
|
return;
|
|
410
|
+
const executionStarted = event[executionStartedSymbol] ===
|
|
411
|
+
true;
|
|
408
412
|
const causes = this.#runtimeCauses.get(event.name) ?? [];
|
|
409
|
-
causes.push(event.error);
|
|
413
|
+
causes.push({ error: event.error, executionStarted });
|
|
410
414
|
this.#runtimeCauses.set(event.name, causes);
|
|
411
415
|
});
|
|
412
416
|
let succeeded = false;
|
|
@@ -554,7 +558,7 @@ class ChatClientImplementation {
|
|
|
554
558
|
run = this.agent.run({ resume: [response] }, { signal });
|
|
555
559
|
}
|
|
556
560
|
}
|
|
557
|
-
#wait(approval, tool, signal) {
|
|
561
|
+
#wait(approval, tool, signal, source = "interrupt") {
|
|
558
562
|
signal.throwIfAborted();
|
|
559
563
|
this.#approval = Object.freeze(approval);
|
|
560
564
|
this.#status = "waiting";
|
|
@@ -565,33 +569,43 @@ class ChatClientImplementation {
|
|
|
565
569
|
signal.removeEventListener("abort", abort);
|
|
566
570
|
tool.lifecycleSignal.removeEventListener("abort", removed);
|
|
567
571
|
};
|
|
568
|
-
this.#pending = { resolve, reject, cleanup };
|
|
572
|
+
this.#pending = { source, resolve, reject, cleanup };
|
|
569
573
|
signal.addEventListener("abort", abort, { once: true });
|
|
570
574
|
tool.lifecycleSignal.addEventListener("abort", removed, { once: true });
|
|
571
575
|
if (tool.lifecycleSignal.aborted)
|
|
572
576
|
removed();
|
|
573
577
|
});
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
578
|
+
try {
|
|
579
|
+
if (this.#operation)
|
|
580
|
+
getRuntimeController(this.runtime).setOperationWaiting(this.#operation, true);
|
|
581
|
+
this.#publish();
|
|
582
|
+
}
|
|
583
|
+
catch (error) {
|
|
584
|
+
this.#clearApproval()?.reject(error instanceof Error ? error : new SubscriptionError(error));
|
|
585
|
+
}
|
|
577
586
|
return waiting;
|
|
578
587
|
}
|
|
579
588
|
async #respond(id, approved, source) {
|
|
580
589
|
const pending = this.#pending;
|
|
581
|
-
if (!pending ||
|
|
582
|
-
|
|
583
|
-
|
|
590
|
+
if (!pending || this.#approval?.id !== id)
|
|
591
|
+
throw new ClientError("TOOL_CONFIRMATION_INVALIDATED", "Confirmation is no longer pending.");
|
|
592
|
+
if (this.#approval.mode !== source ||
|
|
584
593
|
typeof approved !== "boolean" ||
|
|
585
594
|
(source === "page" && !approved))
|
|
586
595
|
throw new ClientError("INVALID_INPUT", "Confirmation is no longer pending or has the wrong source.");
|
|
587
|
-
|
|
596
|
+
this.#clearApproval();
|
|
597
|
+
pending.resolve(approved);
|
|
598
|
+
this.#publish();
|
|
599
|
+
}
|
|
600
|
+
#clearApproval() {
|
|
601
|
+
const pending = this.#pending;
|
|
602
|
+
pending?.cleanup();
|
|
588
603
|
this.#pending = undefined;
|
|
589
604
|
this.#approval = undefined;
|
|
590
605
|
this.#status = "running";
|
|
591
606
|
if (this.#operation)
|
|
592
607
|
getRuntimeController(this.runtime).setOperationWaiting(this.#operation, false);
|
|
593
|
-
pending
|
|
594
|
-
this.#publish();
|
|
608
|
+
return pending;
|
|
595
609
|
}
|
|
596
610
|
respondToApproval = (id, response) => this.#respond(id, response.approved, "chat");
|
|
597
611
|
confirmPage() {
|
|
@@ -609,6 +623,11 @@ class ChatClientImplementation {
|
|
|
609
623
|
const approval = this.#approval;
|
|
610
624
|
if (!approval)
|
|
611
625
|
return;
|
|
626
|
+
if (this.#pending?.source === "callback") {
|
|
627
|
+
this.#clearApproval()?.reject(new ClientError("TOOL_CONFIRMATION_INVALIDATED", message));
|
|
628
|
+
this.#publish();
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
612
631
|
const previousIds = new Set(this.#messages.map((item) => item.id));
|
|
613
632
|
this.#messages = settleApproval(this.#messages, approval, "TOOL_CONFIRMATION_INVALIDATED", message, "binding_changed").map((item) => previousIds.has(item.id)
|
|
614
633
|
? item
|
|
@@ -622,14 +641,7 @@ class ChatClientImplementation {
|
|
|
622
641
|
if (this.agent) {
|
|
623
642
|
agentController(this.agent).settle(approval.id, { type: "invalidate", message, reason: "binding_changed" }, this.#messages);
|
|
624
643
|
}
|
|
625
|
-
|
|
626
|
-
pending?.cleanup();
|
|
627
|
-
this.#pending = undefined;
|
|
628
|
-
this.#approval = undefined;
|
|
629
|
-
this.#status = "running";
|
|
630
|
-
if (this.#operation)
|
|
631
|
-
getRuntimeController(this.runtime).setOperationWaiting(this.#operation, false);
|
|
632
|
-
pending?.reject(new ClientError("TOOL_CONFIRMATION_INVALIDATED", message));
|
|
644
|
+
this.#clearApproval()?.reject(new ClientError("TOOL_CONFIRMATION_INVALIDATED", message));
|
|
633
645
|
this.#publish();
|
|
634
646
|
}
|
|
635
647
|
cancel = () => {
|
|
@@ -646,7 +658,7 @@ class ChatClientImplementation {
|
|
|
646
658
|
this.#publish();
|
|
647
659
|
return;
|
|
648
660
|
}
|
|
649
|
-
if (this.#approval) {
|
|
661
|
+
if (this.#approval && this.#pending?.source !== "callback") {
|
|
650
662
|
this.#messages = settleApproval(this.#messages, this.#approval, "TOOL_CANCELLED", "The pending Tool confirmation was cancelled.", "interrupt_cancelled");
|
|
651
663
|
if (this.agent)
|
|
652
664
|
agentController(this.agent).settle(this.#approval.id, { type: "cancel" }, this.#messages);
|
|
@@ -678,8 +690,8 @@ class ChatClientImplementation {
|
|
|
678
690
|
: undefined;
|
|
679
691
|
let interruptId;
|
|
680
692
|
let inFlight = false;
|
|
681
|
-
let
|
|
682
|
-
let
|
|
693
|
+
let browserExecutionStarted = false;
|
|
694
|
+
let serverExecution = "unknown";
|
|
683
695
|
let stopTimeout;
|
|
684
696
|
try {
|
|
685
697
|
for (;;) {
|
|
@@ -714,7 +726,7 @@ class ChatClientImplementation {
|
|
|
714
726
|
},
|
|
715
727
|
};
|
|
716
728
|
RunAgentInputSchema.parse(input);
|
|
717
|
-
|
|
729
|
+
serverExecution = "unknown";
|
|
718
730
|
inFlight = true;
|
|
719
731
|
interruptId = undefined;
|
|
720
732
|
const response = await abortable(this.#runHandler(input, { signal }), signal);
|
|
@@ -741,9 +753,10 @@ class ChatClientImplementation {
|
|
|
741
753
|
if (event.metadata?.nexface?.appendOnly === true)
|
|
742
754
|
this.#appendOnly = true;
|
|
743
755
|
const capabilities = event.metadata?.nexface;
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
756
|
+
if (capabilities?.clientToolExecutionOnly === true)
|
|
757
|
+
serverExecution = "client-only";
|
|
758
|
+
else if (capabilities?.serverToolExecutionReadOnly === true)
|
|
759
|
+
serverExecution = "read-only";
|
|
747
760
|
if (capabilities?.cooperativeStop === true) {
|
|
748
761
|
const stop = () => {
|
|
749
762
|
const controller = this.#controller;
|
|
@@ -815,7 +828,7 @@ class ChatClientImplementation {
|
|
|
815
828
|
this.#state = state;
|
|
816
829
|
this.#cancelledInterrupt = undefined;
|
|
817
830
|
inFlight = false;
|
|
818
|
-
|
|
831
|
+
browserExecutionStarted = false;
|
|
819
832
|
const stopping = this.#cancelled && this.#stopRemote;
|
|
820
833
|
this.#stopRemote = undefined;
|
|
821
834
|
let snapshot = wireSnapshot;
|
|
@@ -853,7 +866,7 @@ class ChatClientImplementation {
|
|
|
853
866
|
if (this.#cancelled && !signal.aborted)
|
|
854
867
|
throw new ClientError("CHAT_RECOVERY_UNAVAILABLE", "The remote run stopped at a pending tool call.");
|
|
855
868
|
const toolResult = await this.#browserAction(decoded.action, catalog, signal);
|
|
856
|
-
|
|
869
|
+
browserExecutionStarted = toolResult.executionStarted;
|
|
857
870
|
const messageId = id();
|
|
858
871
|
this.#event({
|
|
859
872
|
type: EventType.TOOL_CALL_RESULT,
|
|
@@ -875,13 +888,15 @@ class ChatClientImplementation {
|
|
|
875
888
|
}
|
|
876
889
|
}
|
|
877
890
|
catch (error) {
|
|
878
|
-
const
|
|
879
|
-
|
|
891
|
+
const executionOutcomeUnknown = (inFlight && serverExecution === "unknown") ||
|
|
892
|
+
browserExecutionStarted ||
|
|
880
893
|
this.#executingBrowserCall !== undefined;
|
|
881
|
-
|
|
894
|
+
const recoveryStateUnknown = executionOutcomeUnknown || (inFlight && !signal.aborted);
|
|
895
|
+
if ((this.#appendOnly && (recoveryStateUnknown || this.#cancelled)) ||
|
|
896
|
+
(error instanceof ClientError && error.code === "CHAT_RECOVERY_UNAVAILABLE")) {
|
|
882
897
|
this.#recoveryUnavailable = true;
|
|
883
898
|
}
|
|
884
|
-
else if (
|
|
899
|
+
else if (recoveryStateUnknown) {
|
|
885
900
|
this.#cancelledInterrupt = undefined;
|
|
886
901
|
}
|
|
887
902
|
else if (interruptId) {
|
|
@@ -897,7 +912,8 @@ class ChatClientImplementation {
|
|
|
897
912
|
for (const call of message.toolCalls) {
|
|
898
913
|
if (receipts.some((item) => item.role === "tool" && item.toolCallId === call.id))
|
|
899
914
|
continue;
|
|
900
|
-
const unknown = inFlight
|
|
915
|
+
const unknown = (inFlight && serverExecution !== "client-only") ||
|
|
916
|
+
this.#executingBrowserCall === call.id;
|
|
901
917
|
this.#event({
|
|
902
918
|
type: EventType.TOOL_CALL_RESULT,
|
|
903
919
|
messageId: id(),
|
|
@@ -915,12 +931,14 @@ class ChatClientImplementation {
|
|
|
915
931
|
});
|
|
916
932
|
}
|
|
917
933
|
}
|
|
918
|
-
if (
|
|
934
|
+
if (executionOutcomeUnknown && !this.#recoveryUnavailable) {
|
|
919
935
|
this.#messages.push({
|
|
920
936
|
id: id(),
|
|
921
937
|
role: "assistant",
|
|
922
|
-
content:
|
|
923
|
-
|
|
938
|
+
content: signal.aborted
|
|
939
|
+
? "The previous run was stopped and its execution outcome is unknown. Check the result before repeating any action."
|
|
940
|
+
: "Check the previous action results before repeating any action.",
|
|
941
|
+
metadata: { nexface: { state: signal.aborted ? "cancelled" : "error" } },
|
|
924
942
|
});
|
|
925
943
|
}
|
|
926
944
|
throw error;
|
|
@@ -973,15 +991,33 @@ class ChatClientImplementation {
|
|
|
973
991
|
throw new ClientError("TOOL_CONFIRMATION_REJECTED", "The user rejected this tool call.");
|
|
974
992
|
}
|
|
975
993
|
signal.throwIfAborted();
|
|
976
|
-
|
|
977
|
-
const result = await this.runtime.execute(tool, action.input, {
|
|
994
|
+
const options = {
|
|
978
995
|
signal,
|
|
996
|
+
[executionStartSymbol]: () => {
|
|
997
|
+
this.#executingBrowserCall = action.toolCallId;
|
|
998
|
+
},
|
|
999
|
+
[hostConfirmationSymbol]: async (message, confirmationSignal) => {
|
|
1000
|
+
const approved = await this.#wait({
|
|
1001
|
+
type: "tool-approval",
|
|
1002
|
+
mode: "chat",
|
|
1003
|
+
id: id(),
|
|
1004
|
+
runId: this.#runId,
|
|
1005
|
+
toolCallId: action.toolCallId,
|
|
1006
|
+
toolName: tool.definition.name,
|
|
1007
|
+
input: structuredClone(action.input),
|
|
1008
|
+
title: message,
|
|
1009
|
+
}, tool, confirmationSignal, "callback");
|
|
1010
|
+
confirmed = true;
|
|
1011
|
+
return approved;
|
|
1012
|
+
},
|
|
979
1013
|
...(action.pageDecision ? { [pageDecisionSymbol]: true } : {}),
|
|
980
1014
|
...(action.progressLabel ? { progressLabel: action.progressLabel } : {}),
|
|
981
|
-
}
|
|
1015
|
+
};
|
|
1016
|
+
const result = await this.runtime.execute(tool, action.input, options);
|
|
982
1017
|
return {
|
|
983
1018
|
toolCallId: action.toolCallId,
|
|
984
1019
|
result: result ?? null,
|
|
1020
|
+
executionStarted: this.#executingBrowserCall === action.toolCallId,
|
|
985
1021
|
...(confirmed ? { confirmed } : {}),
|
|
986
1022
|
};
|
|
987
1023
|
}
|
|
@@ -994,9 +1030,11 @@ class ChatClientImplementation {
|
|
|
994
1030
|
this.#toolCauses.set(action.toolCallId, error);
|
|
995
1031
|
const code = error instanceof ClientError || isToolRuntimeError(error) ? error.code : "TOOL_ERROR";
|
|
996
1032
|
// Once execution starts, an executor cannot report an approval rejection.
|
|
997
|
-
const
|
|
1033
|
+
const executionStarted = this.#executingBrowserCall === action.toolCallId;
|
|
1034
|
+
const executionError = code === "TOOL_CONFIRMATION_REJECTED" && executionStarted;
|
|
998
1035
|
return {
|
|
999
1036
|
toolCallId: action.toolCallId,
|
|
1037
|
+
executionStarted,
|
|
1000
1038
|
error: {
|
|
1001
1039
|
code: executionError ? "TOOL_ERROR" : code,
|
|
1002
1040
|
message: error instanceof Error ? error.message : String(error),
|