@tangle-network/agent-app 0.46.30 → 0.46.32
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/chat-routes/gateway-adapter.d.ts +17 -0
- package/dist/chat-routes/index.d.ts +1 -0
- package/dist/chat-routes/index.js +189 -2
- package/dist/chat-routes/index.js.map +1 -1
- package/dist/chat-routes/turn-routes.d.ts +5 -0
- package/dist/{chunk-57J36H7C.js → chunk-64EVYEGI.js} +2 -1
- package/dist/{chunk-57J36H7C.js.map → chunk-64EVYEGI.js.map} +1 -1
- package/dist/sandbox/diagnostics.d.ts +3 -5
- package/dist/sandbox/index.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { SandboxStreamEvent } from '../sandbox/index';
|
|
2
|
+
import type { ChatTurnRoutes } from './turn-routes';
|
|
3
|
+
import { type ChatTurnRequestPayload } from './wire';
|
|
4
|
+
export interface StreamChatRouteAsSandboxOptions {
|
|
5
|
+
routes: ChatTurnRoutes;
|
|
6
|
+
request: Request;
|
|
7
|
+
payload: ChatTurnRequestPayload;
|
|
8
|
+
waitUntil?: (promise: Promise<unknown>) => void;
|
|
9
|
+
/** Abort the in-process route when the gateway client disconnects. */
|
|
10
|
+
signal?: AbortSignal;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Drive the normal persisted chat route and expose its events to an
|
|
14
|
+
* agent-gateway sandbox adapter. The route remains the only turn owner: auth,
|
|
15
|
+
* locking, live delivery, transcript writes, and completion hooks run once.
|
|
16
|
+
*/
|
|
17
|
+
export declare function streamChatRouteAsSandboxEvents(options: StreamChatRouteAsSandboxOptions): AsyncGenerator<SandboxStreamEvent>;
|
|
@@ -10,6 +10,7 @@ export * from './wire';
|
|
|
10
10
|
export * from './binary-sniff';
|
|
11
11
|
export * from './attachment-validation';
|
|
12
12
|
export * from './turn-routes';
|
|
13
|
+
export * from './gateway-adapter';
|
|
13
14
|
export * from './stale-turn-lock';
|
|
14
15
|
export * from './sandbox-producer';
|
|
15
16
|
export * from './model-failover-stream';
|
|
@@ -112,7 +112,7 @@ import {
|
|
|
112
112
|
flattenHistory,
|
|
113
113
|
readSandboxBinaryBytes,
|
|
114
114
|
statSandboxFileSize
|
|
115
|
-
} from "../chunk-
|
|
115
|
+
} from "../chunk-64EVYEGI.js";
|
|
116
116
|
import "../chunk-TH7L265V.js";
|
|
117
117
|
import "../chunk-LWSJK546.js";
|
|
118
118
|
import "../chunk-P5PIAQVS.js";
|
|
@@ -644,6 +644,9 @@ function createChatTurnRoutes(options) {
|
|
|
644
644
|
stream: (async function* () {
|
|
645
645
|
producer = await options.produce(produceArgs);
|
|
646
646
|
let source = producer.stream;
|
|
647
|
+
if (ctx?.cancelOnDisconnect) {
|
|
648
|
+
source = abortOnSignal(source, request.signal);
|
|
649
|
+
}
|
|
647
650
|
if (options.onRawEvent) {
|
|
648
651
|
source = tapRawEvents(source, (event) => options.onRawEvent(event, context), log);
|
|
649
652
|
}
|
|
@@ -665,6 +668,11 @@ function createChatTurnRoutes(options) {
|
|
|
665
668
|
},
|
|
666
669
|
...options.transformFinalText ? { transformFinalText: options.transformFinalText } : {},
|
|
667
670
|
persistAssistantMessage: async ({ finalText }) => {
|
|
671
|
+
if (ctx?.cancelOnDisconnect && request.signal.aborted) {
|
|
672
|
+
await draft?.discard();
|
|
673
|
+
assistantMessageId = null;
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
668
676
|
const rawParts = producer?.assistantParts ? producer.assistantParts() : void 0;
|
|
669
677
|
const projected = rawParts && options.transformFinalText ? await Promise.all(
|
|
670
678
|
rawParts.map(
|
|
@@ -752,7 +760,11 @@ function createChatTurnRoutes(options) {
|
|
|
752
760
|
});
|
|
753
761
|
}
|
|
754
762
|
const failed = runFailed || drainError !== void 0;
|
|
755
|
-
|
|
763
|
+
if (ctx?.cancelOnDisconnect && request.signal.aborted) {
|
|
764
|
+
await draft?.discard();
|
|
765
|
+
} else {
|
|
766
|
+
await draft?.close();
|
|
767
|
+
}
|
|
756
768
|
try {
|
|
757
769
|
await tap.done(failed ? "error" : "complete");
|
|
758
770
|
} catch (err) {
|
|
@@ -868,6 +880,180 @@ function concatStreams(streams) {
|
|
|
868
880
|
}
|
|
869
881
|
});
|
|
870
882
|
}
|
|
883
|
+
async function* abortOnSignal(source, signal) {
|
|
884
|
+
const iterator = source[Symbol.asyncIterator]();
|
|
885
|
+
try {
|
|
886
|
+
for (; ; ) {
|
|
887
|
+
const next = await nextWithAbort(iterator, signal);
|
|
888
|
+
if (next.done) return;
|
|
889
|
+
yield next.value;
|
|
890
|
+
}
|
|
891
|
+
} finally {
|
|
892
|
+
await closeIteratorAfterAbort(iterator);
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
function nextWithAbort(iterator, signal) {
|
|
896
|
+
if (signal.aborted) return Promise.reject(new Error("chat turn cancelled"));
|
|
897
|
+
return new Promise((resolve, reject) => {
|
|
898
|
+
let settled = false;
|
|
899
|
+
const cleanup = () => signal.removeEventListener("abort", onAbort);
|
|
900
|
+
const onAbort = () => {
|
|
901
|
+
if (settled) return;
|
|
902
|
+
settled = true;
|
|
903
|
+
cleanup();
|
|
904
|
+
reject(new Error("chat turn cancelled"));
|
|
905
|
+
};
|
|
906
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
907
|
+
if (signal.aborted) {
|
|
908
|
+
onAbort();
|
|
909
|
+
return;
|
|
910
|
+
}
|
|
911
|
+
Promise.resolve().then(() => iterator.next()).then(
|
|
912
|
+
(result) => {
|
|
913
|
+
if (settled) return;
|
|
914
|
+
settled = true;
|
|
915
|
+
cleanup();
|
|
916
|
+
resolve(result);
|
|
917
|
+
},
|
|
918
|
+
(error) => {
|
|
919
|
+
if (settled) return;
|
|
920
|
+
settled = true;
|
|
921
|
+
cleanup();
|
|
922
|
+
reject(error);
|
|
923
|
+
}
|
|
924
|
+
);
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
var ABORT_CLEANUP_TIMEOUT_MS = 50;
|
|
928
|
+
async function closeIteratorAfterAbort(iterator) {
|
|
929
|
+
let closing;
|
|
930
|
+
try {
|
|
931
|
+
const result = iterator.return?.();
|
|
932
|
+
if (result) closing = Promise.resolve(result);
|
|
933
|
+
} catch {
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
936
|
+
if (!closing) return;
|
|
937
|
+
let timeout;
|
|
938
|
+
try {
|
|
939
|
+
await Promise.race([
|
|
940
|
+
Promise.resolve(closing).catch(() => void 0),
|
|
941
|
+
new Promise((resolve) => {
|
|
942
|
+
timeout = setTimeout(resolve, ABORT_CLEANUP_TIMEOUT_MS);
|
|
943
|
+
})
|
|
944
|
+
]);
|
|
945
|
+
} finally {
|
|
946
|
+
if (timeout !== void 0) clearTimeout(timeout);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
// src/chat-routes/gateway-adapter.ts
|
|
951
|
+
async function* streamChatRouteAsSandboxEvents(options) {
|
|
952
|
+
const routeAbort = new AbortController();
|
|
953
|
+
const parentSignal = options.signal ?? options.request.signal;
|
|
954
|
+
const abortRoute = () => routeAbort.abort();
|
|
955
|
+
if (parentSignal.aborted) abortRoute();
|
|
956
|
+
else parentSignal.addEventListener("abort", abortRoute, { once: true });
|
|
957
|
+
const request = new Request(options.request.url, {
|
|
958
|
+
...chatTurnRequestInit(options.payload),
|
|
959
|
+
headers: options.request.headers,
|
|
960
|
+
signal: routeAbort.signal
|
|
961
|
+
});
|
|
962
|
+
request.headers.set("Content-Type", "application/json");
|
|
963
|
+
let reader;
|
|
964
|
+
let complete = false;
|
|
965
|
+
try {
|
|
966
|
+
const response = await options.routes.turn(
|
|
967
|
+
request,
|
|
968
|
+
{
|
|
969
|
+
cancelOnDisconnect: true,
|
|
970
|
+
...options.waitUntil ? { waitUntil: options.waitUntil } : {}
|
|
971
|
+
}
|
|
972
|
+
);
|
|
973
|
+
if (!response.ok) {
|
|
974
|
+
await response.body?.cancel().catch(() => void 0);
|
|
975
|
+
throw new Error(`chat route rejected the gateway turn (${response.status})`);
|
|
976
|
+
}
|
|
977
|
+
if (!response.body) throw new Error("chat route returned no stream");
|
|
978
|
+
reader = response.body.getReader();
|
|
979
|
+
const decoder = new TextDecoder();
|
|
980
|
+
let buffer = "";
|
|
981
|
+
for (; ; ) {
|
|
982
|
+
const chunk = await reader.read();
|
|
983
|
+
if (chunk.done) {
|
|
984
|
+
complete = true;
|
|
985
|
+
buffer += decoder.decode();
|
|
986
|
+
const event = toSandboxEvent(parseEvent(buffer));
|
|
987
|
+
if (event) yield event;
|
|
988
|
+
break;
|
|
989
|
+
}
|
|
990
|
+
buffer += decoder.decode(chunk.value, { stream: true });
|
|
991
|
+
const lines = buffer.split("\n");
|
|
992
|
+
buffer = lines.pop() ?? "";
|
|
993
|
+
for (const line of lines) {
|
|
994
|
+
const event = toSandboxEvent(parseEvent(line));
|
|
995
|
+
if (event) yield event;
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
} finally {
|
|
999
|
+
if (!complete) {
|
|
1000
|
+
routeAbort.abort();
|
|
1001
|
+
await reader?.cancel().catch(() => void 0);
|
|
1002
|
+
}
|
|
1003
|
+
reader?.releaseLock();
|
|
1004
|
+
parentSignal.removeEventListener("abort", abortRoute);
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
function parseEvent(line) {
|
|
1008
|
+
if (!line.trim()) return null;
|
|
1009
|
+
let value;
|
|
1010
|
+
try {
|
|
1011
|
+
value = JSON.parse(line);
|
|
1012
|
+
} catch {
|
|
1013
|
+
throw new Error("chat route returned invalid NDJSON");
|
|
1014
|
+
}
|
|
1015
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
1016
|
+
throw new Error("chat route returned a non-object event");
|
|
1017
|
+
}
|
|
1018
|
+
const event = value;
|
|
1019
|
+
if (event.type !== void 0 && typeof event.type !== "string") {
|
|
1020
|
+
throw new Error("chat route returned an invalid event type");
|
|
1021
|
+
}
|
|
1022
|
+
const data = event.data;
|
|
1023
|
+
if (data !== void 0 && (!data || typeof data !== "object" || Array.isArray(data))) {
|
|
1024
|
+
throw new Error("chat route returned invalid event data");
|
|
1025
|
+
}
|
|
1026
|
+
if (event.text !== void 0 && typeof event.text !== "string") {
|
|
1027
|
+
throw new Error("chat route returned an invalid text event");
|
|
1028
|
+
}
|
|
1029
|
+
return {
|
|
1030
|
+
...typeof event.type === "string" ? { type: event.type } : {},
|
|
1031
|
+
...data ? { data } : {},
|
|
1032
|
+
...typeof event.text === "string" ? { text: event.text } : {}
|
|
1033
|
+
};
|
|
1034
|
+
}
|
|
1035
|
+
function toSandboxEvent(event) {
|
|
1036
|
+
if (!event) return null;
|
|
1037
|
+
if (event.type === "text") {
|
|
1038
|
+
if (event.text === void 0) throw new Error("chat route text event had no text");
|
|
1039
|
+
return {
|
|
1040
|
+
type: "message.part.updated",
|
|
1041
|
+
data: { part: { type: "text" }, delta: event.text }
|
|
1042
|
+
};
|
|
1043
|
+
}
|
|
1044
|
+
if (event.type === "reasoning" || event.type === "usage") return null;
|
|
1045
|
+
if (event.type === "input-required" && !event.data?.inputRequired) {
|
|
1046
|
+
const prompt = typeof event.data?.prompt === "string" ? event.data.prompt : void 0;
|
|
1047
|
+
return {
|
|
1048
|
+
type: "input-required",
|
|
1049
|
+
data: { inputRequired: prompt ? { prompt } : {} }
|
|
1050
|
+
};
|
|
1051
|
+
}
|
|
1052
|
+
return {
|
|
1053
|
+
...event.type ? { type: event.type } : {},
|
|
1054
|
+
...event.data ? { data: event.data } : {}
|
|
1055
|
+
};
|
|
1056
|
+
}
|
|
871
1057
|
|
|
872
1058
|
// src/chat-routes/model-failover-stream.ts
|
|
873
1059
|
var TERMINAL_FAILURE_TYPES = /* @__PURE__ */ new Set(["error", "session.run.failed"]);
|
|
@@ -3342,6 +3528,7 @@ export {
|
|
|
3342
3528
|
sniffBinary,
|
|
3343
3529
|
sniffMimeFromName,
|
|
3344
3530
|
storeSupportsDraftPersistence,
|
|
3531
|
+
streamChatRouteAsSandboxEvents,
|
|
3345
3532
|
streamWithModelFailover,
|
|
3346
3533
|
summarizeFailoverReason,
|
|
3347
3534
|
validateSandboxMentionPath,
|