@steerable/agent-shell-web 0.12.3 → 0.12.5
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/package.json +4 -4
- package/src/AppShell.tsx +14 -2
- package/src/fixtures/browser-dev-data.json +117 -199
- package/src/lib/browser-dev-host-mock.ts +873 -338
- package/src/lib/demo-playbook.ts +766 -0
- package/src/lib/host-bridge.ts +21 -0
- package/src/lib/http-bridge.ts +35 -0
- package/src/pages/AgentPage.tsx +128 -67
package/src/lib/host-bridge.ts
CHANGED
|
@@ -16,6 +16,20 @@ export type LocalBackendRequestInput = {
|
|
|
16
16
|
body?: unknown;
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
+
export type LocalBackendBinaryRequestInput = {
|
|
20
|
+
method?: 'GET' | 'POST';
|
|
21
|
+
path: string;
|
|
22
|
+
body?: unknown;
|
|
23
|
+
signal?: AbortSignal;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export interface LocalBackendBinaryResponse {
|
|
27
|
+
status: number;
|
|
28
|
+
headers: Headers;
|
|
29
|
+
arrayBuffer: () => Promise<ArrayBuffer>;
|
|
30
|
+
blob: () => Promise<Blob>;
|
|
31
|
+
}
|
|
32
|
+
|
|
19
33
|
export type LocalBackendStreamEvent =
|
|
20
34
|
| { type: 'data'; chunk: string; parsed?: SSEEvent }
|
|
21
35
|
| { type: 'end'; status: number }
|
|
@@ -180,6 +194,13 @@ export interface HostBridge {
|
|
|
180
194
|
};
|
|
181
195
|
localBackend: {
|
|
182
196
|
request: <T>(input: LocalBackendRequestInput) => Promise<T>;
|
|
197
|
+
requestBinary?: (
|
|
198
|
+
input: LocalBackendBinaryRequestInput,
|
|
199
|
+
) => Promise<LocalBackendBinaryResponse>;
|
|
200
|
+
resolveAuthorizedFetch?: (path: string) => {
|
|
201
|
+
url: string;
|
|
202
|
+
headers: Record<string, string>;
|
|
203
|
+
};
|
|
183
204
|
startStream: (
|
|
184
205
|
input: LocalBackendRequestInput,
|
|
185
206
|
onEvent: (payload: LocalBackendStreamEvent) => void,
|
package/src/lib/http-bridge.ts
CHANGED
|
@@ -18,6 +18,8 @@ import type {
|
|
|
18
18
|
ApprovalPromptRequest,
|
|
19
19
|
AskUserPromptRequest,
|
|
20
20
|
HostBridge,
|
|
21
|
+
LocalBackendBinaryRequestInput,
|
|
22
|
+
LocalBackendBinaryResponse,
|
|
21
23
|
LocalBackendRequestInput,
|
|
22
24
|
LocalBackendStreamEvent,
|
|
23
25
|
TerminalSession,
|
|
@@ -73,6 +75,34 @@ async function http<T>(method: string, path: string, body?: unknown): Promise<T>
|
|
|
73
75
|
return data;
|
|
74
76
|
}
|
|
75
77
|
|
|
78
|
+
async function httpBinary(
|
|
79
|
+
input: LocalBackendBinaryRequestInput,
|
|
80
|
+
): Promise<LocalBackendBinaryResponse> {
|
|
81
|
+
const method = input.method ?? 'GET';
|
|
82
|
+
const res = await fetch(input.path, {
|
|
83
|
+
method,
|
|
84
|
+
headers: {
|
|
85
|
+
...(input.body !== undefined ? { 'Content-Type': 'application/json' } : {}),
|
|
86
|
+
...authHeaders(),
|
|
87
|
+
},
|
|
88
|
+
body: input.body !== undefined ? JSON.stringify(input.body) : undefined,
|
|
89
|
+
signal: input.signal,
|
|
90
|
+
});
|
|
91
|
+
if (!res.ok) {
|
|
92
|
+
const data = await res.json().catch(() => null);
|
|
93
|
+
const err = new Error(httpErrorMessage(data, res.status)) as Error & { status?: number; data?: unknown };
|
|
94
|
+
err.status = res.status;
|
|
95
|
+
err.data = data;
|
|
96
|
+
throw err;
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
status: res.status,
|
|
100
|
+
headers: res.headers,
|
|
101
|
+
arrayBuffer: () => res.arrayBuffer(),
|
|
102
|
+
blob: () => res.blob(),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
76
106
|
// ---------------------------------------------------------------------------
|
|
77
107
|
// 事件总线:一条 EventSource 承载所有 channel(channel 名与 IPC 通道一致)。
|
|
78
108
|
// ---------------------------------------------------------------------------
|
|
@@ -256,6 +286,11 @@ export function createHttpBridge(): HostBridge {
|
|
|
256
286
|
localBackend: {
|
|
257
287
|
request: <T>(input: LocalBackendRequestInput) =>
|
|
258
288
|
http<T>(input.method, input.path, input.body),
|
|
289
|
+
requestBinary: (input: LocalBackendBinaryRequestInput) => httpBinary(input),
|
|
290
|
+
resolveAuthorizedFetch: (path: string) => ({
|
|
291
|
+
url: path,
|
|
292
|
+
headers: authHeaders(),
|
|
293
|
+
}),
|
|
259
294
|
startStream,
|
|
260
295
|
cancelStream,
|
|
261
296
|
steerChat: async (chatId, content, options) => {
|
package/src/pages/AgentPage.tsx
CHANGED
|
@@ -70,7 +70,13 @@ import {
|
|
|
70
70
|
} from "@/lib/pending-first-message";
|
|
71
71
|
import { readOrchestrationSetting } from "@/lib/orchestration-settings";
|
|
72
72
|
import { pickDefaultAgentId } from "@/brand";
|
|
73
|
-
import {
|
|
73
|
+
import { isDemoMode } from "@/lib/demo-flag";
|
|
74
|
+
import {
|
|
75
|
+
DEMO_STARTER_PROMPTS_EN,
|
|
76
|
+
DEMO_STARTER_PROMPTS_ZH,
|
|
77
|
+
type DemoStarterPrompt,
|
|
78
|
+
} from "@/lib/demo-playbook";
|
|
79
|
+
import { getLocale, t } from "@/i18n";
|
|
74
80
|
import {
|
|
75
81
|
appendAttachmentRefs,
|
|
76
82
|
collectImageAttachments,
|
|
@@ -1751,77 +1757,106 @@ function EmptyChatGate() {
|
|
|
1751
1757
|
[ctx],
|
|
1752
1758
|
);
|
|
1753
1759
|
|
|
1754
|
-
const
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
.filter((ref) => ref.type === "agent")
|
|
1768
|
-
.map((ref) => ref.id);
|
|
1769
|
-
const referencedChatIds = mentionReferences
|
|
1770
|
-
.filter((ref) => ref.type === "chat")
|
|
1771
|
-
.map((ref) => ref.id);
|
|
1772
|
-
// 文件路径引用与图像元数据在会话建好、附件落盘之后再装配(见下)——
|
|
1773
|
-
// 落地页提交时还没有 chatId,不能像旧代码那样直接用 `f.path`(浏览器
|
|
1774
|
-
// 模式下它是空串,会把一条空引用写进正文,模型以为收到了文件却读不到)。
|
|
1775
|
-
const baseMetadata = {
|
|
1776
|
-
...(mode === "plan" ? { mode: "plan" as const } : {}),
|
|
1777
|
-
...(execPolicy === "full" ? { execPolicy: "full" as const } : {}),
|
|
1778
|
-
...(ctx.selectedAgentId ? { agentId: ctx.selectedAgentId } : {}),
|
|
1779
|
-
...(mentionedAgentIds.length > 0 ? { mentionedAgentIds } : {}),
|
|
1780
|
-
...(referencedChatIds.length > 0 ? { referencedChatIds } : {}),
|
|
1781
|
-
...(modelOverride ? { model: modelOverride } : {}),
|
|
1782
|
-
...(effortOverride ? { reasoningEffort: effortOverride } : {}),
|
|
1783
|
-
};
|
|
1760
|
+
const submitPrompt = useCallback(
|
|
1761
|
+
async (rawText: string, overrideMode?: ChatMode) => {
|
|
1762
|
+
if (isCreating) return;
|
|
1763
|
+
const trimmed = rawText.trim();
|
|
1764
|
+
const effectiveMode = overrideMode ?? mode;
|
|
1765
|
+
if (!trimmed && files.length === 0) return;
|
|
1766
|
+
trackBehavior('composer_send', {
|
|
1767
|
+
empty: false,
|
|
1768
|
+
home: true,
|
|
1769
|
+
mode: effectiveMode === 'plan' ? 'plan' : 'agent',
|
|
1770
|
+
execPolicy,
|
|
1771
|
+
length: trimmed.length,
|
|
1772
|
+
});
|
|
1784
1773
|
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
const
|
|
1789
|
-
|
|
1774
|
+
const mentionedAgentIds = mentionReferences
|
|
1775
|
+
.filter((ref) => ref.type === "agent")
|
|
1776
|
+
.map((ref) => ref.id);
|
|
1777
|
+
const referencedChatIds = mentionReferences
|
|
1778
|
+
.filter((ref) => ref.type === "chat")
|
|
1779
|
+
.map((ref) => ref.id);
|
|
1780
|
+
// 文件路径引用与图像元数据在会话建好、附件落盘之后再装配(见下)——
|
|
1781
|
+
// 落地页提交时还没有 chatId,不能像旧代码那样直接用 `f.path`(浏览器
|
|
1782
|
+
// 模式下它是空串,会把一条空引用写进正文,模型以为收到了文件却读不到)。
|
|
1783
|
+
const baseMetadata = {
|
|
1784
|
+
...(effectiveMode === "plan" ? { mode: "plan" as const } : {}),
|
|
1785
|
+
...(execPolicy === "full" ? { execPolicy: "full" as const } : {}),
|
|
1790
1786
|
...(ctx.selectedAgentId ? { agentId: ctx.selectedAgentId } : {}),
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
await ctx.
|
|
1801
|
-
|
|
1787
|
+
...(mentionedAgentIds.length > 0 ? { mentionedAgentIds } : {}),
|
|
1788
|
+
...(referencedChatIds.length > 0 ? { referencedChatIds } : {}),
|
|
1789
|
+
...(modelOverride ? { model: modelOverride } : {}),
|
|
1790
|
+
...(effortOverride ? { reasoningEffort: effortOverride } : {}),
|
|
1791
|
+
};
|
|
1792
|
+
|
|
1793
|
+
setIsCreating(true);
|
|
1794
|
+
setCreateError(null);
|
|
1795
|
+
try {
|
|
1796
|
+
const id = await ctx.createChat({
|
|
1797
|
+
...(selectedProjectId && showProjectsChrome ? { projectId: selectedProjectId } : {}),
|
|
1798
|
+
...(ctx.selectedAgentId ? { agentId: ctx.selectedAgentId } : {}),
|
|
1799
|
+
});
|
|
1800
|
+
if (!id) throw new Error(t("Failed to create the chat. Please try again."));
|
|
1801
|
+
// 会话已建,把附件落进会话空间(与 LocalChatPanel 同一套语义):
|
|
1802
|
+
// 所有文件都写落盘路径引用(agent 用 local_read_file 读回);仅图片
|
|
1803
|
+
// 额外进 metadata.images 走多模态,非图片文件不会被图片逻辑吞掉。
|
|
1804
|
+
const { files: resolvedFiles, failures } = await saveChatAttachments(id, files);
|
|
1805
|
+
if (failures.length > 0) {
|
|
1806
|
+
// 落地页一旦跳转就没法让用户重试了,所以这里宁可中止本次发送:
|
|
1807
|
+
// 删掉刚建的空会话,保留输入框(含失败文件),把原因显示出来。
|
|
1808
|
+
await ctx.deleteChat(id);
|
|
1809
|
+
setCreateError(formatAttachmentFailures(failures));
|
|
1810
|
+
setIsCreating(false);
|
|
1811
|
+
return;
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
const content = appendAttachmentRefs(trimmed, resolvedFiles);
|
|
1815
|
+
const imageAttachments = collectImageAttachments(resolvedFiles);
|
|
1816
|
+
const metadata = {
|
|
1817
|
+
...baseMetadata,
|
|
1818
|
+
...(imageAttachments.length > 0 ? { images: imageAttachments } : {}),
|
|
1819
|
+
};
|
|
1820
|
+
|
|
1821
|
+
setPendingFirstMessage({
|
|
1822
|
+
chatId: id,
|
|
1823
|
+
content,
|
|
1824
|
+
metadata: Object.keys(metadata).length > 0 ? metadata : undefined,
|
|
1825
|
+
});
|
|
1826
|
+
navigate(`/agent/${id}`);
|
|
1827
|
+
// 跳转成功后组件即卸载,不用清 isCreating;失败才恢复可交互。
|
|
1828
|
+
} catch (err) {
|
|
1829
|
+
setCreateError(err instanceof Error ? err.message : String(err));
|
|
1802
1830
|
setIsCreating(false);
|
|
1803
|
-
return;
|
|
1804
1831
|
}
|
|
1832
|
+
},
|
|
1833
|
+
[
|
|
1834
|
+
inputValue,
|
|
1835
|
+
files,
|
|
1836
|
+
mentionReferences,
|
|
1837
|
+
mode,
|
|
1838
|
+
execPolicy,
|
|
1839
|
+
modelOverride,
|
|
1840
|
+
effortOverride,
|
|
1841
|
+
isCreating,
|
|
1842
|
+
selectedProjectId,
|
|
1843
|
+
showProjectsChrome,
|
|
1844
|
+
ctx,
|
|
1845
|
+
navigate,
|
|
1846
|
+
],
|
|
1847
|
+
);
|
|
1805
1848
|
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
});
|
|
1818
|
-
navigate(`/agent/${id}`);
|
|
1819
|
-
// 跳转成功后组件即卸载,不用清 isCreating;失败才恢复可交互。
|
|
1820
|
-
} catch (err) {
|
|
1821
|
-
setCreateError(err instanceof Error ? err.message : String(err));
|
|
1822
|
-
setIsCreating(false);
|
|
1823
|
-
}
|
|
1824
|
-
}, [inputValue, files, mentionReferences, mode, execPolicy, modelOverride, effortOverride, isCreating, selectedProjectId, ctx, navigate]);
|
|
1849
|
+
const handleSubmit = useCallback(async () => {
|
|
1850
|
+
await submitPrompt(inputValue);
|
|
1851
|
+
}, [submitPrompt, inputValue]);
|
|
1852
|
+
|
|
1853
|
+
const demoStarters: readonly DemoStarterPrompt[] = useMemo(() => {
|
|
1854
|
+
if (!isDemoMode()) return [];
|
|
1855
|
+
const prefersZh =
|
|
1856
|
+
getLocale().startsWith('zh') ||
|
|
1857
|
+
(typeof navigator !== 'undefined' && /^zh/i.test(navigator.language || ''));
|
|
1858
|
+
return prefersZh ? DEMO_STARTER_PROMPTS_ZH : DEMO_STARTER_PROMPTS_EN;
|
|
1859
|
+
}, []);
|
|
1825
1860
|
|
|
1826
1861
|
return (
|
|
1827
1862
|
<div
|
|
@@ -1887,6 +1922,32 @@ function EmptyChatGate() {
|
|
|
1887
1922
|
onMentionReferencesChange={setMentionReferences}
|
|
1888
1923
|
/>
|
|
1889
1924
|
</div>
|
|
1925
|
+
{demoStarters.length > 0 && (
|
|
1926
|
+
<div
|
|
1927
|
+
className="grid w-full grid-cols-1 gap-2 sm:grid-cols-2"
|
|
1928
|
+
data-testid="demo-playbook-starters"
|
|
1929
|
+
>
|
|
1930
|
+
{demoStarters.map((item) => (
|
|
1931
|
+
<button
|
|
1932
|
+
key={item.id}
|
|
1933
|
+
type="button"
|
|
1934
|
+
disabled={isCreating}
|
|
1935
|
+
onClick={() => void submitPrompt(item.prompt, item.mode)}
|
|
1936
|
+
className="group flex flex-col items-start gap-1 rounded-agent-md border border-agent-border bg-agent-canvas px-3 py-2.5 text-left transition-colors hover:border-agent-foreground/40 hover:bg-agent-muted/60 disabled:cursor-not-allowed disabled:opacity-60"
|
|
1937
|
+
>
|
|
1938
|
+
<span className="flex w-full items-center justify-between gap-2 text-xs font-semibold text-agent-foreground">
|
|
1939
|
+
<span>{item.title}</span>
|
|
1940
|
+
<span className="shrink-0 rounded border border-agent-border bg-agent-muted px-1.5 py-0.5 font-mono text-[10px] font-medium text-agent-muted-foreground">
|
|
1941
|
+
{item.tag}
|
|
1942
|
+
</span>
|
|
1943
|
+
</span>
|
|
1944
|
+
<span className="line-clamp-2 text-[11px] leading-relaxed text-agent-muted-foreground">
|
|
1945
|
+
{item.summary}
|
|
1946
|
+
</span>
|
|
1947
|
+
</button>
|
|
1948
|
+
))}
|
|
1949
|
+
</div>
|
|
1950
|
+
)}
|
|
1890
1951
|
{isCreating && (
|
|
1891
1952
|
<p className="text-xs text-agent-muted-foreground">
|
|
1892
1953
|
{t("Creating chat…")}
|