@zhin.js/adapter-sandbox 1.1.20 → 1.1.22
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/CHANGELOG.md +64 -0
- package/README.md +55 -7
- package/adapters/sandbox/index.js +3 -12
- package/adapters/sandbox/index.ts +2 -12
- package/lib/adapter.d.ts +17 -0
- package/lib/adapter.js +17 -0
- package/lib/client.d.ts +7 -14
- package/lib/client.js +12 -11
- package/lib/endpoint.d.ts +16 -13
- package/lib/endpoint.js +171 -138
- package/lib/index.d.ts +2 -1
- package/lib/index.js +2 -1
- package/lib/protocol.d.ts +22 -1
- package/lib/protocol.js +34 -4
- package/package.json +17 -17
- package/pages/index/ConversationRecoveryPanel.js +131 -0
- package/pages/index/ConversationRecoveryPanel.tsx +134 -0
- package/pages/index/SandboxChat.js +319 -111
- package/pages/index/SandboxChat.tsx +356 -126
- package/pages/index/agentTrace.js +21 -10
- package/pages/index/agentTrace.ts +24 -10
- package/pages/index/conversationRecovery.js +40 -0
- package/pages/index/conversationRecovery.ts +68 -0
- package/pages/index/index.js +5 -5
- package/pages/index/index.tsx +5 -5
- package/pages/index/messages.js +24 -6
- package/pages/index/messages.ts +24 -6
- package/pages/index/playgroundState.js +51 -1
- package/pages/index/playgroundState.ts +61 -1
- package/pages/index/sandboxTransport.js +25 -0
- package/pages/index/sandboxTransport.ts +35 -0
- package/pages/index/useSandboxHistory.js +161 -0
- package/pages/index/useSandboxHistory.ts +164 -0
- package/pages/index/workspaceDirectory.js +49 -0
- package/pages/index/workspaceDirectory.ts +74 -0
- package/pages/index/workspaceDirectoryState.js +120 -0
- package/pages/index/workspaceDirectoryState.ts +124 -0
- package/src/adapter.ts +22 -0
- package/src/client.ts +16 -18
- package/src/endpoint.ts +179 -140
- package/src/index.ts +1 -1
- package/src/protocol.ts +59 -13
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// Generated by build-plugin-runtime-entries.mjs. Do not edit.
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
|
4
|
+
import { MarkdownContent } from '@zhin.js/client';
|
|
5
|
+
import { sandboxText } from './messages.js';
|
|
6
|
+
import { captureSandboxRequestContext } from './sandboxTransport.js';
|
|
7
|
+
import { prependResultPage, readConversationResults, readPendingInteractions, resultDeliveryStatus } from './conversationRecovery.js';
|
|
8
|
+
/** Mounted under an exact connection and scene key; its requests never outlive that owner. */
|
|
9
|
+
export default function ConversationRecoveryPanel(props) {
|
|
10
|
+
const { connectionId, sessionKey, locale } = props;
|
|
11
|
+
const [pending, setPending] = useState([]);
|
|
12
|
+
const [page, setPage] = useState(null);
|
|
13
|
+
const [pendingError, setPendingError] = useState(null);
|
|
14
|
+
const [resultError, setResultError] = useState(null);
|
|
15
|
+
const [submitted, setSubmitted] = useState(new Set());
|
|
16
|
+
const [loadingEarlier, setLoadingEarlier] = useState(false);
|
|
17
|
+
const retiredListener = useRef(props.onRetiredInteractions);
|
|
18
|
+
retiredListener.current = props.onRetiredInteractions;
|
|
19
|
+
const owner = useRef(null);
|
|
20
|
+
const reloadPending = useCallback(async (explicit = false) => {
|
|
21
|
+
const current = owner.current;
|
|
22
|
+
if (!current)
|
|
23
|
+
return;
|
|
24
|
+
// A later transport event must not reuse a GET started before that event.
|
|
25
|
+
current.pendingController?.abort();
|
|
26
|
+
const controller = new AbortController();
|
|
27
|
+
current.pendingController = controller;
|
|
28
|
+
const active = () => owner.current === current && current.pendingController === controller
|
|
29
|
+
&& !current.controller.signal.aborted && !controller.signal.aborted;
|
|
30
|
+
current.pending = (async () => {
|
|
31
|
+
try {
|
|
32
|
+
const data = await readPendingInteractions(connectionId, sessionKey, controller.signal, current.context);
|
|
33
|
+
if (!active())
|
|
34
|
+
return;
|
|
35
|
+
const pending = data?.pending ?? [];
|
|
36
|
+
const retired = data ? current.interactions.filter(previous => !pending.some(next => next.generation === previous.generation && next.id === previous.id)) : [];
|
|
37
|
+
if (data)
|
|
38
|
+
current.interactions = pending;
|
|
39
|
+
setPending(pending);
|
|
40
|
+
// Only an explicit successful authority read can release a still-current submitted choice.
|
|
41
|
+
const ids = new Set(pending.map(interaction => interaction.id));
|
|
42
|
+
setSubmitted(previous => new Set([...previous].filter(id => !explicit && ids.has(id))));
|
|
43
|
+
setPendingError(null);
|
|
44
|
+
if (retired.length)
|
|
45
|
+
retiredListener.current?.(retired);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (active())
|
|
49
|
+
setPendingError(error instanceof Error ? error.message : String(error));
|
|
50
|
+
}
|
|
51
|
+
finally {
|
|
52
|
+
if (current.pendingController === controller)
|
|
53
|
+
current.pending = undefined;
|
|
54
|
+
}
|
|
55
|
+
})();
|
|
56
|
+
return current.pending;
|
|
57
|
+
}, [connectionId, sessionKey]);
|
|
58
|
+
const reloadResults = useCallback(async () => {
|
|
59
|
+
const current = owner.current;
|
|
60
|
+
if (!current)
|
|
61
|
+
return;
|
|
62
|
+
if (current.results)
|
|
63
|
+
return current.results;
|
|
64
|
+
current.results = (async () => {
|
|
65
|
+
try {
|
|
66
|
+
const data = await readConversationResults(connectionId, sessionKey, current.controller.signal, undefined, current.context);
|
|
67
|
+
if (owner.current !== current || current.controller.signal.aborted)
|
|
68
|
+
return;
|
|
69
|
+
if (data && data.sessionKey !== sessionKey)
|
|
70
|
+
throw new Error('Result belongs to another conversation');
|
|
71
|
+
setPage(data);
|
|
72
|
+
setResultError(null);
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
if (owner.current === current && !current.controller.signal.aborted)
|
|
76
|
+
setResultError(error instanceof Error ? error.message : String(error));
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
current.results = undefined;
|
|
80
|
+
}
|
|
81
|
+
})();
|
|
82
|
+
return current.results;
|
|
83
|
+
}, [connectionId, sessionKey]);
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
const current = { controller: new AbortController(), context: props.requestContext ?? captureSandboxRequestContext(), interactions: [] };
|
|
86
|
+
owner.current = current;
|
|
87
|
+
return () => { current.pendingController?.abort(); if (owner.current === current)
|
|
88
|
+
owner.current = null; current.controller.abort(); };
|
|
89
|
+
}, [reloadPending, reloadResults, props.requestContext]);
|
|
90
|
+
useEffect(() => { void reloadPending(); }, [reloadPending, props.pendingRefreshEpoch, props.requestContext]);
|
|
91
|
+
useEffect(() => { void reloadResults(); }, [reloadResults, props.requestContext]);
|
|
92
|
+
const earlier = async () => {
|
|
93
|
+
const current = owner.current;
|
|
94
|
+
if (!current || !page?.hasMore || !page.nextCursor || current.results)
|
|
95
|
+
return;
|
|
96
|
+
const previous = page;
|
|
97
|
+
setLoadingEarlier(true);
|
|
98
|
+
current.results = (async () => {
|
|
99
|
+
try {
|
|
100
|
+
const next = await readConversationResults(connectionId, sessionKey, current.controller.signal, previous.nextCursor, current.context);
|
|
101
|
+
if (owner.current !== current || current.controller.signal.aborted)
|
|
102
|
+
return;
|
|
103
|
+
if (!next)
|
|
104
|
+
throw new Error('Result paging is unavailable');
|
|
105
|
+
setPage(prependResultPage(previous, next));
|
|
106
|
+
setResultError(null);
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
if (owner.current === current && !current.controller.signal.aborted)
|
|
110
|
+
setResultError(error instanceof Error ? error.message : String(error));
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
current.results = undefined;
|
|
114
|
+
if (owner.current === current)
|
|
115
|
+
setLoadingEarlier(false);
|
|
116
|
+
}
|
|
117
|
+
})();
|
|
118
|
+
return current.results;
|
|
119
|
+
};
|
|
120
|
+
const results = page?.results.filter(result => resultDeliveryStatus(result) !== 'sent') ?? [];
|
|
121
|
+
if (!pending.length && !results.length && !pendingError && !resultError && !page?.hasMore)
|
|
122
|
+
return null;
|
|
123
|
+
return _jsxs("div", { className: "agent-playground-recovery", hidden: props.hidden, children: [pendingError ? _jsxs("p", { role: "alert", children: [pendingError, " ", _jsx("button", { type: "button", onClick: () => void reloadPending(true), children: sandboxText(locale, '重试') })] }) : null, pending.length ? _jsx("button", { type: "button", onClick: () => void reloadPending(true), children: sandboxText(locale, '重新读取') }) : null, pending.map(interaction => _jsxs("section", { children: [_jsx("h3", { children: sandboxText(locale, '待处理操作') }), props.renderInteraction(interaction.segments, interaction.id, submitted.has(interaction.id), () => {
|
|
124
|
+
setSubmitted(previous => new Set([...previous, interaction.id]));
|
|
125
|
+
void reloadPending();
|
|
126
|
+
})] }, `${interaction.generation}:${interaction.id}`)), resultError ? _jsxs("p", { role: "alert", children: [resultError, " ", _jsx("button", { type: "button", onClick: () => void reloadResults(), children: sandboxText(locale, '重试') })] }) : null, page?.hasMore ? _jsx("button", { type: "button", disabled: loadingEarlier, onClick: () => void earlier(), children: sandboxText(locale, '加载更早结果') }) : null, results.map(result => {
|
|
127
|
+
const status = resultDeliveryStatus(result);
|
|
128
|
+
const resources = new Map(result.deliveries.flatMap(delivery => delivery.resources).map(resource => [resource.url, resource]));
|
|
129
|
+
return _jsxs("details", { children: [_jsxs("summary", { children: [sandboxText(locale, status === 'not_sent' ? '已完成,未投递' : status === 'partial' ? '已完成,部分内容未投递' : '已完成,投递结果未知'), _jsx("time", { children: new Date(result.completedAt).toLocaleString(locale) })] }), _jsx(MarkdownContent, { text: result.message }), [...resources.values()].map(resource => _jsx("a", { href: resource.url, target: "_blank", rel: "noopener noreferrer", children: resource.type }, resource.url))] }, result.turnId);
|
|
130
|
+
})] });
|
|
131
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { MarkdownContent, type MessageSegment } from '@zhin.js/client';
|
|
3
|
+
import { sandboxText, type SandboxLocale } from './messages.js';
|
|
4
|
+
import { captureSandboxRequestContext, type SandboxRequestContext } from './sandboxTransport.js';
|
|
5
|
+
import { prependResultPage, readConversationResults, readPendingInteractions, resultDeliveryStatus,
|
|
6
|
+
type PendingInteraction, type RecoveredResultPage } from './conversationRecovery.js';
|
|
7
|
+
|
|
8
|
+
export interface ConversationRecoveryProps {
|
|
9
|
+
readonly connectionId: string;
|
|
10
|
+
readonly sessionKey: string;
|
|
11
|
+
readonly locale: SandboxLocale;
|
|
12
|
+
readonly hidden?: boolean;
|
|
13
|
+
readonly requestContext?: SandboxRequestContext;
|
|
14
|
+
/** Existing transport activity invalidates this read; it is not an approval acknowledgement. */
|
|
15
|
+
readonly pendingRefreshEpoch?: number;
|
|
16
|
+
readonly onRetiredInteractions?: (interactions: readonly PendingInteraction[]) => void;
|
|
17
|
+
readonly renderInteraction: (segments: MessageSegment[], id: string, submitted: boolean, onSubmitted: () => void) => React.ReactNode;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Mounted under an exact connection and scene key; its requests never outlive that owner. */
|
|
21
|
+
export default function ConversationRecoveryPanel(props: ConversationRecoveryProps) {
|
|
22
|
+
const { connectionId, sessionKey, locale } = props;
|
|
23
|
+
const [pending, setPending] = useState<readonly PendingInteraction[]>([]);
|
|
24
|
+
const [page, setPage] = useState<RecoveredResultPage | null>(null);
|
|
25
|
+
const [pendingError, setPendingError] = useState<string | null>(null);
|
|
26
|
+
const [resultError, setResultError] = useState<string | null>(null);
|
|
27
|
+
const [submitted, setSubmitted] = useState<ReadonlySet<string>>(new Set());
|
|
28
|
+
const [loadingEarlier, setLoadingEarlier] = useState(false);
|
|
29
|
+
const retiredListener = useRef(props.onRetiredInteractions);
|
|
30
|
+
retiredListener.current = props.onRetiredInteractions;
|
|
31
|
+
const owner = useRef<{ controller: AbortController; context: SandboxRequestContext; interactions: readonly PendingInteraction[]; pendingController?: AbortController; pending?: Promise<void>; results?: Promise<void> } | null>(null);
|
|
32
|
+
const reloadPending = useCallback(async (explicit = false) => {
|
|
33
|
+
const current = owner.current;
|
|
34
|
+
if (!current) return;
|
|
35
|
+
// A later transport event must not reuse a GET started before that event.
|
|
36
|
+
current.pendingController?.abort();
|
|
37
|
+
const controller = new AbortController();
|
|
38
|
+
current.pendingController = controller;
|
|
39
|
+
const active = () => owner.current === current && current.pendingController === controller
|
|
40
|
+
&& !current.controller.signal.aborted && !controller.signal.aborted;
|
|
41
|
+
current.pending = (async () => {
|
|
42
|
+
try {
|
|
43
|
+
const data = await readPendingInteractions(connectionId, sessionKey, controller.signal, current.context);
|
|
44
|
+
if (!active()) return;
|
|
45
|
+
const pending = data?.pending ?? [];
|
|
46
|
+
const retired = data ? current.interactions.filter(previous => !pending.some(next => next.generation === previous.generation && next.id === previous.id)) : [];
|
|
47
|
+
if (data) current.interactions = pending;
|
|
48
|
+
setPending(pending);
|
|
49
|
+
// Only an explicit successful authority read can release a still-current submitted choice.
|
|
50
|
+
const ids = new Set(pending.map(interaction => interaction.id));
|
|
51
|
+
setSubmitted(previous => new Set([...previous].filter(id => !explicit && ids.has(id))));
|
|
52
|
+
setPendingError(null);
|
|
53
|
+
if (retired.length) retiredListener.current?.(retired);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
if (active()) setPendingError(error instanceof Error ? error.message : String(error));
|
|
56
|
+
} finally { if (current.pendingController === controller) current.pending = undefined; }
|
|
57
|
+
})();
|
|
58
|
+
return current.pending;
|
|
59
|
+
}, [connectionId, sessionKey]);
|
|
60
|
+
|
|
61
|
+
const reloadResults = useCallback(async () => {
|
|
62
|
+
const current = owner.current;
|
|
63
|
+
if (!current) return;
|
|
64
|
+
if (current.results) return current.results;
|
|
65
|
+
current.results = (async () => {
|
|
66
|
+
try {
|
|
67
|
+
const data = await readConversationResults(connectionId, sessionKey, current.controller.signal, undefined, current.context);
|
|
68
|
+
if (owner.current !== current || current.controller.signal.aborted) return;
|
|
69
|
+
if (data && data.sessionKey !== sessionKey) throw new Error('Result belongs to another conversation');
|
|
70
|
+
setPage(data);
|
|
71
|
+
setResultError(null);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if (owner.current === current && !current.controller.signal.aborted) setResultError(error instanceof Error ? error.message : String(error));
|
|
74
|
+
} finally { current.results = undefined; }
|
|
75
|
+
})();
|
|
76
|
+
return current.results;
|
|
77
|
+
}, [connectionId, sessionKey]);
|
|
78
|
+
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
const current: NonNullable<typeof owner.current> = { controller: new AbortController(), context: props.requestContext ?? captureSandboxRequestContext(), interactions: [] };
|
|
81
|
+
owner.current = current;
|
|
82
|
+
return () => { current.pendingController?.abort(); if (owner.current === current) owner.current = null; current.controller.abort(); };
|
|
83
|
+
}, [reloadPending, reloadResults, props.requestContext]);
|
|
84
|
+
useEffect(() => { void reloadPending(); }, [reloadPending, props.pendingRefreshEpoch, props.requestContext]);
|
|
85
|
+
useEffect(() => { void reloadResults(); }, [reloadResults, props.requestContext]);
|
|
86
|
+
|
|
87
|
+
const earlier = async () => {
|
|
88
|
+
const current = owner.current;
|
|
89
|
+
if (!current || !page?.hasMore || !page.nextCursor || current.results) return;
|
|
90
|
+
const previous = page;
|
|
91
|
+
setLoadingEarlier(true);
|
|
92
|
+
current.results = (async () => {
|
|
93
|
+
try {
|
|
94
|
+
const next = await readConversationResults(connectionId, sessionKey, current.controller.signal, previous.nextCursor!, current.context);
|
|
95
|
+
if (owner.current !== current || current.controller.signal.aborted) return;
|
|
96
|
+
if (!next) throw new Error('Result paging is unavailable');
|
|
97
|
+
setPage(prependResultPage(previous, next));
|
|
98
|
+
setResultError(null);
|
|
99
|
+
} catch (error) {
|
|
100
|
+
if (owner.current === current && !current.controller.signal.aborted) setResultError(error instanceof Error ? error.message : String(error));
|
|
101
|
+
} finally {
|
|
102
|
+
current.results = undefined;
|
|
103
|
+
if (owner.current === current) setLoadingEarlier(false);
|
|
104
|
+
}
|
|
105
|
+
})();
|
|
106
|
+
return current.results;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const results = page?.results.filter(result => resultDeliveryStatus(result) !== 'sent') ?? [];
|
|
110
|
+
if (!pending.length && !results.length && !pendingError && !resultError && !page?.hasMore) return null;
|
|
111
|
+
return <div className="agent-playground-recovery" hidden={props.hidden}>
|
|
112
|
+
{pendingError ? <p role="alert">{pendingError} <button type="button" onClick={() => void reloadPending(true)}>{sandboxText(locale, '重试')}</button></p> : null}
|
|
113
|
+
{pending.length ? <button type="button" onClick={() => void reloadPending(true)}>{sandboxText(locale, '重新读取')}</button> : null}
|
|
114
|
+
{pending.map(interaction => <section key={`${interaction.generation}:${interaction.id}`}>
|
|
115
|
+
<h3>{sandboxText(locale, '待处理操作')}</h3>
|
|
116
|
+
{props.renderInteraction(interaction.segments, interaction.id, submitted.has(interaction.id), () => {
|
|
117
|
+
setSubmitted(previous => new Set([...previous, interaction.id]));
|
|
118
|
+
void reloadPending();
|
|
119
|
+
})}
|
|
120
|
+
</section>)}
|
|
121
|
+
{resultError ? <p role="alert">{resultError} <button type="button" onClick={() => void reloadResults()}>{sandboxText(locale, '重试')}</button></p> : null}
|
|
122
|
+
{page?.hasMore ? <button type="button" disabled={loadingEarlier} onClick={() => void earlier()}>{sandboxText(locale, '加载更早结果')}</button> : null}
|
|
123
|
+
{results.map(result => {
|
|
124
|
+
const status = resultDeliveryStatus(result);
|
|
125
|
+
const resources = new Map(result.deliveries.flatMap(delivery => delivery.resources).map(resource => [resource.url, resource]));
|
|
126
|
+
return <details key={result.turnId}>
|
|
127
|
+
<summary>{sandboxText(locale, status === 'not_sent' ? '已完成,未投递' : status === 'partial' ? '已完成,部分内容未投递' : '已完成,投递结果未知')}
|
|
128
|
+
<time>{new Date(result.completedAt).toLocaleString(locale)}</time></summary>
|
|
129
|
+
<MarkdownContent text={result.message} />
|
|
130
|
+
{[...resources.values()].map(resource => <a key={resource.url} href={resource.url} target="_blank" rel="noopener noreferrer">{resource.type}</a>)}
|
|
131
|
+
</details>;
|
|
132
|
+
})}
|
|
133
|
+
</div>;
|
|
134
|
+
}
|