Repository navigation
Expand file tree
/
Copy pathapi.ts
More file actions
91 lines (84 loc) · 3.5 KB
/
Copy pathapi.ts
File metadata and controls
91 lines (84 loc) · 3.5 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
import { tr, currentLanguage } from "./i18n";
import {
OpenAIAgentsClient,
type AgentSession,
type AgentTurn,
type ListPage,
type PageOptions,
type SessionArtifact,
} from "@oac/agents-client";
import type { StatusKind } from "../components/ui/status-icon";
export const api = new OpenAIAgentsClient();
export const application = "parsar-example";
export async function readHistory<T>(
read: (options: PageOptions) => Promise<ListPage<T>>,
signal: AbortSignal,
) {
const data: T[] = [];
let after: string | undefined;
for (let pageNumber = 0; pageNumber < 100; pageNumber++) {
const page = await read({ limit: 100, order: "asc", after, signal });
data.push(...page.data);
if (!page.has_more) return data;
if (!page.last_id || page.last_id === after)
throw new Error(tr("Core 返回了无效的历史分页游标。"));
after = page.last_id;
}
throw new Error(tr("历史超过 10,000 条,当前示例无法完整展示。"));
}
export function sessionState(
session: AgentSession,
latest?: AgentTurn,
): { label: string; icon: StatusKind } {
if (session.status === "failed") return { label: tr("失败"), icon: "failed" };
if (session.status === "requires_action")
return { label: tr("等待处理"), icon: "interrupted" };
if (session.status === "in_progress")
return { label: tr("执行中"), icon: "running" };
if (latest?.status === "cancelled")
return { label: tr("已取消"), icon: "cancelled" };
if (latest?.status === "failed") return { label: tr("失败"), icon: "failed" };
if (latest?.status === "completed")
return { label: tr("已完成"), icon: "completed" };
return { label: tr("空闲"), icon: "queued" };
}
export function sessionTitle(session: AgentSession) {
return session.metadata.title || session.agent.name || tr("未命名会话");
}
export const errorText = (error: unknown) =>
error instanceof Error
? error.message
: typeof error === "string"
? error
: tr("请求失败,请重试。");
export const dateTime = (seconds: number) =>
new Intl.DateTimeFormat(currentLanguage === "en" ? "en-US" : "zh-CN", {
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
}).format(seconds * 1000);
// Saves one Session Artifact; Core keeps Artifacts after the live workspace is gone.
export async function downloadArtifact(sessionId: string, file: { id: string; path: string }) {
const response = await fetch(
`/v1/agents/sessions/${encodeURIComponent(sessionId)}/artifacts/${encodeURIComponent(file.id)}/content`,
);
if (!response.ok) {
const value = await response.json().catch(() => ({}));
throw new Error(value.error?.message || tr("下载失败,请重试。"));
}
const url = URL.createObjectURL(await response.blob());
const link = document.createElement("a");
link.href = url;
link.download = file.path.split("/").at(-1) || "artifact";
link.click();
setTimeout(() => URL.revokeObjectURL(url), 1000);
}
export async function listSessionArtifacts(sessionId: string, options: PageOptions): Promise<ListPage<SessionArtifact>> {
const params = new URLSearchParams({ limit: String(options.limit || 50), order: options.order || "desc" });
if (options.after) params.set("after", options.after);
const response = await fetch(`/v1/agents/sessions/${encodeURIComponent(sessionId)}/artifacts?${params}`, { signal: options.signal });
const value = await response.json();
if (!response.ok) throw new Error(value.error?.message || tr("无法读取产物。"));
return value;
}