@inupedia/spotlight-vue 0.5.19 → 0.7.0

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.
@@ -1,474 +1,279 @@
1
- import { serializeSkillsForRemote as H } from "@inupedia/spotlight-client";
2
- import { useAgentSessionStore as E } from "../session/agentSession.js";
3
- import { useSpotlightMemoryPreferenceStore as q } from "../store/memoryPreferenceStore.js";
4
- import { useSpotlightRuntimeStore as D } from "../store/runtimeStore.js";
5
- import { SPOTLIGHT_PIPELINE_STEP_IDS as r } from "../store/pipeline/constants.js";
6
- import { isLoopPlanningChunk as F, splitIntentStepContent as J } from "../store/pipeline/displayText.js";
7
- import { toolLaneStepId as x, resolveToolLane as j, toolLaneStepLabel as B } from "../store/pipeline/toolLane.js";
8
- import { getSpotlightConfig as N } from "../plugin.js";
9
- import { getSkillsPoolForRun as W } from "../skills/index.js";
10
- import { executeRemoteHostTool as z, ensureHostToolsManifest as L } from "./hostToolRunner.js";
11
- import { getSpotlightServerBase as w, buildSpotlightJsonHeaders as h, getSpotlightProjectId as G } from "./httpConfig.js";
12
- import { ensureSpotlightMeta as U } from "./meta.js";
13
- const C = /* @__PURE__ */ new WeakMap();
14
- function V(t) {
15
- return t.type !== "host_action_request" && t.type !== "run_completed" && t.type !== "run_error" && t.type !== "ping" && t.type !== "memory_decision";
16
- }
17
- function d(t, e, n) {
18
- const s = t.getSteps().find((a) => a.id === e);
19
- if (s) return s;
20
- const o = { id: e, label: n, status: "pending" };
21
- return t.setSteps([...t.getSteps(), o]), o;
1
+ import { serializeSkillsForRemote as C } from "@inupedia/spotlight-client";
2
+ import { useAgentSessionStore as g } from "../session/agentSession.js";
3
+ import { useSpotlightMemoryPreferenceStore as N } from "../store/memoryPreferenceStore.js";
4
+ import { useSpotlightRuntimeStore as P } from "../store/runtimeStore.js";
5
+ import { SPOTLIGHT_PIPELINE_STEP_IDS as l } from "../store/pipeline/constants.js";
6
+ import { toolLaneStepId as R, resolveToolLane as b, toolLaneStepLabel as x } from "../store/pipeline/toolLane.js";
7
+ import { getSpotlightAppClient as v, getSpotlightConfig as T } from "../plugin.js";
8
+ import { getSkillsPoolForRun as E } from "../skills/index.js";
9
+ import { executeRemoteHostTool as A, ensureHostToolsManifest as M } from "./hostToolRunner.js";
10
+ import { getSpotlightProjectId as _ } from "./httpConfig.js";
11
+ import { ensureSpotlightMeta as L } from "./meta.js";
12
+ const k = /* @__PURE__ */ new WeakMap();
13
+ function f(t, o, s) {
14
+ const e = t.getSteps().find((r) => r.id === o);
15
+ if (e) return e;
16
+ const n = { id: o, label: s, status: "pending" };
17
+ return t.setSteps([...t.getSteps(), n]), n;
22
18
  }
23
- function $() {
19
+ function j() {
24
20
  return new Promise((t) => {
25
21
  requestAnimationFrame(() => t());
26
22
  });
27
23
  }
28
- function Y(t) {
29
- return Array.isArray(t) ? t.flatMap((n, s) => {
30
- if (!n || typeof n != "object") return [];
31
- const o = n, a = typeof o.content == "string" ? o.content.trim() : "";
32
- if (!a) return [];
33
- const i = typeof o.title == "string" && o.title.trim() || typeof o.url == "string" && o.url.trim() || `资料 ${s + 1}`, m = typeof o.url == "string" ? o.url.trim() : "";
34
- return [[i, m && m !== i ? m : "", a].filter(Boolean).join(`
24
+ function D(t) {
25
+ return Array.isArray(t) ? t.flatMap((s, e) => {
26
+ if (!s || typeof s != "object") return [];
27
+ const n = s, r = typeof n.content == "string" ? n.content.trim() : "";
28
+ if (!r) return [];
29
+ const a = typeof n.title == "string" && n.title.trim() || typeof n.url == "string" && n.url.trim() || `资料 ${e + 1}`, u = typeof n.url == "string" ? n.url.trim() : "";
30
+ return [[a, u && u !== a ? u : "", r].filter(Boolean).join(`
35
31
  `)];
36
32
  }).join(`
37
33
 
38
34
  `) : "";
39
35
  }
40
- function K(t) {
41
- const e = Y(t.output);
42
- if (e) return e;
43
- const n = t.summary?.trim();
44
- if (n && n.length > 48 && !n.startsWith("{") && !n.startsWith("["))
45
- return n;
46
- if (t.output != null && typeof t.output == "object")
47
- try {
48
- const s = JSON.stringify(t.output, null, 2);
49
- if (s && s !== "{}") return s;
50
- } catch {
51
- }
52
- return n || "";
53
- }
54
- function Q(t) {
55
- const e = K(t.result);
56
- return {
57
- id: t.result.call.id,
58
- name: t.result.call.name,
59
- displayName: t.result.call.displayName,
60
- argsText: JSON.stringify(t.result.call.input ?? {}, null, 2),
61
- resultText: e,
62
- summary: t.result.summary,
63
- errorCode: t.result.errorCode,
64
- trace: t.result.trace,
65
- status: t.result.success ? "done" : "error"
66
- };
67
- }
68
- function X(t) {
69
- return {
70
- id: t.call.id,
71
- name: t.call.name,
72
- displayName: t.call.displayName,
73
- argsText: JSON.stringify(t.call.input ?? {}, null, 2),
74
- status: "running"
75
- };
76
- }
77
- function Z(t, e) {
78
- const n = e.success ? "前端操作已执行。" : e.error || "前端操作执行失败。";
36
+ function H(t, o) {
37
+ const s = o.success ? "前端操作已执行。" : o.error || "前端操作执行失败。";
79
38
  return {
80
39
  id: t.id,
81
40
  name: t.name,
82
41
  displayName: t.displayName,
83
42
  argsText: JSON.stringify(t.input ?? {}, null, 2),
84
- resultText: n,
85
- summary: n,
86
- errorCode: e.errorCode,
87
- trace: e.trace,
88
- status: e.success ? "done" : "error"
43
+ resultText: s,
44
+ summary: s,
45
+ errorCode: o.errorCode,
46
+ trace: o.trace,
47
+ status: o.success ? "done" : "error"
89
48
  };
90
49
  }
91
- function v(t, e) {
92
- return j(t, e?.sideEffectByName?.get(t));
93
- }
94
- function c(t, e, n) {
95
- const s = t.getSteps().find((o) => o.id === e);
96
- !s || s.status === "done" || s.status === "error" || t.setStep(e, "done", s.content ?? n);
50
+ function B(t, o) {
51
+ return b(t, o?.sideEffectByName?.get(t));
97
52
  }
98
- function _(t, e) {
99
- c(t, r.understand);
100
- const n = x(e), s = B(e);
101
- d(t, n, s);
102
- const o = t.getSteps().find((a) => a.id === n);
103
- !o || o.status === "active" || o.status === "done" || t.setStep(n, "active", o.content);
53
+ function p(t, o, s) {
54
+ const e = t.getSteps().find((n) => n.id === o);
55
+ !e || e.status === "done" || e.status === "error" || t.setStep(o, "done", e.content ?? s);
104
56
  }
105
- function p(t, e, n) {
106
- const s = v(e, n);
107
- return _(t, s), x(s);
57
+ function F(t, o) {
58
+ p(t, l.understand);
59
+ const s = R(o), e = x(o);
60
+ f(t, s, e);
61
+ const n = t.getSteps().find((r) => r.id === s);
62
+ !n || n.status === "active" || n.status === "done" || t.setStep(s, "active", n.content);
108
63
  }
109
- function tt(t, e, n) {
110
- const s = p(t, e.name, n);
111
- t.appendToolCallsToStep(s, [
112
- {
113
- id: e.id,
114
- name: e.name,
115
- displayName: e.displayName,
116
- argsText: JSON.stringify(e.input ?? {}, null, 2),
117
- status: "running"
118
- }
119
- ]);
64
+ function h(t, o, s) {
65
+ const e = B(o, s);
66
+ return F(t, e), R(e);
120
67
  }
121
- function et(t, e, n, s) {
122
- const o = p(t, e.name, s);
123
- t.appendToolCallsToStep(o, [Z(e, n)]);
68
+ function q(t, o, s, e) {
69
+ const n = h(t, o.name, e);
70
+ t.appendToolCallsToStep(n, [H(o, s)]);
124
71
  }
125
- function P(t, e) {
126
- return e || (t === r.gather ? "获取信息" : t === r.act ? "操作页面" : t === r.answer ? "回答" : t === r.understand ? "理解问题" : t === r.tool ? "获取信息" : t);
72
+ function S(t, o, s, e, n) {
73
+ f(t, o, s), t.setStep(o, e, n);
127
74
  }
128
- function f(t, e, n, s, o) {
129
- d(t, e, n), t.setStep(e, s, o);
75
+ function O(t) {
76
+ const o = t.status === "completed" ? "done" : t.status === "failed" ? "error" : "running";
77
+ return t.type === "knowledge_search" ? {
78
+ id: t.id,
79
+ name: t.tool,
80
+ displayName: t.displayName,
81
+ argsText: JSON.stringify({ query: t.query }, null, 2),
82
+ resultText: D(t.result),
83
+ summary: t.summary,
84
+ status: o
85
+ } : {
86
+ id: t.id,
87
+ name: t.tool,
88
+ displayName: t.displayName,
89
+ argsText: JSON.stringify(t.arguments ?? {}, null, 2),
90
+ resultText: t.result === void 0 ? void 0 : typeof t.result == "string" ? t.result : JSON.stringify(t.result, null, 2),
91
+ summary: t.summary ?? t.error?.message,
92
+ errorCode: t.error?.code,
93
+ trace: t.trace,
94
+ status: o
95
+ };
130
96
  }
131
- function st(t, e) {
132
- const n = e.summary?.trim();
133
- switch (e.phase) {
134
- case "routing":
135
- case "analyzing":
136
- f(
97
+ async function z(t, o, s) {
98
+ if (o.type !== "ping") {
99
+ if (o.type === "turn.started") {
100
+ S(
137
101
  t,
138
- r.understand,
102
+ l.understand,
139
103
  "理解问题",
140
104
  "active",
141
- n ?? "正在分析用户意图。"
142
- );
143
- return;
144
- case "router_done":
145
- f(
146
- t,
147
- r.understand,
148
- "理解问题",
149
- "done",
150
- n ?? "意图分析已完成。"
151
- );
152
- return;
153
- case "knowledge_agent_start":
154
- c(t, r.understand), f(
155
- t,
156
- r.gather,
157
- "获取信息",
158
- "active",
159
- n ?? "正在检索资料。"
105
+ "正在理解您的问题。"
160
106
  );
161
107
  return;
162
- case "knowledge_agent_done":
163
- c(
164
- t,
165
- r.gather,
166
- n ?? "资料已就绪。"
167
- );
168
- return;
169
- case "action_agent_start":
170
- c(t, r.understand);
171
- return;
172
- case "action_agent_done":
173
- c(t, r.gather), c(
174
- t,
175
- r.act,
176
- n ?? "页面操作已完成。"
177
- );
178
- return;
179
- case "memory_replay":
180
- f(
181
- t,
182
- r.understand,
183
- "理解问题",
184
- "done",
185
- n ?? "已复用项目记忆,跳过本轮检索。"
186
- );
187
- return;
188
- default:
189
- return;
190
- }
191
- }
192
- async function ot(t, e, n) {
193
- if (e.type !== "ping") {
194
- if (e.type === "step_sync")
195
- t.setSteps(
196
- e.steps.map((s) => ({
197
- id: s.id,
198
- label: s.label,
199
- status: s.status,
200
- content: t.getSteps().find((o) => o.id === s.id)?.content
201
- }))
202
- );
203
- else if (e.type === "step_status")
204
- d(t, e.stepId, e.label ?? e.stepId), t.setStep(e.stepId, e.status, e.content);
205
- else if (e.type === "step_content") {
206
- let s = e.stepId, o = e.content;
207
- if ((s === r.intent || s === r.understand) && F(o))
208
- s = r.gather;
209
- else if ((s === r.intent || s === r.understand) && e.mode === "replace") {
210
- const { intent: a, misplacedPlanning: i } = J(o);
211
- o = a, i.trim() && (d(t, r.gather, "获取信息"), t.appendChunkToStep(
212
- r.gather,
213
- i
214
- ));
108
+ }
109
+ if (o.type === "item.started" || o.type === "item.updated" || o.type === "item.completed") {
110
+ const e = o.item;
111
+ if (e.type === "reasoning") {
112
+ if (e.category === "routing" || e.category === "memory")
113
+ S(
114
+ t,
115
+ l.understand,
116
+ "理解问题",
117
+ o.type === "item.completed" ? "done" : "active",
118
+ e.summary
119
+ );
120
+ else {
121
+ const n = /页面|操作|执行/u.test(e.summary), r = n ? l.act : l.gather;
122
+ f(t, r, n ? "操作页面" : "获取信息"), t.setStep(
123
+ r,
124
+ o.type === "item.completed" ? "done" : "active",
125
+ e.summary
126
+ );
127
+ }
128
+ return;
215
129
  }
216
- if (d(t, s, P(s, e.label)), e.mode === "replace")
217
- t.setStep(
218
- s,
219
- t.getSteps().find((a) => a.id === s)?.status ?? "active",
220
- o
130
+ if (e.type === "skill_use") {
131
+ const n = "skills", r = f(t, n, "使用 Skill"), a = `- ${e.displayName}${e.skill === e.displayName ? "" : `(${e.skill})`}`, u = r.content?.includes(a) ? r.content : [r.content, a].filter(Boolean).join(`
132
+ `);
133
+ t.setStep(n, e.status === "failed" ? "error" : "done", u), g().setInvokedSkills([
134
+ ...g().invokedSkills.filter(
135
+ (m) => m.skillName !== e.skill
136
+ ),
137
+ { skillName: e.skill, invokedAt: o.at }
138
+ ]);
139
+ return;
140
+ }
141
+ if (e.type === "tool_call" || e.type === "knowledge_search") {
142
+ const n = (e.type === "tool_call", e.tool), r = h(t, n, s);
143
+ t.appendToolCallsToStep(r, [O(e)]), (e.status === "completed" || e.status === "failed") && p(t, r);
144
+ return;
145
+ }
146
+ if (e.type === "memory") {
147
+ S(
148
+ t,
149
+ l.understand,
150
+ "理解问题",
151
+ e.action === "reuse" ? "done" : "active",
152
+ e.summary
221
153
  );
222
- else {
223
- if (s === r.tool || s === r.gather || s === r.act) {
224
- const a = s === r.act ? "act" : "gather";
225
- _(t, a);
226
- }
227
- t.appendChunkToStep(s, o), await $();
154
+ return;
228
155
  }
229
- } else if (e.type === "step_artifact") {
230
- const s = e.stepId === r.tool ? r.gather : e.stepId;
231
- d(t, s, P(s, e.label)), (s === r.gather || s === r.act) && _(
232
- t,
233
- s === r.act ? "act" : "gather"
234
- ), e.artifact === "tool_calls" && e.toolCalls?.length ? t.appendToolCallsToStep(s, e.toolCalls) : e.artifact === "attachments" && e.attachments?.length ? t.appendAttachmentsToStep(s, e.attachments) : e.artifact === "files" && e.files?.length ? t.appendFilesToStep(s, e.files) : e.artifact === "artifacts" && e.artifacts?.length ? t.appendArtifactsToStep(s, e.artifacts) : e.artifact === "chat_items" && e.chatItems?.length && t.appendChatItemsToStep(s, e.chatItems);
235
- } else if (e.type === "tool_start") {
236
- const s = p(t, e.call.name, n);
237
- t.appendToolCallsToStep(s, [X(e)]);
238
- } else if (e.type === "tool_progress") {
239
- const s = p(t, e.call.name, n);
240
- t.appendToolCallsToStep(s, [
241
- {
242
- id: e.call.id,
243
- name: e.call.name,
244
- displayName: e.call.displayName,
245
- argsText: JSON.stringify(e.call.input ?? {}, null, 2),
246
- summary: e.summary,
247
- status: "running"
248
- }
249
- ]);
250
- } else if (e.type === "tool_result") {
251
- const s = p(t, e.result.call.name, n);
252
- t.appendToolCallsToStep(s, [Q(e)]), e.result.attachments?.length && t.appendAttachmentsToStep(s, e.result.attachments), e.result.files?.length && t.appendFilesToStep(s, e.result.files), e.result.toolCalls?.length && t.appendToolCallsToStep(s, e.result.toolCalls);
253
- } else if (e.type === "skill_permission_request") {
254
- const { useSpotlightStore: s } = await import("../store/spotlightStore.js");
255
- s().setPendingSkillPermission({
256
- skillName: e.skillName,
257
- displayName: e.displayName,
258
- reason: e.reason,
259
- source: e.source,
260
- at: e.at
261
- });
262
- } else if (e.type === "fork_progress") {
263
- const s = r.gather;
264
- d(t, s, "获取信息"), t.appendChunkToStep(
265
- s,
266
- `
267
- [fork ${e.agentId}] 第 ${e.iteration} 轮 · ${e.phase}:${e.summary}
268
- `
269
- );
270
- } else if (e.type === "turn_transition")
271
- st(t, e);
272
- else if (e.type === "memory_decision") {
273
- const s = {
274
- reuse: "已复用项目记忆",
275
- augment: "正在结合历史项目结论",
276
- refresh: "资料可能变化,正在重新验证",
277
- ignore: "未发现可直接使用的项目记忆"
278
- }, o = t.getSteps().find((i) => i.id === r.understand), a = e.decision.action === "reuse" || o?.status === "done" ? "done" : "active";
279
- f(
280
- t,
281
- r.understand,
282
- "理解问题",
283
- a,
284
- s[e.decision.action]
285
- );
286
- } else e.type === "assistant_response" && (c(t, r.understand), c(t, r.gather), c(t, r.act), d(t, r.answer, "回答"), t.setStep(
287
- r.answer,
288
- "done",
289
- e.content.trim()
290
- ));
291
- V(e) && t.appendExecutionEvent?.(e);
292
- }
293
- }
294
- async function nt(t) {
295
- await fetch(
296
- `${w()}/v1/runs/${encodeURIComponent(t.runId)}/host-results`,
297
- {
298
- method: "POST",
299
- headers: h(),
300
- body: JSON.stringify({
301
- correlationId: t.correlationId,
302
- success: t.result.success,
303
- output: t.result.data,
304
- error: t.result.error,
305
- errorCode: t.result.errorCode,
306
- trace: t.result.trace
307
- }),
308
- signal: t.signal
309
- }
310
- );
311
- }
312
- function rt(t) {
313
- const e = t.split(/\n\n/u), n = e.pop() ?? "";
314
- return { events: e.flatMap((o) => {
315
- const a = o.split(/\n/u).find((i) => i.startsWith("data: "));
316
- if (!a) return [];
317
- try {
318
- return [JSON.parse(a.slice(6))];
319
- } catch {
320
- return [];
156
+ if (e.type === "agent_message") {
157
+ p(t, l.understand), p(t, l.gather), p(t, l.act), f(t, l.answer, "回答"), t.setStep(l.answer, "done", e.text.trim());
158
+ return;
159
+ }
160
+ e.type === "error" && t.setError(e.message);
161
+ return;
321
162
  }
322
- }), rest: n };
163
+ o.type === "turn.failed" && t.setError(o.error.message);
164
+ }
323
165
  }
324
- async function Tt(t) {
166
+ async function ot(t) {
325
167
  await Promise.allSettled([
326
- L(),
327
- U(t)
168
+ v().initialize(t),
169
+ L(t)
328
170
  ]);
329
171
  }
330
- async function at(t, e, n) {
331
- const s = E(), o = D(), a = q(), i = await L();
172
+ async function J(t, o, s) {
173
+ const e = g(), n = P(), r = N(), a = await M();
332
174
  return {
333
- hostManifest: i,
175
+ hostManifest: a,
334
176
  payload: {
335
- projectId: G(),
336
- sessionId: s.sessionId,
337
- memorySubjectId: N().getMemorySubjectId?.() ?? void 0,
338
- userQuestion: t,
339
- memoryRefreshRequested: n?.forceMemoryRefresh === !0,
340
- uiContext: N().getUiContext?.() ?? {},
177
+ projectId: _(),
178
+ input: t,
179
+ memorySubjectId: T().getMemorySubjectId?.() ?? void 0,
180
+ memoryRefreshRequested: s?.forceMemoryRefresh === !0,
181
+ uiContext: T().getUiContext?.() ?? {},
341
182
  sessionState: {
342
- sessionId: s.sessionId,
343
- activeTaskId: s.activeTaskId,
344
- activeTopic: s.activeTopic,
345
- pendingTask: s.pendingTask,
346
- conversationSummary: s.conversationSummary,
347
- summarizedTurnCount: s.summarizedTurnCount,
348
- conversationHistory: s.conversationHistory,
349
- lastAssistantReply: s.getLastAssistantContent(),
350
- invokedSkills: s.invokedSkills,
351
- skillPermissionGrants: s.skillPermissionGrants,
352
- memoryEnabled: a.enabled,
353
- memoryReadEnabled: a.enabled,
183
+ sessionId: e.sessionId,
184
+ activeTaskId: e.activeTaskId,
185
+ activeTopic: e.activeTopic,
186
+ pendingTask: e.pendingTask,
187
+ conversationSummary: e.conversationSummary,
188
+ summarizedTurnCount: e.summarizedTurnCount,
189
+ conversationHistory: e.conversationHistory,
190
+ lastAssistantReply: e.getLastAssistantContent(),
191
+ invokedSkills: e.invokedSkills,
192
+ skillPermissionGrants: e.skillPermissionGrants,
193
+ memoryEnabled: r.enabled,
194
+ memoryReadEnabled: r.enabled,
354
195
  memoryWriteEnabled: !0
355
196
  },
356
197
  runtimeState: {
357
- activeDomain: o.activeDomain,
358
- activeTarget: o.activeTarget,
359
- activeAction: o.activeAction,
360
- resumableAction: o.resumableAction,
361
- lastResolvedTarget: o.lastResolvedTarget
198
+ activeDomain: n.activeDomain,
199
+ activeTarget: n.activeTarget,
200
+ activeAction: n.activeAction,
201
+ resumableAction: n.resumableAction,
202
+ lastResolvedTarget: n.lastResolvedTarget
362
203
  },
363
- clientToolsManifestVersion: i.manifestDigest,
364
- clientToolManifest: i,
365
- skills: H(W()),
366
- frontendBuildId: i.frontendBuildId,
367
- manifestDigest: i.manifestDigest
204
+ clientToolsManifestVersion: a.manifestDigest,
205
+ clientToolManifest: a,
206
+ skills: C(E()),
207
+ frontendBuildId: a.frontendBuildId,
208
+ manifestDigest: a.manifestDigest
368
209
  }
369
210
  };
370
211
  }
371
- async function It(t, e, n) {
372
- const s = w(), o = e.getSignal(), { payload: a, hostManifest: i } = await at(
212
+ async function nt(t, o, s) {
213
+ const e = o.getSignal(), { payload: n, hostManifest: r } = await J(
373
214
  t,
374
- o,
375
- n
376
- ), m = new Set(i.tools.map((u) => u.name)), T = {
215
+ e,
216
+ s
217
+ ), a = new Set(r.tools.map((i) => i.name)), u = {
377
218
  sideEffectByName: new Map(
378
- i.tools.map((u) => [u.name, u.sideEffect])
219
+ r.tools.map((i) => [i.name, i.sideEffect])
379
220
  )
380
- }, g = await fetch(`${s}/v1/runs`, {
381
- method: "POST",
382
- headers: h(),
383
- body: JSON.stringify(a),
384
- signal: o
385
- });
386
- if (!g.ok) {
387
- const u = await g.text().catch(() => "");
388
- throw new Error(
389
- `Spotlight 后端创建 run 失败:${g.status} ${u}`
390
- );
391
- }
392
- const { runId: y } = await g.json();
393
- o && (C.set(o, y), o.addEventListener(
221
+ }, m = v(), I = g(), w = await m.startThread(I.sessionId, e), y = await m.startTurn(w.id, n, e);
222
+ e && (k.set(e, y.id), e.addEventListener(
394
223
  "abort",
395
224
  () => {
396
- A(y);
225
+ m.cancelTurn(y.id);
397
226
  },
398
227
  { once: !0 }
399
228
  ));
400
- const S = await fetch(
401
- `${s}/v1/runs/${encodeURIComponent(y)}/events`,
402
- {
403
- headers: h(),
404
- signal: o
229
+ for await (const i of m.streamTurn(y.id, e)) {
230
+ if (await z(o, i, u), (i.type === "item.started" || i.type === "item.updated") && i.item.type === "tool_call" && i.item.status === "waiting_for_client" && i.item.clientRequest) {
231
+ const d = {
232
+ id: i.item.id,
233
+ name: i.item.tool,
234
+ input: i.item.arguments,
235
+ displayName: i.item.displayName
236
+ }, c = await A(d, o, {
237
+ allowedHostNames: a
238
+ });
239
+ q(o, d, c, u), await m.submitToolResult(y.id, {
240
+ correlationId: i.item.clientRequest.correlationId,
241
+ success: c.success,
242
+ output: c.data,
243
+ error: c.error,
244
+ errorCode: c.errorCode,
245
+ trace: c.trace,
246
+ uiContext: T().getUiContext?.() ?? void 0
247
+ }, e);
248
+ continue;
405
249
  }
406
- );
407
- if (!S.ok || !S.body)
408
- throw new Error(`Spotlight 后端事件流连接失败:${S.status}`);
409
- const R = S.body.getReader(), M = new TextDecoder();
410
- let I = "";
411
- try {
412
- for (; ; ) {
413
- const { done: u, value: O } = await R.read();
414
- if (u) break;
415
- I += M.decode(O, { stream: !0 });
416
- const b = rt(I);
417
- I = b.rest;
418
- for (const l of b.events) {
419
- if (l.type === "host_action_request") {
420
- tt(e, l.request.call, T);
421
- const k = await z(l.request.call, e, {
422
- allowedHostNames: m,
423
- hostEffect: l.request.hostEffect
424
- });
425
- et(e, l.request.call, k, T), await nt({
426
- runId: y,
427
- correlationId: l.request.correlationId,
428
- result: k,
429
- signal: o
430
- });
431
- continue;
432
- }
433
- if (l.type === "run_error")
434
- throw new Error(l.error);
435
- if (await ot(e, l, T), await $(), l.type === "run_completed")
436
- return l.sessionPatch && E().applySessionPatch(l.sessionPatch), {
437
- command: null,
438
- memoryReplay: l.memoryReplay ?? null,
439
- memoryDecision: l.memoryDecision ?? null,
440
- assistantReply: l.assistantReply
441
- };
442
- }
250
+ if (await j(), i.type === "turn.failed")
251
+ throw new Error(i.error.message);
252
+ if (i.type === "turn.completed") {
253
+ const d = i.metadata ?? {}, c = d.sessionPatch;
254
+ return c && I.applySessionPatch(c), {
255
+ command: null,
256
+ memoryReplay: d.memoryReplay ?? null,
257
+ memoryDecision: d.memoryDecision ?? null,
258
+ assistantReply: i.finalResponse
259
+ };
443
260
  }
444
- } finally {
445
- R.releaseLock();
446
261
  }
447
- return {
448
- command: null
449
- };
262
+ return { command: null };
450
263
  }
451
- async function A(t) {
452
- await fetch(
453
- `${w()}/v1/runs/${encodeURIComponent(t)}`,
454
- {
455
- method: "DELETE",
456
- headers: h()
457
- }
458
- );
264
+ async function $(t) {
265
+ await v().cancelTurn(t);
459
266
  }
460
- function Ct(t) {
267
+ function st(t) {
461
268
  if (!t) return;
462
- const e = C.get(t);
463
- e && (C.delete(t), A(e));
269
+ const o = k.get(t);
270
+ o && (k.delete(t), $(o));
464
271
  }
465
272
  export {
466
- st as applyLangGraphTransition,
467
- ot as applyRemoteEvent,
468
- tt as beginHostToolCall,
469
- A as cancelRemoteSpotlightRun,
470
- Ct as cancelRemoteSpotlightRunForSignal,
471
- It as runRemoteSpotlightPipeline,
472
- et as settleHostToolCall,
473
- Tt as warmupSpotlightRemoteContext
273
+ z as applyLifecycleEvent,
274
+ $ as cancelRemoteSpotlightRun,
275
+ st as cancelRemoteSpotlightRunForSignal,
276
+ nt as runRemoteSpotlightPipeline,
277
+ q as settleHostToolCall,
278
+ ot as warmupSpotlightRemoteContext
474
279
  };