@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.
@@ -50859,6 +50859,7 @@ ${observationText}
50859
50859
  const MSG_REMOTER_READY = "next-sdk:remoter-ready";
50860
50860
  const MSG_ROUTE_STATE_INITIAL = "next-sdk:route-state-initial";
50861
50861
  const activePages = /* @__PURE__ */ new Map();
50862
+ const normalizeRoute = (value) => value.replace(/\/+$/, "") || "/";
50862
50863
  const broadcastTargets = /* @__PURE__ */ new Set();
50863
50864
  function initBroadcastTargets() {
50864
50865
  if (typeof window !== "undefined") {
@@ -50905,6 +50906,88 @@ ${observationText}
50905
50906
  function setNavigator(fn) {
50906
50907
  _navigator = fn;
50907
50908
  }
50909
+ function waitForPageReady(path, timeoutMs = 1500) {
50910
+ if (typeof window === "undefined") {
50911
+ return Promise.resolve();
50912
+ }
50913
+ const target = normalizeRoute(path);
50914
+ return new Promise((resolve2) => {
50915
+ let done = false;
50916
+ const cleanup = () => {
50917
+ if (done) return;
50918
+ done = true;
50919
+ window.removeEventListener("message", handleMessage);
50920
+ resolve2();
50921
+ };
50922
+ const handleMessage = (event) => {
50923
+ if (event.source !== window || event.data?.type !== MSG_PAGE_READY) return;
50924
+ const route = normalizeRoute(String(event.data.route ?? ""));
50925
+ if (route === target) {
50926
+ cleanup();
50927
+ }
50928
+ };
50929
+ window.addEventListener("message", handleMessage);
50930
+ setTimeout(cleanup, timeoutMs);
50931
+ });
50932
+ }
50933
+ function registerNavigateTool(server, options) {
50934
+ const name16 = options?.name ?? "navigate_to_page";
50935
+ const title2 = options?.title ?? "页面跳转";
50936
+ const description2 = options?.description ?? '当需要的工具在当前页面不可用时,使用此工具跳转到特定页面。例如:要查询订单时跳转到 "/orders",要创建价保时跳转到 "/price-protection"。';
50937
+ const timeoutMs = options?.timeoutMs ?? 1500;
50938
+ return server.registerTool(
50939
+ name16,
50940
+ {
50941
+ title: title2,
50942
+ description: description2,
50943
+ inputSchema: {
50944
+ path: stringType().describe('目标页面的路由地址,例如 "/orders"、"/inventory"、"/price-protection" 等。')
50945
+ }
50946
+ },
50947
+ async ({ path }) => {
50948
+ if (typeof window === "undefined") {
50949
+ return {
50950
+ content: [{ type: "text", text: "当前环境不支持页面跳转(window 不存在)。" }]
50951
+ };
50952
+ }
50953
+ if (!_navigator) {
50954
+ return {
50955
+ content: [
50956
+ {
50957
+ type: "text",
50958
+ text: "页面跳转失败:尚未在应用入口调用 setNavigator 注册导航函数,无法执行路由跳转。"
50959
+ }
50960
+ ]
50961
+ };
50962
+ }
50963
+ try {
50964
+ const target = normalizeRoute(path);
50965
+ const current = normalizeRoute(window.location.pathname);
50966
+ const isAlreadyOnTarget = current === target || current.endsWith(target) && (current.length === target.length || current[current.lastIndexOf(target) - 1] === "/");
50967
+ if (isAlreadyOnTarget) {
50968
+ return {
50969
+ content: [{ type: "text", text: `当前已在页面:${path}。请继续你的下一步操作。` }]
50970
+ };
50971
+ }
50972
+ const readyPromise = waitForPageReady(path, timeoutMs);
50973
+ await _navigator(path);
50974
+ await readyPromise;
50975
+ return {
50976
+ content: [{ type: "text", text: `已成功跳转至页面:${path}。请继续你的下一步操作。` }]
50977
+ };
50978
+ } catch (err) {
50979
+ return {
50980
+ content: [
50981
+ {
50982
+ type: "text",
50983
+ text: `页面跳转失败:${err instanceof Error ? err.message : String(err)}。`
50984
+ }
50985
+ ]
50986
+ };
50987
+ }
50988
+ }
50989
+ );
50990
+ }
50908
50991
  function buildPageHandler(name16, route, timeout = 3e4, effectConfig) {
50909
50992
  return (input) => {
50910
50993
  const callId = randomUUID();
@@ -50994,10 +51077,10 @@ ${observationText}
50994
51077
  }
50995
51078
  function registerPageTool(options) {
50996
51079
  const { route: routeOption, handlers } = options;
50997
- const normalizeRoute = (value) => value.replace(/\/+$/, "") || "/";
50998
- const route = normalizeRoute(routeOption ?? window.location.pathname);
51080
+ const normalizeRoute2 = (value) => value.replace(/\/+$/, "") || "/";
51081
+ const route = normalizeRoute2(routeOption ?? window.location.pathname);
50999
51082
  const handleMessage = async (event) => {
51000
- if (event.source !== window || event.data?.type !== MSG_TOOL_CALL || normalizeRoute(String(event.data?.route ?? "")) !== route || !(event.data.toolName in handlers)) {
51083
+ if (event.source !== window || event.data?.type !== MSG_TOOL_CALL || normalizeRoute2(String(event.data?.route ?? "")) !== route || !(event.data.toolName in handlers)) {
51001
51084
  return;
51002
51085
  }
51003
51086
  const { callId, toolName, input } = event.data;
@@ -51218,6 +51301,7 @@ ${lines.join("\n")}
51218
51301
  exports2.isSSEClientTransport = isSSEClientTransport;
51219
51302
  exports2.isStreamableHTTPClientTransport = isStreamableHTTPClientTransport;
51220
51303
  exports2.parseSkillFrontMatter = parseSkillFrontMatter;
51304
+ exports2.registerNavigateTool = registerNavigateTool;
51221
51305
  exports2.registerPageTool = registerPageTool;
51222
51306
  exports2.setNavigator = setNavigator;
51223
51307
  exports2.withPageTools = withPageTools;