arkflow-magpie-plugins 0.0.0-stage → 2.0.19
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/LICENSE +21 -0
- package/NOTICE +17 -0
- package/README.en.md +29 -0
- package/README.md +28 -2
- package/bundled/meeting-actions/SKILL.md +11 -0
- package/docs/MAGPIE-LICENSE +21 -0
- package/index.mjs +9 -0
- package/package.json +79 -4
- package/page/app.css +275 -0
- package/page/app.js +784 -0
- package/page/bootstrap.js +59 -0
- package/page/connection.js +24 -0
- package/page/icon.svg +3 -0
- package/page/index.html +28 -0
- package/page/logo.png +0 -0
- package/page/magpie-components.css +131 -0
- package/page/request.js +101 -0
- package/page/service.html +3 -0
- package/page/store-model.mjs +107 -0
- package/page/theme.js +51 -0
- package/src/accounts.mjs +124 -0
- package/src/api-client.mjs +292 -0
- package/src/bundled-skills.mjs +13 -0
- package/src/config.mjs +71 -0
- package/src/credentials.mjs +90 -0
- package/src/host-adapter.mjs +707 -0
- package/src/install-defaults.mjs +92 -0
- package/src/install-identity.mjs +32 -0
- package/src/manual-initialization.mjs +225 -0
- package/src/native-auth.mjs +62 -0
- package/src/native-control.mjs +236 -0
- package/src/native-providers.mjs +422 -0
- package/src/page-service.mjs +358 -0
- package/src/provider.mjs +44 -0
- package/src/runtime.mjs +302 -0
- package/src/session.mjs +366 -0
- package/src/skill-metadata.mjs +32 -0
- package/src/skill-packages.mjs +226 -0
- package/src/skills.mjs +454 -0
- package/src/state-store.mjs +89 -0
- package/src/sync-engine.mjs +392 -0
package/page/app.js
ADDED
|
@@ -0,0 +1,784 @@
|
|
|
1
|
+
const token = document.querySelector('meta[name="novaflow-page-token"]').content
|
|
2
|
+
const root = document.getElementById("app")
|
|
3
|
+
const dialog = document.getElementById("detail")
|
|
4
|
+
const setupDialog = document.getElementById("login-setup")
|
|
5
|
+
let setupView = ""
|
|
6
|
+
const storeModel = globalThis.NovaFlowStore
|
|
7
|
+
let state = null, busy = false, loading = false, resourceLoading = false
|
|
8
|
+
let local = null, localError = "", storeError = "", pageError = ""
|
|
9
|
+
let accountKey = "", resourcesFor = "", resourcesAt = 0, revision = 0, toastTimer, dialogVersion = 0
|
|
10
|
+
let loginError = ""
|
|
11
|
+
let category = "all", installationFilter = "all", query = ""
|
|
12
|
+
let shareDraft = null
|
|
13
|
+
let backdropPressed = false
|
|
14
|
+
|
|
15
|
+
function el(tag, cls = "", text = "") {
|
|
16
|
+
const node = document.createElement(tag)
|
|
17
|
+
node.className = cls
|
|
18
|
+
node.textContent = String(text ?? "")
|
|
19
|
+
return node
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function button(text, action, cls = "") {
|
|
23
|
+
const node = el("button", `text ${cls}`, text)
|
|
24
|
+
node.type = "button"
|
|
25
|
+
node.dataset.lock = "true"
|
|
26
|
+
node.disabled = busy || resourceLoading
|
|
27
|
+
node.onclick = action
|
|
28
|
+
return node
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function enterpriseOnly(node, allowed = storeModel.companyVisible(state)) {
|
|
32
|
+
if (!allowed) {
|
|
33
|
+
node.dataset.unavailable = "true"
|
|
34
|
+
node.disabled = true
|
|
35
|
+
node.title = "企业用户才可使用,开通或加入企业后即可使用"
|
|
36
|
+
node.setAttribute("aria-disabled", "true")
|
|
37
|
+
}
|
|
38
|
+
return node
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function enterpriseGuide() {
|
|
42
|
+
const body = el("div")
|
|
43
|
+
body.append(el("p", "", "企业用户可使用公司技能商店、安装更新、本机技能与技能分享。"))
|
|
44
|
+
const steps = el("ol", "login-steps")
|
|
45
|
+
steps.append(el("li", "", "打开 ArkFlow 平台并登录,进入个人资料。"), el("li", "", "自行开通:选择创建企业,填写企业名称并按页面确认。"), el("li", "", "加入已有企业:向企业管理员获取企业注册码,在个人资料中选择加入企业。"), el("li", "", "完成后回到插件,点击立即同步;若身份尚未更新,请重新连接账户。"))
|
|
46
|
+
body.append(steps, button("前往平台个人资料", () => command("enterprise-guide").catch(error => toast(error.message)), "primary"))
|
|
47
|
+
modal("开通企业账号指引", body)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function toast(message) {
|
|
51
|
+
const node = document.getElementById("toast")
|
|
52
|
+
clearTimeout(toastTimer)
|
|
53
|
+
node.textContent = message
|
|
54
|
+
node.classList.add("visible")
|
|
55
|
+
toastTimer = setTimeout(() => node.classList.remove("visible"), 5000)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const request = globalThis.createNovaFlowPageRequest({ token })
|
|
59
|
+
|
|
60
|
+
function command(operation, payload = {}) {
|
|
61
|
+
return request("api/command", { operation, payload })
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function identity(value) {
|
|
65
|
+
const session = value?.session || {}
|
|
66
|
+
return [session.status, session.userId, session.accountMode, session.company?.id].join(":")
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function acceptState(value) {
|
|
70
|
+
if (!value?.session) return
|
|
71
|
+
const nextKey = identity(value)
|
|
72
|
+
if (nextKey !== accountKey) {
|
|
73
|
+
accountKey = nextKey
|
|
74
|
+
resourcesFor = ""
|
|
75
|
+
resourcesAt = 0
|
|
76
|
+
local = null
|
|
77
|
+
loginError = localError = storeError = ""
|
|
78
|
+
shareDraft = null
|
|
79
|
+
category = "all"
|
|
80
|
+
installationFilter = "all"
|
|
81
|
+
dialogVersion++
|
|
82
|
+
if (dialog.open) dialog.close()
|
|
83
|
+
}
|
|
84
|
+
state = value
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function lockControls() {
|
|
88
|
+
root.setAttribute("aria-busy", String(busy || loading || resourceLoading))
|
|
89
|
+
for (const node of document.querySelectorAll("[data-lock]")) {
|
|
90
|
+
node.disabled = busy || resourceLoading || node.dataset.unavailable === "true"
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function acceptLocal(result) {
|
|
95
|
+
if (result.libraryAvailable === false || result.skills?.libraryAvailable === false) throw new Error("Magpie 本机资源库暂不可用")
|
|
96
|
+
local = storeModel.localSkills(result)
|
|
97
|
+
localError = ""
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function readLocal() {
|
|
101
|
+
try { acceptLocal(await command("local-skills")); return true }
|
|
102
|
+
catch (error) { local = null; localError = error.message; return false }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function run(operation, payload = {}, message = "已完成", after, onError) {
|
|
106
|
+
if (busy || resourceLoading) return null
|
|
107
|
+
busy = true
|
|
108
|
+
revision++
|
|
109
|
+
render()
|
|
110
|
+
try {
|
|
111
|
+
const result = await command(operation, payload)
|
|
112
|
+
acceptState(result.state || (result.session ? result : await command("status")))
|
|
113
|
+
if (after) await after(result)
|
|
114
|
+
if (message) toast(message)
|
|
115
|
+
return result
|
|
116
|
+
} catch (error) {
|
|
117
|
+
if (operation === "install-store-skill") {
|
|
118
|
+
try { acceptState(await command("status")) } catch {}
|
|
119
|
+
await readLocal()
|
|
120
|
+
}
|
|
121
|
+
toast(error.message)
|
|
122
|
+
if (onError) onError(error)
|
|
123
|
+
return null
|
|
124
|
+
} finally {
|
|
125
|
+
busy = false
|
|
126
|
+
render()
|
|
127
|
+
if (operation === "login" && state?.login?.pending) polling.retry()
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function row(title, value, control) {
|
|
132
|
+
const node = el("div", "row"), text = el("div", "who")
|
|
133
|
+
text.append(el("h3", "", title), el("p", "", value))
|
|
134
|
+
node.append(text)
|
|
135
|
+
if (control) node.append(control)
|
|
136
|
+
return node
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function closeDialog() { backdropPressed = false; dialogVersion++; dialog.close() }
|
|
140
|
+
|
|
141
|
+
function modal(title, body) {
|
|
142
|
+
const top = el("div", "dialog-heading"), close = button("关闭", closeDialog, "quiet")
|
|
143
|
+
delete close.dataset.lock
|
|
144
|
+
close.disabled = false
|
|
145
|
+
top.append(el("h2", "", title), close)
|
|
146
|
+
const content = el("div", "dialog-content")
|
|
147
|
+
content.append(body)
|
|
148
|
+
dialog.replaceChildren(top, content)
|
|
149
|
+
backdropPressed = false
|
|
150
|
+
if (!dialog.open) dialog.showModal()
|
|
151
|
+
return ++dialogVersion
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function roleLabel(session) {
|
|
155
|
+
if (!storeModel.enterprise(state)) return session.role === "platform_admin" ? "平台管理员 · 个人账户" : "个人账户"
|
|
156
|
+
return session.role === "company_admin" ? "公司管理员" : "员工"
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function timeLabel(value) {
|
|
160
|
+
if (!value) return "尚未完成首次同步"
|
|
161
|
+
const date = new Date(value)
|
|
162
|
+
return Number.isNaN(date.getTime()) ? "同步时间暂不可用" : date.toLocaleString("zh-CN", { month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit" })
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function renderHeader() {
|
|
166
|
+
const session = state.session || {}, header = el("header", "page-header"), who = el("div", "identity"), name = el("div")
|
|
167
|
+
name.append(el("h1", "", "ArkFlow"), el("p", "identity-subtitle", "账户与技能同步"))
|
|
168
|
+
const mark = el("img", "brand-icon")
|
|
169
|
+
mark.src = "logo.png"
|
|
170
|
+
mark.alt = ""
|
|
171
|
+
who.append(mark, name)
|
|
172
|
+
header.append(who)
|
|
173
|
+
if (session.status === "authenticated") {
|
|
174
|
+
const account = el("div", "account-summary"), text = el("div", "account-text")
|
|
175
|
+
text.append(el("strong", "", session.displayName || "已连接 ArkFlow"), el("small", "muted", [roleLabel(session), ...(storeModel.enterprise(state) && session.company?.displayName ? [session.company.displayName] : [])].join(" · ")))
|
|
176
|
+
account.append(el("span", "connection-dot"), text, button("更换账户", () => {
|
|
177
|
+
const body = el("div")
|
|
178
|
+
body.append(el("p", "", "退出后可在浏览器选择另一个 ArkFlow 账户。"), button("退出并更换账户", () => run("logout", {}, "已退出", closeDialog), "primary"))
|
|
179
|
+
modal("更换 ArkFlow 账户", body)
|
|
180
|
+
}, "quiet"), button("退出登录", confirmLogout, "quiet"))
|
|
181
|
+
header.append(account)
|
|
182
|
+
}
|
|
183
|
+
root.append(header)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function renderSummary() {
|
|
187
|
+
const sync = state.sync || {}, summary = el("section", "sync-summary"), text = el("div", "summary-main"), detail = el("div", "summary-detail")
|
|
188
|
+
text.append(el("span", `status-dot ${sync.status === "ready" && !sync.stale ? "ready" : ""}`), el("strong", "", busy ? "正在处理…" : storeModel.syncLabel(state)))
|
|
189
|
+
const localLink = button(`本机已安装 ${Array.isArray(local) ? local.length : "—"}`, () => {
|
|
190
|
+
category = "all"
|
|
191
|
+
installationFilter = "installed"
|
|
192
|
+
query = ""
|
|
193
|
+
render()
|
|
194
|
+
document.querySelector(".store")?.scrollIntoView({ block: "start", behavior: "smooth" })
|
|
195
|
+
}, "local-link")
|
|
196
|
+
delete localLink.dataset.lock
|
|
197
|
+
detail.append(el("span", "", `${state.accounts?.routeCount || 0} 个可用模型`), localLink, el("span", "summary-time", sync.lastSuccessAt ? `上次同步 ${timeLabel(sync.lastSuccessAt)}` : timeLabel("")))
|
|
198
|
+
if (state.sync?.status === "ready" && !state.accounts?.keyCount) detail.append(el("span", "", "暂无可用模型分组"))
|
|
199
|
+
const amount = state.sync?.usage?.[storeModel.enterprise(state) ? "company" : "personal"]?.available
|
|
200
|
+
if (amount != null) detail.append(el("span", "", `可用余额 ¥${amount}`))
|
|
201
|
+
text.append(detail)
|
|
202
|
+
summary.append(text, button("立即同步", syncNow, "sync-action"))
|
|
203
|
+
root.append(summary)
|
|
204
|
+
const failures = state.accounts?.native?.failures || []
|
|
205
|
+
if (failures.length) root.append(renderSyncFailures(failures))
|
|
206
|
+
if (pageError) root.append(el("p", "inline-error", `状态刷新失败:${pageError}。当前显示上次读取结果。`), button("重新连接", retryConnection))
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function renderSyncFailures(failures) {
|
|
210
|
+
const list = el("ul", "sync-failures")
|
|
211
|
+
list.setAttribute("aria-label", "同步失败详情")
|
|
212
|
+
const byProvider = new Map()
|
|
213
|
+
for (const failure of failures) {
|
|
214
|
+
const key = failure.id || failure.code
|
|
215
|
+
if (!byProvider.has(key)) byProvider.set(key, [])
|
|
216
|
+
byProvider.get(key).push(failure)
|
|
217
|
+
}
|
|
218
|
+
for (const entries of byProvider.values()) {
|
|
219
|
+
const { groupName, providerName } = entries[0]
|
|
220
|
+
const item = el("li", "inline-error sync-failure")
|
|
221
|
+
const description = el("div", "sync-failure-detail")
|
|
222
|
+
if (groupName) description.append(el("strong", "", groupName))
|
|
223
|
+
if (providerName && providerName !== groupName) description.append(el("span", "sync-failure-provider", providerName))
|
|
224
|
+
description.append(el("p", "", [...new Set(entries.map(entry => entry.message))].join(" · ")))
|
|
225
|
+
item.append(description)
|
|
226
|
+
const reset = entries.find(entry => entry.resetRevision)
|
|
227
|
+
if (reset) {
|
|
228
|
+
const action = button("重置", () => confirmProviderReset(reset), "secondary")
|
|
229
|
+
action.setAttribute("aria-label", `重置 ${providerName || groupName}`)
|
|
230
|
+
item.append(action)
|
|
231
|
+
}
|
|
232
|
+
list.append(item)
|
|
233
|
+
}
|
|
234
|
+
return list
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function confirmProviderReset(failure) {
|
|
238
|
+
if (busy || resourceLoading) return
|
|
239
|
+
const body = el("div"), label = failure.providerName || failure.groupName
|
|
240
|
+
const confirm = button("确认重置", async () => {
|
|
241
|
+
const response = await run("reset-provider", { providerId: failure.id, revision: failure.resetRevision }, null)
|
|
242
|
+
if (!response) return
|
|
243
|
+
closeDialog()
|
|
244
|
+
toast(response.result.modelSyncFailed ? "连接已重置,模型同步仍需重试" : "分组连接已重置")
|
|
245
|
+
}, "primary")
|
|
246
|
+
body.append(el("p", "", `将「${label}」恢复为 ArkFlow 默认连接。手动添加的密钥、自定义地址和请求设置会被清除。`), confirm)
|
|
247
|
+
modal("重置分组配置", body)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function installButton(item) {
|
|
251
|
+
const status = storeModel.installation(item, state, local), sourceAllowed = item.source_kind === "builtin" || item.source_kind === "company" && storeModel.companyVisible(state)
|
|
252
|
+
const label = item.unavailable ? "本机副本" : status.updateAvailable ? "更新" : status.isInstalled ? "已安装" : status.unknown ? "待确认" : status.downloaded ? "重试登记" : "安装"
|
|
253
|
+
const node = button(label, () => installSkill(item), `install-button pm-go pm-act ${status.isInstalled && !status.updateAvailable ? "installed done" : "get"}`)
|
|
254
|
+
if (item.unavailable || status.isInstalled && !status.updateAvailable || status.unknown || !sourceAllowed) {
|
|
255
|
+
node.dataset.unavailable = "true"
|
|
256
|
+
node.disabled = true
|
|
257
|
+
}
|
|
258
|
+
node.setAttribute("aria-label", `${label} ${item.display_name || item.slug || "技能"}`)
|
|
259
|
+
if (status.unknown) node.title = "无法读取本机资源库,刷新后确认安装状态"
|
|
260
|
+
return node
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async function installSkill(item) {
|
|
264
|
+
const status = storeModel.installation(item, state, local)
|
|
265
|
+
if (status.isInstalled && !status.updateAvailable || status.unknown) return
|
|
266
|
+
await run("install-store-skill", { sourceKind: item.source_kind, skillId: item.skill_id, targetAgents: [] }, null, async () => {
|
|
267
|
+
const verified = await readLocal(), installed = storeModel.installation(item, state, local)
|
|
268
|
+
if (verified && installed.isInstalled && !installed.updateAvailable) { closeDialog(); toast("已登记到 Magpie 资源库,可在 Agent 中按需使用") }
|
|
269
|
+
else if (!verified) toast("安装命令已返回,本机登记状态未能确认,请刷新本机状态")
|
|
270
|
+
else toast("技能尚未完成本机登记,请重试;未确认安装成功")
|
|
271
|
+
})
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function skillCard(item) {
|
|
275
|
+
const card = el("article", "skill-card pm-card"), top = el("div", "card-top pm-top"), who = el("div", "who pm-who"), title = el("div", "pm-name"), publisher = el("div", "publisher pm-by")
|
|
276
|
+
title.append(el("b", "", item.display_name || item.slug || "未命名技能"))
|
|
277
|
+
publisher.append(el("span", "", storeModel.publisherName(item)))
|
|
278
|
+
who.append(title, publisher)
|
|
279
|
+
const icon = el("div", "skill-icon pm-logo", String(item.display_name || item.slug || "S").slice(0, 1))
|
|
280
|
+
const install = installButton(item), action = install.onclick
|
|
281
|
+
install.onclick = (event) => { event.stopPropagation(); action(event) }
|
|
282
|
+
top.append(icon, who, install)
|
|
283
|
+
const meta = el("div", "card-meta pm-meta")
|
|
284
|
+
meta.append(el("span", "category-label pm-chip kind pv", storeModel.categoryName(item, state)), el("span", "version pm-ver", `v${item.current_version ?? "—"}`))
|
|
285
|
+
if (item.file_count != null && Number.isFinite(Number(item.file_count))) meta.append(el("span", "file-count", `${item.file_count} 个文件`))
|
|
286
|
+
card.append(top, el("p", "skill-description pm-sum", item.description || "发布者暂未添加技能说明。"), meta)
|
|
287
|
+
if (item.unavailable) card.append(el("p", "muted", "远端已不可下载或更新,保留本机副本。"))
|
|
288
|
+
card.tabIndex = 0
|
|
289
|
+
card.setAttribute("role", "button")
|
|
290
|
+
card.setAttribute("aria-label", `${item.display_name || item.slug},查看技能详情`)
|
|
291
|
+
card.onclick = () => item.unavailable ? toast("远端已不可下载或更新,本机副本及登记继续保留。") : detail(item)
|
|
292
|
+
card.onkeydown = (event) => {
|
|
293
|
+
if (event.target === card && ["Enter", " "].includes(event.key)) { event.preventDefault(); item.unavailable ? toast("远端已不可下载或更新,本机副本及登记继续保留。") : void detail(item) }
|
|
294
|
+
}
|
|
295
|
+
return card
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function renderStore() {
|
|
299
|
+
const section = el("section", "store"), heading = el("div", "section-heading"), text = el("div"), actions = el("div", "actions")
|
|
300
|
+
text.append(el("h2", "", "技能商店"))
|
|
301
|
+
actions.append(button(resourceLoading ? "正在刷新…" : "刷新", () => refreshResources(true), "quiet"))
|
|
302
|
+
if (canContribute()) actions.append(button("+ 分享技能", () => sharePicker(), "primary"))
|
|
303
|
+
heading.append(text, actions)
|
|
304
|
+
section.append(heading)
|
|
305
|
+
const chips = el("div", "category-chips segs"), results = el("div", "store-results"), categories = storeModel.categories(state)
|
|
306
|
+
chips.setAttribute("role", "group")
|
|
307
|
+
chips.setAttribute("aria-label", "技能分类")
|
|
308
|
+
if (!categories.some((value) => value.id === category)) category = "all"
|
|
309
|
+
for (const option of categories) {
|
|
310
|
+
const node = button(option.name, () => {
|
|
311
|
+
category = option.id
|
|
312
|
+
for (const child of chips.children) {
|
|
313
|
+
const selected = child.dataset.category === category
|
|
314
|
+
child.classList.toggle("active", selected)
|
|
315
|
+
child.classList.toggle("on", selected)
|
|
316
|
+
child.setAttribute("aria-pressed", String(selected))
|
|
317
|
+
}
|
|
318
|
+
draw()
|
|
319
|
+
}, `category-chip opt ${category === option.id ? "active on" : ""}`)
|
|
320
|
+
node.dataset.category = option.id
|
|
321
|
+
delete node.dataset.lock
|
|
322
|
+
node.setAttribute("aria-pressed", String(category === option.id))
|
|
323
|
+
chips.append(node)
|
|
324
|
+
}
|
|
325
|
+
const tools = el("div", "store-toolbar pm-ghbar"), filters = el("div", "installation-filters segs"), searchWrap = el("label", "search-box pm-find pm-ghfind"), search = el("input")
|
|
326
|
+
filters.setAttribute("role", "group")
|
|
327
|
+
filters.setAttribute("aria-label", "安装状态")
|
|
328
|
+
for (const [id, label] of [["all", "全部"], ["installed", "已安装"], ["uninstalled", "未安装"]]) {
|
|
329
|
+
const node = button(label, () => {
|
|
330
|
+
installationFilter = id
|
|
331
|
+
for (const child of filters.children) {
|
|
332
|
+
const selected = child.dataset.filter === id
|
|
333
|
+
child.classList.toggle("active", selected)
|
|
334
|
+
child.classList.toggle("on", selected)
|
|
335
|
+
child.setAttribute("aria-pressed", String(selected))
|
|
336
|
+
}
|
|
337
|
+
draw()
|
|
338
|
+
}, `opt ${installationFilter === id ? "active on" : ""}`)
|
|
339
|
+
delete node.dataset.lock
|
|
340
|
+
node.dataset.filter = id
|
|
341
|
+
node.setAttribute("aria-pressed", String(installationFilter === id))
|
|
342
|
+
filters.append(node)
|
|
343
|
+
}
|
|
344
|
+
search.id = "skill-search"
|
|
345
|
+
search.type = "search"
|
|
346
|
+
search.placeholder = "搜索技能、发布者或关键词"
|
|
347
|
+
search.setAttribute("aria-label", "搜索技能")
|
|
348
|
+
search.value = query
|
|
349
|
+
search.oninput = () => { query = search.value; draw() }
|
|
350
|
+
const searchIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg")
|
|
351
|
+
searchIcon.setAttribute("width", "14"); searchIcon.setAttribute("height", "14"); searchIcon.setAttribute("viewBox", "0 0 24 24"); searchIcon.setAttribute("fill", "none"); searchIcon.setAttribute("stroke", "currentColor"); searchIcon.setAttribute("stroke-width", "1.6"); searchIcon.setAttribute("aria-hidden", "true")
|
|
352
|
+
const lens = document.createElementNS("http://www.w3.org/2000/svg", "circle"), handle = document.createElementNS("http://www.w3.org/2000/svg", "path")
|
|
353
|
+
lens.setAttribute("cx", "10.5"); lens.setAttribute("cy", "10.5"); lens.setAttribute("r", "6.5"); handle.setAttribute("d", "m16 16 4 4")
|
|
354
|
+
searchIcon.append(lens, handle)
|
|
355
|
+
searchWrap.append(searchIcon, search)
|
|
356
|
+
tools.append(chips, filters, searchWrap)
|
|
357
|
+
section.append(tools)
|
|
358
|
+
if (storeError) section.append(el("p", "inline-error", `技能商店读取失败:${storeError}。请点击刷新重试。`))
|
|
359
|
+
if (localError) section.append(el("p", "inline-error", `本机技能读取失败:${localError}。安装计数暂不可用。`))
|
|
360
|
+
section.append(results)
|
|
361
|
+
root.append(section)
|
|
362
|
+
function draw() {
|
|
363
|
+
results.replaceChildren()
|
|
364
|
+
const matched = storeModel.filterItems(state, local, { category, installation: installationFilter, query })
|
|
365
|
+
let count = 0
|
|
366
|
+
for (const [source, label] of [["builtin", "插件推荐"], ["company", "公司分享"], ["platform", "保留的平台技能副本"]]) {
|
|
367
|
+
const list = matched.filter((item) => item.source_kind === source)
|
|
368
|
+
if (!list.length) continue
|
|
369
|
+
const group = el("section", "skill-group pm-sec"), grid = el("div", "skill-grid pm-grid"), caption = el("div", "group-heading pm-sechead")
|
|
370
|
+
caption.append(el("h3", "", label), el("span", "muted", `${list.length} 项`))
|
|
371
|
+
group.append(caption)
|
|
372
|
+
for (const item of list) grid.append(skillCard(item))
|
|
373
|
+
group.append(grid)
|
|
374
|
+
results.append(group)
|
|
375
|
+
count += list.length
|
|
376
|
+
}
|
|
377
|
+
if (category === "all" && installationFilter === "installed") {
|
|
378
|
+
const other = storeModel.otherLocalSkills(state, local, query)
|
|
379
|
+
if (other.length) {
|
|
380
|
+
const group = el("section", "skill-group pm-sec"), heading = el("div", "group-heading pm-sechead"), grid = el("div", "skill-grid pm-grid")
|
|
381
|
+
heading.append(el("h3", "", "本机其他技能"), el("span", "muted", `${other.length} 项`))
|
|
382
|
+
group.append(heading)
|
|
383
|
+
for (const skill of other) {
|
|
384
|
+
const card = el("article", "skill-card pm-card local-card"), top = el("div", "card-top pm-top"), text = el("div", "who pm-who"), name = el("div", "pm-name"), publisher = el("div", "publisher pm-by")
|
|
385
|
+
name.append(el("b", "", skill.name)); publisher.append(el("span", "", "Magpie 本机资源库"))
|
|
386
|
+
text.append(name, publisher)
|
|
387
|
+
top.append(el("div", "skill-icon pm-logo", skill.name.slice(0, 1).toUpperCase()), text, el("span", "local-installed pm-chip", "已安装"))
|
|
388
|
+
card.append(top, el("p", "skill-description pm-sum", skill.agents.length ? `已关联 Agent:${skill.agents.join("、")}` : "暂未关联 Agent,可在 Magpie 资源库选择使用。"))
|
|
389
|
+
const meta = el("div", "card-meta pm-meta")
|
|
390
|
+
meta.append(el("span", "category-label pm-chip kind", "本机技能"))
|
|
391
|
+
if (canContribute()) meta.append(button("分享到公司", () => share(skill), "text-action"))
|
|
392
|
+
card.append(meta)
|
|
393
|
+
grid.append(card)
|
|
394
|
+
}
|
|
395
|
+
group.append(grid)
|
|
396
|
+
results.append(group)
|
|
397
|
+
count += other.length
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
if (!count) {
|
|
401
|
+
let title = "暂无匹配技能", description = "试试其他分类或搜索词。"
|
|
402
|
+
if (!state.skills?.store && resourceLoading) { title = "正在加载技能商店"; description = "正在读取公司技能与本机安装状态…" }
|
|
403
|
+
else if (installationFilter === "installed" && !Array.isArray(local)) { title = "暂时无法确认本机安装状态"; description = "请刷新本机资源库后再查看已安装技能。" }
|
|
404
|
+
else if (!query && category === "company") { title = "公司还没有分享技能"; description = canContribute() ? "可以从 Magpie 本机资源库选择技能,预览文件后分享。" : "公司成员分享的技能会显示在这里。" }
|
|
405
|
+
else if (!query && installationFilter === "installed") { title = "本机还没有已安装技能"; description = "在商店中安装技能后,可在 Magpie 资源库查看。" }
|
|
406
|
+
else if (!query && installationFilter === "uninstalled") { title = "当前没有待安装技能"; description = "可以切换分类,或在已安装中查看本机技能。" }
|
|
407
|
+
else if (!query && !storeModel.items(state).length) { title = "技能商店暂未发布内容"; description = "公司成员分享的技能会在这里展示,可稍后刷新。" }
|
|
408
|
+
const empty = el("div", "empty")
|
|
409
|
+
empty.append(el("div", "empty-icon", "◇"), el("h3", "", title), el("p", "", description))
|
|
410
|
+
results.append(empty)
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
draw()
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
async function detail(item) {
|
|
417
|
+
if (busy || resourceLoading) return
|
|
418
|
+
const body = el("div", "detail-body")
|
|
419
|
+
body.append(el("p", "muted", "正在读取技能详情…"))
|
|
420
|
+
const version = modal(item.display_name || item.slug, body), key = accountKey
|
|
421
|
+
try {
|
|
422
|
+
const data = await command("skill-detail", { sourceKind: item.source_kind, skillId: item.skill_id })
|
|
423
|
+
if (dialogVersion !== version || accountKey !== key || !dialog.open) return
|
|
424
|
+
const skill = { ...item, ...data.skill, source_kind: item.source_kind }, current = data.versions.find((value) => Number(value.version) === Number(skill.current_version))
|
|
425
|
+
body.replaceChildren(el("p", "", skill.description || "暂无说明"))
|
|
426
|
+
body.append(row("发布者", storeModel.publisherName({ ...skill, source_kind: item.source_kind })), row("分类", storeModel.categoryName(skill, state)), row("当前版本", `v${skill.current_version}`), row("许可", skill.license || "未填写"))
|
|
427
|
+
const files = el("div", "files")
|
|
428
|
+
files.append(el("h3", "", "技能包文件"))
|
|
429
|
+
for (const file of current?.files || []) files.append(row(file.path, `${file.size} B`))
|
|
430
|
+
body.append(files, el("p", "muted", "安装前校验包完整性,安装过程不执行技能脚本。Agent 关联使用本页已保存的本机偏好。"), installButton(skill))
|
|
431
|
+
if (item.source_kind === "company" && canContribute() && (skill.is_mine || state.session.role === "company_admin")) body.append(button("发布新版本", () => sharePicker(skill), "secondary"))
|
|
432
|
+
} catch (error) {
|
|
433
|
+
if (dialogVersion === version && accountKey === key) body.replaceChildren(el("p", "inline-error", error.message))
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function field(title, input, hint = "") {
|
|
438
|
+
const label = el("label", "field")
|
|
439
|
+
label.append(el("span", "field-title", title), input)
|
|
440
|
+
if (hint) label.append(el("small", "muted", hint))
|
|
441
|
+
return label
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function canContribute() {
|
|
445
|
+
return storeModel.enterprise(state) && state?.session?.capabilities?.contribute_skills === true
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function shareImportGuide(target) {
|
|
449
|
+
const guide = el("section", "share-import-guide")
|
|
450
|
+
guide.append(el("p", "", "只有已添加到 Magpie 资料库(资源库)的技能才可以分享。仅安装在 Agent 中、尚未导入资料库的技能不会出现在下方列表。"), el("h3", "", "如何导入技能"))
|
|
451
|
+
const steps = el("ol", "login-steps")
|
|
452
|
+
steps.append(
|
|
453
|
+
el("li", "", "在 Magpie 左侧打开「资源库」,切换到「技能」。"),
|
|
454
|
+
el("li", "", "Agent 中已有技能:在「Agent 中已有」区域找到技能,点击「导入」或「全部导入」。"),
|
|
455
|
+
el("li", "", "本机文件夹:在顶部输入技能文件夹路径,点击「查找技能」,勾选后确认添加。文件夹内必须包含 SKILL.md;ZIP 请先解压。"),
|
|
456
|
+
el("li", "", "导入完成后回到此处,点击「刷新技能列表」,再选择「预览并分享」。"),
|
|
457
|
+
)
|
|
458
|
+
guide.append(steps, button("刷新技能列表", () => sharePicker(target), "secondary"))
|
|
459
|
+
return guide
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
async function sharePicker(target = null) {
|
|
463
|
+
if (!canContribute() || busy || resourceLoading) return
|
|
464
|
+
busy = true
|
|
465
|
+
lockControls()
|
|
466
|
+
const body = el("div", "share-list"), key = accountKey
|
|
467
|
+
const guide = shareImportGuide(target)
|
|
468
|
+
body.append(guide, el("p", "muted", "正在读取 Magpie 本机资源库…"))
|
|
469
|
+
const version = modal("分享本机技能", body)
|
|
470
|
+
await readLocal()
|
|
471
|
+
busy = false
|
|
472
|
+
render()
|
|
473
|
+
if (accountKey !== key || dialogVersion !== version || !dialog.open) return
|
|
474
|
+
body.replaceChildren(guide)
|
|
475
|
+
if (localError) { body.append(el("p", "inline-error", localError)); return }
|
|
476
|
+
body.append(el("p", "muted", "从已登记技能中选择,确认包文件后再分享到当前公司。"))
|
|
477
|
+
for (const skill of local || []) body.append(row(skill.name, skill.agents.length ? `Agent:${skill.agents.join("、")}` : "尚未关联 Agent", button("预览并分享", () => share(skill, target), "secondary")))
|
|
478
|
+
if (!local?.length) body.append(el("p", "empty", "资料库中暂无可分享的技能,请按上方指引导入后刷新。"))
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
async function share(skill, target = null) {
|
|
482
|
+
if (!canContribute() || busy || resourceLoading) return
|
|
483
|
+
busy = true
|
|
484
|
+
lockControls()
|
|
485
|
+
const body = el("div"), key = accountKey
|
|
486
|
+
body.append(el("p", "muted", "正在读取并检查技能文件…"))
|
|
487
|
+
const version = modal("预览技能文件", body)
|
|
488
|
+
try {
|
|
489
|
+
const preview = await command("preview-skill", { sourceName: skill.name })
|
|
490
|
+
if (accountKey !== key || dialogVersion !== version || !dialog.open) return
|
|
491
|
+
if (!shareDraft || shareDraft.sourceName !== skill.name || shareDraft.updateSkillId !== target?.skill_id || shareDraft.previewHash !== preview.packageHash || shareDraft.expectedVersion !== target?.current_version) {
|
|
492
|
+
shareDraft = { category_id: target?.category_id || "", sourceName: skill.name, display_name: target?.display_name || skill.name, description: target?.description || preview.description || skill.description || "", idempotencyKey: globalThis.crypto?.randomUUID?.() || `${Date.now()}-${Math.random().toString(36).slice(2)}` }
|
|
493
|
+
}
|
|
494
|
+
Object.assign(shareDraft, { previewHash: preview.packageHash, updateSkillId: target?.skill_id, expectedVersion: target?.current_version })
|
|
495
|
+
const form = el("form", "share-form")
|
|
496
|
+
form.onsubmit = (event) => event.preventDefault()
|
|
497
|
+
form.append(el("p", "settings-note", `分享到:${state.session.company?.displayName || "当前公司"}。请确认用途说明及将分享的文件。`))
|
|
498
|
+
const companyCategory = el("select")
|
|
499
|
+
companyCategory.id = "share-category"
|
|
500
|
+
companyCategory.dataset.lock = "true"
|
|
501
|
+
companyCategory.append(new Option("未分类", ""))
|
|
502
|
+
for (const category of state.skills?.store?.categories || []) companyCategory.append(new Option(category.name, String(category.id)))
|
|
503
|
+
companyCategory.value = shareDraft.category_id || ""
|
|
504
|
+
companyCategory.onchange = () => { shareDraft.category_id = companyCategory.value }
|
|
505
|
+
const name = el("input"), description = el("textarea")
|
|
506
|
+
for (const [node, id, property] of [[name, "share-name", "display_name"], [description, "share-description", "description"]]) {
|
|
507
|
+
node.id = id
|
|
508
|
+
node.value = shareDraft[property]
|
|
509
|
+
node.dataset.lock = "true"
|
|
510
|
+
node.oninput = () => { shareDraft[property] = node.value }
|
|
511
|
+
}
|
|
512
|
+
name.required = true
|
|
513
|
+
name.maxLength = 160
|
|
514
|
+
description.maxLength = 2000
|
|
515
|
+
description.rows = 3
|
|
516
|
+
form.append(field("公司分类", companyCategory, "分享后立即上架,供当前公司成员手动安装。"), field("显示名称", name), field("用途说明", description, "已读取资料库中的技能说明,可按需修改。"))
|
|
517
|
+
const files = el("div", "files")
|
|
518
|
+
files.append(el("h3", "", `将分享 ${preview.files?.length || 0} 个文件`))
|
|
519
|
+
for (const file of preview.files || []) files.append(row(file.path, `${file.size} B`))
|
|
520
|
+
form.append(files)
|
|
521
|
+
if (preview.excluded?.length) form.append(el("p", "muted", `已排除 ${preview.excluded.length} 项凭据、缓存或不支持的内容。`))
|
|
522
|
+
const error = el("p", "inline-error")
|
|
523
|
+
error.hidden = true
|
|
524
|
+
error.setAttribute("role", "alert")
|
|
525
|
+
form.append(error, button(target ? `确认更新「${target.display_name}」v${target.current_version}` : "确认分享到公司", async () => {
|
|
526
|
+
if (busy || !form.reportValidity()) return
|
|
527
|
+
error.textContent = ""
|
|
528
|
+
error.hidden = true
|
|
529
|
+
let failure = ""
|
|
530
|
+
const result = await run("share-skill", { ...shareDraft, display_name: shareDraft.display_name.trim() }, null, null, cause => { failure = cause.message })
|
|
531
|
+
if (!result) { error.textContent = `分享未完成:${failure || "请稍后重试"}。草稿已保留。`; error.hidden = false; return }
|
|
532
|
+
shareDraft = null
|
|
533
|
+
closeDialog()
|
|
534
|
+
toast("已分享到公司")
|
|
535
|
+
await refreshResources(true, false)
|
|
536
|
+
}, "primary"))
|
|
537
|
+
body.replaceChildren(form)
|
|
538
|
+
} catch (error) {
|
|
539
|
+
if (dialogVersion === version) body.replaceChildren(el("p", "inline-error", error.message))
|
|
540
|
+
} finally { busy = false; render() }
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
function confirmLogout() {
|
|
544
|
+
if (busy || resourceLoading) return
|
|
545
|
+
const body = el("div")
|
|
546
|
+
body.append(el("p", "", "退出会撤销此次授权,并清理本插件管理的模型连接。"), button("确认退出", () => run("logout", {}, "已退出 ArkFlow", closeDialog), "primary"))
|
|
547
|
+
modal("退出 ArkFlow", body)
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
function renderLogin() {
|
|
551
|
+
const box = el("section", "login-panel")
|
|
552
|
+
const hero = el("div", "login-hero"), mark = el("img", "brand-icon"), pending = state.login?.pending
|
|
553
|
+
mark.src = "logo.png"
|
|
554
|
+
mark.alt = ""
|
|
555
|
+
hero.append(mark, el("h1", "", pending ? "等待浏览器确认" : "在 Magpie 中使用 ArkFlow"),
|
|
556
|
+
el("p", "login-lead", pending ? "请在系统浏览器完成授权,此页会自动更新。" : "连接你的 ArkFlow 账户,在 Magpie 中使用已授权的模型和技能。"))
|
|
557
|
+
if (pending) {
|
|
558
|
+
const cancel = button("取消登录", () => run("cancel-login", {}, null), "secondary login-action")
|
|
559
|
+
cancel.id = "cancel-login"
|
|
560
|
+
hero.append(cancel)
|
|
561
|
+
if (state.login.url) {
|
|
562
|
+
const address = el("input")
|
|
563
|
+
address.id = "login-address"
|
|
564
|
+
address.readOnly = true
|
|
565
|
+
address.value = state.login.url
|
|
566
|
+
address.onclick = () => address.select()
|
|
567
|
+
hero.append(field("浏览器未打开?复制链接到本机浏览器", address))
|
|
568
|
+
}
|
|
569
|
+
} else {
|
|
570
|
+
const connect = button("登录 ArkFlow", () => { loginError = ""; return run("login", {}, null, null, error => { loginError = error.message }) }, "primary login-action")
|
|
571
|
+
connect.id = "login-action"
|
|
572
|
+
hero.append(connect)
|
|
573
|
+
}
|
|
574
|
+
const error = state.login?.error || loginError || pageError
|
|
575
|
+
if (error) {
|
|
576
|
+
const alert = el("p", "inline-error", error)
|
|
577
|
+
alert.setAttribute("role", "alert")
|
|
578
|
+
hero.append(alert)
|
|
579
|
+
if (pageError) hero.append(button("重新连接", retryConnection))
|
|
580
|
+
}
|
|
581
|
+
const features = el("dl", "login-features")
|
|
582
|
+
for (const [title, description] of [
|
|
583
|
+
["模型同步", "自动添加账户有权使用的模型分组。"],
|
|
584
|
+
["技能资料库", "安装内置技能,企业成员还可分享团队技能。"],
|
|
585
|
+
["配置初始化", "登录后自动配置生图模型和 Magpie 生图工具。"],
|
|
586
|
+
]) {
|
|
587
|
+
const feature = el("div")
|
|
588
|
+
feature.append(el("dt", "", title), el("dd", "", description))
|
|
589
|
+
features.append(feature)
|
|
590
|
+
}
|
|
591
|
+
box.append(hero, features)
|
|
592
|
+
root.append(box)
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
function renderLoginSetup(setup) {
|
|
596
|
+
const { stage, id } = setup
|
|
597
|
+
const failed = stage === "sync_error" || stage === "config_error"
|
|
598
|
+
const settled = failed || stage === "done"
|
|
599
|
+
const imageNote = state.defaults?.results?.find(item => item.id === "imageGen" && item.status === "skipped")?.message
|
|
600
|
+
const syncFailures = stage === "sync_error" ? state.accounts?.native?.failures || [] : []
|
|
601
|
+
let detail = ""
|
|
602
|
+
if (stage === "config" && state.defaults?.status === "running") detail = ({
|
|
603
|
+
agentModels: "正在选择 Agent 的默认模型…",
|
|
604
|
+
mcp: "正在关联 Magpie 生图工具…",
|
|
605
|
+
imageGen: "正在设置生图模型…",
|
|
606
|
+
agentEffort: "正在设置新会话默认思考强度…",
|
|
607
|
+
automaticRoutingGroups: "正在关闭自动创建路由组…",
|
|
608
|
+
autostart: "正在开启 Magpie 开机自启…",
|
|
609
|
+
hostUpdates: "正在设置每日版和自动更新…",
|
|
610
|
+
})[state.defaults.currentTask] || "正在初始化 Magpie 配置…"
|
|
611
|
+
if (stage === "config_error") detail = state.defaults?.results?.filter(item => item.status === "failed").map(item => item.message).join(" · ")
|
|
612
|
+
if (stage === "sync_error" && !syncFailures.length) detail = state.sync?.errorCode || "账户数据未完全同步,请检查网络后重试。"
|
|
613
|
+
const view = JSON.stringify([id, stage, busy, pageError, imageNote, detail, syncFailures])
|
|
614
|
+
if (!setupDialog.open || setupView !== view) {
|
|
615
|
+
const card = el("div", "setup-card"), mark = el("img", "brand-icon")
|
|
616
|
+
mark.src = "logo.png"
|
|
617
|
+
mark.alt = ""
|
|
618
|
+
const title = el("h1", "", failed ? "部分设置需要处理" : settled ? "同步完成" : "正在准备 ArkFlow")
|
|
619
|
+
title.id = "setup-title"
|
|
620
|
+
title.setAttribute("aria-live", "polite")
|
|
621
|
+
const subtitle = el("p", "setup-subtitle", failed ? "可以重新同步,或先进入插件。" : settled ? "现在可以开始使用你的工作区。" : "正在同步账户数据并配置 Magpie。")
|
|
622
|
+
const steps = el("ol", "setup-steps")
|
|
623
|
+
const syncState = stage === "sync" ? "running" : stage === "sync_error" ? "failed" : "done"
|
|
624
|
+
const configState = { sync: "pending", sync_error: "pending", config: "running", config_error: "failed", done: "done" }[stage]
|
|
625
|
+
const labels = { done: "已完成", running: "进行中", pending: "待处理", failed: "未完成" }
|
|
626
|
+
for (const [name, status] of [["连接 ArkFlow 账户", "done"], ["同步模型与技能", syncState], ["初始化 Magpie 配置", configState]]) {
|
|
627
|
+
const step = el("li", `setup-step ${status}`), icon = el("span", "setup-icon", status === "done" ? "✓" : status === "failed" ? "!" : "")
|
|
628
|
+
icon.setAttribute("aria-hidden", "true")
|
|
629
|
+
step.append(icon, el("strong", "", name), el("small", "", labels[status]))
|
|
630
|
+
steps.append(step)
|
|
631
|
+
}
|
|
632
|
+
card.append(mark, title, subtitle, steps)
|
|
633
|
+
if (syncFailures.length) card.append(renderSyncFailures(syncFailures))
|
|
634
|
+
if (detail) card.append(el("p", failed ? "inline-error" : "setup-note", detail))
|
|
635
|
+
if (settled && imageNote && stage !== "sync_error") card.append(el("p", "setup-note", imageNote))
|
|
636
|
+
if (pageError) card.append(el("p", "inline-error", pageError), button("重新连接", retryConnection))
|
|
637
|
+
const actions = el("div", "setup-actions")
|
|
638
|
+
const showError = error => { pageError = error.message }
|
|
639
|
+
if (failed) actions.append(button("重试", () => run("retry-setup", { id }, null, null, showError), "secondary"))
|
|
640
|
+
const finish = button(failed ? "进入插件" : "完成", async () => {
|
|
641
|
+
if (await run("finish-setup", { id }, null, null, showError)) await refreshResources(true, false)
|
|
642
|
+
}, "primary")
|
|
643
|
+
finish.dataset.unavailable = String(!settled)
|
|
644
|
+
finish.disabled = !settled || busy
|
|
645
|
+
actions.append(finish)
|
|
646
|
+
card.append(actions)
|
|
647
|
+
setupDialog.replaceChildren(card)
|
|
648
|
+
setupView = view
|
|
649
|
+
}
|
|
650
|
+
if (!setupDialog.open) setupDialog.showModal()
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
function render() {
|
|
654
|
+
const active = document.activeElement, focusId = root.contains(active) ? active.id : ""
|
|
655
|
+
let selection = null
|
|
656
|
+
if (focusId && ["INPUT", "TEXTAREA"].includes(active.tagName)) { try { selection = [active.selectionStart, active.selectionEnd] } catch {} }
|
|
657
|
+
if (state?.session?.status === "authenticated" && state.session.setup) {
|
|
658
|
+
root.replaceChildren()
|
|
659
|
+
renderLoginSetup(state.session.setup)
|
|
660
|
+
lockControls()
|
|
661
|
+
return
|
|
662
|
+
}
|
|
663
|
+
if (setupDialog.open) { setupDialog.close(); setupView = "" }
|
|
664
|
+
root.replaceChildren()
|
|
665
|
+
if (!state) { root.append(el("p", "empty", "正在连接 ArkFlow…")); return }
|
|
666
|
+
if (state.session?.status === "authenticated") {
|
|
667
|
+
renderHeader()
|
|
668
|
+
renderSummary()
|
|
669
|
+
renderStore()
|
|
670
|
+
} else renderLogin()
|
|
671
|
+
lockControls()
|
|
672
|
+
const restored = focusId && document.getElementById(focusId)
|
|
673
|
+
if (restored) {
|
|
674
|
+
restored.focus({ preventScroll: true })
|
|
675
|
+
if (selection) try { restored.setSelectionRange(...selection) } catch {}
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
async function refreshResources(includeStore = false, showToast = true) {
|
|
680
|
+
if (state?.session?.status !== "authenticated" || state.session.setup || busy || resourceLoading) return
|
|
681
|
+
resourceLoading = true
|
|
682
|
+
const key = accountKey, version = revision
|
|
683
|
+
render()
|
|
684
|
+
const results = await Promise.allSettled([command("local-skills"), ...(includeStore && storeModel.companyVisible(state) ? [command("skill-store")] : [])])
|
|
685
|
+
resourceLoading = false
|
|
686
|
+
if (key !== accountKey || version !== revision) { render(); return }
|
|
687
|
+
const [localResult, storeResult] = results
|
|
688
|
+
if (localResult.status === "fulfilled") {
|
|
689
|
+
try { acceptLocal(localResult.value) } catch (error) { local = null; localError = error.message }
|
|
690
|
+
} else { local = null; localError = localResult.reason.message }
|
|
691
|
+
if (storeResult?.status === "fulfilled") { acceptState(storeResult.value.state || storeResult.value); storeError = "" }
|
|
692
|
+
else if (storeResult) storeError = storeResult.reason.message
|
|
693
|
+
resourcesFor = key
|
|
694
|
+
resourcesAt = Date.now()
|
|
695
|
+
render()
|
|
696
|
+
if (showToast) toast(localError || storeError ? "部分内容读取失败,请查看页面提示" : "技能商店与本机状态已刷新")
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
async function syncNow() {
|
|
700
|
+
const result = await run("sync", {}, null)
|
|
701
|
+
if (!result) return
|
|
702
|
+
toast(state.sync?.status === "ready" && !state.sync?.stale ? "连接与模型状态已同步" : `${storeModel.syncLabel(state)}${state.sync?.errorCode ? `:${state.sync.errorCode}` : ""}`)
|
|
703
|
+
await refreshResources(true, false)
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
async function load(signal) {
|
|
707
|
+
if (busy || loading || resourceLoading) return
|
|
708
|
+
loading = true
|
|
709
|
+
const version = revision
|
|
710
|
+
try {
|
|
711
|
+
const result = await request("api/state", undefined, { signal })
|
|
712
|
+
if (!result?.session || typeof result.session.status !== "string") throw new Error("ArkFlow 状态暂不可用,请重新连接")
|
|
713
|
+
if (signal?.aborted || version !== revision) return
|
|
714
|
+
acceptState(result)
|
|
715
|
+
pageError = ""
|
|
716
|
+
render()
|
|
717
|
+
} finally { loading = false; lockControls() }
|
|
718
|
+
if (!signal?.aborted && version === revision && (resourcesFor !== accountKey || Date.now() - resourcesAt > 5 * 60_000)) {
|
|
719
|
+
void refreshResources(resourcesFor !== accountKey, false)
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
const polling = globalThis.createNovaFlowStatePolling({
|
|
724
|
+
read: load,
|
|
725
|
+
interval: () => state?.login?.pending || state?.session?.setup ? 1000 : 15_000,
|
|
726
|
+
active: () => !document.hidden && globalThis.NovaFlowHost?.visible !== false,
|
|
727
|
+
onError(error) {
|
|
728
|
+
if (!busy && !resourceLoading && (error.code === "PAGE_REQUEST_TIMEOUT" || error.code === "PAGE_RESPONSE_INVALID" || error instanceof TypeError) && globalThis.NovaFlowConnection?.recover({ onStalled: recoveryStalled })) {
|
|
729
|
+
polling.stop()
|
|
730
|
+
pageError = "正在重新连接 Magpie…"
|
|
731
|
+
if (state) render()
|
|
732
|
+
return
|
|
733
|
+
}
|
|
734
|
+
pageError = error.message
|
|
735
|
+
if (state) render()
|
|
736
|
+
else root.replaceChildren(el("p", "empty inline-error", `连接失败:${error.message}`), button("重试连接", retryConnection, "primary"))
|
|
737
|
+
},
|
|
738
|
+
})
|
|
739
|
+
function recoveryStalled() {
|
|
740
|
+
pageError = "无法自动重新连接,请关闭并重新打开 ArkFlow 插件页。无需退出账户。"
|
|
741
|
+
if (state) render()
|
|
742
|
+
else root.replaceChildren(el("p", "empty inline-error", pageError), button("重试连接", retryConnection, "primary"))
|
|
743
|
+
}
|
|
744
|
+
function retryConnection() {
|
|
745
|
+
if (globalThis.NovaFlowConnection?.recover({ manual: true, onStalled: recoveryStalled })) polling.stop()
|
|
746
|
+
else polling.retry()
|
|
747
|
+
}
|
|
748
|
+
function resumeConnection() {
|
|
749
|
+
if (document.hidden || globalThis.NovaFlowHost?.visible === false) {
|
|
750
|
+
polling.stop()
|
|
751
|
+
if (!document.hidden && navigator.onLine === false) {
|
|
752
|
+
pageError = "当前处于离线状态,恢复联网后将重新连接"
|
|
753
|
+
if (state) render()
|
|
754
|
+
else root.replaceChildren(el("p", "empty inline-error", pageError), button("重新连接", resumeConnection, "primary"))
|
|
755
|
+
}
|
|
756
|
+
} else polling.retry()
|
|
757
|
+
}
|
|
758
|
+
globalThis.NovaFlowHost?.onVisibility(resumeConnection)
|
|
759
|
+
document.addEventListener("visibilitychange", resumeConnection)
|
|
760
|
+
window.addEventListener("online", resumeConnection)
|
|
761
|
+
window.addEventListener("offline", resumeConnection)
|
|
762
|
+
window.addEventListener("pageshow", resumeConnection)
|
|
763
|
+
window.addEventListener("pagehide", () => polling.stop())
|
|
764
|
+
|
|
765
|
+
function onDialogBackdrop(event) {
|
|
766
|
+
if (event.target !== dialog) return false
|
|
767
|
+
const rect = dialog.getBoundingClientRect()
|
|
768
|
+
return event.clientX < rect.left || event.clientX > rect.right || event.clientY < rect.top || event.clientY > rect.bottom
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
document.addEventListener("keydown", event => {
|
|
772
|
+
if (setupDialog.open && !dialog.open && event.key === "Escape") event.preventDefault()
|
|
773
|
+
})
|
|
774
|
+
setupDialog.addEventListener("cancel", event => event.preventDefault())
|
|
775
|
+
dialog.addEventListener("pointerdown", event => { backdropPressed = event.button === 0 && onDialogBackdrop(event) })
|
|
776
|
+
dialog.addEventListener("pointercancel", () => { backdropPressed = false })
|
|
777
|
+
dialog.addEventListener("click", event => {
|
|
778
|
+
const dismiss = backdropPressed && onDialogBackdrop(event)
|
|
779
|
+
backdropPressed = false
|
|
780
|
+
if (dismiss) closeDialog()
|
|
781
|
+
})
|
|
782
|
+
dialog.addEventListener("close", () => { backdropPressed = false; dialogVersion++ })
|
|
783
|
+
document.getElementById("website").onclick = () => request("api/open-website", {}).catch((error) => toast(error.message))
|
|
784
|
+
resumeConnection()
|