Repository navigation
Expand file tree
/
Copy pathComposer.tsx
More file actions
189 lines (189 loc) · 6.4 KB
/
Copy pathComposer.tsx
File metadata and controls
189 lines (189 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { tr } from "./lib/i18n";
import { useEffect, useState } from "react";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import {
AgentCoreError,
randomUuid,
type AgentSession,
} from "@oac/agents-client";
import { ArrowUp, Square } from "lucide-react";
import { api } from "./lib/api";
import { product, ProductError } from "./lib/product";
import { ErrorNotice } from "./components/shared";
export function Composer({
recordId,
session,
turnId,
attachment = "",
onPendingChange,
}: {
recordId: string;
session: AgentSession;
turnId?: string;
attachment?: string;
onPendingChange?: (pending: boolean) => void;
}) {
const cache = useQueryClient();
const storageKey = `oac-example-message-${session.id}`;
const [pending, setPending] = useState<{
key: string;
text: string;
} | null>(() => {
try {
return JSON.parse(sessionStorage.getItem(storageKey) || "null");
} catch {
return null;
}
});
useEffect(() => {
onPendingChange?.(Boolean(pending));
}, [pending, onPendingChange]);
const draftKey = `oac-example-draft-${session.id}`;
const [text, setText] = useState(
() => pending?.text || sessionStorage.getItem(draftKey) || "",
);
useEffect(() => {
if (text) sessionStorage.setItem(draftKey, text);
else sessionStorage.removeItem(draftKey);
}, [draftKey, text]);
useEffect(() => {
if (attachment)
setText((previous) =>
tr("{{value0}}{{value1}}请处理文件:{{value2}}", {
value0: previous,
value1: previous ? "\n" : "",
value2: attachment,
}),
);
}, [attachment]);
const [cancelKey, setCancelKey] = useState(randomUuid);
const [cancelRequested, setCancelRequested] = useState(false);
useEffect(() => {
setCancelKey(randomUuid());
setCancelRequested(false);
}, [turnId]);
const running =
session.status === "in_progress" || session.status === "requires_action";
// Input during an active Turn reaches the running task; questions are answered on their card.
const waiting = session.status === "requires_action";
const refresh = () => {
void cache.invalidateQueries({ queryKey: ["session", session.id] });
void cache.invalidateQueries({ queryKey: ["sessions"] });
void cache.invalidateQueries({ queryKey: ["session-record", recordId] });
};
const send = useMutation({
mutationFn: async () => {
const operation = pending || { key: randomUuid(), text };
sessionStorage.setItem(storageKey, JSON.stringify(operation));
setPending(operation);
// Parsar records the Turn so it can watch it and recover a stall.
await product(`sessions/${recordId}/messages`, "POST", {
text: operation.text,
key: operation.key,
});
},
onSuccess: () => {
sessionStorage.removeItem(draftKey);
setText("");
setPending(null);
sessionStorage.removeItem(storageKey);
refresh();
},
onError: (error) => {
if (
(error instanceof AgentCoreError || error instanceof ProductError) &&
[400, 401, 403, 404, 413, 422].includes(error.status)
) {
sessionStorage.removeItem(storageKey);
setPending(null);
}
},
});
const cancel = useMutation({
mutationFn: () => api.cancelTurn(session.id, cancelKey),
onSuccess: () => {
setCancelRequested(true);
refresh();
},
});
return (
<div className="mx-auto w-full max-w-3xl px-6 pb-7 pt-3">
<ErrorNotice error={send.error || cancel.error} />
{cancelRequested && running && (
<p role="status" className="pb-3 text-sm text-fg-muted">
{tr("已请求取消,等待执行结束。")}
</p>
)}
<form
className="rounded-3xl border border-line bg-surface p-3 app-shadow-floating transition-colors focus-within:border-line-strong"
onSubmit={(e) => {
e.preventDefault();
if (text.trim() && !waiting && !send.isPending) send.mutate();
}}
>
<textarea
aria-label={tr("继续对话")}
rows={2}
value={text}
onKeyDown={(e) => {
if (
e.key === "Enter" &&
!e.shiftKey &&
!e.nativeEvent.isComposing
) {
e.preventDefault();
e.currentTarget.form?.requestSubmit();
}
}}
disabled={
waiting ||
send.isPending ||
session.status === "failed" ||
!!pending
}
onChange={(e) => setText(e.target.value)}
placeholder={
waiting
? tr("请先回答上方的问题")
: running
? tr("补充说明,Agent 会在当前任务中看到\u2026")
: session.status === "failed"
? tr("当前会话已失败,请开始新对话")
: tr("继续对话\u2026")
}
className="block max-h-60 w-full resize-none bg-transparent px-3 pt-2 text-base leading-relaxed outline-none placeholder:text-fg-subtle disabled:opacity-60"
/>
<div className="flex justify-end gap-2 pt-1">
{running && (
<button
type="button"
aria-label={tr("取消执行")}
title={tr("停止")}
disabled={cancel.isPending || cancelRequested}
onClick={() => cancel.mutate()}
className={
text.trim() && !waiting
? "flex h-9 w-9 items-center justify-center rounded-full border border-line-strong text-fg hover:app-hover disabled:opacity-30"
: "flex h-9 w-9 items-center justify-center rounded-full bg-surface-emphasis text-fg-on-emphasis disabled:opacity-30"
}
>
<Square className="h-4 w-4" fill="currentColor" />
</button>
)}
{(!running || (text.trim() && !waiting)) && (
<button
type="submit"
aria-label={pending ? tr("重试发送") : tr("发送")}
disabled={
!text.trim() || send.isPending || session.status === "failed"
}
className="flex h-9 w-9 items-center justify-center rounded-full bg-surface-emphasis text-fg-on-emphasis disabled:opacity-30"
>
<ArrowUp className="h-5 w-5" />
</button>
)}
</div>
</form>
</div>
);
}