@agent-native/dispatch 0.43.0-nightly-20261120212328 → 0.43.7
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/CHANGELOG.md +194 -174
- package/dist/actions/ask_app.d.ts +3 -0
- package/dist/actions/ask_app.js +7 -2
- package/dist/actions/ask_app.js.map +1 -1
- package/dist/actions/ask_app_status.d.ts +3 -0
- package/dist/actions/ask_app_status.js +5 -1
- package/dist/actions/ask_app_status.js.map +1 -1
- package/dist/actions/call_app_action.d.ts +1 -0
- package/dist/actions/call_app_action.js +21 -5
- package/dist/actions/call_app_action.js.map +1 -1
- package/dist/actions/create_embed_session.d.ts +3 -2
- package/dist/actions/create_embed_session.js +4 -2
- package/dist/actions/create_embed_session.js.map +1 -1
- package/dist/actions/create_workspace_app_embed_session.d.ts +3 -2
- package/dist/actions/create_workspace_app_embed_session.js +3 -1
- package/dist/actions/create_workspace_app_embed_session.js.map +1 -1
- package/dist/actions/delete-destination.d.ts +12 -12
- package/dist/actions/execute-action-suggestion.d.ts +44 -0
- package/dist/actions/execute-action-suggestion.d.ts.map +1 -0
- package/dist/actions/execute-action-suggestion.js +148 -0
- package/dist/actions/execute-action-suggestion.js.map +1 -0
- package/dist/actions/import-agent.d.ts +13 -13
- package/dist/actions/index.d.ts.map +1 -1
- package/dist/actions/index.js +5 -1
- package/dist/actions/index.js.map +1 -1
- package/dist/actions/list-connected-agents.js +3 -2
- package/dist/actions/list-connected-agents.js.map +1 -1
- package/dist/actions/list-workspace-connections.d.ts +1 -0
- package/dist/actions/list-workspace-connections.d.ts.map +1 -1
- package/dist/actions/list_app_actions.js +1 -1
- package/dist/actions/list_app_actions.js.map +1 -1
- package/dist/actions/list_apps.d.ts +2 -0
- package/dist/actions/list_apps.js +11 -3
- package/dist/actions/list_apps.js.map +1 -1
- package/dist/actions/navigate.js +1 -0
- package/dist/actions/navigate.js.map +1 -1
- package/dist/actions/open_app.d.ts +4 -0
- package/dist/actions/open_app.js +14 -6
- package/dist/actions/open_app.js.map +1 -1
- package/dist/actions/propose-action-suggestions.d.ts +45 -0
- package/dist/actions/propose-action-suggestions.d.ts.map +1 -0
- package/dist/actions/propose-action-suggestions.js +32 -0
- package/dist/actions/propose-action-suggestions.js.map +1 -0
- package/dist/actions/provider-api-register.d.ts +12 -12
- package/dist/actions/read_view.js +1 -1
- package/dist/actions/read_view.js.map +1 -1
- package/dist/actions/remember-pointer.js +1 -1
- package/dist/actions/remember-pointer.js.map +1 -1
- package/dist/actions/resolve-pointer.js +1 -1
- package/dist/actions/resolve-pointer.js.map +1 -1
- package/dist/actions/route-app-intent.d.ts +6 -0
- package/dist/actions/route-app-intent.js +79 -5
- package/dist/actions/route-app-intent.js.map +1 -1
- package/dist/actions/send-code-agent-remote-command.js +4 -0
- package/dist/actions/send-code-agent-remote-command.js.map +1 -1
- package/dist/actions/start-workspace-app-creation.js +1 -1
- package/dist/actions/start-workspace-app-creation.js.map +1 -1
- package/dist/actions/upsert-destination.d.ts +12 -12
- package/dist/actions/view-screen.d.ts +12 -1
- package/dist/actions/view-screen.js +53 -5
- package/dist/actions/view-screen.js.map +1 -1
- package/dist/components/chat-action-suggestions.d.ts +8 -0
- package/dist/components/chat-action-suggestions.d.ts.map +1 -0
- package/dist/components/chat-action-suggestions.js +191 -0
- package/dist/components/chat-action-suggestions.js.map +1 -0
- package/dist/components/chat-home-apps.d.ts.map +1 -1
- package/dist/components/chat-home-apps.js +1 -1
- package/dist/components/chat-home-apps.js.map +1 -1
- package/dist/components/google-broker-settings-group.d.ts +2 -0
- package/dist/components/google-broker-settings-group.d.ts.map +1 -0
- package/dist/components/google-broker-settings-group.js +53 -0
- package/dist/components/google-broker-settings-group.js.map +1 -0
- package/dist/components/layout/Layout.d.ts +4 -1
- package/dist/components/layout/Layout.d.ts.map +1 -1
- package/dist/components/layout/Layout.js +246 -69
- package/dist/components/layout/Layout.js.map +1 -1
- package/dist/components/messaging-setup-panel.d.ts.map +1 -1
- package/dist/components/messaging-setup-panel.js +47 -15
- package/dist/components/messaging-setup-panel.js.map +1 -1
- package/dist/components/workspace-app-host.d.ts +27 -4
- package/dist/components/workspace-app-host.d.ts.map +1 -1
- package/dist/components/workspace-app-host.js +355 -74
- package/dist/components/workspace-app-host.js.map +1 -1
- package/dist/config.d.ts +5 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js.map +1 -1
- package/dist/db/google-broker-admission.d.ts +36 -0
- package/dist/db/google-broker-admission.d.ts.map +1 -0
- package/dist/db/google-broker-admission.js +129 -0
- package/dist/db/google-broker-admission.js.map +1 -0
- package/dist/db/migrations.d.ts +1 -1
- package/dist/db/migrations.d.ts.map +1 -1
- package/dist/db/migrations.js +42 -0
- package/dist/db/migrations.js.map +1 -1
- package/dist/lib/action-suggestions.d.ts +20 -0
- package/dist/lib/action-suggestions.d.ts.map +1 -0
- package/dist/lib/action-suggestions.js +219 -0
- package/dist/lib/action-suggestions.js.map +1 -0
- package/dist/lib/chat-first-app-route.d.ts +11 -5
- package/dist/lib/chat-first-app-route.d.ts.map +1 -1
- package/dist/lib/chat-first-app-route.js +9 -5
- package/dist/lib/chat-first-app-route.js.map +1 -1
- package/dist/lib/chat-first-pane.d.ts +25 -1
- package/dist/lib/chat-first-pane.d.ts.map +1 -1
- package/dist/lib/chat-first-pane.js +67 -1
- package/dist/lib/chat-first-pane.js.map +1 -1
- package/dist/lib/data-intent.d.ts +3 -0
- package/dist/lib/data-intent.d.ts.map +1 -0
- package/dist/lib/data-intent.js +14 -0
- package/dist/lib/data-intent.js.map +1 -0
- package/dist/lib/dispatch-app-identity.d.ts +10 -0
- package/dist/lib/dispatch-app-identity.d.ts.map +1 -0
- package/dist/lib/dispatch-app-identity.js +18 -0
- package/dist/lib/dispatch-app-identity.js.map +1 -0
- package/dist/lib/other-apps.d.ts +15 -0
- package/dist/lib/other-apps.d.ts.map +1 -1
- package/dist/lib/other-apps.js +31 -0
- package/dist/lib/other-apps.js.map +1 -1
- package/dist/lib/workspace-app-frame-view.d.ts +15 -0
- package/dist/lib/workspace-app-frame-view.d.ts.map +1 -0
- package/dist/lib/workspace-app-frame-view.js +72 -0
- package/dist/lib/workspace-app-frame-view.js.map +1 -0
- package/dist/lib/workspace-apps.d.ts +5 -0
- package/dist/lib/workspace-apps.d.ts.map +1 -1
- package/dist/lib/workspace-apps.js +85 -23
- package/dist/lib/workspace-apps.js.map +1 -1
- package/dist/lib/workspace-apps.test.js +6 -0
- package/dist/lib/workspace-apps.test.js.map +1 -1
- package/dist/routes/pages/_index.d.ts +2 -2
- package/dist/routes/pages/chat.d.ts.map +1 -1
- package/dist/routes/pages/chat.js +33 -2
- package/dist/routes/pages/chat.js.map +1 -1
- package/dist/routes/pages/settings-page.d.ts +1 -1
- package/dist/routes/pages/settings-page.d.ts.map +1 -1
- package/dist/routes/pages/settings-page.js +3 -2
- package/dist/routes/pages/settings-page.js.map +1 -1
- package/dist/server/index.d.ts +4 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +5 -5
- package/dist/server/index.js.map +1 -1
- package/dist/server/lib/action-suggestions.d.ts +41 -0
- package/dist/server/lib/action-suggestions.d.ts.map +1 -0
- package/dist/server/lib/action-suggestions.js +94 -0
- package/dist/server/lib/action-suggestions.js.map +1 -0
- package/dist/server/lib/app-action-result.d.ts +5 -0
- package/dist/server/lib/app-action-result.d.ts.map +1 -1
- package/dist/server/lib/app-action-result.js +7 -0
- package/dist/server/lib/app-action-result.js.map +1 -1
- package/dist/server/lib/app-creation-store.d.ts.map +1 -1
- package/dist/server/lib/app-creation-store.js +63 -13
- package/dist/server/lib/app-creation-store.js.map +1 -1
- package/dist/server/lib/curated-workspace-templates.d.ts.map +1 -1
- package/dist/server/lib/curated-workspace-templates.js +3 -0
- package/dist/server/lib/curated-workspace-templates.js.map +1 -1
- package/dist/server/lib/dispatch-config.d.ts +5 -0
- package/dist/server/lib/dispatch-config.d.ts.map +1 -0
- package/dist/server/lib/dispatch-config.js +11 -0
- package/dist/server/lib/dispatch-config.js.map +1 -0
- package/dist/server/lib/dispatch-pane-context.d.ts +11 -0
- package/dist/server/lib/dispatch-pane-context.d.ts.map +1 -0
- package/dist/server/lib/dispatch-pane-context.js +35 -0
- package/dist/server/lib/dispatch-pane-context.js.map +1 -0
- package/dist/server/lib/google-broker-provider.d.ts +41 -0
- package/dist/server/lib/google-broker-provider.d.ts.map +1 -0
- package/dist/server/lib/google-broker-provider.js +96 -0
- package/dist/server/lib/google-broker-provider.js.map +1 -0
- package/dist/server/lib/google-broker-rpc.d.ts +26 -0
- package/dist/server/lib/google-broker-rpc.d.ts.map +1 -0
- package/dist/server/lib/google-broker-rpc.js +52 -0
- package/dist/server/lib/google-broker-rpc.js.map +1 -0
- package/dist/server/lib/hosted-apps-settings.d.ts.map +1 -1
- package/dist/server/lib/hosted-apps-settings.js +4 -0
- package/dist/server/lib/hosted-apps-settings.js.map +1 -1
- package/dist/server/lib/mcp-app-discovery.d.ts +17 -0
- package/dist/server/lib/mcp-app-discovery.d.ts.map +1 -0
- package/dist/server/lib/mcp-app-discovery.js +231 -0
- package/dist/server/lib/mcp-app-discovery.js.map +1 -0
- package/dist/server/lib/mcp-gateway.d.ts +64 -21
- package/dist/server/lib/mcp-gateway.d.ts.map +1 -1
- package/dist/server/lib/mcp-gateway.js +730 -88
- package/dist/server/lib/mcp-gateway.js.map +1 -1
- package/dist/server/plugins/agent-chat.js +9 -7
- package/dist/server/plugins/agent-chat.js.map +1 -1
- package/dist/server/plugins/auth.d.ts.map +1 -1
- package/dist/server/plugins/auth.js +1 -0
- package/dist/server/plugins/auth.js.map +1 -1
- package/dist/server/plugins/db.d.ts.map +1 -1
- package/dist/server/plugins/db.js +0 -46
- package/dist/server/plugins/db.js.map +1 -1
- package/dist/server/plugins/integrations.d.ts.map +1 -1
- package/dist/server/plugins/integrations.js +4 -1
- package/dist/server/plugins/integrations.js.map +1 -1
- package/dist/shared/chat-first-pane.d.ts +24 -0
- package/dist/shared/chat-first-pane.d.ts.map +1 -1
- package/dist/shared/chat-first-pane.js +34 -0
- package/dist/shared/chat-first-pane.js.map +1 -1
- package/dist/shared/labs.d.ts +1 -0
- package/dist/shared/labs.d.ts.map +1 -1
- package/dist/shared/labs.js +7 -0
- package/dist/shared/labs.js.map +1 -1
- package/package.json +7 -6
- package/src/actions/action-suggestions.spec.ts +342 -0
- package/src/actions/app_actions.spec.ts +103 -0
- package/src/actions/ask_app.ts +9 -2
- package/src/actions/ask_app_status.ts +12 -1
- package/src/actions/call_app_action.ts +28 -8
- package/src/actions/create_embed_session.ts +8 -2
- package/src/actions/create_workspace_app_embed_session.ts +6 -1
- package/src/actions/execute-action-suggestion.ts +177 -0
- package/src/actions/index.spec.ts +6 -3
- package/src/actions/index.ts +5 -1
- package/src/actions/list-connected-agents.spec.ts +26 -1
- package/src/actions/list-connected-agents.ts +7 -4
- package/src/actions/list_app_actions.ts +1 -1
- package/src/actions/list_apps.spec.ts +58 -22
- package/src/actions/list_apps.ts +12 -5
- package/src/actions/navigate.ts +1 -0
- package/src/actions/open_app.spec.ts +17 -1
- package/src/actions/open_app.ts +21 -8
- package/src/actions/propose-action-suggestions.ts +42 -0
- package/src/actions/read_view.ts +1 -1
- package/src/actions/remember-pointer.ts +1 -1
- package/src/actions/resolve-pointer.ts +1 -1
- package/src/actions/route-app-intent.spec.ts +234 -5
- package/src/actions/route-app-intent.ts +89 -4
- package/src/actions/send-code-agent-remote-command.spec.ts +40 -0
- package/src/actions/send-code-agent-remote-command.ts +5 -0
- package/src/actions/start-workspace-app-creation.ts +1 -1
- package/src/actions/view-screen.bundle.spec.ts +15 -2
- package/src/actions/view-screen.spec.ts +81 -3
- package/src/actions/view-screen.ts +60 -4
- package/src/components/chat-action-suggestions.spec.tsx +362 -0
- package/src/components/chat-action-suggestions.tsx +432 -0
- package/src/components/chat-home-apps.spec.tsx +19 -0
- package/src/components/chat-home-apps.tsx +0 -1
- package/src/components/google-broker-settings-group.tsx +186 -0
- package/src/components/layout/Layout.spec.tsx +419 -28
- package/src/components/layout/Layout.tsx +304 -66
- package/src/components/messaging-setup-panel.spec.tsx +278 -4
- package/src/components/messaging-setup-panel.tsx +82 -19
- package/src/components/workspace-app-card.spec.tsx +79 -10
- package/src/components/workspace-app-chat-rail.spec.tsx +81 -3
- package/src/components/workspace-app-host.spec.tsx +269 -16
- package/src/components/workspace-app-host.tsx +464 -87
- package/src/config.ts +5 -0
- package/src/db/google-broker-admission.ts +160 -0
- package/src/db/migrations.spec.ts +165 -2
- package/src/db/migrations.ts +43 -1
- package/src/lib/action-suggestions.ts +267 -0
- package/src/lib/chat-first-app-route.spec.ts +15 -2
- package/src/lib/chat-first-app-route.ts +19 -7
- package/src/lib/chat-first-pane.spec.ts +71 -0
- package/src/lib/chat-first-pane.ts +101 -4
- package/src/lib/data-intent.spec.ts +21 -0
- package/src/lib/data-intent.ts +13 -0
- package/src/lib/dispatch-app-identity.ts +40 -0
- package/src/lib/other-apps.spec.ts +110 -1
- package/src/lib/other-apps.ts +41 -0
- package/src/lib/workspace-app-frame-view.spec.ts +13 -0
- package/src/lib/workspace-app-frame-view.ts +98 -0
- package/src/lib/workspace-apps.spec.ts +101 -0
- package/src/lib/workspace-apps.test.ts +18 -0
- package/src/lib/workspace-apps.ts +102 -24
- package/src/routes/pages/chat.spec.tsx +92 -1
- package/src/routes/pages/chat.tsx +41 -2
- package/src/routes/pages/settings-page.tsx +22 -17
- package/src/routes/pages/settings.spec.tsx +4 -0
- package/src/server/index.ts +6 -6
- package/src/server/lib/action-suggestions.ts +122 -0
- package/src/server/lib/app-action-result.ts +8 -0
- package/src/server/lib/app-creation-store.spec.ts +251 -0
- package/src/server/lib/app-creation-store.ts +72 -12
- package/src/server/lib/community-apps.spec.ts +19 -1
- package/src/server/lib/curated-workspace-templates.spec.ts +12 -0
- package/src/server/lib/curated-workspace-templates.ts +2 -0
- package/src/server/lib/dispatch-config.ts +15 -0
- package/src/server/lib/dispatch-pane-context.spec.ts +54 -0
- package/src/server/lib/dispatch-pane-context.ts +54 -0
- package/src/server/lib/google-broker-provider.ts +121 -0
- package/src/server/lib/google-broker-rpc.spec.ts +95 -0
- package/src/server/lib/google-broker-rpc.ts +107 -0
- package/src/server/lib/hosted-apps-settings.ts +4 -0
- package/src/server/lib/mcp-app-discovery.ts +318 -0
- package/src/server/lib/mcp-gateway.spec.ts +2252 -524
- package/src/server/lib/mcp-gateway.ts +1044 -181
- package/src/server/plugins/agent-chat.direct-mcp.spec.ts +84 -5
- package/src/server/plugins/agent-chat.ts +9 -7
- package/src/server/plugins/auth.spec.ts +5 -1
- package/src/server/plugins/auth.ts +1 -0
- package/src/server/plugins/db.ts +0 -51
- package/src/server/plugins/integrations.ts +4 -1
- package/src/shared/chat-first-pane.spec.ts +36 -0
- package/src/shared/chat-first-pane.ts +55 -0
- package/src/shared/labs.ts +8 -0
- package/src/server/plugins/agent-chat.spec.ts +0 -206
- package/src/server/plugins/integrations.spec.ts +0 -60
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createCipheriv,
|
|
3
|
+
createDecipheriv,
|
|
4
|
+
createHash,
|
|
5
|
+
createHmac,
|
|
6
|
+
randomBytes,
|
|
7
|
+
} from "node:crypto";
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
appCapabilityManifestSchema,
|
|
11
|
+
resolveManifestScreenPath,
|
|
12
|
+
validateAppManifestPath,
|
|
13
|
+
type AppCapabilityManifest,
|
|
14
|
+
} from "@agent-native/core";
|
|
1
15
|
import {
|
|
2
16
|
A2AClient,
|
|
3
17
|
canonicalA2AAudience,
|
|
@@ -10,11 +24,13 @@ import {
|
|
|
10
24
|
type A2APersistedMutationReceipt,
|
|
11
25
|
type Task,
|
|
12
26
|
} from "@agent-native/core/a2a";
|
|
13
|
-
import { fail } from "@agent-native/core/action";
|
|
27
|
+
import { fail, NoWriteError } from "@agent-native/core/action";
|
|
14
28
|
import { getAppConfig } from "@agent-native/core/app-config";
|
|
15
29
|
import { isFeatureFlagEnabled } from "@agent-native/core/feature-flags";
|
|
16
30
|
import {
|
|
17
31
|
buildMcpToolName,
|
|
32
|
+
getMcpOAuthAccessToken,
|
|
33
|
+
listRemoteServers,
|
|
18
34
|
McpClientManager,
|
|
19
35
|
mcpToolCallWasNotSent,
|
|
20
36
|
} from "@agent-native/core/mcp-client";
|
|
@@ -28,8 +44,11 @@ import {
|
|
|
28
44
|
getConfiguredAppBasePath,
|
|
29
45
|
getRequestContext,
|
|
30
46
|
getRequestOrgId,
|
|
47
|
+
getRequestRunContext,
|
|
31
48
|
getRequestUserEmail,
|
|
49
|
+
platformWaitUntil,
|
|
32
50
|
resolveAppRuntimeUrl,
|
|
51
|
+
type ViewportMode,
|
|
33
52
|
} from "@agent-native/core/server";
|
|
34
53
|
import {
|
|
35
54
|
discoverAgents,
|
|
@@ -38,12 +57,15 @@ import {
|
|
|
38
57
|
} from "@agent-native/core/server/agent-discovery";
|
|
39
58
|
import { EMBED_START_PATH } from "@agent-native/core/shared";
|
|
40
59
|
|
|
60
|
+
import { matchDispatchAppCandidates } from "../../lib/dispatch-app-identity.js";
|
|
61
|
+
import { DISPATCH_CHILD_BROWSER_TAB_PARAM } from "../../shared/chat-first-pane.js";
|
|
41
62
|
import { DESKTOP_WORKSPACE_SSO_FLAG } from "../../shared/feature-flags.js";
|
|
42
63
|
import {
|
|
43
64
|
DISPATCH_WORKSPACE_SSO_FLAG,
|
|
44
65
|
isWorkspaceSsoAppUrl,
|
|
45
66
|
} from "../../shared/workspace-sso.js";
|
|
46
67
|
import { listWorkspaceApps } from "./app-creation-store.js";
|
|
68
|
+
import { resolveDispatchChildBrowserTab } from "./dispatch-pane-context.js";
|
|
47
69
|
import {
|
|
48
70
|
projectEnvironmentUrl,
|
|
49
71
|
requestEnvironmentLane,
|
|
@@ -53,6 +75,10 @@ import {
|
|
|
53
75
|
isAppAllowedByMcpAccess,
|
|
54
76
|
type DispatchMcpAppAccessSettings,
|
|
55
77
|
} from "./mcp-access-store.js";
|
|
78
|
+
import {
|
|
79
|
+
discoverDispatchMcpApps,
|
|
80
|
+
type DispatchMcpDegradedSource,
|
|
81
|
+
} from "./mcp-app-discovery.js";
|
|
56
82
|
|
|
57
83
|
const DISPATCH_APP_ID = "dispatch";
|
|
58
84
|
const DISPATCH_NAME = "Dispatch";
|
|
@@ -66,6 +92,8 @@ const TARGET_EMBED_SESSION_BUDGET_MS = 95_000;
|
|
|
66
92
|
// open_app answers with its link by then; a panel is worth a short wait, not
|
|
67
93
|
// the full mint budget.
|
|
68
94
|
const OPEN_APP_EMBED_WAIT_MS = 15_000;
|
|
95
|
+
const BROWSER_EMBED_PREFETCH_WAIT_MS = 1_000;
|
|
96
|
+
const PENDING_EMBED_LIMIT = 32;
|
|
69
97
|
const DISPATCH_ASK_APP_DEFAULT_INLINE_WAIT_MS = 20_000;
|
|
70
98
|
const DISPATCH_ASK_APP_MAX_INLINE_WAIT_MS = 20_000;
|
|
71
99
|
const DISPATCH_ASK_APP_POLL_INTERVAL_MS = 1_500;
|
|
@@ -77,6 +105,212 @@ const DISPATCH_ASK_APP_TERMINAL_STATES = new Set([
|
|
|
77
105
|
"canceled",
|
|
78
106
|
"input-required",
|
|
79
107
|
]);
|
|
108
|
+
const DISPATCH_TASK_HANDLE_VERSION = 1;
|
|
109
|
+
const DISPATCH_TASK_HANDLE_TTL_SECONDS = 60 * 60;
|
|
110
|
+
const DISPATCH_TASK_HANDLE_DOMAIN =
|
|
111
|
+
"agent-native:dispatch-ask-app-task-handle:v1";
|
|
112
|
+
const DISPATCH_TASK_HANDLE_PREFIX = "v1";
|
|
113
|
+
const DISPATCH_TASK_HANDLE_IV_BYTES = 12;
|
|
114
|
+
const DISPATCH_TASK_HANDLE_AUTH_TAG_BYTES = 16;
|
|
115
|
+
const INVALID_DISPATCH_ASK_APP_TASK_HANDLE_ERROR =
|
|
116
|
+
"Invalid or expired ask_app task handle. To keep polling the same task, " +
|
|
117
|
+
"call ask_app_status with the app and taskId from the ask_app result and " +
|
|
118
|
+
"leave out taskHandle. Do not resubmit ask_app; that can duplicate work.";
|
|
119
|
+
|
|
120
|
+
let ephemeralDevTaskHandleSecret: string | null = null;
|
|
121
|
+
|
|
122
|
+
function getEphemeralDevTaskHandleSecret(): string {
|
|
123
|
+
if (ephemeralDevTaskHandleSecret === null) {
|
|
124
|
+
ephemeralDevTaskHandleSecret = randomBytes(32).toString("hex");
|
|
125
|
+
}
|
|
126
|
+
return ephemeralDevTaskHandleSecret;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function dispatchTaskHandleSecrets(orgSecret?: string | null): string[] {
|
|
130
|
+
const globalA2A = getGlobalA2ASecret()?.trim();
|
|
131
|
+
const betterAuth = process.env.BETTER_AUTH_SECRET?.trim();
|
|
132
|
+
const org = orgSecret?.trim();
|
|
133
|
+
const configured = [globalA2A, betterAuth, org].filter(
|
|
134
|
+
(value): value is string => Boolean(value),
|
|
135
|
+
);
|
|
136
|
+
if (configured.length > 0) return [...new Set(configured)];
|
|
137
|
+
if (process.env.NODE_ENV === "production") return [];
|
|
138
|
+
return [getEphemeralDevTaskHandleSecret()];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function dispatchTaskHandleKey(rootSecret: string): Buffer {
|
|
142
|
+
return createHmac("sha256", rootSecret)
|
|
143
|
+
.update(DISPATCH_TASK_HANDLE_DOMAIN)
|
|
144
|
+
.digest();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function normalizeTaskHandleIdentity(value: string): string {
|
|
148
|
+
return value.trim().toLowerCase();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function validDispatchTaskTargetUrl(value: string): string | null {
|
|
152
|
+
const trimmed = value.trim();
|
|
153
|
+
if (!trimmed) return null;
|
|
154
|
+
let parsed: URL;
|
|
155
|
+
try {
|
|
156
|
+
parsed = new URL(trimmed);
|
|
157
|
+
} catch {
|
|
158
|
+
// coercion-ok: invalid target URL fails task handle validation closed.
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
if (parsed.username || parsed.password || parsed.search || parsed.hash) {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
return parsed.toString().replace(/\/+$/, "");
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function sealDispatchAskAppTaskHandle(params: {
|
|
171
|
+
app: string;
|
|
172
|
+
targetUrl: string;
|
|
173
|
+
taskId: string;
|
|
174
|
+
userEmail: string;
|
|
175
|
+
orgId: string | null;
|
|
176
|
+
orgSecret: string | null;
|
|
177
|
+
expiresInSeconds?: number;
|
|
178
|
+
}): string | undefined {
|
|
179
|
+
const secrets = dispatchTaskHandleSecrets(params.orgSecret);
|
|
180
|
+
const primarySecret = secrets[0];
|
|
181
|
+
if (!primarySecret) return undefined;
|
|
182
|
+
const app = normalizeAppId(params.app);
|
|
183
|
+
const targetUrl = validDispatchTaskTargetUrl(params.targetUrl);
|
|
184
|
+
const taskId = params.taskId.trim();
|
|
185
|
+
const userEmail = normalizeTaskHandleIdentity(params.userEmail);
|
|
186
|
+
const orgId = normalizeTaskHandleIdentity(params.orgId ?? "");
|
|
187
|
+
if (!app || !targetUrl || !taskId || !userEmail) return undefined;
|
|
188
|
+
|
|
189
|
+
const payload = Buffer.from(
|
|
190
|
+
JSON.stringify({
|
|
191
|
+
version: DISPATCH_TASK_HANDLE_VERSION,
|
|
192
|
+
app,
|
|
193
|
+
targetUrl,
|
|
194
|
+
taskId,
|
|
195
|
+
userEmail,
|
|
196
|
+
orgId,
|
|
197
|
+
expiresAt:
|
|
198
|
+
Math.floor(Date.now() / 1000) +
|
|
199
|
+
(params.expiresInSeconds ?? DISPATCH_TASK_HANDLE_TTL_SECONDS),
|
|
200
|
+
}),
|
|
201
|
+
"utf8",
|
|
202
|
+
);
|
|
203
|
+
const iv = randomBytes(DISPATCH_TASK_HANDLE_IV_BYTES);
|
|
204
|
+
const cipher = createCipheriv(
|
|
205
|
+
"aes-256-gcm",
|
|
206
|
+
dispatchTaskHandleKey(primarySecret),
|
|
207
|
+
iv,
|
|
208
|
+
);
|
|
209
|
+
cipher.setAAD(Buffer.from(DISPATCH_TASK_HANDLE_DOMAIN, "utf8"));
|
|
210
|
+
const ciphertext = Buffer.concat([cipher.update(payload), cipher.final()]);
|
|
211
|
+
const authTag = cipher.getAuthTag();
|
|
212
|
+
return [
|
|
213
|
+
DISPATCH_TASK_HANDLE_PREFIX,
|
|
214
|
+
iv.toString("base64url"),
|
|
215
|
+
ciphertext.toString("base64url"),
|
|
216
|
+
authTag.toString("base64url"),
|
|
217
|
+
].join(".");
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function unsealDispatchAskAppTaskHandle(
|
|
221
|
+
handle: string,
|
|
222
|
+
expected: {
|
|
223
|
+
app: string;
|
|
224
|
+
taskId: string;
|
|
225
|
+
userEmail: string;
|
|
226
|
+
orgId: string | null;
|
|
227
|
+
orgSecret: string | null;
|
|
228
|
+
},
|
|
229
|
+
): { app: string; targetUrl: string; taskId: string } {
|
|
230
|
+
const invalid = () => new Error(INVALID_DISPATCH_ASK_APP_TASK_HANDLE_ERROR);
|
|
231
|
+
const trimmedHandle = handle.trim();
|
|
232
|
+
const expectedApp = normalizeAppId(expected.app);
|
|
233
|
+
const expectedTaskId = expected.taskId.trim();
|
|
234
|
+
const expectedUserEmail = normalizeTaskHandleIdentity(expected.userEmail);
|
|
235
|
+
const expectedOrgId = normalizeTaskHandleIdentity(expected.orgId ?? "");
|
|
236
|
+
if (!trimmedHandle || !expectedApp || !expectedTaskId || !expectedUserEmail) {
|
|
237
|
+
throw invalid();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const parts = trimmedHandle.split(".");
|
|
241
|
+
const [prefix, ivPart, ciphertextPart, authTagPart] = parts;
|
|
242
|
+
if (
|
|
243
|
+
parts.length !== 4 ||
|
|
244
|
+
prefix !== DISPATCH_TASK_HANDLE_PREFIX ||
|
|
245
|
+
!ivPart ||
|
|
246
|
+
!ciphertextPart ||
|
|
247
|
+
!authTagPart
|
|
248
|
+
) {
|
|
249
|
+
throw invalid();
|
|
250
|
+
}
|
|
251
|
+
const iv = Buffer.from(ivPart, "base64url");
|
|
252
|
+
const ciphertext = Buffer.from(ciphertextPart, "base64url");
|
|
253
|
+
const authTag = Buffer.from(authTagPart, "base64url");
|
|
254
|
+
if (
|
|
255
|
+
iv.length !== DISPATCH_TASK_HANDLE_IV_BYTES ||
|
|
256
|
+
ciphertext.length === 0 ||
|
|
257
|
+
authTag.length !== DISPATCH_TASK_HANDLE_AUTH_TAG_BYTES ||
|
|
258
|
+
iv.toString("base64url") !== ivPart ||
|
|
259
|
+
ciphertext.toString("base64url") !== ciphertextPart ||
|
|
260
|
+
authTag.toString("base64url") !== authTagPart
|
|
261
|
+
) {
|
|
262
|
+
throw invalid();
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const secrets = dispatchTaskHandleSecrets(expected.orgSecret);
|
|
266
|
+
const nowSeconds = Math.floor(Date.now() / 1000);
|
|
267
|
+
for (const rootSecret of secrets) {
|
|
268
|
+
let parsed: unknown;
|
|
269
|
+
try {
|
|
270
|
+
const decipher = createDecipheriv(
|
|
271
|
+
"aes-256-gcm",
|
|
272
|
+
dispatchTaskHandleKey(rootSecret),
|
|
273
|
+
iv,
|
|
274
|
+
);
|
|
275
|
+
decipher.setAAD(Buffer.from(DISPATCH_TASK_HANDLE_DOMAIN, "utf8"));
|
|
276
|
+
decipher.setAuthTag(authTag);
|
|
277
|
+
const plaintext = Buffer.concat([
|
|
278
|
+
decipher.update(ciphertext),
|
|
279
|
+
decipher.final(),
|
|
280
|
+
]);
|
|
281
|
+
parsed = JSON.parse(plaintext.toString("utf8")) as unknown;
|
|
282
|
+
} catch {
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
const claims = parsed as Record<string, unknown>;
|
|
289
|
+
const targetUrl =
|
|
290
|
+
typeof claims.targetUrl === "string"
|
|
291
|
+
? validDispatchTaskTargetUrl(claims.targetUrl)
|
|
292
|
+
: null;
|
|
293
|
+
if (
|
|
294
|
+
claims.version !== DISPATCH_TASK_HANDLE_VERSION ||
|
|
295
|
+
claims.app !== expectedApp ||
|
|
296
|
+
claims.taskId !== expectedTaskId ||
|
|
297
|
+
claims.userEmail !== expectedUserEmail ||
|
|
298
|
+
claims.orgId !== expectedOrgId ||
|
|
299
|
+
targetUrl === null ||
|
|
300
|
+
typeof claims.expiresAt !== "number" ||
|
|
301
|
+
!Number.isFinite(claims.expiresAt) ||
|
|
302
|
+
claims.expiresAt <= nowSeconds
|
|
303
|
+
) {
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
return {
|
|
307
|
+
app: expectedApp,
|
|
308
|
+
targetUrl,
|
|
309
|
+
taskId: expectedTaskId,
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
throw invalid();
|
|
313
|
+
}
|
|
80
314
|
|
|
81
315
|
class DispatchAskAppInlineDeadlineError extends Error {
|
|
82
316
|
constructor() {
|
|
@@ -92,9 +326,66 @@ export interface DispatchMcpAccessibleApp {
|
|
|
92
326
|
url: string;
|
|
93
327
|
homeUrl?: string;
|
|
94
328
|
mcpUrl?: string;
|
|
329
|
+
workspaceMount?: boolean;
|
|
95
330
|
color: string;
|
|
331
|
+
source?: DiscoveredAgent["source"];
|
|
332
|
+
aliases?: string[];
|
|
96
333
|
granted: boolean;
|
|
97
334
|
mountedPathUnavailable?: boolean;
|
|
335
|
+
manifest?: AppCapabilityManifest;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const grantedAppManifestCache = new Map<string, AppCapabilityManifest>();
|
|
339
|
+
|
|
340
|
+
/** Registers or clears a parsed capability manifest for a granted app id. */
|
|
341
|
+
export function registerGrantedAppManifest(
|
|
342
|
+
appId: string,
|
|
343
|
+
manifest: unknown,
|
|
344
|
+
): AppCapabilityManifest | undefined {
|
|
345
|
+
const key = normalizeAppId(appId);
|
|
346
|
+
if (!key) return undefined;
|
|
347
|
+
if (manifest === undefined || manifest === null) {
|
|
348
|
+
grantedAppManifestCache.delete(key);
|
|
349
|
+
return undefined;
|
|
350
|
+
}
|
|
351
|
+
const parsed = appCapabilityManifestSchema.safeParse(manifest);
|
|
352
|
+
if (!parsed.success) return undefined;
|
|
353
|
+
grantedAppManifestCache.set(key, parsed.data);
|
|
354
|
+
return parsed.data;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** Clears cached granted app capability manifests. */
|
|
358
|
+
export function clearGrantedAppManifestCache(): void {
|
|
359
|
+
grantedAppManifestCache.clear();
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function getGrantedAppManifest(
|
|
363
|
+
target: DispatchMcpAccessibleApp,
|
|
364
|
+
): AppCapabilityManifest | undefined {
|
|
365
|
+
if (target.manifest) {
|
|
366
|
+
const parsed = appCapabilityManifestSchema.safeParse(target.manifest);
|
|
367
|
+
if (parsed.success) return parsed.data;
|
|
368
|
+
}
|
|
369
|
+
return grantedAppManifestCache.get(normalizeAppId(target.id));
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function stripTargetBasePath(
|
|
373
|
+
target: DispatchMcpAccessibleApp,
|
|
374
|
+
rawPath: string,
|
|
375
|
+
): string {
|
|
376
|
+
const basePath = appBasePath(target);
|
|
377
|
+
if (!basePath) return rawPath;
|
|
378
|
+
const parsed = new URL(rawPath, "https://agent-native.invalid");
|
|
379
|
+
const lowerBase = basePath.toLowerCase();
|
|
380
|
+
const lowerPathname = parsed.pathname.toLowerCase();
|
|
381
|
+
if (lowerPathname === lowerBase) {
|
|
382
|
+
return `/${parsed.search}${parsed.hash}`;
|
|
383
|
+
}
|
|
384
|
+
if (lowerPathname.startsWith(`${lowerBase}/`)) {
|
|
385
|
+
const stripped = parsed.pathname.slice(basePath.length) || "/";
|
|
386
|
+
return `${stripped}${parsed.search}${parsed.hash}`;
|
|
387
|
+
}
|
|
388
|
+
return rawPath;
|
|
98
389
|
}
|
|
99
390
|
|
|
100
391
|
function normalizeAppId(value: string): string {
|
|
@@ -197,6 +488,7 @@ type DispatchAskAppTaskResult = {
|
|
|
197
488
|
app: string;
|
|
198
489
|
routedVia: "a2a";
|
|
199
490
|
taskId: string;
|
|
491
|
+
taskHandle?: string;
|
|
200
492
|
status: string;
|
|
201
493
|
response?: string;
|
|
202
494
|
receipts?: DispatchMutationReceipt[];
|
|
@@ -207,7 +499,10 @@ type DispatchAskAppTaskResult = {
|
|
|
207
499
|
errorCategory?: DispatchAskAppStatusErrorCategory;
|
|
208
500
|
attempts?: number;
|
|
209
501
|
pollAfterMs?: number;
|
|
210
|
-
poll?: {
|
|
502
|
+
poll?: {
|
|
503
|
+
tool: "ask_app_status";
|
|
504
|
+
arguments: { app: string; taskId: string; taskHandle?: string };
|
|
505
|
+
};
|
|
211
506
|
message?: string;
|
|
212
507
|
};
|
|
213
508
|
|
|
@@ -219,6 +514,7 @@ function dispatchAskAppTaskResult(
|
|
|
219
514
|
orgId: string | null;
|
|
220
515
|
orgSecret: string | null;
|
|
221
516
|
},
|
|
517
|
+
taskHandle?: string,
|
|
222
518
|
): DispatchAskAppTaskResult {
|
|
223
519
|
const status = String(task.status.state);
|
|
224
520
|
const response = stripA2APersistedArtifactMarkers(dispatchTaskText(task));
|
|
@@ -256,14 +552,21 @@ function dispatchAskAppTaskResult(
|
|
|
256
552
|
}
|
|
257
553
|
return {
|
|
258
554
|
...base,
|
|
555
|
+
...(taskHandle ? { taskHandle } : {}),
|
|
259
556
|
pollAfterMs: DISPATCH_ASK_APP_POLL_INTERVAL_MS,
|
|
260
557
|
poll: {
|
|
261
558
|
tool: "ask_app_status",
|
|
262
|
-
arguments: {
|
|
559
|
+
arguments: {
|
|
560
|
+
app,
|
|
561
|
+
taskId: task.id,
|
|
562
|
+
...(taskHandle ? { taskHandle } : {}),
|
|
563
|
+
},
|
|
263
564
|
},
|
|
264
565
|
message:
|
|
265
566
|
`ask_app is still ${status}. Call ask_app_status with ` +
|
|
266
|
-
|
|
567
|
+
(taskHandle
|
|
568
|
+
? `taskId "${task.id}" and the returned taskHandle to retrieve the final response.`
|
|
569
|
+
: `taskId "${task.id}" to retrieve the final response.`),
|
|
267
570
|
};
|
|
268
571
|
}
|
|
269
572
|
|
|
@@ -274,6 +577,8 @@ async function createDispatchA2AClient(input: {
|
|
|
274
577
|
orgDomain?: string;
|
|
275
578
|
orgSecret?: string;
|
|
276
579
|
deadline?: number;
|
|
580
|
+
childBrowserTabId?: string;
|
|
581
|
+
viewportMode?: ViewportMode;
|
|
277
582
|
}): Promise<{
|
|
278
583
|
client: A2AClient;
|
|
279
584
|
metadata: Record<string, unknown>;
|
|
@@ -312,6 +617,18 @@ async function createDispatchA2AClient(input: {
|
|
|
312
617
|
...(getRequestContext()?.requestOrigin
|
|
313
618
|
? { requestOrigin: getRequestContext()?.requestOrigin }
|
|
314
619
|
: {}),
|
|
620
|
+
...(input.childBrowserTabId
|
|
621
|
+
? { browserTabId: input.childBrowserTabId }
|
|
622
|
+
: {}),
|
|
623
|
+
...(input.viewportMode ? { viewportMode: input.viewportMode } : {}),
|
|
624
|
+
};
|
|
625
|
+
const transportHeaders: Record<string, string> = {
|
|
626
|
+
...(input.childBrowserTabId
|
|
627
|
+
? { "x-agent-native-browser-tab": input.childBrowserTabId }
|
|
628
|
+
: {}),
|
|
629
|
+
...(input.viewportMode
|
|
630
|
+
? { "x-agent-native-viewport-mode": input.viewportMode }
|
|
631
|
+
: {}),
|
|
315
632
|
};
|
|
316
633
|
const remainingMs =
|
|
317
634
|
input.deadline == null ? null : input.deadline - Date.now();
|
|
@@ -322,6 +639,7 @@ async function createDispatchA2AClient(input: {
|
|
|
322
639
|
? DISPATCH_A2A_REQUEST_TIMEOUT_MS
|
|
323
640
|
: Math.max(1, Math.min(DISPATCH_A2A_REQUEST_TIMEOUT_MS, remainingMs)),
|
|
324
641
|
...(apiKeys.length > 1 ? { fallbackApiKeys: apiKeys.slice(1) } : {}),
|
|
642
|
+
...(Object.keys(transportHeaders).length > 0 ? { transportHeaders } : {}),
|
|
325
643
|
}),
|
|
326
644
|
metadata,
|
|
327
645
|
};
|
|
@@ -378,11 +696,13 @@ function dispatchAskAppStatusReadUnavailableResult(
|
|
|
378
696
|
taskId: string,
|
|
379
697
|
errorCategory: DispatchAskAppStatusErrorCategory,
|
|
380
698
|
attempts: number,
|
|
699
|
+
taskHandle?: string,
|
|
381
700
|
): DispatchAskAppTaskResult {
|
|
382
701
|
return {
|
|
383
702
|
app,
|
|
384
703
|
routedVia: "a2a",
|
|
385
704
|
taskId,
|
|
705
|
+
...(taskHandle ? { taskHandle } : {}),
|
|
386
706
|
status: "unknown",
|
|
387
707
|
statusRead: "unavailable",
|
|
388
708
|
retryable: true,
|
|
@@ -391,12 +711,18 @@ function dispatchAskAppStatusReadUnavailableResult(
|
|
|
391
711
|
pollAfterMs: DISPATCH_ASK_APP_POLL_INTERVAL_MS,
|
|
392
712
|
poll: {
|
|
393
713
|
tool: "ask_app_status",
|
|
394
|
-
arguments: {
|
|
714
|
+
arguments: {
|
|
715
|
+
app,
|
|
716
|
+
taskId,
|
|
717
|
+
...(taskHandle ? { taskHandle } : {}),
|
|
718
|
+
},
|
|
395
719
|
},
|
|
396
720
|
message:
|
|
397
721
|
"The durable ask_app task status could not be read after bounded retries. " +
|
|
398
722
|
"The task may still be running or completed. Retry ask_app_status " +
|
|
399
|
-
|
|
723
|
+
(taskHandle
|
|
724
|
+
? "with the same app, taskId, and taskHandle; do not resubmit ask_app."
|
|
725
|
+
: "with the same app and taskId; do not resubmit ask_app."),
|
|
400
726
|
};
|
|
401
727
|
}
|
|
402
728
|
|
|
@@ -644,21 +970,87 @@ function targetEndpointKeepsCredentialsPrivate(endpoint: string): boolean {
|
|
|
644
970
|
}
|
|
645
971
|
}
|
|
646
972
|
|
|
647
|
-
function
|
|
973
|
+
function collapseRepeatedLeadingPathSegment(pathname: string): string {
|
|
974
|
+
const segments = pathname.split("/").filter(Boolean);
|
|
975
|
+
if (segments.length < 2) return pathname;
|
|
976
|
+
let start = 0;
|
|
977
|
+
while (start + 1 < segments.length) {
|
|
978
|
+
const current = segments[start];
|
|
979
|
+
const next = segments[start + 1];
|
|
980
|
+
if (!current || !next || current.toLowerCase() !== next.toLowerCase()) {
|
|
981
|
+
break;
|
|
982
|
+
}
|
|
983
|
+
start += 1;
|
|
984
|
+
}
|
|
985
|
+
if (start === 0) return pathname;
|
|
986
|
+
return `/${segments.slice(start).join("/")}`;
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
export function appHomeBaseUrl(
|
|
990
|
+
app: Pick<DispatchMcpAccessibleApp, "url" | "homeUrl">,
|
|
991
|
+
): string {
|
|
992
|
+
const normalize = (raw: string): string => {
|
|
993
|
+
const parsed = new URL(raw);
|
|
994
|
+
parsed.pathname = collapseRepeatedLeadingPathSegment(parsed.pathname);
|
|
995
|
+
return parsed.toString().replace(/\/+$/, "");
|
|
996
|
+
};
|
|
648
997
|
const configured = app.homeUrl?.trim();
|
|
649
998
|
if (configured) {
|
|
650
999
|
try {
|
|
651
1000
|
const url = new URL(configured);
|
|
652
1001
|
if (url.protocol === "http:" || url.protocol === "https:") {
|
|
653
|
-
return
|
|
1002
|
+
return normalize(configured);
|
|
654
1003
|
}
|
|
655
1004
|
} catch {
|
|
656
1005
|
// coercion-ok: invalid optional home URL falls back to endpoint.
|
|
657
|
-
// Fall back to the registered endpoint below. The endpoint has already
|
|
658
|
-
// passed the app-origin validation before this helper is reached.
|
|
659
1006
|
}
|
|
660
1007
|
}
|
|
661
|
-
|
|
1008
|
+
try {
|
|
1009
|
+
return normalize(app.url);
|
|
1010
|
+
} catch {
|
|
1011
|
+
// coercion-ok: fallback preserves pre-validated app base URL string.
|
|
1012
|
+
return app.url.replace(/\/+$/, "");
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
export function joinAppBaseUrlAndPath(
|
|
1017
|
+
baseUrl: string,
|
|
1018
|
+
relPath: string,
|
|
1019
|
+
): string {
|
|
1020
|
+
const cleanBase = baseUrl.replace(/\/+$/, "");
|
|
1021
|
+
let parsedBase: URL;
|
|
1022
|
+
let parsedRel: URL;
|
|
1023
|
+
try {
|
|
1024
|
+
parsedBase = new URL(cleanBase);
|
|
1025
|
+
parsedRel = new URL(relPath, "https://agent-native.invalid");
|
|
1026
|
+
} catch {
|
|
1027
|
+
// coercion-ok: fallback preserves direct concatenation for non-URL test inputs.
|
|
1028
|
+
return `${cleanBase}${relPath}`;
|
|
1029
|
+
}
|
|
1030
|
+
const basePath = collapseRepeatedLeadingPathSegment(
|
|
1031
|
+
parsedBase.pathname.replace(/\/+$/, "") || "/",
|
|
1032
|
+
);
|
|
1033
|
+
const relPathname = collapseRepeatedLeadingPathSegment(
|
|
1034
|
+
parsedRel.pathname || "/",
|
|
1035
|
+
);
|
|
1036
|
+
const normalizedBasePath = basePath === "/" ? "" : basePath;
|
|
1037
|
+
const suffix = `${parsedRel.search}${parsedRel.hash}`;
|
|
1038
|
+
if (!normalizedBasePath) {
|
|
1039
|
+
return `${parsedBase.origin}${relPathname}${suffix}`;
|
|
1040
|
+
}
|
|
1041
|
+
const lowerBase = normalizedBasePath.toLowerCase();
|
|
1042
|
+
const lowerRel = relPathname.toLowerCase();
|
|
1043
|
+
const effectiveRel =
|
|
1044
|
+
lowerRel === lowerBase
|
|
1045
|
+
? "/"
|
|
1046
|
+
: lowerRel.startsWith(`${lowerBase}/`)
|
|
1047
|
+
? relPathname.slice(normalizedBasePath.length)
|
|
1048
|
+
: relPathname;
|
|
1049
|
+
const joinedPath =
|
|
1050
|
+
effectiveRel === "/"
|
|
1051
|
+
? `${normalizedBasePath}/`
|
|
1052
|
+
: `${normalizedBasePath}${effectiveRel.startsWith("/") ? effectiveRel : `/${effectiveRel}`}`;
|
|
1053
|
+
return `${parsedBase.origin}${joinedPath}${suffix}`;
|
|
662
1054
|
}
|
|
663
1055
|
|
|
664
1056
|
function workspaceGatewayMcpUrl(appUrl: string): string | undefined {
|
|
@@ -818,6 +1210,8 @@ function toAccessibleApp(
|
|
|
818
1210
|
description: typeof agent.description === "string" ? agent.description : "",
|
|
819
1211
|
url: projectEnvironmentUrl(agent.url),
|
|
820
1212
|
color: agent.color,
|
|
1213
|
+
...(agent.source ? { source: agent.source } : {}),
|
|
1214
|
+
...(agent.aliases?.length ? { aliases: agent.aliases } : {}),
|
|
821
1215
|
granted: isAppAllowedByMcpAccess(agent.id, settings),
|
|
822
1216
|
};
|
|
823
1217
|
}
|
|
@@ -825,23 +1219,19 @@ function toAccessibleApp(
|
|
|
825
1219
|
export async function listDispatchMcpApps(): Promise<{
|
|
826
1220
|
settings: DispatchMcpAppAccessSettings;
|
|
827
1221
|
apps: DispatchMcpAccessibleApp[];
|
|
1222
|
+
degradedSources: DispatchMcpDegradedSource[];
|
|
828
1223
|
}> {
|
|
829
|
-
const [settings, agents, mountedApps] =
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
"[dispatch] Workspace app lookup failed during MCP app discovery",
|
|
835
|
-
{ status: httpStatusFromError(error) },
|
|
836
|
-
);
|
|
837
|
-
return [];
|
|
838
|
-
}),
|
|
839
|
-
]);
|
|
1224
|
+
const [settings, { agents, mountedApps, degradedSources }] =
|
|
1225
|
+
await Promise.all([
|
|
1226
|
+
getDispatchMcpAppAccessSettings(),
|
|
1227
|
+
discoverDispatchMcpApps(),
|
|
1228
|
+
]);
|
|
840
1229
|
const mountedAppsById = new Map(
|
|
841
1230
|
mountedApps.map((app) => [normalizeAppId(app.id), app]),
|
|
842
1231
|
);
|
|
843
1232
|
return {
|
|
844
1233
|
settings,
|
|
1234
|
+
degradedSources,
|
|
845
1235
|
apps: [
|
|
846
1236
|
dispatchSelfApp(settings),
|
|
847
1237
|
...agents
|
|
@@ -857,9 +1247,9 @@ export async function listDispatchMcpApps(): Promise<{
|
|
|
857
1247
|
mountedApp && sameOriginMount
|
|
858
1248
|
? mountedWorkspaceAppHomeUrl(mountedApp)
|
|
859
1249
|
: null;
|
|
860
|
-
if (homeUrl) return { ...accessible, homeUrl };
|
|
861
|
-
|
|
862
|
-
|
|
1250
|
+
if (homeUrl) return { ...accessible, homeUrl, workspaceMount: true };
|
|
1251
|
+
return agent.source === "workspace" ||
|
|
1252
|
+
isDispatchAppPageUrl(accessible.url)
|
|
863
1253
|
? { ...accessible, mountedPathUnavailable: true }
|
|
864
1254
|
: accessible;
|
|
865
1255
|
}),
|
|
@@ -867,13 +1257,23 @@ export async function listDispatchMcpApps(): Promise<{
|
|
|
867
1257
|
};
|
|
868
1258
|
}
|
|
869
1259
|
|
|
1260
|
+
export async function listGrantedDispatchMcpAppsWithStatus(): Promise<{
|
|
1261
|
+
apps: DispatchMcpAccessibleApp[];
|
|
1262
|
+
degradedSources: DispatchMcpDegradedSource[];
|
|
1263
|
+
}> {
|
|
1264
|
+
const { apps, degradedSources } = await listDispatchMcpApps();
|
|
1265
|
+
return {
|
|
1266
|
+
degradedSources,
|
|
1267
|
+
apps: apps.filter(
|
|
1268
|
+
(app) => app.granted && !app.mountedPathUnavailable && safeAppOrigin(app),
|
|
1269
|
+
),
|
|
1270
|
+
};
|
|
1271
|
+
}
|
|
1272
|
+
|
|
870
1273
|
export async function listGrantedDispatchMcpApps(): Promise<
|
|
871
1274
|
DispatchMcpAccessibleApp[]
|
|
872
1275
|
> {
|
|
873
|
-
|
|
874
|
-
return apps.filter(
|
|
875
|
-
(app) => app.granted && !app.mountedPathUnavailable && safeAppOrigin(app),
|
|
876
|
-
);
|
|
1276
|
+
return (await listGrantedDispatchMcpAppsWithStatus()).apps;
|
|
877
1277
|
}
|
|
878
1278
|
|
|
879
1279
|
export async function listGrantedDispatchMcpAppOrigins(): Promise<string[]> {
|
|
@@ -887,10 +1287,13 @@ export async function resolveGrantedDispatchMcpApp(
|
|
|
887
1287
|
const target = normalizeAppId(app);
|
|
888
1288
|
if (!target) throw new Error("app is required");
|
|
889
1289
|
const { apps } = await listDispatchMcpApps();
|
|
890
|
-
const
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
1290
|
+
const candidates = matchDispatchAppCandidates(apps, target);
|
|
1291
|
+
if (candidates.length > 1) {
|
|
1292
|
+
throw new Error(
|
|
1293
|
+
`Ambiguous app "${app}". Choose one of: ${candidates.map((candidate) => candidate.id).join(", ")}.`,
|
|
1294
|
+
);
|
|
1295
|
+
}
|
|
1296
|
+
const match = candidates[0];
|
|
894
1297
|
if (!match) {
|
|
895
1298
|
throw new Error(
|
|
896
1299
|
`Unknown app "${app}". Call list_apps to see apps available through Dispatch MCP.`,
|
|
@@ -961,6 +1364,7 @@ async function listWorkspaceSsoApps(): Promise<DispatchMcpAccessibleApp[]> {
|
|
|
961
1364
|
description: app.description,
|
|
962
1365
|
url: app.url,
|
|
963
1366
|
homeUrl,
|
|
1367
|
+
workspaceMount: true,
|
|
964
1368
|
...(mcpUrl ? { mcpUrl } : {}),
|
|
965
1369
|
color: DISPATCH_COLOR,
|
|
966
1370
|
granted: true,
|
|
@@ -992,10 +1396,13 @@ async function resolveWorkspaceSsoApp(
|
|
|
992
1396
|
const target = normalizeAppId(app);
|
|
993
1397
|
if (!target) throw new Error("app is required");
|
|
994
1398
|
const apps = await listWorkspaceSsoApps();
|
|
995
|
-
const
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
1399
|
+
const candidates = matchDispatchAppCandidates(apps, target);
|
|
1400
|
+
if (candidates.length > 1) {
|
|
1401
|
+
throw new Error(
|
|
1402
|
+
`Ambiguous app "${app}". Choose one of: ${candidates.map((candidate) => candidate.id).join(", ")}.`,
|
|
1403
|
+
);
|
|
1404
|
+
}
|
|
1405
|
+
const match = candidates[0];
|
|
999
1406
|
if (!match) {
|
|
1000
1407
|
throw new Error(
|
|
1001
1408
|
`Workspace app "${app}" is not registered for workspace sign-in.`,
|
|
@@ -1007,7 +1414,11 @@ async function resolveWorkspaceSsoApp(
|
|
|
1007
1414
|
export async function askGrantedDispatchMcpApp(
|
|
1008
1415
|
app: string,
|
|
1009
1416
|
message: string,
|
|
1010
|
-
options?: {
|
|
1417
|
+
options?: {
|
|
1418
|
+
async?: boolean;
|
|
1419
|
+
maxWaitMs?: number;
|
|
1420
|
+
viewportMode?: ViewportMode;
|
|
1421
|
+
},
|
|
1011
1422
|
): Promise<ReturnType<typeof dispatchAskAppTaskResult>> {
|
|
1012
1423
|
const trimmedMessage = message.trim();
|
|
1013
1424
|
if (!trimmedMessage) throw new Error("message is required");
|
|
@@ -1023,6 +1434,15 @@ export async function askGrantedDispatchMcpApp(
|
|
|
1023
1434
|
])
|
|
1024
1435
|
: [null, null];
|
|
1025
1436
|
|
|
1437
|
+
const requestedViewportMode =
|
|
1438
|
+
options?.viewportMode ?? getRequestRunContext()?.viewportMode;
|
|
1439
|
+
const childBrowserTabId =
|
|
1440
|
+
requestedViewportMode === "background"
|
|
1441
|
+
? undefined
|
|
1442
|
+
: await resolveDispatchChildBrowserTab(target);
|
|
1443
|
+
const viewportMode: ViewportMode | undefined =
|
|
1444
|
+
requestedViewportMode ?? (childBrowserTabId ? "foreground" : undefined);
|
|
1445
|
+
|
|
1026
1446
|
const inlineWaitMs =
|
|
1027
1447
|
options?.async === true
|
|
1028
1448
|
? 0
|
|
@@ -1038,6 +1458,8 @@ export async function askGrantedDispatchMcpApp(
|
|
|
1038
1458
|
orgDomain: orgDomain ?? undefined,
|
|
1039
1459
|
orgSecret: orgSecret ?? undefined,
|
|
1040
1460
|
deadline: submissionDeadline,
|
|
1461
|
+
...(childBrowserTabId ? { childBrowserTabId } : {}),
|
|
1462
|
+
...(viewportMode ? { viewportMode } : {}),
|
|
1041
1463
|
});
|
|
1042
1464
|
const idempotencyKey = await dispatchAskAppIdempotencyKey(
|
|
1043
1465
|
target,
|
|
@@ -1056,20 +1478,36 @@ export async function askGrantedDispatchMcpApp(
|
|
|
1056
1478
|
},
|
|
1057
1479
|
);
|
|
1058
1480
|
const finalOrRunning = await waitForDispatchA2ATask(client, task, deadline);
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1481
|
+
const taskHandle = isTerminalDispatchTask(finalOrRunning)
|
|
1482
|
+
? undefined
|
|
1483
|
+
: sealDispatchAskAppTaskHandle({
|
|
1484
|
+
app: target.id,
|
|
1485
|
+
targetUrl: target.url,
|
|
1486
|
+
taskId: finalOrRunning.id,
|
|
1487
|
+
userEmail,
|
|
1488
|
+
orgId: orgId ?? null,
|
|
1489
|
+
orgSecret: orgSecret ?? null,
|
|
1490
|
+
});
|
|
1491
|
+
return dispatchAskAppTaskResult(
|
|
1492
|
+
target.id,
|
|
1493
|
+
finalOrRunning,
|
|
1494
|
+
{
|
|
1495
|
+
userEmail,
|
|
1496
|
+
orgId: orgId ?? null,
|
|
1497
|
+
orgSecret: orgSecret ?? null,
|
|
1498
|
+
},
|
|
1499
|
+
taskHandle,
|
|
1500
|
+
);
|
|
1064
1501
|
}
|
|
1065
1502
|
|
|
1066
1503
|
export async function getGrantedDispatchMcpAppTask(
|
|
1067
1504
|
app: string,
|
|
1068
1505
|
taskId: string,
|
|
1506
|
+
options?: { taskHandle?: string },
|
|
1069
1507
|
): Promise<DispatchAskAppTaskResult> {
|
|
1070
1508
|
const trimmedTaskId = taskId.trim();
|
|
1071
1509
|
if (!trimmedTaskId) throw new Error("taskId is required");
|
|
1072
|
-
|
|
1510
|
+
if (!normalizeAppId(app)) throw new Error("app is required");
|
|
1073
1511
|
const userEmail = getRequestUserEmail();
|
|
1074
1512
|
if (!userEmail) throw new Error("no authenticated user");
|
|
1075
1513
|
|
|
@@ -1080,8 +1518,36 @@ export async function getGrantedDispatchMcpAppTask(
|
|
|
1080
1518
|
getOrgA2ASecret(orgId).catch(() => null),
|
|
1081
1519
|
])
|
|
1082
1520
|
: [null, null];
|
|
1521
|
+
|
|
1522
|
+
let targetAppId: string;
|
|
1523
|
+
let targetUrl: string;
|
|
1524
|
+
let verifiedHandle: string | undefined;
|
|
1525
|
+
|
|
1526
|
+
if (options?.taskHandle !== undefined) {
|
|
1527
|
+
const verified = unsealDispatchAskAppTaskHandle(options.taskHandle, {
|
|
1528
|
+
app,
|
|
1529
|
+
taskId: trimmedTaskId,
|
|
1530
|
+
userEmail,
|
|
1531
|
+
orgId: orgId ?? null,
|
|
1532
|
+
orgSecret: orgSecret ?? null,
|
|
1533
|
+
});
|
|
1534
|
+
const settings = await getDispatchMcpAppAccessSettings();
|
|
1535
|
+
if (!isAppAllowedByMcpAccess(verified.app, settings)) {
|
|
1536
|
+
throw new Error(
|
|
1537
|
+
`Dispatch MCP access to "${verified.app}" is not granted. Open Dispatch > Agents to change MCP app access.`,
|
|
1538
|
+
);
|
|
1539
|
+
}
|
|
1540
|
+
targetAppId = verified.app;
|
|
1541
|
+
targetUrl = verified.targetUrl;
|
|
1542
|
+
verifiedHandle = options.taskHandle.trim();
|
|
1543
|
+
} else {
|
|
1544
|
+
const target = await resolveGrantedDispatchMcpApp(app);
|
|
1545
|
+
targetAppId = target.id;
|
|
1546
|
+
targetUrl = target.url;
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1083
1549
|
const { client } = await createDispatchA2AClient({
|
|
1084
|
-
targetUrl
|
|
1550
|
+
targetUrl,
|
|
1085
1551
|
userEmail,
|
|
1086
1552
|
orgId: orgId ?? undefined,
|
|
1087
1553
|
orgDomain: orgDomain ?? undefined,
|
|
@@ -1096,11 +1562,16 @@ export async function getGrantedDispatchMcpAppTask(
|
|
|
1096
1562
|
const startedAt = Date.now();
|
|
1097
1563
|
try {
|
|
1098
1564
|
const task = await client.getTask(trimmedTaskId);
|
|
1099
|
-
return dispatchAskAppTaskResult(
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1565
|
+
return dispatchAskAppTaskResult(
|
|
1566
|
+
targetAppId,
|
|
1567
|
+
task,
|
|
1568
|
+
{
|
|
1569
|
+
userEmail,
|
|
1570
|
+
orgId: orgId ?? null,
|
|
1571
|
+
orgSecret: orgSecret ?? null,
|
|
1572
|
+
},
|
|
1573
|
+
verifiedHandle,
|
|
1574
|
+
);
|
|
1104
1575
|
} catch (err) {
|
|
1105
1576
|
const delayMs = DISPATCH_ASK_APP_STATUS_RETRY_DELAYS_MS[attempt];
|
|
1106
1577
|
const errorCategory = dispatchAskAppStatusErrorCategory(err);
|
|
@@ -1108,10 +1579,10 @@ export async function getGrantedDispatchMcpAppTask(
|
|
|
1108
1579
|
const willRetry = retryable && delayMs != null;
|
|
1109
1580
|
if (retryable) {
|
|
1110
1581
|
console.warn("[ask_app_status] tasks/get attempt failed", {
|
|
1111
|
-
app:
|
|
1582
|
+
app: targetAppId,
|
|
1112
1583
|
routedVia: "a2a",
|
|
1113
1584
|
taskId: trimmedTaskId,
|
|
1114
|
-
originHost: dispatchAskAppStatusOriginHost(
|
|
1585
|
+
originHost: dispatchAskAppStatusOriginHost(targetUrl),
|
|
1115
1586
|
attempt: attempt + 1,
|
|
1116
1587
|
maxAttempts,
|
|
1117
1588
|
elapsedMs: Date.now() - startedAt,
|
|
@@ -1124,10 +1595,11 @@ export async function getGrantedDispatchMcpAppTask(
|
|
|
1124
1595
|
if (!retryable) throw err;
|
|
1125
1596
|
if (delayMs == null) {
|
|
1126
1597
|
return dispatchAskAppStatusReadUnavailableResult(
|
|
1127
|
-
|
|
1598
|
+
targetAppId,
|
|
1128
1599
|
trimmedTaskId,
|
|
1129
1600
|
errorCategory,
|
|
1130
1601
|
maxAttempts,
|
|
1602
|
+
verifiedHandle,
|
|
1131
1603
|
);
|
|
1132
1604
|
}
|
|
1133
1605
|
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
@@ -1136,18 +1608,25 @@ export async function getGrantedDispatchMcpAppTask(
|
|
|
1136
1608
|
throw new Error("ask_app_status retry loop exited unexpectedly.");
|
|
1137
1609
|
}
|
|
1138
1610
|
|
|
1139
|
-
export async function openGrantedDispatchMcpApp(
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1611
|
+
export async function openGrantedDispatchMcpApp(
|
|
1612
|
+
input: {
|
|
1613
|
+
app: string;
|
|
1614
|
+
screen?: string | undefined;
|
|
1615
|
+
view?: string | undefined;
|
|
1616
|
+
path?: string | undefined;
|
|
1617
|
+
params?: Record<string, string | number | boolean> | undefined;
|
|
1618
|
+
focus?: boolean | undefined;
|
|
1619
|
+
embed?: boolean | undefined;
|
|
1620
|
+
chrome?: "full" | "minimal" | undefined;
|
|
1621
|
+
},
|
|
1622
|
+
options?: { prefetchEmbed: boolean },
|
|
1623
|
+
): Promise<{
|
|
1147
1624
|
app: string;
|
|
1625
|
+
screen?: string;
|
|
1148
1626
|
view?: string;
|
|
1149
1627
|
path?: string;
|
|
1150
1628
|
url: string;
|
|
1629
|
+
focus?: boolean;
|
|
1151
1630
|
embed?: boolean;
|
|
1152
1631
|
chrome?: "full" | "minimal";
|
|
1153
1632
|
embedStartUrl?: string;
|
|
@@ -1155,30 +1634,77 @@ export async function openGrantedDispatchMcpApp(input: {
|
|
|
1155
1634
|
embedExpiresAt?: number;
|
|
1156
1635
|
embedUnavailable?: string;
|
|
1157
1636
|
}> {
|
|
1637
|
+
const screen = input.screen?.trim() ?? "";
|
|
1158
1638
|
const view = input.view?.trim() ?? "";
|
|
1159
1639
|
const hasPathInput = input.path != null;
|
|
1160
|
-
|
|
1161
|
-
const path = safeAppPath(input.path ?? (view ? undefined : "/"));
|
|
1640
|
+
let path = safeAppPath(input.path ?? (view || screen ? undefined : "/"));
|
|
1162
1641
|
if (hasPathInput && !path) {
|
|
1163
1642
|
throw new Error("path must be a safe app-relative route");
|
|
1164
1643
|
}
|
|
1165
1644
|
const target = await resolveGrantedDispatchMcpApp(input.app);
|
|
1166
|
-
|
|
1645
|
+
const manifest = getGrantedAppManifest(target);
|
|
1646
|
+
let effectiveParams = input.params;
|
|
1647
|
+
if (screen && !path) {
|
|
1648
|
+
if (manifest?.screens && Object.keys(manifest.screens).length > 0) {
|
|
1649
|
+
const resolvedScreen = resolveManifestScreenPath(
|
|
1650
|
+
manifest,
|
|
1651
|
+
screen,
|
|
1652
|
+
input.params,
|
|
1653
|
+
);
|
|
1654
|
+
if ("error" in resolvedScreen) {
|
|
1655
|
+
fail(
|
|
1656
|
+
`Cannot open screen "${screen}" on app "${target.id}": ${resolvedScreen.error}`,
|
|
1657
|
+
{ errorCode: "invalid_app_route", statusCode: 400 },
|
|
1658
|
+
);
|
|
1659
|
+
}
|
|
1660
|
+
path = safeAppPath(resolvedScreen.path);
|
|
1661
|
+
if (!path) {
|
|
1662
|
+
fail(
|
|
1663
|
+
`Resolved route "${resolvedScreen.path}" for screen "${screen}" on app "${target.id}" is not a safe app-relative route.`,
|
|
1664
|
+
{ errorCode: "invalid_app_route", statusCode: 400 },
|
|
1665
|
+
);
|
|
1666
|
+
}
|
|
1667
|
+
effectiveParams =
|
|
1668
|
+
Object.keys(resolvedScreen.remainingParams).length > 0
|
|
1669
|
+
? resolvedScreen.remainingParams
|
|
1670
|
+
: undefined;
|
|
1671
|
+
} else if (!view) {
|
|
1672
|
+
path = "/";
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
if (path) {
|
|
1676
|
+
assertAppCanOpenPath(target, path);
|
|
1677
|
+
if (manifest) {
|
|
1678
|
+
const manifestPath = stripTargetBasePath(target, path);
|
|
1679
|
+
const validation = validateAppManifestPath(manifest, manifestPath);
|
|
1680
|
+
if (!validation.valid) {
|
|
1681
|
+
const templates = validation.validTemplates
|
|
1682
|
+
.map((entry) => `${entry.screenId} (${entry.route})`)
|
|
1683
|
+
.join(", ");
|
|
1684
|
+
fail(
|
|
1685
|
+
`Invalid route "${path}" for app "${target.id}". Valid screens: ${templates}.`,
|
|
1686
|
+
{ errorCode: "invalid_app_route", statusCode: 400 },
|
|
1687
|
+
);
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1167
1691
|
const relUrl = path
|
|
1168
|
-
? appendParamsToPath(path,
|
|
1692
|
+
? appendParamsToPath(path, effectiveParams)
|
|
1169
1693
|
: buildDeepLink({
|
|
1170
1694
|
app: target.id,
|
|
1171
|
-
view,
|
|
1172
|
-
params:
|
|
1695
|
+
view: view || screen,
|
|
1696
|
+
params: effectiveParams,
|
|
1173
1697
|
});
|
|
1174
|
-
const url =
|
|
1698
|
+
const url = joinAppBaseUrlAndPath(appHomeBaseUrl(target), relUrl);
|
|
1175
1699
|
let embedSession: Awaited<
|
|
1176
1700
|
ReturnType<typeof createGrantedDispatchMcpEmbedSession>
|
|
1177
1701
|
> | null = null;
|
|
1178
1702
|
// Says why no session came back, so the caller can tell a plain link it
|
|
1179
1703
|
// asked for from a panel that failed to open.
|
|
1180
1704
|
let embedUnavailable: string | undefined;
|
|
1181
|
-
if (input.embed) {
|
|
1705
|
+
if (options?.prefetchEmbed && input.embed !== false) {
|
|
1706
|
+
prefetchGrantedEmbedSession(target, url);
|
|
1707
|
+
} else if (input.embed) {
|
|
1182
1708
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
1183
1709
|
const abort = new AbortController();
|
|
1184
1710
|
try {
|
|
@@ -1216,9 +1742,11 @@ export async function openGrantedDispatchMcpApp(input: {
|
|
|
1216
1742
|
}
|
|
1217
1743
|
return {
|
|
1218
1744
|
app: target.id,
|
|
1745
|
+
...(screen ? { screen } : {}),
|
|
1219
1746
|
...(view ? { view } : {}),
|
|
1220
1747
|
...(path ? { path } : {}),
|
|
1221
1748
|
url,
|
|
1749
|
+
...(input.focus === true ? { focus: true } : {}),
|
|
1222
1750
|
...(input.embed === true ? { embed: true } : {}),
|
|
1223
1751
|
...(input.chrome ? { chrome: input.chrome } : {}),
|
|
1224
1752
|
...(embedSession?.startUrl ? { embedStartUrl: embedSession.startUrl } : {}),
|
|
@@ -1484,6 +2012,7 @@ export type GrantedAppAction = {
|
|
|
1484
2012
|
description: string;
|
|
1485
2013
|
inputSchema?: Record<string, unknown>;
|
|
1486
2014
|
readOnly?: boolean;
|
|
2015
|
+
destructive?: boolean;
|
|
1487
2016
|
};
|
|
1488
2017
|
|
|
1489
2018
|
export type GrantedAppActionResult = {
|
|
@@ -1542,7 +2071,12 @@ async function appActionHeaders(
|
|
|
1542
2071
|
const headers = await appActionHeaderProvider(request);
|
|
1543
2072
|
if (headers === undefined) return {};
|
|
1544
2073
|
for (const [name, value] of Object.entries(headers)) {
|
|
1545
|
-
if (
|
|
2074
|
+
if (
|
|
2075
|
+
name.toLowerCase() === "authorization" ||
|
|
2076
|
+
name.toLowerCase() === "x-agent-native-browser-tab" ||
|
|
2077
|
+
name.toLowerCase() === "x-agent-native-viewport-mode" ||
|
|
2078
|
+
typeof value !== "string"
|
|
2079
|
+
) {
|
|
1546
2080
|
throw new Error(
|
|
1547
2081
|
`The app action header provider returned an invalid "${name}" header.`,
|
|
1548
2082
|
);
|
|
@@ -1551,6 +2085,94 @@ async function appActionHeaders(
|
|
|
1551
2085
|
return headers;
|
|
1552
2086
|
}
|
|
1553
2087
|
|
|
2088
|
+
type RemoteAppOAuth = {
|
|
2089
|
+
serverId?: string;
|
|
2090
|
+
credential?: TargetMcpCredential;
|
|
2091
|
+
};
|
|
2092
|
+
|
|
2093
|
+
async function findRemoteAppOAuth(
|
|
2094
|
+
mcpUrl: string,
|
|
2095
|
+
ownerEmail: string,
|
|
2096
|
+
): Promise<RemoteAppOAuth> {
|
|
2097
|
+
try {
|
|
2098
|
+
const resource = new URL(mcpUrl).href;
|
|
2099
|
+
const servers = await listRemoteServers("user", ownerEmail);
|
|
2100
|
+
let reconnectServerId: string | undefined;
|
|
2101
|
+
for (const server of servers) {
|
|
2102
|
+
if (
|
|
2103
|
+
!server.oauthSecretKey ||
|
|
2104
|
+
!URL.canParse(server.url) ||
|
|
2105
|
+
new URL(server.url).href !== resource
|
|
2106
|
+
)
|
|
2107
|
+
continue;
|
|
2108
|
+
reconnectServerId ??= server.id;
|
|
2109
|
+
const options = {
|
|
2110
|
+
key: server.oauthSecretKey,
|
|
2111
|
+
scope: "user" as const,
|
|
2112
|
+
scopeId: ownerEmail,
|
|
2113
|
+
serverUrl: mcpUrl,
|
|
2114
|
+
throwOnTransientRefreshFailure: true,
|
|
2115
|
+
};
|
|
2116
|
+
const token = await getMcpOAuthAccessToken(options);
|
|
2117
|
+
if (!token) continue;
|
|
2118
|
+
return {
|
|
2119
|
+
serverId: server.id,
|
|
2120
|
+
credential: {
|
|
2121
|
+
strategy: "oauth",
|
|
2122
|
+
token,
|
|
2123
|
+
oauthAccessRefresh: {
|
|
2124
|
+
identity: createHash("sha256")
|
|
2125
|
+
.update(
|
|
2126
|
+
JSON.stringify([ownerEmail, server.oauthSecretKey, resource]),
|
|
2127
|
+
)
|
|
2128
|
+
.digest("hex"),
|
|
2129
|
+
refresh: () =>
|
|
2130
|
+
getMcpOAuthAccessToken({ ...options, forceRefresh: true }),
|
|
2131
|
+
},
|
|
2132
|
+
},
|
|
2133
|
+
};
|
|
2134
|
+
}
|
|
2135
|
+
return reconnectServerId ? { serverId: reconnectServerId } : {};
|
|
2136
|
+
} catch {
|
|
2137
|
+
throw new NoWriteError(
|
|
2138
|
+
"The app's saved connection is unavailable. Try again.",
|
|
2139
|
+
{
|
|
2140
|
+
errorCode: "app_connection_unavailable",
|
|
2141
|
+
statusCode: 503,
|
|
2142
|
+
},
|
|
2143
|
+
);
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
|
|
2147
|
+
function appOAuthStartUrl(
|
|
2148
|
+
target: DispatchMcpAccessibleApp,
|
|
2149
|
+
mcpUrl: string,
|
|
2150
|
+
serverId?: string,
|
|
2151
|
+
): string {
|
|
2152
|
+
const params = serverId
|
|
2153
|
+
? new URLSearchParams({ scope: "user", serverId })
|
|
2154
|
+
: new URLSearchParams({
|
|
2155
|
+
scope: "user",
|
|
2156
|
+
name: `app-${createHash("sha256").update(mcpUrl).digest("hex").slice(0, 16)}`,
|
|
2157
|
+
url: mcpUrl,
|
|
2158
|
+
description: target.name,
|
|
2159
|
+
});
|
|
2160
|
+
return `${dispatchSelfBaseUrl()}/_agent-native/mcp/servers/oauth/start?${params}`;
|
|
2161
|
+
}
|
|
2162
|
+
|
|
2163
|
+
function appAuthorizationRequired(
|
|
2164
|
+
target: DispatchMcpAccessibleApp,
|
|
2165
|
+
mcpUrl: string,
|
|
2166
|
+
serverId?: string,
|
|
2167
|
+
): never {
|
|
2168
|
+
const authorizationUrl = appOAuthStartUrl(target, mcpUrl, serverId);
|
|
2169
|
+
throw new NoWriteError(`Connect ${target.name} to use its actions.`, {
|
|
2170
|
+
errorCode: "authorization_required",
|
|
2171
|
+
statusCode: 401,
|
|
2172
|
+
details: { app: target.id, authorizationUrl },
|
|
2173
|
+
});
|
|
2174
|
+
}
|
|
2175
|
+
|
|
1554
2176
|
// Runs `withClient` against a connected MCP client for the granted app, signed
|
|
1555
2177
|
// as the requesting user, the way embed sessions reach the same target. Without a
|
|
1556
2178
|
// header provider no per-turn grant is attached, so a target action that
|
|
@@ -1559,6 +2181,7 @@ async function withGrantedAppMcp<T>(
|
|
|
1559
2181
|
app: string,
|
|
1560
2182
|
action: string | undefined,
|
|
1561
2183
|
withClient: (manager: McpClientManager, serverId: string) => Promise<T>,
|
|
2184
|
+
options?: { viewportMode?: ViewportMode },
|
|
1562
2185
|
): Promise<T> {
|
|
1563
2186
|
if (!dispatchDirectAppActionsEnabled()) {
|
|
1564
2187
|
fail("Direct app actions are not enabled on this Dispatch deployment.", {
|
|
@@ -1583,14 +2206,46 @@ async function withGrantedAppMcp<T>(
|
|
|
1583
2206
|
{ id: target.id, url: mcpUrl },
|
|
1584
2207
|
{ nodeEnv: "production", environmentLane: requestEnvironmentLane() },
|
|
1585
2208
|
);
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
ownerEmail
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
2209
|
+
let remote: RemoteAppOAuth | undefined;
|
|
2210
|
+
const loadRemote = async () => {
|
|
2211
|
+
remote ??= await findRemoteAppOAuth(mcpUrl, ownerEmail);
|
|
2212
|
+
return remote;
|
|
2213
|
+
};
|
|
2214
|
+
if (
|
|
2215
|
+
!firstPartyMcp &&
|
|
2216
|
+
new URL(mcpUrl).origin !== new URL(dispatchSelfBaseUrl()).origin
|
|
2217
|
+
) {
|
|
2218
|
+
await loadRemote();
|
|
2219
|
+
}
|
|
2220
|
+
const prepared = remote?.credential
|
|
2221
|
+
? undefined
|
|
2222
|
+
: await createTargetMcpUserCredentials({
|
|
2223
|
+
mcpUrl,
|
|
2224
|
+
ownerEmail,
|
|
2225
|
+
fullCatalog: true,
|
|
2226
|
+
allowMissing: true,
|
|
2227
|
+
...(firstPartyMcp ? { firstPartyMcp: true } : {}),
|
|
2228
|
+
...(orgId !== undefined ? { orgId } : {}),
|
|
2229
|
+
});
|
|
2230
|
+
const credentials = remote?.credential
|
|
2231
|
+
? [remote.credential]
|
|
2232
|
+
: [...(prepared?.credentials ?? [])];
|
|
2233
|
+
if (credentials.length === 0) {
|
|
2234
|
+
const oauth = await loadRemote();
|
|
2235
|
+
if (oauth.credential) credentials.push(oauth.credential);
|
|
2236
|
+
else appAuthorizationRequired(target, mcpUrl, oauth.serverId);
|
|
2237
|
+
}
|
|
2238
|
+
const requestedViewportMode =
|
|
2239
|
+
options?.viewportMode ?? getRequestRunContext()?.viewportMode;
|
|
2240
|
+
const childBrowserTabId =
|
|
2241
|
+
action === undefined || requestedViewportMode === "background"
|
|
2242
|
+
? undefined
|
|
2243
|
+
: await resolveDispatchChildBrowserTab(target);
|
|
2244
|
+
const viewportMode: ViewportMode | undefined =
|
|
2245
|
+
action === undefined
|
|
2246
|
+
? undefined
|
|
2247
|
+
: (requestedViewportMode ??
|
|
2248
|
+
(childBrowserTabId ? "foreground" : undefined));
|
|
1594
2249
|
const extraHeaders = await appActionHeaders({
|
|
1595
2250
|
app: target.id,
|
|
1596
2251
|
action,
|
|
@@ -1598,7 +2253,9 @@ async function withGrantedAppMcp<T>(
|
|
|
1598
2253
|
...(orgId !== undefined ? { orgId } : {}),
|
|
1599
2254
|
});
|
|
1600
2255
|
const serverId = "target";
|
|
1601
|
-
let orgMemberTried =
|
|
2256
|
+
let orgMemberTried = credentials.some(
|
|
2257
|
+
(credential) => credential.strategy === "org-member-delegation",
|
|
2258
|
+
);
|
|
1602
2259
|
for (const [index, credential] of credentials.entries()) {
|
|
1603
2260
|
const manager = new McpClientManager(
|
|
1604
2261
|
{
|
|
@@ -1606,8 +2263,17 @@ async function withGrantedAppMcp<T>(
|
|
|
1606
2263
|
[serverId]: {
|
|
1607
2264
|
type: "http",
|
|
1608
2265
|
url: mcpUrl,
|
|
2266
|
+
...(credential.oauthAccessRefresh
|
|
2267
|
+
? { oauthAccessRefresh: credential.oauthAccessRefresh }
|
|
2268
|
+
: {}),
|
|
1609
2269
|
headers: {
|
|
1610
2270
|
...extraHeaders,
|
|
2271
|
+
...(childBrowserTabId
|
|
2272
|
+
? { "x-agent-native-browser-tab": childBrowserTabId }
|
|
2273
|
+
: {}),
|
|
2274
|
+
...(viewportMode
|
|
2275
|
+
? { "x-agent-native-viewport-mode": viewportMode }
|
|
2276
|
+
: {}),
|
|
1611
2277
|
Authorization: `Bearer ${credential.token}`,
|
|
1612
2278
|
...(credential.firstPartyMcp
|
|
1613
2279
|
? { "x-agent-native-mcp-inline-apps": "1" }
|
|
@@ -1622,17 +2288,44 @@ async function withGrantedAppMcp<T>(
|
|
|
1622
2288
|
await manager.start();
|
|
1623
2289
|
const status = manager.getConnectErrorStatus(serverId);
|
|
1624
2290
|
if (status === 401 || status === 403) {
|
|
1625
|
-
|
|
2291
|
+
if (credential.strategy === "oauth") {
|
|
2292
|
+
throw new NoWriteError(
|
|
2293
|
+
`The ${target.name} app rejected the saved connection.`,
|
|
2294
|
+
{
|
|
2295
|
+
errorCode: "app_connection_rejected",
|
|
2296
|
+
statusCode: 403,
|
|
2297
|
+
details: {
|
|
2298
|
+
app: target.id,
|
|
2299
|
+
authorizationUrl: appOAuthStartUrl(
|
|
2300
|
+
target,
|
|
2301
|
+
mcpUrl,
|
|
2302
|
+
remote?.serverId,
|
|
2303
|
+
),
|
|
2304
|
+
},
|
|
2305
|
+
},
|
|
2306
|
+
);
|
|
2307
|
+
}
|
|
1626
2308
|
if (index === credentials.length - 1 && !orgMemberTried) {
|
|
1627
2309
|
orgMemberTried = true;
|
|
1628
|
-
const orgMember = await prepared
|
|
2310
|
+
const orgMember = await prepared?.loadOrgMemberCredential();
|
|
1629
2311
|
if (orgMember) credentials.push(orgMember);
|
|
1630
2312
|
}
|
|
1631
2313
|
if (index < credentials.length - 1) continue;
|
|
1632
|
-
|
|
2314
|
+
const oauth = await loadRemote();
|
|
2315
|
+
if (oauth.credential) {
|
|
2316
|
+
credentials.push(oauth.credential);
|
|
2317
|
+
continue;
|
|
2318
|
+
}
|
|
2319
|
+
appAuthorizationRequired(target, mcpUrl, oauth.serverId);
|
|
1633
2320
|
}
|
|
1634
2321
|
if (!manager.hasServer(serverId)) {
|
|
1635
|
-
throw new
|
|
2322
|
+
throw new NoWriteError(
|
|
2323
|
+
`The ${target.name} app's actions are unreachable.`,
|
|
2324
|
+
{
|
|
2325
|
+
errorCode: "app_connection_unreachable",
|
|
2326
|
+
statusCode: 502,
|
|
2327
|
+
},
|
|
2328
|
+
);
|
|
1636
2329
|
}
|
|
1637
2330
|
return await withClient(manager, serverId);
|
|
1638
2331
|
} finally {
|
|
@@ -1641,7 +2334,7 @@ async function withGrantedAppMcp<T>(
|
|
|
1641
2334
|
});
|
|
1642
2335
|
}
|
|
1643
2336
|
}
|
|
1644
|
-
|
|
2337
|
+
appAuthorizationRequired(target, mcpUrl, remote?.serverId);
|
|
1645
2338
|
}
|
|
1646
2339
|
|
|
1647
2340
|
export async function listGrantedDispatchMcpAppActions(
|
|
@@ -1660,6 +2353,9 @@ export async function listGrantedDispatchMcpAppActions(
|
|
|
1660
2353
|
? tool.description
|
|
1661
2354
|
: (tool.description.split(/(?<=\.)\s/)[0] ?? "").slice(0, 160),
|
|
1662
2355
|
...(detailed ? { inputSchema: tool.inputSchema } : {}),
|
|
2356
|
+
...(detailed && tool.annotations?.destructiveHint === true
|
|
2357
|
+
? { destructive: true }
|
|
2358
|
+
: {}),
|
|
1663
2359
|
...(tool.annotations?.readOnlyHint === true
|
|
1664
2360
|
? { readOnly: true }
|
|
1665
2361
|
: {}),
|
|
@@ -1688,64 +2384,80 @@ export async function callGrantedDispatchMcpAppAction(
|
|
|
1688
2384
|
app: string,
|
|
1689
2385
|
action: string,
|
|
1690
2386
|
args: Record<string, unknown>,
|
|
2387
|
+
options?: { viewportMode?: ViewportMode },
|
|
1691
2388
|
): Promise<GrantedAppActionResult> {
|
|
1692
2389
|
if (TARGET_APP_HIDDEN_ACTIONS.has(action)) {
|
|
1693
|
-
throw new
|
|
2390
|
+
throw new NoWriteError(`"${action}" is not an app action.`, {
|
|
2391
|
+
errorCode: "app_action_not_found",
|
|
2392
|
+
statusCode: 404,
|
|
2393
|
+
});
|
|
1694
2394
|
}
|
|
1695
|
-
return withGrantedAppMcp(
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
)
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
} catch (error) {
|
|
1706
|
-
const reason = error instanceof Error ? error.message : String(error);
|
|
1707
|
-
if (mcpToolCallWasNotSent(error)) {
|
|
1708
|
-
fail(`The app action did not run (${reason}).`, {
|
|
1709
|
-
errorCode: "app_action_not_sent",
|
|
1710
|
-
statusCode:
|
|
1711
|
-
(error as { statusCode?: unknown }).statusCode === 403 ? 403 : 502,
|
|
1712
|
-
});
|
|
2395
|
+
return withGrantedAppMcp(
|
|
2396
|
+
app,
|
|
2397
|
+
action,
|
|
2398
|
+
async (manager, serverId) => {
|
|
2399
|
+
const toolName = buildMcpToolName(serverId, action);
|
|
2400
|
+
if (!manager.getTool(toolName)) {
|
|
2401
|
+
throw new NoWriteError(
|
|
2402
|
+
`The app has no action named "${action}". Call list_app_actions to see them.`,
|
|
2403
|
+
{ errorCode: "app_action_not_found", statusCode: 404 },
|
|
2404
|
+
);
|
|
1713
2405
|
}
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
)
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
2406
|
+
let result: unknown;
|
|
2407
|
+
try {
|
|
2408
|
+
result = await manager.callTool(toolName, args);
|
|
2409
|
+
} catch (error) {
|
|
2410
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
2411
|
+
if (mcpToolCallWasNotSent(error)) {
|
|
2412
|
+
throw new NoWriteError(`The app action did not run (${reason}).`, {
|
|
2413
|
+
errorCode: "app_action_not_sent",
|
|
2414
|
+
statusCode:
|
|
2415
|
+
(error as { statusCode?: unknown }).statusCode === 403
|
|
2416
|
+
? 403
|
|
2417
|
+
: 502,
|
|
2418
|
+
});
|
|
2419
|
+
}
|
|
2420
|
+
fail(
|
|
2421
|
+
`The app action failed before returning a result (${reason}). Its write outcome may be unknown; inspect the app before retrying.`,
|
|
2422
|
+
{ errorCode: "app_action_outcome_unknown", statusCode: 502 },
|
|
2423
|
+
);
|
|
2424
|
+
}
|
|
2425
|
+
if (
|
|
2426
|
+
result === null ||
|
|
2427
|
+
typeof result !== "object" ||
|
|
2428
|
+
!("content" in result) ||
|
|
2429
|
+
!Array.isArray(result.content)
|
|
2430
|
+
) {
|
|
2431
|
+
fail(
|
|
2432
|
+
"The app action returned an unreadable result. Its write outcome may be unknown; inspect the app before retrying.",
|
|
2433
|
+
{ errorCode: "app_action_result_unreadable", statusCode: 502 },
|
|
2434
|
+
);
|
|
2435
|
+
}
|
|
2436
|
+
const approvalReview = approvalReviewFrom(result);
|
|
2437
|
+
return {
|
|
2438
|
+
...("isError" in result && result.isError === true
|
|
2439
|
+
? { isError: true }
|
|
2440
|
+
: {}),
|
|
2441
|
+
...(approvalReview ? { approvalReview } : {}),
|
|
2442
|
+
...("structuredContent" in result &&
|
|
2443
|
+
result.structuredContent !== undefined
|
|
2444
|
+
? { structuredContent: result.structuredContent }
|
|
2445
|
+
: {}),
|
|
2446
|
+
content: result.content as unknown[],
|
|
2447
|
+
};
|
|
2448
|
+
},
|
|
2449
|
+
options,
|
|
2450
|
+
);
|
|
1743
2451
|
}
|
|
1744
2452
|
|
|
1745
2453
|
type TargetMcpCredential = {
|
|
1746
|
-
strategy: "deployment-user" | "org-member-delegation";
|
|
2454
|
+
strategy: "deployment-user" | "org-member-delegation" | "oauth";
|
|
1747
2455
|
token: string;
|
|
1748
2456
|
firstPartyMcp?: boolean;
|
|
2457
|
+
oauthAccessRefresh?: {
|
|
2458
|
+
identity: string;
|
|
2459
|
+
refresh: () => Promise<string | null>;
|
|
2460
|
+
};
|
|
1749
2461
|
};
|
|
1750
2462
|
|
|
1751
2463
|
async function createTargetMcpUserCredentials(input: {
|
|
@@ -1754,6 +2466,7 @@ async function createTargetMcpUserCredentials(input: {
|
|
|
1754
2466
|
orgId?: string;
|
|
1755
2467
|
firstPartyMcp?: boolean;
|
|
1756
2468
|
fullCatalog?: boolean;
|
|
2469
|
+
allowMissing?: boolean;
|
|
1757
2470
|
runtime?: "workspace-app-chat";
|
|
1758
2471
|
}): Promise<{
|
|
1759
2472
|
credentials: TargetMcpCredential[];
|
|
@@ -1841,7 +2554,7 @@ async function createTargetMcpUserCredentials(input: {
|
|
|
1841
2554
|
const orgMemberCredential = await loadOrgMemberCredential();
|
|
1842
2555
|
if (orgMemberCredential) credentials.push(orgMemberCredential);
|
|
1843
2556
|
}
|
|
1844
|
-
if (credentials.length === 0) {
|
|
2557
|
+
if (credentials.length === 0 && !input.allowMissing) {
|
|
1845
2558
|
throw new Error(
|
|
1846
2559
|
"Cross-app embed sessions require a deployment A2A secret or an organization secret with member delegation enabled on the target.",
|
|
1847
2560
|
);
|
|
@@ -1872,7 +2585,7 @@ async function resolveEmbedTarget(
|
|
|
1872
2585
|
return {
|
|
1873
2586
|
app: explicitApp,
|
|
1874
2587
|
path,
|
|
1875
|
-
url:
|
|
2588
|
+
url: joinAppBaseUrlAndPath(appHomeBaseUrl(explicitApp), path),
|
|
1876
2589
|
};
|
|
1877
2590
|
}
|
|
1878
2591
|
|
|
@@ -1892,7 +2605,7 @@ async function resolveEmbedTarget(
|
|
|
1892
2605
|
return {
|
|
1893
2606
|
app: explicitApp,
|
|
1894
2607
|
path,
|
|
1895
|
-
url:
|
|
2608
|
+
url: joinAppBaseUrlAndPath(appHomeBaseUrl(explicitApp), path),
|
|
1896
2609
|
};
|
|
1897
2610
|
}
|
|
1898
2611
|
|
|
@@ -1906,7 +2619,11 @@ async function resolveEmbedTarget(
|
|
|
1906
2619
|
const path = safeAppPath(appRelativePath(target, parsed));
|
|
1907
2620
|
if (!path) throw new Error("Embed URL path is not safe.");
|
|
1908
2621
|
assertAppCanOpenPath(target, path);
|
|
1909
|
-
return {
|
|
2622
|
+
return {
|
|
2623
|
+
app: target,
|
|
2624
|
+
path,
|
|
2625
|
+
url: joinAppBaseUrlAndPath(appHomeBaseUrl(target), path),
|
|
2626
|
+
};
|
|
1910
2627
|
}
|
|
1911
2628
|
|
|
1912
2629
|
async function resolveDispatchEmbedTarget(input: {
|
|
@@ -1970,6 +2687,7 @@ type InternalEmbedSession = {
|
|
|
1970
2687
|
startUrl?: string;
|
|
1971
2688
|
chatToken?: string;
|
|
1972
2689
|
targetPath?: string;
|
|
2690
|
+
childBrowserTabId?: string;
|
|
1973
2691
|
expiresAt?: number;
|
|
1974
2692
|
app: string;
|
|
1975
2693
|
appBaseUrl: string;
|
|
@@ -1978,6 +2696,7 @@ type InternalEmbedSession = {
|
|
|
1978
2696
|
function publicEmbedSession({
|
|
1979
2697
|
startUrl,
|
|
1980
2698
|
targetPath,
|
|
2699
|
+
childBrowserTabId,
|
|
1981
2700
|
expiresAt,
|
|
1982
2701
|
app,
|
|
1983
2702
|
}: InternalEmbedSession) {
|
|
@@ -1986,11 +2705,117 @@ function publicEmbedSession({
|
|
|
1986
2705
|
return {
|
|
1987
2706
|
startUrl,
|
|
1988
2707
|
...(targetPath ? { targetPath } : {}),
|
|
2708
|
+
...(childBrowserTabId ? { childBrowserTabId } : {}),
|
|
1989
2709
|
...(typeof expiresAt === "number" ? { expiresAt } : {}),
|
|
1990
2710
|
app,
|
|
1991
2711
|
};
|
|
1992
2712
|
}
|
|
1993
2713
|
|
|
2714
|
+
type ResolvedEmbedTarget = Awaited<
|
|
2715
|
+
ReturnType<typeof resolveDispatchEmbedTarget>
|
|
2716
|
+
>;
|
|
2717
|
+
type PendingEmbedSession = {
|
|
2718
|
+
result: Promise<InternalEmbedSession>;
|
|
2719
|
+
release: () => void;
|
|
2720
|
+
abort: () => void;
|
|
2721
|
+
};
|
|
2722
|
+
|
|
2723
|
+
const pendingGrantedEmbeds = new Map<string, PendingEmbedSession>();
|
|
2724
|
+
|
|
2725
|
+
function grantedEmbedPrefetchKey(
|
|
2726
|
+
ownerEmail: string,
|
|
2727
|
+
target: ResolvedEmbedTarget,
|
|
2728
|
+
): string {
|
|
2729
|
+
const request = getRequestContext();
|
|
2730
|
+
return JSON.stringify([
|
|
2731
|
+
ownerEmail,
|
|
2732
|
+
getRequestOrgId(),
|
|
2733
|
+
request?.requestOrigin,
|
|
2734
|
+
request?.identityAuthenticatedAtMs,
|
|
2735
|
+
request?.mcpCredentialIssuedAtMs,
|
|
2736
|
+
request?.a2aIdentityAssurance,
|
|
2737
|
+
request?.credentialScope,
|
|
2738
|
+
target.app.id,
|
|
2739
|
+
appMcpUrl(target.app),
|
|
2740
|
+
target.url,
|
|
2741
|
+
]);
|
|
2742
|
+
}
|
|
2743
|
+
|
|
2744
|
+
function prefetchGrantedEmbedSession(
|
|
2745
|
+
app: DispatchMcpAccessibleApp,
|
|
2746
|
+
url: string,
|
|
2747
|
+
) {
|
|
2748
|
+
const ownerEmail = getRequestUserEmail();
|
|
2749
|
+
if (!ownerEmail || app.id === DISPATCH_APP_ID) return;
|
|
2750
|
+
const work = (async () => {
|
|
2751
|
+
const target = await resolveDispatchEmbedTarget({ resolvedApp: app, url });
|
|
2752
|
+
const key = grantedEmbedPrefetchKey(ownerEmail, target);
|
|
2753
|
+
if (pendingGrantedEmbeds.has(key)) return;
|
|
2754
|
+
if (pendingGrantedEmbeds.size >= PENDING_EMBED_LIMIT) {
|
|
2755
|
+
pendingGrantedEmbeds.values().next().value?.abort();
|
|
2756
|
+
}
|
|
2757
|
+
const controller = new AbortController();
|
|
2758
|
+
const result = Promise.race([
|
|
2759
|
+
createEmbedSessionForResolvedApp({
|
|
2760
|
+
ownerEmail,
|
|
2761
|
+
orgId: getRequestOrgId(),
|
|
2762
|
+
target,
|
|
2763
|
+
chrome: "minimal",
|
|
2764
|
+
bindBrowserTab: true,
|
|
2765
|
+
signal: controller.signal,
|
|
2766
|
+
}),
|
|
2767
|
+
new Promise<never>((_resolve, reject) => {
|
|
2768
|
+
controller.signal.addEventListener(
|
|
2769
|
+
"abort",
|
|
2770
|
+
() => reject(controller.signal.reason),
|
|
2771
|
+
{ once: true },
|
|
2772
|
+
);
|
|
2773
|
+
}),
|
|
2774
|
+
]);
|
|
2775
|
+
const release = () => {
|
|
2776
|
+
clearTimeout(timer);
|
|
2777
|
+
if (pendingGrantedEmbeds.get(key) === entry)
|
|
2778
|
+
pendingGrantedEmbeds.delete(key);
|
|
2779
|
+
};
|
|
2780
|
+
const abort = () => {
|
|
2781
|
+
release();
|
|
2782
|
+
controller.abort();
|
|
2783
|
+
};
|
|
2784
|
+
const timer = setTimeout(abort, OPEN_APP_EMBED_WAIT_MS);
|
|
2785
|
+
timer.unref();
|
|
2786
|
+
const entry = { result, release, abort };
|
|
2787
|
+
pendingGrantedEmbeds.set(key, entry);
|
|
2788
|
+
try {
|
|
2789
|
+
await result;
|
|
2790
|
+
} catch {
|
|
2791
|
+
release();
|
|
2792
|
+
}
|
|
2793
|
+
})().catch(() => {});
|
|
2794
|
+
const waitUntil = getRequestRunContext()?.waitUntil ?? platformWaitUntil();
|
|
2795
|
+
waitUntil?.(work);
|
|
2796
|
+
}
|
|
2797
|
+
|
|
2798
|
+
async function takePrefetchedGrantedEmbedSession(
|
|
2799
|
+
ownerEmail: string,
|
|
2800
|
+
target: ResolvedEmbedTarget,
|
|
2801
|
+
): Promise<InternalEmbedSession | undefined> {
|
|
2802
|
+
const key = grantedEmbedPrefetchKey(ownerEmail, target);
|
|
2803
|
+
const entry = pendingGrantedEmbeds.get(key);
|
|
2804
|
+
if (!entry) return;
|
|
2805
|
+
pendingGrantedEmbeds.delete(key);
|
|
2806
|
+
const claimDeadline = setTimeout(entry.abort, BROWSER_EMBED_PREFETCH_WAIT_MS);
|
|
2807
|
+
try {
|
|
2808
|
+
const session = await entry.result;
|
|
2809
|
+
if (session.expiresAt === undefined || session.expiresAt > Date.now())
|
|
2810
|
+
return session;
|
|
2811
|
+
} catch {
|
|
2812
|
+
// coercion-ok: A failed prefetch requires the normal foreground mint.
|
|
2813
|
+
} finally {
|
|
2814
|
+
clearTimeout(claimDeadline);
|
|
2815
|
+
entry.release();
|
|
2816
|
+
}
|
|
2817
|
+
}
|
|
2818
|
+
|
|
1994
2819
|
async function createGrantedDispatchMcpEmbedSessionResult(input: {
|
|
1995
2820
|
app?: string;
|
|
1996
2821
|
resolvedApp?: DispatchMcpAccessibleApp;
|
|
@@ -1998,11 +2823,19 @@ async function createGrantedDispatchMcpEmbedSessionResult(input: {
|
|
|
1998
2823
|
path?: string;
|
|
1999
2824
|
chrome?: "full" | "minimal";
|
|
2000
2825
|
runtime?: "workspace-app-chat";
|
|
2826
|
+
bindBrowserTab?: boolean;
|
|
2001
2827
|
signal?: AbortSignal;
|
|
2002
2828
|
}): Promise<InternalEmbedSession> {
|
|
2003
2829
|
const userEmail = getRequestUserEmail();
|
|
2004
2830
|
if (!userEmail) throw new Error("no authenticated user");
|
|
2005
2831
|
const target = await resolveDispatchEmbedTarget(input);
|
|
2832
|
+
if (input.bindBrowserTab && input.chrome === "minimal") {
|
|
2833
|
+
const prefetched = await takePrefetchedGrantedEmbedSession(
|
|
2834
|
+
userEmail,
|
|
2835
|
+
target,
|
|
2836
|
+
);
|
|
2837
|
+
if (prefetched) return prefetched;
|
|
2838
|
+
}
|
|
2006
2839
|
|
|
2007
2840
|
return createEmbedSessionForResolvedApp({
|
|
2008
2841
|
ownerEmail: userEmail,
|
|
@@ -2010,21 +2843,20 @@ async function createGrantedDispatchMcpEmbedSessionResult(input: {
|
|
|
2010
2843
|
target,
|
|
2011
2844
|
chrome: input.chrome,
|
|
2012
2845
|
runtime: input.runtime,
|
|
2846
|
+
bindBrowserTab: input.bindBrowserTab,
|
|
2013
2847
|
...(input.signal ? { signal: input.signal } : {}),
|
|
2014
2848
|
});
|
|
2015
2849
|
}
|
|
2016
2850
|
|
|
2017
|
-
export async function createGrantedDispatchMcpEmbedSession(
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
app: string;
|
|
2027
|
-
}> {
|
|
2851
|
+
export async function createGrantedDispatchMcpEmbedSession(
|
|
2852
|
+
input: {
|
|
2853
|
+
app?: string | undefined;
|
|
2854
|
+
url?: string | undefined;
|
|
2855
|
+
path?: string | undefined;
|
|
2856
|
+
chrome?: "full" | "minimal" | undefined;
|
|
2857
|
+
},
|
|
2858
|
+
options?: { bindBrowserTab: boolean },
|
|
2859
|
+
): Promise<ReturnType<typeof publicEmbedSession>> {
|
|
2028
2860
|
// Copy only public fields so a caller cannot supply a pre-resolved app.
|
|
2029
2861
|
const { app, url, path, chrome } = input;
|
|
2030
2862
|
return publicEmbedSession(
|
|
@@ -2033,6 +2865,7 @@ export async function createGrantedDispatchMcpEmbedSession(input: {
|
|
|
2033
2865
|
...(url === undefined ? {} : { url }),
|
|
2034
2866
|
...(path === undefined ? {} : { path }),
|
|
2035
2867
|
...(chrome === undefined ? {} : { chrome }),
|
|
2868
|
+
...(options?.bindBrowserTab ? { bindBrowserTab: true } : {}),
|
|
2036
2869
|
}),
|
|
2037
2870
|
);
|
|
2038
2871
|
}
|
|
@@ -2079,11 +2912,11 @@ export async function createGrantedDispatchMcpEmbedSessionForWorkspaceAppChat(
|
|
|
2079
2912
|
|
|
2080
2913
|
async function createEmbedSessionForResolvedApp(input: {
|
|
2081
2914
|
ownerEmail: string;
|
|
2082
|
-
orgId?: string;
|
|
2915
|
+
orgId?: string | undefined;
|
|
2083
2916
|
target: { app: DispatchMcpAccessibleApp; path: string; url: string };
|
|
2084
|
-
chrome?: "full" | "minimal";
|
|
2085
|
-
runtime?: "workspace-app-chat";
|
|
2086
|
-
|
|
2917
|
+
chrome?: "full" | "minimal" | undefined;
|
|
2918
|
+
runtime?: "workspace-app-chat" | undefined;
|
|
2919
|
+
bindBrowserTab?: boolean | undefined;
|
|
2087
2920
|
signal?: AbortSignal;
|
|
2088
2921
|
}): Promise<InternalEmbedSession> {
|
|
2089
2922
|
const { ownerEmail, orgId, target, chrome } = input;
|
|
@@ -2102,19 +2935,18 @@ async function createEmbedSessionForResolvedApp(input: {
|
|
|
2102
2935
|
};
|
|
2103
2936
|
}
|
|
2104
2937
|
|
|
2105
|
-
const targetMcpUrl =
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
);
|
|
2938
|
+
const targetMcpUrl =
|
|
2939
|
+
input.runtime && target.app.workspaceMount
|
|
2940
|
+
? `${appHomeBaseUrl(target.app)}/mcp`
|
|
2941
|
+
: appMcpUrl(target.app);
|
|
2942
|
+
const firstPartyMcp = isWorkspaceSsoAppUrl(
|
|
2943
|
+
{ id: target.app.id, url: targetMcpUrl },
|
|
2944
|
+
{
|
|
2945
|
+
nodeEnv: "production",
|
|
2946
|
+
environmentLane: requestEnvironmentLane(),
|
|
2947
|
+
registryRaw: getAppConfig().auth.identitySsoAppRegistryJson,
|
|
2948
|
+
},
|
|
2949
|
+
);
|
|
2118
2950
|
if (input.runtime && !firstPartyMcp)
|
|
2119
2951
|
throw new Error("App chat requires a first-party workspace target.");
|
|
2120
2952
|
const targetCredentials = await createTargetMcpUserCredentials({
|
|
@@ -2129,6 +2961,16 @@ async function createEmbedSessionForResolvedApp(input: {
|
|
|
2129
2961
|
mcpUrl: targetMcpUrl,
|
|
2130
2962
|
url: target.url,
|
|
2131
2963
|
});
|
|
2964
|
+
const childBrowserTabId = input.bindBrowserTab
|
|
2965
|
+
? crypto.randomUUID()
|
|
2966
|
+
: undefined;
|
|
2967
|
+
const launchUrl = new URL(target.url);
|
|
2968
|
+
if (childBrowserTabId) {
|
|
2969
|
+
launchUrl.searchParams.set(
|
|
2970
|
+
DISPATCH_CHILD_BROWSER_TAB_PARAM,
|
|
2971
|
+
childBrowserTabId,
|
|
2972
|
+
);
|
|
2973
|
+
}
|
|
2132
2974
|
let parsed:
|
|
2133
2975
|
| {
|
|
2134
2976
|
startUrl?: string;
|
|
@@ -2149,7 +2991,7 @@ async function createEmbedSessionForResolvedApp(input: {
|
|
|
2149
2991
|
const result = await callTargetCreateEmbedSession({
|
|
2150
2992
|
mcpUrl: targetMcpUrl,
|
|
2151
2993
|
token: credential.token,
|
|
2152
|
-
url: target.url,
|
|
2994
|
+
url: childBrowserTabId ? launchUrl.toString() : target.url,
|
|
2153
2995
|
...(chrome !== undefined ? { chrome } : {}),
|
|
2154
2996
|
...(input.runtime ? { runtime: input.runtime } : {}),
|
|
2155
2997
|
...(input.signal ? { signal: input.signal } : {}),
|
|
@@ -2212,7 +3054,9 @@ async function createEmbedSessionForResolvedApp(input: {
|
|
|
2212
3054
|
targetPath: parsed.targetPath,
|
|
2213
3055
|
expiresAt: parsed.expiresAt,
|
|
2214
3056
|
app: target.app.id,
|
|
2215
|
-
appBaseUrl:
|
|
3057
|
+
appBaseUrl: target.app.workspaceMount
|
|
3058
|
+
? appHomeBaseUrl(target.app)
|
|
3059
|
+
: appChatBaseUrl(target.app),
|
|
2216
3060
|
};
|
|
2217
3061
|
}
|
|
2218
3062
|
if (!parsed.startUrl || parsed.token) {
|
|
@@ -2232,7 +3076,21 @@ async function createEmbedSessionForResolvedApp(input: {
|
|
|
2232
3076
|
app: target.app.id,
|
|
2233
3077
|
appBaseUrl: appChatBaseUrl(target.app),
|
|
2234
3078
|
};
|
|
2235
|
-
if (parsed.targetPath)
|
|
3079
|
+
if (parsed.targetPath) {
|
|
3080
|
+
output.targetPath = parsed.targetPath;
|
|
3081
|
+
const safePath = childBrowserTabId && safeAppPath(parsed.targetPath);
|
|
3082
|
+
if (safePath) {
|
|
3083
|
+
const reported = new URL(safePath, launchUrl.origin);
|
|
3084
|
+
if (
|
|
3085
|
+
reported.searchParams.get(DISPATCH_CHILD_BROWSER_TAB_PARAM) ===
|
|
3086
|
+
childBrowserTabId
|
|
3087
|
+
) {
|
|
3088
|
+
reported.searchParams.delete(DISPATCH_CHILD_BROWSER_TAB_PARAM);
|
|
3089
|
+
output.targetPath = reported.pathname + reported.search + reported.hash;
|
|
3090
|
+
output.childBrowserTabId = childBrowserTabId;
|
|
3091
|
+
}
|
|
3092
|
+
}
|
|
3093
|
+
}
|
|
2236
3094
|
if (typeof parsed.expiresAt === "number") output.expiresAt = parsed.expiresAt;
|
|
2237
3095
|
return output;
|
|
2238
3096
|
}
|
|
@@ -2243,6 +3101,7 @@ async function createWorkspaceSsoEmbedSessionResult(input: {
|
|
|
2243
3101
|
path?: string | undefined;
|
|
2244
3102
|
chrome?: "full" | "minimal" | undefined;
|
|
2245
3103
|
runtime?: "workspace-app-chat" | undefined;
|
|
3104
|
+
bindBrowserTab?: boolean;
|
|
2246
3105
|
}): Promise<InternalEmbedSession> {
|
|
2247
3106
|
const ownerEmail = getRequestUserEmail();
|
|
2248
3107
|
if (!ownerEmail) {
|
|
@@ -2283,24 +3142,28 @@ async function createWorkspaceSsoEmbedSessionResult(input: {
|
|
|
2283
3142
|
target,
|
|
2284
3143
|
chrome: input.chrome,
|
|
2285
3144
|
runtime: input.runtime,
|
|
2286
|
-
|
|
3145
|
+
bindBrowserTab: input.bindBrowserTab,
|
|
2287
3146
|
});
|
|
2288
3147
|
}
|
|
2289
3148
|
|
|
2290
|
-
export async function createWorkspaceSsoEmbedSession(
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
app: string;
|
|
2300
|
-
}> {
|
|
3149
|
+
export async function createWorkspaceSsoEmbedSession(
|
|
3150
|
+
input: {
|
|
3151
|
+
app?: string | undefined;
|
|
3152
|
+
url?: string | undefined;
|
|
3153
|
+
path?: string | undefined;
|
|
3154
|
+
chrome?: "full" | "minimal" | undefined;
|
|
3155
|
+
},
|
|
3156
|
+
options?: { bindBrowserTab: boolean },
|
|
3157
|
+
): Promise<ReturnType<typeof publicEmbedSession>> {
|
|
2301
3158
|
const { app, url, path, chrome } = input;
|
|
2302
3159
|
return publicEmbedSession(
|
|
2303
|
-
await createWorkspaceSsoEmbedSessionResult({
|
|
3160
|
+
await createWorkspaceSsoEmbedSessionResult({
|
|
3161
|
+
app,
|
|
3162
|
+
url,
|
|
3163
|
+
path,
|
|
3164
|
+
chrome,
|
|
3165
|
+
...(options?.bindBrowserTab ? { bindBrowserTab: true } : {}),
|
|
3166
|
+
}),
|
|
2304
3167
|
);
|
|
2305
3168
|
}
|
|
2306
3169
|
|