@opentiny/next-sdk 0.2.8 → 0.2.9

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.
@@ -50466,6 +50466,7 @@ const MSG_PAGE_LEAVE = "next-sdk:page-leave";
50466
50466
  const MSG_REMOTER_READY = "next-sdk:remoter-ready";
50467
50467
  const MSG_ROUTE_STATE_INITIAL = "next-sdk:route-state-initial";
50468
50468
  const activePages = /* @__PURE__ */ new Map();
50469
+ const normalizeRoute = (value) => value.replace(/\/+$/, "") || "/";
50469
50470
  const broadcastTargets = /* @__PURE__ */ new Set();
50470
50471
  function initBroadcastTargets() {
50471
50472
  if (typeof window !== "undefined") {
@@ -50512,6 +50513,88 @@ let _navigator = null;
50512
50513
  function setNavigator(fn) {
50513
50514
  _navigator = fn;
50514
50515
  }
50516
+ function waitForPageReady(path, timeoutMs = 1500) {
50517
+ if (typeof window === "undefined") {
50518
+ return Promise.resolve();
50519
+ }
50520
+ const target = normalizeRoute(path);
50521
+ return new Promise((resolve2) => {
50522
+ let done = false;
50523
+ const cleanup = () => {
50524
+ if (done) return;
50525
+ done = true;
50526
+ window.removeEventListener("message", handleMessage);
50527
+ resolve2();
50528
+ };
50529
+ const handleMessage = (event) => {
50530
+ if (event.source !== window || event.data?.type !== MSG_PAGE_READY) return;
50531
+ const route = normalizeRoute(String(event.data.route ?? ""));
50532
+ if (route === target) {
50533
+ cleanup();
50534
+ }
50535
+ };
50536
+ window.addEventListener("message", handleMessage);
50537
+ setTimeout(cleanup, timeoutMs);
50538
+ });
50539
+ }
50540
+ function registerNavigateTool(server, options) {
50541
+ const name16 = options?.name ?? "navigate_to_page";
50542
+ const title2 = options?.title ?? "页面跳转";
50543
+ const description2 = options?.description ?? '当需要的工具在当前页面不可用时,使用此工具跳转到特定页面。例如:要查询订单时跳转到 "/orders",要创建价保时跳转到 "/price-protection"。';
50544
+ const timeoutMs = options?.timeoutMs ?? 1500;
50545
+ return server.registerTool(
50546
+ name16,
50547
+ {
50548
+ title: title2,
50549
+ description: description2,
50550
+ inputSchema: {
50551
+ path: stringType().describe('目标页面的路由地址,例如 "/orders"、"/inventory"、"/price-protection" 等。')
50552
+ }
50553
+ },
50554
+ async ({ path }) => {
50555
+ if (typeof window === "undefined") {
50556
+ return {
50557
+ content: [{ type: "text", text: "当前环境不支持页面跳转(window 不存在)。" }]
50558
+ };
50559
+ }
50560
+ if (!_navigator) {
50561
+ return {
50562
+ content: [
50563
+ {
50564
+ type: "text",
50565
+ text: "页面跳转失败:尚未在应用入口调用 setNavigator 注册导航函数,无法执行路由跳转。"
50566
+ }
50567
+ ]
50568
+ };
50569
+ }
50570
+ try {
50571
+ const target = normalizeRoute(path);
50572
+ const current = normalizeRoute(window.location.pathname);
50573
+ const isAlreadyOnTarget = current === target || current.endsWith(target) && (current.length === target.length || current[current.lastIndexOf(target) - 1] === "/");
50574
+ if (isAlreadyOnTarget) {
50575
+ return {
50576
+ content: [{ type: "text", text: `当前已在页面:${path}。请继续你的下一步操作。` }]
50577
+ };
50578
+ }
50579
+ const readyPromise = waitForPageReady(path, timeoutMs);
50580
+ await _navigator(path);
50581
+ await readyPromise;
50582
+ return {
50583
+ content: [{ type: "text", text: `已成功跳转至页面:${path}。请继续你的下一步操作。` }]
50584
+ };
50585
+ } catch (err) {
50586
+ return {
50587
+ content: [
50588
+ {
50589
+ type: "text",
50590
+ text: `页面跳转失败:${err instanceof Error ? err.message : String(err)}。`
50591
+ }
50592
+ ]
50593
+ };
50594
+ }
50595
+ }
50596
+ );
50597
+ }
50515
50598
  function buildPageHandler(name16, route, timeout = 3e4, effectConfig) {
50516
50599
  return (input) => {
50517
50600
  const callId = randomUUID();
@@ -50601,10 +50684,10 @@ function withPageTools(server) {
50601
50684
  }
50602
50685
  function registerPageTool(options) {
50603
50686
  const { route: routeOption, handlers } = options;
50604
- const normalizeRoute = (value) => value.replace(/\/+$/, "") || "/";
50605
- const route = normalizeRoute(routeOption ?? window.location.pathname);
50687
+ const normalizeRoute2 = (value) => value.replace(/\/+$/, "") || "/";
50688
+ const route = normalizeRoute2(routeOption ?? window.location.pathname);
50606
50689
  const handleMessage = async (event) => {
50607
- if (event.source !== window || event.data?.type !== MSG_TOOL_CALL || normalizeRoute(String(event.data?.route ?? "")) !== route || !(event.data.toolName in handlers)) {
50690
+ if (event.source !== window || event.data?.type !== MSG_TOOL_CALL || normalizeRoute2(String(event.data?.route ?? "")) !== route || !(event.data.toolName in handlers)) {
50608
50691
  return;
50609
50692
  }
50610
50693
  const { callId, toolName, input } = event.data;
@@ -50826,6 +50909,7 @@ export {
50826
50909
  isSSEClientTransport,
50827
50910
  isStreamableHTTPClientTransport,
50828
50911
  parseSkillFrontMatter,
50912
+ registerNavigateTool,
50829
50913
  registerPageTool,
50830
50914
  setNavigator,
50831
50915
  withPageTools,