@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.
@@ -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,
@@ -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) => {
@@ -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 { t } from "@/i18n";
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 handleSubmit = useCallback(async () => {
1755
- if (isCreating) return;
1756
- const rawText = inputValue.trim();
1757
- if (!rawText && files.length === 0) return;
1758
- trackBehavior('composer_send', {
1759
- empty: false,
1760
- home: true,
1761
- mode: mode === 'plan' ? 'plan' : 'agent',
1762
- execPolicy,
1763
- length: rawText.length,
1764
- });
1765
-
1766
- const mentionedAgentIds = mentionReferences
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
- setIsCreating(true);
1786
- setCreateError(null);
1787
- try {
1788
- const id = await ctx.createChat({
1789
- ...(selectedProjectId && showProjectsChrome ? { projectId: selectedProjectId } : {}),
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
- if (!id) throw new Error(t("Failed to create the chat. Please try again."));
1793
- // 会话已建,把附件落进会话空间(与 LocalChatPanel 同一套语义):
1794
- // 所有文件都写落盘路径引用(agent 用 local_read_file 读回);仅图片
1795
- // 额外进 metadata.images 走多模态,非图片文件不会被图片逻辑吞掉。
1796
- const { files: resolvedFiles, failures } = await saveChatAttachments(id, files);
1797
- if (failures.length > 0) {
1798
- // 落地页一旦跳转就没法让用户重试了,所以这里宁可中止本次发送:
1799
- // 删掉刚建的空会话,保留输入框(含失败文件),把原因显示出来。
1800
- await ctx.deleteChat(id);
1801
- setCreateError(formatAttachmentFailures(failures));
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
- const content = appendAttachmentRefs(rawText, resolvedFiles);
1807
- const imageAttachments = collectImageAttachments(resolvedFiles);
1808
- const metadata = {
1809
- ...baseMetadata,
1810
- ...(imageAttachments.length > 0 ? { images: imageAttachments } : {}),
1811
- };
1812
-
1813
- setPendingFirstMessage({
1814
- chatId: id,
1815
- content,
1816
- metadata: Object.keys(metadata).length > 0 ? metadata : undefined,
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…")}