feat(runtime): establish miniapp kernel and sdk design
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* Chat Core App 的 Host 装配器。
|
||||
*
|
||||
* Chat 的样式、Shell、Renderer 和 DOM 上下文都属于 Chat 自己;Runtime Host
|
||||
* 只负责根据 app_scope 选择这个装配器,main.ts 不再直接依赖 Chat 的内部模块。
|
||||
*/
|
||||
import "@/core-apps/chat/styles/app.css";
|
||||
import "@/core-apps/chat/styles/capability-card.css";
|
||||
import "@/core-apps/chat/styles/execution-progress.css";
|
||||
import { LineUpRuntime } from "@/runtime/coordination/lineup-runtime";
|
||||
import type { ChatRuntimeSDK } from "@/runtime/app-management/app-sdk";
|
||||
import { mountChatShell } from "@/core-apps/chat/chat-shell";
|
||||
import { RendererRegistry } from "@/core-apps/chat/renderer-registry";
|
||||
import {
|
||||
TrustedDOMRendererContext,
|
||||
type CapabilityInteractionHandlers,
|
||||
type TaskInteractionHandlers,
|
||||
type ToolInteractionHandlers,
|
||||
} from "@/core-apps/chat/trusted-dom-renderers";
|
||||
|
||||
export type ChatAppHostOptions = {
|
||||
toolInteractions?: ToolInteractionHandlers;
|
||||
taskInteractions?: TaskInteractionHandlers;
|
||||
capabilityInteractions?: CapabilityInteractionHandlers;
|
||||
};
|
||||
|
||||
export type MountedChatApp = {
|
||||
app_scope: "chat";
|
||||
sdk: ChatRuntimeSDK;
|
||||
messages: HTMLElement;
|
||||
loginView: HTMLElement;
|
||||
chatView: HTMLElement;
|
||||
presence: HTMLElement;
|
||||
rendererContext: TrustedDOMRendererContext;
|
||||
rendererRegistry: RendererRegistry<TrustedDOMRendererContext>;
|
||||
};
|
||||
|
||||
function query<T extends Element>(root: ParentNode, selector: string): T {
|
||||
const found = root.querySelector<T>(selector);
|
||||
if (!found) throw new Error(`Missing Chat element: ${selector}`);
|
||||
return found;
|
||||
}
|
||||
|
||||
/** Mounts the trusted Chat implementation selected by the Runtime App Host. */
|
||||
export function mountChatApp(
|
||||
runtime: LineUpRuntime,
|
||||
root: HTMLElement,
|
||||
options: ChatAppHostOptions,
|
||||
): MountedChatApp {
|
||||
mountChatShell(root);
|
||||
const messages = query<HTMLElement>(root, "#messages");
|
||||
const loginView = query<HTMLElement>(root, "#login-view");
|
||||
const chatView = query<HTMLElement>(root, "#chat-view");
|
||||
const presence = query<HTMLElement>(root, "#presence");
|
||||
const rendererContext = new TrustedDOMRendererContext(
|
||||
messages,
|
||||
presence,
|
||||
options.toolInteractions,
|
||||
options.taskInteractions,
|
||||
options.capabilityInteractions,
|
||||
);
|
||||
const rendererRegistry = new RendererRegistry<TrustedDOMRendererContext>();
|
||||
registerChatRenderers(rendererRegistry);
|
||||
|
||||
return {
|
||||
app_scope: "chat",
|
||||
sdk: runtime.openApp("chat"),
|
||||
messages,
|
||||
loginView,
|
||||
chatView,
|
||||
presence,
|
||||
rendererContext,
|
||||
rendererRegistry,
|
||||
};
|
||||
}
|
||||
|
||||
function registerChatRenderers(registry: RendererRegistry<TrustedDOMRendererContext>): void {
|
||||
registry.register("user-message", (item, context) => context.renderUserMessage(item));
|
||||
registry.register("markdown", (item, context) => context.renderMarkdown(item));
|
||||
registry.register("agent-status", (item, context) => context.renderAgentStatus(item));
|
||||
registry.register("execution-summary", (item, context) => context.renderExecutionTrace(item));
|
||||
registry.register("progress", (item, context) => context.renderProgress(item));
|
||||
registry.register("error", (item, context) => context.renderError(item));
|
||||
registry.register("tool-call", (item, context) => context.renderToolCall(item));
|
||||
registry.register("tool-result", (item, context) => context.renderToolResult(item));
|
||||
registry.register("tool-cancel", (item, context) => context.renderToolCancel(item));
|
||||
registry.register("surface", (item, context) => context.renderSurface(item));
|
||||
registry.register("app-call", (item, context) => context.renderAppCall(item));
|
||||
registry.register("fallback", (item, context) => context.renderFallback(item));
|
||||
}
|
||||
Reference in New Issue
Block a user