@kingsoftcloud/agentkit-harness-ui 0.0.17 → 0.0.18
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/package.json +5 -4
- package/src/engine-view/adapter.mjs +69 -0
- package/src/engine-view/app.mjs +54 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kingsoftcloud/agentkit-harness-ui",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.18",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Reusable React task-process UI for AgentKit Native: an explicitly versioned event protocol, a pure state reducer, and an accessible timeline. No Studio/DSH/Python imports; React is a peer dependency.",
|
|
@@ -41,7 +41,8 @@
|
|
|
41
41
|
"./host/native-approval-actions": "./src/host/native-approval-actions.mjs",
|
|
42
42
|
"./host/native-a2a": "./src/host/native-a2a.mjs",
|
|
43
43
|
"./host/state/native-a2a": "./src/host/state/native-a2a.mjs",
|
|
44
|
-
"./host/state/native-task-read-model": "./src/host/state/native-task-read-model.mjs"
|
|
44
|
+
"./host/state/native-task-read-model": "./src/host/state/native-task-read-model.mjs",
|
|
45
|
+
"./engine-view/adapter": "./src/engine-view/adapter.mjs"
|
|
45
46
|
},
|
|
46
47
|
"files": [
|
|
47
48
|
"src",
|
|
@@ -66,7 +67,7 @@
|
|
|
66
67
|
},
|
|
67
68
|
"scripts": {
|
|
68
69
|
"test": "node --test test/*.test.mjs",
|
|
69
|
-
"build": "node markdown/build/build-markdown.cjs"
|
|
70
|
+
"build": "node markdown/build/build-markdown.cjs && node scripts/build-engine-view.mjs"
|
|
70
71
|
},
|
|
71
72
|
"license": "Apache-2.0",
|
|
72
73
|
"engines": {
|
|
@@ -97,6 +98,6 @@
|
|
|
97
98
|
"registry": "https://registry.npmjs.org"
|
|
98
99
|
},
|
|
99
100
|
"dependencies": {
|
|
100
|
-
"@kingsoftcloud/agentkit-harness-core": "0.0.
|
|
101
|
+
"@kingsoftcloud/agentkit-harness-core": "0.0.18"
|
|
101
102
|
}
|
|
102
103
|
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// agentkit.engine-event/v1 -> harness-ui protocol v1.
|
|
2
|
+
//
|
|
3
|
+
// Pure and React-free. The engine projects its journal into engine events; this
|
|
4
|
+
// adapter turns them into the timeline's own event stream. It is deterministic
|
|
5
|
+
// and keyed by engine sequence numbers, so replaying any prefix is a no-op.
|
|
6
|
+
import { PROTOCOL_VERSION } from "../protocol.mjs";
|
|
7
|
+
|
|
8
|
+
export const ENGINE_EVENT_SCHEMA = "agentkit.engine-event/v1";
|
|
9
|
+
export const ENGINE_VIEW_CONTRACT = "agentkit.engine-view/v1";
|
|
10
|
+
|
|
11
|
+
const OUTCOMES = new Set(["completed", "unverified", "blocked", "failed", "cancelled"]);
|
|
12
|
+
const text = value => typeof value === "string" && value.trim().length > 0;
|
|
13
|
+
const input = value => value === undefined || value === null ? {} : typeof value === "object" ? value : { value };
|
|
14
|
+
|
|
15
|
+
function outcomeOf(event) {
|
|
16
|
+
if (event.title === "failed" || event.status === "failed") return "failed";
|
|
17
|
+
if (event.title === "cancelled" || event.status === "cancelled") return "cancelled";
|
|
18
|
+
if (OUTCOMES.has(event.status)) return event.status;
|
|
19
|
+
return event.title === "completed" ? "completed" : "unverified";
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Convert one engine event; returns zero or more harness-ui events. */
|
|
23
|
+
export function engineEventToTimeline(event, { runId = "run" } = {}) {
|
|
24
|
+
if (!event || event.schema !== ENGINE_EVENT_SCHEMA || !Number.isSafeInteger(event.seq)) return [];
|
|
25
|
+
const v = PROTOCOL_VERSION, id = suffix => `${runId}:${event.seq}:${suffix}`;
|
|
26
|
+
switch (event.kind) {
|
|
27
|
+
case "run.started":
|
|
28
|
+
return [{ v, id: id("start"), type: "task.started", taskId: runId, runId }];
|
|
29
|
+
case "model.completed": {
|
|
30
|
+
const out = [];
|
|
31
|
+
// Commentary that accompanies tool calls is progress, not the answer.
|
|
32
|
+
if (text(event.detail) && (event.title === "initial_plan" || Array.isArray(event.calls) && event.calls.length)) {
|
|
33
|
+
const itemId = `${runId}:note:${event.seq}`;
|
|
34
|
+
out.push({ v, id: id("note"), type: "text.delta", itemId, delta: event.detail },
|
|
35
|
+
{ v, id: id("note-done"), type: "text.done", itemId });
|
|
36
|
+
}
|
|
37
|
+
for (const call of Array.isArray(event.calls) ? event.calls : []) {
|
|
38
|
+
if (!text(call?.callId) || !text(call?.name)) continue;
|
|
39
|
+
out.push({ v, id: id(`call:${call.callId}`), type: "tool.scheduled", callId: call.callId, name: call.name, input: input(call.arguments) },
|
|
40
|
+
{ v, id: id(`run:${call.callId}`), type: "tool.started", callId: call.callId });
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
case "tool.completed": {
|
|
45
|
+
if (!text(event.callId)) return [];
|
|
46
|
+
return event.status === "ok"
|
|
47
|
+
? [{ v, id: id("result"), type: "tool.result", callId: event.callId, result: event.result ?? null }]
|
|
48
|
+
: [{ v, id: id("error"), type: "tool.error", callId: event.callId,
|
|
49
|
+
error: { message: typeof event.result?.message === "string" ? event.result.message : `工具 ${event.title ?? ""} 未成功`.trim() } }];
|
|
50
|
+
}
|
|
51
|
+
case "model.failure_observed":
|
|
52
|
+
return [{ v, id: id("retry"), type: "task.state", state: "running", reason: "model_retry", message: `模型请求失败(${event.detail ?? "unknown"}),正在重试。` }];
|
|
53
|
+
case "run.terminal": {
|
|
54
|
+
const out = [];
|
|
55
|
+
if (text(event.detail)) {
|
|
56
|
+
const itemId = `${runId}:final`;
|
|
57
|
+
out.push({ v, id: id("final"), type: "text.delta", itemId, delta: event.detail }, { v, id: id("final-done"), type: "text.done", itemId });
|
|
58
|
+
}
|
|
59
|
+
if (event.title === "paused") return [...out, { v, id: id("paused"), type: "task.state", state: "paused", reason: event.reason ?? null }];
|
|
60
|
+
return [...out, { v, id: id("finished"), type: "task.finished", outcome: outcomeOf(event), ...(event.reason ? { reason: event.reason } : {}) }];
|
|
61
|
+
}
|
|
62
|
+
default:
|
|
63
|
+
return [];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function engineEventsToTimeline(events, options) {
|
|
68
|
+
return (Array.isArray(events) ? events : []).flatMap(event => engineEventToTimeline(event, options));
|
|
69
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Browser entry for the engine's own task view (agentkit.engine-view/v1).
|
|
2
|
+
//
|
|
3
|
+
// Bundled into one self-contained HTML document that the engine hands to any
|
|
4
|
+
// host over the protocol. The host embeds it in a sandboxed iframe and only
|
|
5
|
+
// relays engine events; it renders nothing engine-specific itself. Changing
|
|
6
|
+
// how a task looks therefore ships with an engine release.
|
|
7
|
+
//
|
|
8
|
+
// Host -> view messages: { contract, type: "events", runId, events: EngineEvent[] }
|
|
9
|
+
// { contract, type: "reset", runId }
|
|
10
|
+
// { contract, type: "theme", theme: "light" | "dark" | "system" }
|
|
11
|
+
// View -> host messages: { contract, type: "ready", version }
|
|
12
|
+
// { contract, type: "height", height }
|
|
13
|
+
// Messages are accepted only from the embedding parent window.
|
|
14
|
+
import React from "react";
|
|
15
|
+
import { createRoot } from "react-dom/client";
|
|
16
|
+
import { createHarnessComponents } from "../react-binding.mjs";
|
|
17
|
+
import { initialState, reduceAll } from "../reducer.mjs";
|
|
18
|
+
import { engineEventsToTimeline, ENGINE_VIEW_CONTRACT } from "./adapter.mjs";
|
|
19
|
+
|
|
20
|
+
const VERSION = globalThis.__ENGINE_VIEW_VERSION__ ?? "dev";
|
|
21
|
+
const { HarnessTimeline } = createHarnessComponents(React);
|
|
22
|
+
const post = message => { try { window.parent.postMessage({ contract: ENGINE_VIEW_CONTRACT, ...message }, "*"); } catch { /* detached */ } };
|
|
23
|
+
|
|
24
|
+
function App() {
|
|
25
|
+
const [view, setView] = React.useState({ runId: "run", state: initialState(), theme: "system" });
|
|
26
|
+
React.useEffect(() => {
|
|
27
|
+
const onMessage = message => {
|
|
28
|
+
if (message.source !== window.parent) return;
|
|
29
|
+
const data = message.data;
|
|
30
|
+
if (!data || data.contract !== ENGINE_VIEW_CONTRACT) return;
|
|
31
|
+
if (data.type === "theme" && ["light", "dark", "system"].includes(data.theme)) {
|
|
32
|
+
// The view's text and native controls follow the host's scheme; its background stays transparent.
|
|
33
|
+
document.documentElement.style.colorScheme = data.theme === "system" ? "light dark" : data.theme;
|
|
34
|
+
setView(current => ({ ...current, theme: data.theme }));
|
|
35
|
+
}
|
|
36
|
+
else if (data.type === "reset") setView(current => ({ ...current, runId: String(data.runId || "run"), state: initialState() }));
|
|
37
|
+
else if (data.type === "events" && Array.isArray(data.events)) setView(current => {
|
|
38
|
+
const runId = String(data.runId || current.runId);
|
|
39
|
+
const base = runId === current.runId ? current.state : initialState();
|
|
40
|
+
return { ...current, runId, state: reduceAll(base, engineEventsToTimeline(data.events, { runId })) };
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
window.addEventListener("message", onMessage);
|
|
44
|
+
post({ type: "ready", version: VERSION });
|
|
45
|
+
// Measure the content, not the document: the document is never shorter than the iframe, so it could only grow.
|
|
46
|
+
const root = document.getElementById("root");
|
|
47
|
+
const observer = new ResizeObserver(() => post({ type: "height", height: Math.ceil(root.getBoundingClientRect().height) }));
|
|
48
|
+
observer.observe(root);
|
|
49
|
+
return () => { window.removeEventListener("message", onMessage); observer.disconnect(); };
|
|
50
|
+
}, []);
|
|
51
|
+
return React.createElement(HarnessTimeline, { state: view.state, theme: view.theme, autoFollow: false });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
createRoot(document.getElementById("root")).render(React.createElement(App));
|