@duct-sdk/sdk 0.1.6
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/README.md +92 -0
- package/dist/adapters/next/index.d.ts +4 -0
- package/dist/adapters/next/index.js +5 -0
- package/dist/adapters/next/receiver.d.ts +44 -0
- package/dist/adapters/next/receiver.js +162 -0
- package/dist/adapters/next/shell.d.ts +88 -0
- package/dist/adapters/next/shell.js +255 -0
- package/dist/adapters/react/index.d.ts +2 -0
- package/dist/adapters/react/index.js +4 -0
- package/dist/adapters/react/shell.d.ts +88 -0
- package/dist/adapters/react/shell.js +255 -0
- package/dist/adapters/remix/index.d.ts +2 -0
- package/dist/adapters/remix/index.js +4 -0
- package/dist/adapters/remix/receiver.d.ts +44 -0
- package/dist/adapters/remix/receiver.js +153 -0
- package/dist/adapters/sveltekit/index.d.ts +2 -0
- package/dist/adapters/sveltekit/index.js +4 -0
- package/dist/adapters/sveltekit/receiver.d.ts +56 -0
- package/dist/adapters/sveltekit/receiver.js +154 -0
- package/dist/adapters/vite/index.d.ts +2 -0
- package/dist/adapters/vite/index.js +4 -0
- package/dist/adapters/vite/receiver.d.ts +82 -0
- package/dist/adapters/vite/receiver.js +164 -0
- package/dist/assets/logo.d.ts +1 -0
- package/dist/assets/logo.js +1 -0
- package/dist/cli/agents/api-agent.d.ts +14 -0
- package/dist/cli/agents/api-agent.js +656 -0
- package/dist/cli/agents/auth-agent.d.ts +6 -0
- package/dist/cli/agents/auth-agent.js +198 -0
- package/dist/cli/agents/deeplink-agent.d.ts +6 -0
- package/dist/cli/agents/deeplink-agent.js +263 -0
- package/dist/cli/agents/llm.d.ts +33 -0
- package/dist/cli/agents/llm.js +276 -0
- package/dist/cli/agents/pipeline.d.ts +51 -0
- package/dist/cli/agents/pipeline.js +236 -0
- package/dist/cli/agents/reviewer-agent.d.ts +11 -0
- package/dist/cli/agents/reviewer-agent.js +442 -0
- package/dist/cli/agents/route-agent.d.ts +13 -0
- package/dist/cli/agents/route-agent.js +346 -0
- package/dist/cli/detectors/auth.d.ts +18 -0
- package/dist/cli/detectors/auth.js +273 -0
- package/dist/cli/detectors/framework.d.ts +22 -0
- package/dist/cli/detectors/framework.js +242 -0
- package/dist/cli/drift.d.ts +39 -0
- package/dist/cli/drift.js +247 -0
- package/dist/cli/fs/sandbox.d.ts +77 -0
- package/dist/cli/fs/sandbox.js +295 -0
- package/dist/cli/generators/config.d.ts +7 -0
- package/dist/cli/generators/config.js +255 -0
- package/dist/cli/generators/receiver.d.ts +12 -0
- package/dist/cli/generators/receiver.js +683 -0
- package/dist/cli/generators/shell.d.ts +18 -0
- package/dist/cli/generators/shell.js +672 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +603 -0
- package/dist/cli/init.d.ts +26 -0
- package/dist/cli/init.js +835 -0
- package/dist/core/action.d.ts +37 -0
- package/dist/core/action.js +140 -0
- package/dist/core/config.d.ts +514 -0
- package/dist/core/config.js +221 -0
- package/dist/core/endpoints.d.ts +17 -0
- package/dist/core/endpoints.js +128 -0
- package/dist/core/token.d.ts +70 -0
- package/dist/core/token.js +174 -0
- package/dist/core/types.d.ts +450 -0
- package/dist/core/types.js +11 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +9 -0
- package/package.json +84 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import React, { type CSSProperties } from "react";
|
|
2
|
+
export interface DuctShellProps {
|
|
3
|
+
/** Your Duct shell ID (e.g., "shell_acme_prod") */
|
|
4
|
+
shellId: string;
|
|
5
|
+
/**
|
|
6
|
+
* User context to pass to the shell as public metadata.
|
|
7
|
+
* Do NOT include sensitive data — this is not authenticated.
|
|
8
|
+
*/
|
|
9
|
+
userContext?: Record<string, string | number | boolean | null>;
|
|
10
|
+
/**
|
|
11
|
+
* Called when the Duct shell requests an auth token.
|
|
12
|
+
* Should call your /api/duct/token endpoint and return the JWT string.
|
|
13
|
+
* Return empty string for unauthenticated/guest users.
|
|
14
|
+
*/
|
|
15
|
+
onTokenRequest?: (shellId: string) => Promise<string>;
|
|
16
|
+
/** Called when the shell requests navigation to a URL (deeplink handoff) */
|
|
17
|
+
onNavigate?: (url: string) => void;
|
|
18
|
+
/** Controlled open state */
|
|
19
|
+
open?: boolean;
|
|
20
|
+
/** Called whenever shell open state changes */
|
|
21
|
+
onOpenChange?: (open: boolean) => void;
|
|
22
|
+
/** Whether the shell is initially open (default: false) */
|
|
23
|
+
defaultOpen?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Duct shell host URL.
|
|
26
|
+
* For local dev: http://localhost:3000
|
|
27
|
+
* For production: https://shell.duct.sh
|
|
28
|
+
*/
|
|
29
|
+
shellHost?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Position of the floating launcher.
|
|
32
|
+
* @default "bottom-right"
|
|
33
|
+
*/
|
|
34
|
+
position?: "bottom-right" | "bottom-left";
|
|
35
|
+
/**
|
|
36
|
+
* Panel width in pixels.
|
|
37
|
+
* @default 420
|
|
38
|
+
*/
|
|
39
|
+
panelWidth?: number;
|
|
40
|
+
/**
|
|
41
|
+
* Panel height in pixels.
|
|
42
|
+
* @default 640
|
|
43
|
+
*/
|
|
44
|
+
panelHeight?: number;
|
|
45
|
+
/** Override container styles (wrapper div). */
|
|
46
|
+
style?: CSSProperties;
|
|
47
|
+
className?: string;
|
|
48
|
+
/**
|
|
49
|
+
* Optional launcher icon source.
|
|
50
|
+
* When omitted, the SDK tries cloud-hosted icon first, then local /assets/logo.png.
|
|
51
|
+
*/
|
|
52
|
+
launcherIconSrc?: string;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* DuctShell — a self-contained floating launcher button + chat panel.
|
|
56
|
+
*
|
|
57
|
+
* Drop this anywhere in your layout. It renders:
|
|
58
|
+
* - A fixed bubble button (bottom-right by default)
|
|
59
|
+
* - A full-height chat panel that opens when clicked
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```tsx
|
|
63
|
+
* // app/layout.tsx
|
|
64
|
+
* <DuctShell
|
|
65
|
+
* shellId={process.env.DUCT_SHELL_ID!}
|
|
66
|
+
* shellHost={process.env.DUCT_SHELL_HOST}
|
|
67
|
+
* onTokenRequest={async () => {
|
|
68
|
+
* const res = await fetch("/api/duct/token", { method: "POST" });
|
|
69
|
+
* const { token } = await res.json();
|
|
70
|
+
* return token;
|
|
71
|
+
* }}
|
|
72
|
+
* />
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export declare function DuctShell({ shellId, userContext, onTokenRequest, onNavigate, open, onOpenChange, defaultOpen, shellHost, position, panelWidth, panelHeight, style, className, launcherIconSrc, }: DuctShellProps): React.ReactElement | null;
|
|
76
|
+
export interface DuctShellButtonProps {
|
|
77
|
+
shellId: string;
|
|
78
|
+
children?: React.ReactNode;
|
|
79
|
+
onToggle?: (open: boolean) => void;
|
|
80
|
+
className?: string;
|
|
81
|
+
style?: CSSProperties;
|
|
82
|
+
open?: boolean;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Headless launch button — use when you want a custom trigger.
|
|
86
|
+
* Pair with DuctShell in controlled mode.
|
|
87
|
+
*/
|
|
88
|
+
export declare function DuctShellButton({ children, onToggle, className, style, open: controlledOpen, }: DuctShellButtonProps): React.ReactElement | null;
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// @duct-sdk/sdk/react — DuctShell React Component
|
|
3
|
+
// Floating launcher + iframe panel with postMessage bridge
|
|
4
|
+
// ============================================================
|
|
5
|
+
"use client";
|
|
6
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
7
|
+
import { useEffect, useRef, useCallback, useState, } from "react";
|
|
8
|
+
// ----------------------------------------------------------------
|
|
9
|
+
// DuctShell — floating launcher + panel
|
|
10
|
+
// ----------------------------------------------------------------
|
|
11
|
+
const DUCT_SHELL_HOST_DEFAULT = (typeof process !== "undefined" && process.env?.["DUCT_SHELL_HOST"]) ||
|
|
12
|
+
"http://localhost:3000";
|
|
13
|
+
const DUCT_LAUNCHER_ICON_LOCAL_FALLBACK = "/assets/logo.png";
|
|
14
|
+
/**
|
|
15
|
+
* DuctShell — a self-contained floating launcher button + chat panel.
|
|
16
|
+
*
|
|
17
|
+
* Drop this anywhere in your layout. It renders:
|
|
18
|
+
* - A fixed bubble button (bottom-right by default)
|
|
19
|
+
* - A full-height chat panel that opens when clicked
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```tsx
|
|
23
|
+
* // app/layout.tsx
|
|
24
|
+
* <DuctShell
|
|
25
|
+
* shellId={process.env.DUCT_SHELL_ID!}
|
|
26
|
+
* shellHost={process.env.DUCT_SHELL_HOST}
|
|
27
|
+
* onTokenRequest={async () => {
|
|
28
|
+
* const res = await fetch("/api/duct/token", { method: "POST" });
|
|
29
|
+
* const { token } = await res.json();
|
|
30
|
+
* return token;
|
|
31
|
+
* }}
|
|
32
|
+
* />
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export function DuctShell({ shellId, userContext, onTokenRequest, onNavigate, open, onOpenChange, defaultOpen = false, shellHost = DUCT_SHELL_HOST_DEFAULT, position = "bottom-right", panelWidth = 420, panelHeight = 640, style, className, launcherIconSrc, }) {
|
|
36
|
+
const iframeRef = useRef(null);
|
|
37
|
+
const [isReady, setIsReady] = useState(false);
|
|
38
|
+
const [isOpen, setIsOpen] = useState(defaultOpen);
|
|
39
|
+
const [isAnimating, setIsAnimating] = useState(false);
|
|
40
|
+
const [iconCandidateIndex, setIconCandidateIndex] = useState(0);
|
|
41
|
+
const [iconLoadFailed, setIconLoadFailed] = useState(false);
|
|
42
|
+
const pendingTokenRequests = useRef(new Map());
|
|
43
|
+
const controlled = open !== undefined;
|
|
44
|
+
const panelOpen = controlled ? open : isOpen;
|
|
45
|
+
const shellUrl = buildShellUrl(shellHost, shellId, userContext);
|
|
46
|
+
const shellOrigin = (() => {
|
|
47
|
+
try {
|
|
48
|
+
return new URL(shellHost).origin;
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return shellHost;
|
|
52
|
+
}
|
|
53
|
+
})();
|
|
54
|
+
const iconCandidates = launcherIconSrc
|
|
55
|
+
? [launcherIconSrc]
|
|
56
|
+
: [`${shellOrigin}/duct-logo.png`, DUCT_LAUNCHER_ICON_LOCAL_FALLBACK];
|
|
57
|
+
const currentIconSrc = iconCandidates[Math.min(iconCandidateIndex, iconCandidates.length - 1)];
|
|
58
|
+
// Send a message to the iframe
|
|
59
|
+
const postToShell = useCallback((message) => {
|
|
60
|
+
iframeRef.current?.contentWindow?.postMessage(message, shellOrigin);
|
|
61
|
+
}, [shellOrigin]);
|
|
62
|
+
// Toggle open state
|
|
63
|
+
const setOpen = useCallback((next) => {
|
|
64
|
+
if (!controlled)
|
|
65
|
+
setIsOpen(next);
|
|
66
|
+
onOpenChange?.(next);
|
|
67
|
+
}, [controlled, onOpenChange]);
|
|
68
|
+
// Animate open
|
|
69
|
+
const openPanel = useCallback(() => {
|
|
70
|
+
setIsAnimating(true);
|
|
71
|
+
setOpen(true);
|
|
72
|
+
requestAnimationFrame(() => requestAnimationFrame(() => setIsAnimating(false)));
|
|
73
|
+
}, [setOpen]);
|
|
74
|
+
// Animate close
|
|
75
|
+
const closePanel = useCallback(() => {
|
|
76
|
+
setIsAnimating(true);
|
|
77
|
+
setTimeout(() => {
|
|
78
|
+
setOpen(false);
|
|
79
|
+
setIsAnimating(false);
|
|
80
|
+
}, 220);
|
|
81
|
+
}, [setOpen]);
|
|
82
|
+
// Handle inbound messages from the iframe
|
|
83
|
+
const handleMessage = useCallback(async (event) => {
|
|
84
|
+
if (event.origin !== shellOrigin)
|
|
85
|
+
return;
|
|
86
|
+
if (typeof event.data !== "object" || !event.data?.type)
|
|
87
|
+
return;
|
|
88
|
+
const msg = event.data;
|
|
89
|
+
switch (msg.type) {
|
|
90
|
+
case "DUCT_READY": {
|
|
91
|
+
setIsReady(true);
|
|
92
|
+
if (userContext && Object.keys(userContext).length > 0) {
|
|
93
|
+
postToShell({ type: "DUCT_USER_CONTEXT", context: userContext });
|
|
94
|
+
}
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
case "DUCT_REQUEST_TOKEN": {
|
|
98
|
+
const { requestId, shellId: reqShellId } = msg;
|
|
99
|
+
if (!onTokenRequest) {
|
|
100
|
+
postToShell({ type: "DUCT_TOKEN_RESPONSE", requestId, token: "" });
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
const token = await onTokenRequest(reqShellId);
|
|
105
|
+
postToShell({ type: "DUCT_TOKEN_RESPONSE", requestId, token: token ?? "" });
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
postToShell({ type: "DUCT_TOKEN_RESPONSE", requestId, token: "" });
|
|
109
|
+
}
|
|
110
|
+
pendingTokenRequests.current.delete(requestId);
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
case "DUCT_DEEPLINK_NAVIGATE": {
|
|
114
|
+
const { url } = msg;
|
|
115
|
+
if (onNavigate) {
|
|
116
|
+
onNavigate(url);
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
window.location.href = url;
|
|
120
|
+
}
|
|
121
|
+
postToShell({ type: "DUCT_NAVIGATE_CONFIRMED", url });
|
|
122
|
+
closePanel();
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
case "DUCT_OPEN": {
|
|
126
|
+
openPanel();
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
case "DUCT_CLOSE": {
|
|
130
|
+
closePanel();
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}, [shellOrigin, userContext, onTokenRequest, postToShell, onNavigate, openPanel, closePanel]);
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
window.addEventListener("message", handleMessage);
|
|
137
|
+
return () => window.removeEventListener("message", handleMessage);
|
|
138
|
+
}, [handleMessage]);
|
|
139
|
+
useEffect(() => {
|
|
140
|
+
if (!isReady || !userContext)
|
|
141
|
+
return;
|
|
142
|
+
postToShell({ type: "DUCT_USER_CONTEXT", context: userContext });
|
|
143
|
+
}, [isReady, userContext, postToShell]);
|
|
144
|
+
// Sync controlled open
|
|
145
|
+
useEffect(() => {
|
|
146
|
+
if (open === undefined)
|
|
147
|
+
return;
|
|
148
|
+
if (open)
|
|
149
|
+
openPanel();
|
|
150
|
+
else
|
|
151
|
+
closePanel();
|
|
152
|
+
}, [open]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
153
|
+
useEffect(() => {
|
|
154
|
+
setIconCandidateIndex(0);
|
|
155
|
+
setIconLoadFailed(false);
|
|
156
|
+
}, [launcherIconSrc, shellOrigin]);
|
|
157
|
+
const handleIconError = useCallback(() => {
|
|
158
|
+
if (iconCandidateIndex < iconCandidates.length - 1) {
|
|
159
|
+
setIconCandidateIndex((prev) => prev + 1);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
setIconLoadFailed(true);
|
|
163
|
+
}, [iconCandidateIndex, iconCandidates.length]);
|
|
164
|
+
if (!shellId) {
|
|
165
|
+
console.error("[DuctShell] shellId prop is required");
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
const isRight = position === "bottom-right";
|
|
169
|
+
const panelVisible = panelOpen || isAnimating;
|
|
170
|
+
// Responsive: full-screen on small viewports
|
|
171
|
+
const panelStyle = {
|
|
172
|
+
position: "fixed",
|
|
173
|
+
bottom: "80px",
|
|
174
|
+
[isRight ? "right" : "left"]: "16px",
|
|
175
|
+
width: `min(${panelWidth}px, calc(100vw - 32px))`,
|
|
176
|
+
height: `min(${panelHeight}px, calc(100dvh - 96px))`,
|
|
177
|
+
zIndex: 9998,
|
|
178
|
+
borderRadius: "16px",
|
|
179
|
+
overflow: "hidden",
|
|
180
|
+
boxShadow: "0 24px 64px rgba(0,0,0,0.18), 0 8px 24px rgba(0,0,0,0.10)",
|
|
181
|
+
// Slide-in / slide-out animation
|
|
182
|
+
// Opens: slides up from below (translateY(100%) → translateY(0))
|
|
183
|
+
// Closes: slides back down
|
|
184
|
+
opacity: panelOpen && !isAnimating ? 1 : 0,
|
|
185
|
+
transform: panelOpen && !isAnimating
|
|
186
|
+
? "translateY(0)"
|
|
187
|
+
: "translateY(100%)",
|
|
188
|
+
transition: "opacity 220ms cubic-bezier(0.16,1,0.3,1), transform 0.25s cubic-bezier(0.16, 1, 0.3, 1)",
|
|
189
|
+
pointerEvents: panelOpen ? "auto" : "none",
|
|
190
|
+
visibility: panelVisible ? "visible" : "hidden",
|
|
191
|
+
...style,
|
|
192
|
+
};
|
|
193
|
+
const buttonStyle = {
|
|
194
|
+
position: "fixed",
|
|
195
|
+
bottom: "16px",
|
|
196
|
+
[isRight ? "right" : "left"]: "16px",
|
|
197
|
+
zIndex: 9999,
|
|
198
|
+
width: "56px",
|
|
199
|
+
height: "56px",
|
|
200
|
+
borderRadius: "50%",
|
|
201
|
+
border: "none",
|
|
202
|
+
cursor: "pointer",
|
|
203
|
+
display: "flex",
|
|
204
|
+
alignItems: "center",
|
|
205
|
+
justifyContent: "center",
|
|
206
|
+
backgroundColor: "#E8622A",
|
|
207
|
+
boxShadow: "0 4px 16px rgba(232,98,42,0.40), 0 2px 8px rgba(0,0,0,0.12)",
|
|
208
|
+
padding: 0,
|
|
209
|
+
transition: "transform 150ms ease, box-shadow 150ms ease",
|
|
210
|
+
};
|
|
211
|
+
return (_jsxs(_Fragment, { children: [!panelOpen && (_jsx("button", { type: "button", style: buttonStyle, onClick: openPanel, "aria-label": "Open Duct Shell", className: className, onMouseEnter: (e) => {
|
|
212
|
+
e.currentTarget.style.transform = "scale(1.07)";
|
|
213
|
+
e.currentTarget.style.boxShadow =
|
|
214
|
+
"0 6px 20px rgba(232,98,42,0.50), 0 2px 8px rgba(0,0,0,0.14)";
|
|
215
|
+
}, onMouseLeave: (e) => {
|
|
216
|
+
e.currentTarget.style.transform = "scale(1)";
|
|
217
|
+
e.currentTarget.style.boxShadow =
|
|
218
|
+
"0 4px 16px rgba(232,98,42,0.40), 0 2px 8px rgba(0,0,0,0.12)";
|
|
219
|
+
}, children: !iconLoadFailed ? (_jsx("img", { src: currentIconSrc, alt: "Duct", width: 28, height: 28, onError: handleIconError, style: { width: "28px", height: "28px", objectFit: "contain" } })) : (_jsx("svg", { width: "28", height: "28", viewBox: "0 0 24 24", fill: "none", "aria-hidden": "true", children: _jsx("path", { d: "M18 12c0-3.3-2.2-5.5-5.5-5.5C9 6.5 7 8.7 7 12s2 5.5 5.5 5.5c2 0 3.6-.8 4.6-2.2l-2.2-1.6c-.6.8-1.4 1.3-2.4 1.3-1.6 0-2.8-1.2-2.8-3s1.2-3 2.8-3c1 0 1.8.5 2.3 1.3L18 12z", fill: "white" }) })) })), _jsx("div", { style: panelStyle, "data-duct-shell": shellId, children: _jsx("iframe", { ref: iframeRef, src: shellUrl, title: "Duct Shell", sandbox: "allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox", allow: "clipboard-write", referrerPolicy: "no-referrer-when-downgrade", style: {
|
|
220
|
+
width: "100%",
|
|
221
|
+
height: "100%",
|
|
222
|
+
border: "none",
|
|
223
|
+
display: "block",
|
|
224
|
+
}, loading: "lazy" }) })] }));
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Headless launch button — use when you want a custom trigger.
|
|
228
|
+
* Pair with DuctShell in controlled mode.
|
|
229
|
+
*/
|
|
230
|
+
export function DuctShellButton({ children, onToggle, className, style, open: controlledOpen, }) {
|
|
231
|
+
const [internalOpen, setInternalOpen] = useState(false);
|
|
232
|
+
const isOpen = controlledOpen !== undefined ? controlledOpen : internalOpen;
|
|
233
|
+
const handleClick = useCallback(() => {
|
|
234
|
+
const next = !isOpen;
|
|
235
|
+
if (controlledOpen === undefined)
|
|
236
|
+
setInternalOpen(next);
|
|
237
|
+
onToggle?.(next);
|
|
238
|
+
}, [isOpen, controlledOpen, onToggle]);
|
|
239
|
+
return (_jsx("button", { type: "button", onClick: handleClick, className: className, style: style, "aria-label": isOpen ? "Close Duct Shell" : "Open Duct Shell", children: children ?? (isOpen ? "✕" : "Ask AI") }));
|
|
240
|
+
}
|
|
241
|
+
// ----------------------------------------------------------------
|
|
242
|
+
// Helpers
|
|
243
|
+
// ----------------------------------------------------------------
|
|
244
|
+
function buildShellUrl(shellHost, shellId, userContext) {
|
|
245
|
+
const base = shellHost.replace(/\/$/, "");
|
|
246
|
+
const url = new URL(`${base}/${shellId}`);
|
|
247
|
+
if (userContext) {
|
|
248
|
+
for (const [key, value] of Object.entries(userContext)) {
|
|
249
|
+
if (value !== null && value !== undefined) {
|
|
250
|
+
url.searchParams.set(`ctx_${key}`, String(value));
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return url.toString();
|
|
255
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { DuctConfig, SessionContext } from "../../core/types.js";
|
|
2
|
+
export interface DuctRemixReceiverOptions {
|
|
3
|
+
/** Your Duct config (import from duct.config.ts) */
|
|
4
|
+
config: DuctConfig;
|
|
5
|
+
/** HMAC secret key — use process.env.DUCT_SECRET_KEY */
|
|
6
|
+
secretKey: string;
|
|
7
|
+
/**
|
|
8
|
+
* Called when a valid deeplink token is received.
|
|
9
|
+
* Use this to pre-load data, set session cookies, etc.
|
|
10
|
+
*/
|
|
11
|
+
onReceive: (deeplinkId: string, state: Record<string, unknown>, session: SessionContext) => Promise<RemixReceiverResult | void>;
|
|
12
|
+
/**
|
|
13
|
+
* Called when token verification fails.
|
|
14
|
+
* Return a Response to override the default 401.
|
|
15
|
+
*/
|
|
16
|
+
onError?: (error: Error, request: Request) => Response | Promise<Response>;
|
|
17
|
+
}
|
|
18
|
+
export interface RemixReceiverResult {
|
|
19
|
+
/** Override the redirect URL (default: derived from config) */
|
|
20
|
+
redirectUrl?: string;
|
|
21
|
+
/** Additional headers to set on the redirect response */
|
|
22
|
+
headers?: Record<string, string>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Create a Remix action function for the Duct deeplink receiver.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* // app/routes/api.duct.receive.ts
|
|
30
|
+
* import { DuctRemixReceiver } from "@duct/sdk/remix";
|
|
31
|
+
* import ductConfig from "../../duct.config";
|
|
32
|
+
*
|
|
33
|
+
* export const action = DuctRemixReceiver({
|
|
34
|
+
* config: ductConfig,
|
|
35
|
+
* secretKey: process.env.DUCT_SECRET_KEY!,
|
|
36
|
+
* async onReceive(deeplinkId, state, session) {
|
|
37
|
+
* console.log("Deeplink received:", deeplinkId, state);
|
|
38
|
+
* },
|
|
39
|
+
* });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function DuctRemixReceiver(options: DuctRemixReceiverOptions): ({ request }: {
|
|
43
|
+
request: Request;
|
|
44
|
+
}) => Promise<Response>;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// @duct/sdk — Remix Deeplink Receiver
|
|
3
|
+
// Verifies Duct deeplink tokens for Remix (v2/v3 loader/action)
|
|
4
|
+
// ============================================================
|
|
5
|
+
import { verifyDeeplinkToken } from "../../core/token.js";
|
|
6
|
+
// ----------------------------------------------------------------
|
|
7
|
+
// Helpers
|
|
8
|
+
// ----------------------------------------------------------------
|
|
9
|
+
function buildRedirectUrl(targetRoute, state, baseUrl) {
|
|
10
|
+
let path = targetRoute;
|
|
11
|
+
const usedKeys = new Set();
|
|
12
|
+
for (const match of [...targetRoute.matchAll(/\[([^\]]+)\]/g)]) {
|
|
13
|
+
const key = match[1];
|
|
14
|
+
const value = state[key] ?? state[`${key}Id`] ?? state[`${key}_id`];
|
|
15
|
+
if (value !== undefined) {
|
|
16
|
+
path = path.replace(match[0], String(value));
|
|
17
|
+
usedKeys.add(key);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const qs = Object.entries(state)
|
|
21
|
+
.filter(([k, v]) => !usedKeys.has(k) && v !== undefined && v !== null)
|
|
22
|
+
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`)
|
|
23
|
+
.join("&");
|
|
24
|
+
return `${baseUrl.replace(/\/$/, "")}${path}${qs ? `?${qs}` : ""}`;
|
|
25
|
+
}
|
|
26
|
+
function buildSession(sub, shellId) {
|
|
27
|
+
return {
|
|
28
|
+
sessionId: `dl_${Date.now()}_${Math.random().toString(36).slice(2)}`,
|
|
29
|
+
shellId,
|
|
30
|
+
user: { id: sub },
|
|
31
|
+
isAgent: false,
|
|
32
|
+
messages: [],
|
|
33
|
+
createdAt: new Date().toISOString(),
|
|
34
|
+
updatedAt: new Date().toISOString(),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
// ----------------------------------------------------------------
|
|
38
|
+
// DuctRemixReceiver — returns a Remix action function
|
|
39
|
+
// ----------------------------------------------------------------
|
|
40
|
+
/**
|
|
41
|
+
* Create a Remix action function for the Duct deeplink receiver.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* // app/routes/api.duct.receive.ts
|
|
46
|
+
* import { DuctRemixReceiver } from "@duct/sdk/remix";
|
|
47
|
+
* import ductConfig from "../../duct.config";
|
|
48
|
+
*
|
|
49
|
+
* export const action = DuctRemixReceiver({
|
|
50
|
+
* config: ductConfig,
|
|
51
|
+
* secretKey: process.env.DUCT_SECRET_KEY!,
|
|
52
|
+
* async onReceive(deeplinkId, state, session) {
|
|
53
|
+
* console.log("Deeplink received:", deeplinkId, state);
|
|
54
|
+
* },
|
|
55
|
+
* });
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export function DuctRemixReceiver(options) {
|
|
59
|
+
const { config, secretKey, onReceive, onError } = options;
|
|
60
|
+
return async function action({ request }) {
|
|
61
|
+
if (request.method !== "POST") {
|
|
62
|
+
return new Response(JSON.stringify({ error: "Method not allowed" }), {
|
|
63
|
+
status: 405,
|
|
64
|
+
headers: { "Content-Type": "application/json" },
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
if (!secretKey) {
|
|
68
|
+
console.error("[Duct] DUCT_SECRET_KEY is not set");
|
|
69
|
+
return new Response(JSON.stringify({ error: "Server configuration error" }), {
|
|
70
|
+
status: 500,
|
|
71
|
+
headers: { "Content-Type": "application/json" },
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
// Extract token
|
|
75
|
+
const authHeader = request.headers.get("authorization");
|
|
76
|
+
let token = null;
|
|
77
|
+
if (authHeader?.startsWith("Bearer ")) {
|
|
78
|
+
token = authHeader.slice(7);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
try {
|
|
82
|
+
const contentType = request.headers.get("content-type") ?? "";
|
|
83
|
+
if (contentType.includes("application/json")) {
|
|
84
|
+
const body = await request.json();
|
|
85
|
+
token = body.token ?? null;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
const form = await request.formData();
|
|
89
|
+
token = form.get("token");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
return new Response(JSON.stringify({ error: "Invalid request body" }), {
|
|
94
|
+
status: 400,
|
|
95
|
+
headers: { "Content-Type": "application/json" },
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (!token) {
|
|
100
|
+
return new Response(JSON.stringify({ error: "No token provided" }), {
|
|
101
|
+
status: 401,
|
|
102
|
+
headers: { "Content-Type": "application/json" },
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
// Verify deeplink token
|
|
106
|
+
const result = await verifyDeeplinkToken(token, {
|
|
107
|
+
secretKey,
|
|
108
|
+
expectedShellId: config.shellId,
|
|
109
|
+
});
|
|
110
|
+
if (!result.valid || !result.payload) {
|
|
111
|
+
const err = new Error(result.error ?? "Invalid token");
|
|
112
|
+
if (onError)
|
|
113
|
+
return onError(err, request);
|
|
114
|
+
return new Response(JSON.stringify({ error: "Invalid token", detail: result.error }), {
|
|
115
|
+
status: 401,
|
|
116
|
+
headers: { "Content-Type": "application/json" },
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
const { deeplink_id, state, sub } = result.payload;
|
|
120
|
+
const deeplinkDef = config.deeplinks.find((d) => d.id === deeplink_id);
|
|
121
|
+
if (!deeplinkDef) {
|
|
122
|
+
return new Response(JSON.stringify({ error: "Unknown deeplink" }), {
|
|
123
|
+
status: 400,
|
|
124
|
+
headers: { "Content-Type": "application/json" },
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
const session = buildSession(sub, config.shellId);
|
|
128
|
+
let redirectUrl = buildRedirectUrl(deeplinkDef.targetRoute, state, config.product.baseUrl);
|
|
129
|
+
try {
|
|
130
|
+
const userResult = await onReceive(deeplink_id, state, session);
|
|
131
|
+
if (userResult?.redirectUrl)
|
|
132
|
+
redirectUrl = userResult.redirectUrl;
|
|
133
|
+
const headers = new Headers({ Location: redirectUrl });
|
|
134
|
+
headers.set("Content-Type", "application/json");
|
|
135
|
+
if (userResult?.headers) {
|
|
136
|
+
for (const [k, v] of Object.entries(userResult.headers)) {
|
|
137
|
+
headers.set(k, v);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return new Response(null, { status: 302, headers });
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
144
|
+
console.error("[Duct] onReceive error:", error);
|
|
145
|
+
if (onError)
|
|
146
|
+
return onError(error, request);
|
|
147
|
+
return new Response(JSON.stringify({ error: "Handler error" }), {
|
|
148
|
+
status: 500,
|
|
149
|
+
headers: { "Content-Type": "application/json" },
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { DuctConfig, SessionContext } from "../../core/types.js";
|
|
2
|
+
/**
|
|
3
|
+
* A SvelteKit RequestEvent — subset of what SvelteKit provides.
|
|
4
|
+
* Typed loosely so the adapter does not require @sveltejs/kit as a
|
|
5
|
+
* peer dependency at import time; the real RequestEvent is assignment-
|
|
6
|
+
* compatible with this interface.
|
|
7
|
+
*/
|
|
8
|
+
export interface SvelteKitRequestEvent {
|
|
9
|
+
request: Request;
|
|
10
|
+
params: Record<string, string>;
|
|
11
|
+
url: URL;
|
|
12
|
+
locals: Record<string, unknown>;
|
|
13
|
+
}
|
|
14
|
+
export interface DuctSvelteKitReceiverOptions {
|
|
15
|
+
/** Your Duct config (import from duct.config.ts) */
|
|
16
|
+
config: DuctConfig;
|
|
17
|
+
/** HMAC secret key — use process.env.DUCT_SECRET_KEY */
|
|
18
|
+
secretKey: string;
|
|
19
|
+
/**
|
|
20
|
+
* Called when a valid deeplink token is received.
|
|
21
|
+
* Use this to pre-load data, set session cookies, etc.
|
|
22
|
+
*/
|
|
23
|
+
onReceive: (deeplinkId: string, state: Record<string, unknown>, session: SessionContext) => Promise<SvelteKitReceiverResult | void>;
|
|
24
|
+
/**
|
|
25
|
+
* Called when token verification fails.
|
|
26
|
+
* Return a Response to override the default 401.
|
|
27
|
+
*/
|
|
28
|
+
onError?: (error: Error, event: SvelteKitRequestEvent) => Response | Promise<Response>;
|
|
29
|
+
}
|
|
30
|
+
export interface SvelteKitReceiverResult {
|
|
31
|
+
/** Override the redirect URL (default: derived from config) */
|
|
32
|
+
redirectUrl?: string;
|
|
33
|
+
/** Additional headers to set on the redirect response */
|
|
34
|
+
headers?: Record<string, string>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Create a SvelteKit POST handler for the Duct deeplink receiver.
|
|
38
|
+
*
|
|
39
|
+
* Mount this at `src/routes/duct/receive/+server.ts`:
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* // src/routes/duct/receive/+server.ts
|
|
44
|
+
* import { DuctSvelteKitReceiver } from "@duct/sdk/sveltekit";
|
|
45
|
+
* import ductConfig from "$lib/duct.config";
|
|
46
|
+
*
|
|
47
|
+
* export const POST = DuctSvelteKitReceiver({
|
|
48
|
+
* config: ductConfig,
|
|
49
|
+
* secretKey: import.meta.env.DUCT_SECRET_KEY,
|
|
50
|
+
* async onReceive(deeplinkId, state, session) {
|
|
51
|
+
* console.log("Deeplink received:", deeplinkId, state);
|
|
52
|
+
* },
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function DuctSvelteKitReceiver(options: DuctSvelteKitReceiverOptions): (event: SvelteKitRequestEvent) => Promise<Response>;
|