@duct-sdk/sdk 0.2.2 → 0.3.0
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 +45 -0
- package/README.md +9 -1
- package/dist/adapters/next/shell.d.ts +2 -141
- package/dist/adapters/next/shell.js +6 -342
- package/dist/adapters/react/shell.d.ts +23 -4
- package/dist/adapters/react/shell.js +185 -35
- package/dist/cli/agents/api-agent.js +1 -1
- package/dist/cli/agents/pipeline.js +128 -72
- package/dist/cli/generators/shell.js +9 -25
- package/dist/cli/index.js +2 -2
- package/dist/cli/init.js +5 -2
- package/dist/cli/processors/openapi-parser.d.ts +27 -0
- package/dist/cli/processors/openapi-parser.js +309 -0
- package/dist/cli/processors/postprocess.js +1 -1
- package/dist/core/config.d.ts +35 -0
- package/dist/core/config.js +7 -0
- package/dist/core/limits.d.ts +5 -0
- package/dist/core/limits.js +5 -0
- package/dist/core/types.d.ts +44 -2
- package/dist/core/types.js +23 -0
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,51 @@
|
|
|
2
2
|
|
|
3
3
|
Public release notes for the Duct SDK. Versioning follows [semver](https://semver.org).
|
|
4
4
|
|
|
5
|
+
## [0.3.0] — 2026-06-09
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
- **Shell size presets** — `widget.preset` and the `<DuctShell preset>` prop accept `'tall'` (vertical rectangle, default), `'square'` (Copilot-style), or `'large'`. Dimensions resolve from a single source of truth (`SHELL_PRESET_DIMENSIONS`, `resolveShellDimensions()`), replacing ad-hoc `width`/`height` guessing. Raw `width`/`height` still work as an override.
|
|
9
|
+
- **Draggable launcher** — the floating bubble can be dragged anywhere on the page (free drop); position is persisted per shell in `localStorage`. `position` now only sets the starting corner. Disable with `draggable={false}`.
|
|
10
|
+
- **Theme-aware bubble** — new `<DuctShell theme>` prop (`'light' | 'dark' | 'system'`, default `'system'`); the launcher chrome adapts to light/dark instead of a hardcoded dark circle.
|
|
11
|
+
- `DUCT_PANEL_SIZE` postMessage now carries `preset`, so a shell's manifest can drive panel sizing on the host.
|
|
12
|
+
|
|
13
|
+
### Changed
|
|
14
|
+
- Launcher logo now uses the Duct word-mark (`duct-text-no-bg.png`) with the previous `duct-logo.png` kept as a fallback candidate.
|
|
15
|
+
- Generated framework embeds (`duct init` for Next/Remix/Vite) now emit `preset` + `position` and drop the fixed `width`/`height` style override.
|
|
16
|
+
- The Next.js adapter (`@duct-sdk/sdk/next`) now re-exports the React implementation to prevent the two adapters from drifting.
|
|
17
|
+
- `WidgetConfig` Zod schema widened to validate `preset`, `theme`, `placeholder`, `width`, `height`, `logoUrl`, and `customCSS` (previously stripped on `duct push`).
|
|
18
|
+
|
|
19
|
+
## [0.2.3] — 2026-06-08
|
|
20
|
+
|
|
21
|
+
### Added
|
|
22
|
+
- `widget.maxToolIterations` and `widget.maxPlanSteps` on `DuctConfig` — configurable agent loop ceilings (defaults 12 / 8).
|
|
23
|
+
- `core/limits.ts` — exported default and hard-ceiling constants for manifest tooling.
|
|
24
|
+
- `duct init --api-spec <path>` now works end-to-end without requiring an LLM.
|
|
25
|
+
A new deterministic OpenAPI parser (`cli/processors/openapi-parser.ts`) maps
|
|
26
|
+
every path + method in an OpenAPI 3.x or Swagger 2.x spec directly to
|
|
27
|
+
`ActionManifest[]` and `RouteManifest[]`:
|
|
28
|
+
- GET operations → `agentAccessible: false` by default (interactive review applies)
|
|
29
|
+
- POST/PUT/PATCH/DELETE → `sideEffects: true`
|
|
30
|
+
- Operations with `operationId` → `confidence: 1.0`; without → `confidence: 0.95`
|
|
31
|
+
- Sensitivity inferred from path + description keywords
|
|
32
|
+
- `x-duct-sensitivity` extension accepted for manual override
|
|
33
|
+
- Internal Duct / NextAuth routes excluded automatically
|
|
34
|
+
- Path + query parameters extracted; `inputSchema` populated from `requestBody`
|
|
35
|
+
- YAML spec support (`.yaml` / `.yml`) via the new `yaml` dependency (v2.x, zero deps).
|
|
36
|
+
|
|
37
|
+
### Fixed
|
|
38
|
+
- **SDK embed security:** React and Next.js `DuctShell` adapters now validate `event.source === iframe.contentWindow` and reject deeplink URLs that are not same-origin or relative paths.
|
|
39
|
+
- **`pipeline.ts` spec-mode bug:** when `--api-spec` was provided the API agent was
|
|
40
|
+
incorrectly added to the skip list, returning zero actions. The deterministic parser
|
|
41
|
+
now runs directly in the pipeline; the Auth agent still runs in parallel to detect
|
|
42
|
+
auth patterns from project source files.
|
|
43
|
+
- **`pipeline.ts`:** `skipAgents` scope bug — deeplink/review skip flags now work in spec-mode as well as normal mode.
|
|
44
|
+
- **`openapi-parser.ts`:** removed unused parameter.
|
|
45
|
+
|
|
46
|
+
### Changed
|
|
47
|
+
- `pipeline.ts`: spec-mode and normal-mode are now separate branches in Stage 1.
|
|
48
|
+
Both paths feed the same Deeplink + Reviewer agents in Stage 2 and Stage 3.
|
|
49
|
+
|
|
5
50
|
## [0.2.2] — 2026-06-07
|
|
6
51
|
|
|
7
52
|
### Changed
|
package/README.md
CHANGED
|
@@ -55,12 +55,20 @@ export default function Layout({ children }) {
|
|
|
55
55
|
return (
|
|
56
56
|
<>
|
|
57
57
|
{children}
|
|
58
|
-
<DuctShell
|
|
58
|
+
<DuctShell
|
|
59
|
+
shellId={process.env.NEXT_PUBLIC_DUCT_SHELL_ID}
|
|
60
|
+
preset="tall" // "tall" | "square" | "large"
|
|
61
|
+
position="bottom-right" // starting corner; the bubble is draggable
|
|
62
|
+
theme="system" // "system" | "light" | "dark"
|
|
63
|
+
/>
|
|
59
64
|
</>
|
|
60
65
|
);
|
|
61
66
|
}
|
|
62
67
|
```
|
|
63
68
|
|
|
69
|
+
The launcher bubble is **draggable** (drop it anywhere — position persists per shell) and **theme-aware**. Use `preset`
|
|
70
|
+
to size the panel, or pass `panelWidth`/`panelHeight` for a custom size. Set `draggable={false}` to pin the bubble.
|
|
71
|
+
|
|
64
72
|
---
|
|
65
73
|
|
|
66
74
|
## CLI reference
|
|
@@ -1,141 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
route?: string;
|
|
4
|
-
/** Only `type` is required — use whatever key names your data model has */
|
|
5
|
-
entity?: {
|
|
6
|
-
type: string;
|
|
7
|
-
[key: string]: unknown;
|
|
8
|
-
};
|
|
9
|
-
errorState?: {
|
|
10
|
-
code?: string;
|
|
11
|
-
message?: string;
|
|
12
|
-
field?: string;
|
|
13
|
-
};
|
|
14
|
-
/** Only `type` is required — use whatever key your model uses for identifiers */
|
|
15
|
-
selection?: {
|
|
16
|
-
type: string;
|
|
17
|
-
[key: string]: unknown;
|
|
18
|
-
};
|
|
19
|
-
formState?: Record<string, unknown>;
|
|
20
|
-
meta?: Record<string, unknown>;
|
|
21
|
-
}
|
|
22
|
-
export interface DuctShellProps {
|
|
23
|
-
/** Your Duct shell ID (e.g., "shell_acme_prod") */
|
|
24
|
-
shellId: string;
|
|
25
|
-
/**
|
|
26
|
-
* User context to pass to the shell as public metadata.
|
|
27
|
-
* Do NOT include sensitive data — this is not authenticated.
|
|
28
|
-
*/
|
|
29
|
-
userContext?: Record<string, string | number | boolean | null>;
|
|
30
|
-
/**
|
|
31
|
-
* Called when the Duct shell requests an auth token.
|
|
32
|
-
* Should call your /api/duct/token endpoint and return the JWT string.
|
|
33
|
-
* Return empty string for unauthenticated/guest users.
|
|
34
|
-
*/
|
|
35
|
-
onTokenRequest?: (shellId: string) => Promise<string>;
|
|
36
|
-
/** Called when the shell requests navigation to a URL (deeplink handoff) */
|
|
37
|
-
onNavigate?: (url: string) => void;
|
|
38
|
-
/**
|
|
39
|
-
* Page Context Protocol — called when the shell needs to understand what
|
|
40
|
-
* the user is currently viewing (triggered by deictic references like "this",
|
|
41
|
-
* "that order", "the error on screen"). Return structured data about the
|
|
42
|
-
* current page state so the shell can resolve on-screen references without
|
|
43
|
-
* screenshots or computer-use techniques.
|
|
44
|
-
*
|
|
45
|
-
* Alternatively, use the `useDuctPageContext` hook in page-level components
|
|
46
|
-
* when this component is in a layout that doesn't have access to page state.
|
|
47
|
-
*/
|
|
48
|
-
onPageContext?: () => DuctPageContext | Promise<DuctPageContext>;
|
|
49
|
-
/** Controlled open state */
|
|
50
|
-
open?: boolean;
|
|
51
|
-
/** Called whenever shell open state changes */
|
|
52
|
-
onOpenChange?: (open: boolean) => void;
|
|
53
|
-
/** Whether the shell is initially open (default: false) */
|
|
54
|
-
defaultOpen?: boolean;
|
|
55
|
-
/**
|
|
56
|
-
* Duct shell host URL.
|
|
57
|
-
* For local dev: http://localhost:3000
|
|
58
|
-
* For production: your Duct shell host URL (local default: http://localhost:3000)
|
|
59
|
-
*/
|
|
60
|
-
shellHost?: string;
|
|
61
|
-
/**
|
|
62
|
-
* Position of the floating launcher.
|
|
63
|
-
* @default "bottom-right"
|
|
64
|
-
*/
|
|
65
|
-
position?: "bottom-right" | "bottom-left";
|
|
66
|
-
/**
|
|
67
|
-
* Panel width in pixels.
|
|
68
|
-
* @default 420
|
|
69
|
-
*/
|
|
70
|
-
panelWidth?: number;
|
|
71
|
-
/**
|
|
72
|
-
* Panel height in pixels.
|
|
73
|
-
* @default 640
|
|
74
|
-
*/
|
|
75
|
-
panelHeight?: number;
|
|
76
|
-
/** Override container styles (wrapper div). */
|
|
77
|
-
style?: CSSProperties;
|
|
78
|
-
className?: string;
|
|
79
|
-
/**
|
|
80
|
-
* Optional launcher icon source.
|
|
81
|
-
* When omitted, the SDK tries cloud-hosted icon first, then local /assets/logo.png.
|
|
82
|
-
*/
|
|
83
|
-
launcherIconSrc?: string;
|
|
84
|
-
/**
|
|
85
|
-
* When true, the launcher is hidden until the shell status endpoint confirms
|
|
86
|
-
* the shell is live. Prevents showing the button before `duct push` has been run.
|
|
87
|
-
* @default false
|
|
88
|
-
*/
|
|
89
|
-
readyCheck?: boolean;
|
|
90
|
-
}
|
|
91
|
-
export interface ShellStatusResult {
|
|
92
|
-
status: 'live' | 'not_ready' | 'suspended' | 'loading';
|
|
93
|
-
manifestReady: boolean;
|
|
94
|
-
widget?: {
|
|
95
|
-
welcomeMessage?: string;
|
|
96
|
-
placeholder?: string;
|
|
97
|
-
theme?: string;
|
|
98
|
-
primaryColor?: string;
|
|
99
|
-
width?: number | string;
|
|
100
|
-
height?: number | string;
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Hook that polls the shell status endpoint once on mount.
|
|
105
|
-
* Use when you need to conditionally render UI based on shell liveness.
|
|
106
|
-
*/
|
|
107
|
-
export declare function useShellStatus(shellId: string, shellHost?: string): ShellStatusResult;
|
|
108
|
-
/**
|
|
109
|
-
* DuctShell — a self-contained floating launcher button + chat panel.
|
|
110
|
-
*
|
|
111
|
-
* Drop this anywhere in your layout. It renders:
|
|
112
|
-
* - A fixed bubble button (bottom-right by default)
|
|
113
|
-
* - A full-height chat panel that opens when clicked
|
|
114
|
-
*
|
|
115
|
-
* @example
|
|
116
|
-
* ```tsx
|
|
117
|
-
* // app/layout.tsx
|
|
118
|
-
* <DuctShell
|
|
119
|
-
* shellId={process.env.DUCT_SHELL_ID!}
|
|
120
|
-
* shellHost={process.env.DUCT_SHELL_HOST}
|
|
121
|
-
* onTokenRequest={async () => {
|
|
122
|
-
* const res = await fetch("/api/duct/token", { method: "POST" });
|
|
123
|
-
* const { token } = await res.json();
|
|
124
|
-
* return token;
|
|
125
|
-
* }} />
|
|
126
|
-
* ```
|
|
127
|
-
*/
|
|
128
|
-
export declare function DuctShell({ shellId, userContext, onTokenRequest, onNavigate, onPageContext, open, onOpenChange, defaultOpen, shellHost, position, panelWidth, panelHeight, style, className, launcherIconSrc, readyCheck, }: DuctShellProps): React.ReactElement | null;
|
|
129
|
-
export interface DuctShellButtonProps {
|
|
130
|
-
shellId: string;
|
|
131
|
-
children?: React.ReactNode;
|
|
132
|
-
onToggle?: (open: boolean) => void;
|
|
133
|
-
className?: string;
|
|
134
|
-
style?: CSSProperties;
|
|
135
|
-
open?: boolean;
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Headless launch button — use when you want a custom trigger.
|
|
139
|
-
* Pair with DuctShell in controlled mode.
|
|
140
|
-
*/
|
|
141
|
-
export declare function DuctShellButton({ children, onToggle, className, style, open: controlledOpen, }: DuctShellButtonProps): React.ReactElement | null;
|
|
1
|
+
export { DuctShell, DuctShellButton, useShellStatus, } from "../react/shell.js";
|
|
2
|
+
export type { DuctShellProps, DuctShellButtonProps, DuctPageContext, ShellStatusResult, } from "../react/shell.js";
|
|
@@ -1,345 +1,9 @@
|
|
|
1
1
|
// ============================================================
|
|
2
|
-
// @duct
|
|
3
|
-
//
|
|
2
|
+
// @duct/sdk/next — DuctShell
|
|
3
|
+
// The Next.js adapter shares the exact same client implementation as the
|
|
4
|
+
// React adapter (floating launcher + iframe panel + postMessage bridge,
|
|
5
|
+
// draggable bubble, size presets, theme-aware chrome). Re-export to avoid
|
|
6
|
+
// drift between the two adapters.
|
|
4
7
|
// ============================================================
|
|
5
8
|
"use client";
|
|
6
|
-
|
|
7
|
-
import React, { useEffect, useRef, useCallback, useState, } from "react";
|
|
8
|
-
/**
|
|
9
|
-
* Hook that polls the shell status endpoint once on mount.
|
|
10
|
-
* Use when you need to conditionally render UI based on shell liveness.
|
|
11
|
-
*/
|
|
12
|
-
export function useShellStatus(shellId, shellHost) {
|
|
13
|
-
const [result, setResult] = React.useState({ status: 'loading', manifestReady: false });
|
|
14
|
-
React.useEffect(() => {
|
|
15
|
-
if (!shellId)
|
|
16
|
-
return;
|
|
17
|
-
const host = (shellHost ?? DUCT_SHELL_HOST_DEFAULT).replace(/\/$/, '');
|
|
18
|
-
let cancelled = false;
|
|
19
|
-
fetch(`${host}/api/shell-status/${shellId}`)
|
|
20
|
-
.then((r) => r.json())
|
|
21
|
-
.then((data) => {
|
|
22
|
-
if (!cancelled) {
|
|
23
|
-
setResult({
|
|
24
|
-
status: (data.status ?? 'live'),
|
|
25
|
-
manifestReady: data.manifestReady ?? true,
|
|
26
|
-
widget: data.widget,
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
})
|
|
30
|
-
.catch(() => {
|
|
31
|
-
if (!cancelled)
|
|
32
|
-
setResult({ status: 'live', manifestReady: true });
|
|
33
|
-
});
|
|
34
|
-
return () => { cancelled = true; };
|
|
35
|
-
}, [shellId, shellHost]);
|
|
36
|
-
return result;
|
|
37
|
-
}
|
|
38
|
-
// ----------------------------------------------------------------
|
|
39
|
-
// DuctShell — floating launcher + panel
|
|
40
|
-
// ----------------------------------------------------------------
|
|
41
|
-
const DUCT_SHELL_HOST_DEFAULT = (typeof process !== "undefined" && process.env?.["DUCT_SHELL_HOST"]) ||
|
|
42
|
-
"http://localhost:3000";
|
|
43
|
-
const DUCT_LAUNCHER_ICON_LOCAL_FALLBACK = "/assets/logo.png";
|
|
44
|
-
/**
|
|
45
|
-
* DuctShell — a self-contained floating launcher button + chat panel.
|
|
46
|
-
*
|
|
47
|
-
* Drop this anywhere in your layout. It renders:
|
|
48
|
-
* - A fixed bubble button (bottom-right by default)
|
|
49
|
-
* - A full-height chat panel that opens when clicked
|
|
50
|
-
*
|
|
51
|
-
* @example
|
|
52
|
-
* ```tsx
|
|
53
|
-
* // app/layout.tsx
|
|
54
|
-
* <DuctShell
|
|
55
|
-
* shellId={process.env.DUCT_SHELL_ID!}
|
|
56
|
-
* shellHost={process.env.DUCT_SHELL_HOST}
|
|
57
|
-
* onTokenRequest={async () => {
|
|
58
|
-
* const res = await fetch("/api/duct/token", { method: "POST" });
|
|
59
|
-
* const { token } = await res.json();
|
|
60
|
-
* return token;
|
|
61
|
-
* }} />
|
|
62
|
-
* ```
|
|
63
|
-
*/
|
|
64
|
-
export function DuctShell({ shellId, userContext, onTokenRequest, onNavigate, onPageContext, open, onOpenChange, defaultOpen = false, shellHost = DUCT_SHELL_HOST_DEFAULT, position = "bottom-right", panelWidth = 420, panelHeight = 640, style, className, launcherIconSrc, readyCheck = false, }) {
|
|
65
|
-
const iframeRef = useRef(null);
|
|
66
|
-
const [isReady, setIsReady] = useState(false);
|
|
67
|
-
const [isOpen, setIsOpen] = useState(defaultOpen);
|
|
68
|
-
const [isAnimating, setIsAnimating] = useState(false);
|
|
69
|
-
const [iconCandidateIndex, setIconCandidateIndex] = useState(0);
|
|
70
|
-
const [iconLoadFailed, setIconLoadFailed] = useState(false);
|
|
71
|
-
// Dynamic panel size — overridden by DUCT_PANEL_SIZE message from shell (manifest widget config)
|
|
72
|
-
const [dynamicWidth, setDynamicWidth] = useState(undefined);
|
|
73
|
-
const [dynamicHeight, setDynamicHeight] = useState(undefined);
|
|
74
|
-
// Shell liveness — only checked when readyCheck=true
|
|
75
|
-
const [shellLive, setShellLive] = useState(!readyCheck);
|
|
76
|
-
const pendingTokenRequests = useRef(new Map());
|
|
77
|
-
// When readyCheck is enabled, poll status once on mount
|
|
78
|
-
useEffect(() => {
|
|
79
|
-
if (!readyCheck || !shellId)
|
|
80
|
-
return;
|
|
81
|
-
const host = shellHost.replace(/\/$/, '');
|
|
82
|
-
fetch(`${host}/api/shell-status/${shellId}`)
|
|
83
|
-
.then((r) => r.json())
|
|
84
|
-
.then((data) => {
|
|
85
|
-
if (data.status === 'live')
|
|
86
|
-
setShellLive(true);
|
|
87
|
-
})
|
|
88
|
-
.catch(() => setShellLive(true)); // fail open
|
|
89
|
-
}, [readyCheck, shellId, shellHost]);
|
|
90
|
-
const controlled = open !== undefined;
|
|
91
|
-
const panelOpen = controlled ? open : isOpen;
|
|
92
|
-
const shellUrl = buildShellUrl(shellHost, shellId, userContext);
|
|
93
|
-
const shellOrigin = (() => {
|
|
94
|
-
try {
|
|
95
|
-
return new URL(shellHost).origin;
|
|
96
|
-
}
|
|
97
|
-
catch {
|
|
98
|
-
return shellHost;
|
|
99
|
-
}
|
|
100
|
-
})();
|
|
101
|
-
const iconCandidates = launcherIconSrc
|
|
102
|
-
? [launcherIconSrc]
|
|
103
|
-
: [`${shellOrigin}/duct-logo.png`, DUCT_LAUNCHER_ICON_LOCAL_FALLBACK];
|
|
104
|
-
const currentIconSrc = iconCandidates[Math.min(iconCandidateIndex, iconCandidates.length - 1)];
|
|
105
|
-
// Send a message to the iframe
|
|
106
|
-
const postToShell = useCallback((message) => {
|
|
107
|
-
iframeRef.current?.contentWindow?.postMessage(message, shellOrigin);
|
|
108
|
-
}, [shellOrigin]);
|
|
109
|
-
// Toggle open state
|
|
110
|
-
const setOpen = useCallback((next) => {
|
|
111
|
-
if (!controlled)
|
|
112
|
-
setIsOpen(next);
|
|
113
|
-
onOpenChange?.(next);
|
|
114
|
-
}, [controlled, onOpenChange]);
|
|
115
|
-
// Animate open
|
|
116
|
-
const openPanel = useCallback(() => {
|
|
117
|
-
setIsAnimating(true);
|
|
118
|
-
setOpen(true);
|
|
119
|
-
requestAnimationFrame(() => requestAnimationFrame(() => setIsAnimating(false)));
|
|
120
|
-
}, [setOpen]);
|
|
121
|
-
// Animate close
|
|
122
|
-
const closePanel = useCallback(() => {
|
|
123
|
-
setIsAnimating(true);
|
|
124
|
-
setTimeout(() => {
|
|
125
|
-
setOpen(false);
|
|
126
|
-
setIsAnimating(false);
|
|
127
|
-
}, 220);
|
|
128
|
-
}, [setOpen]);
|
|
129
|
-
// Handle inbound messages from the iframe
|
|
130
|
-
const handleMessage = useCallback(async (event) => {
|
|
131
|
-
if (event.origin !== shellOrigin)
|
|
132
|
-
return;
|
|
133
|
-
if (typeof event.data !== "object" || !event.data?.type)
|
|
134
|
-
return;
|
|
135
|
-
const msg = event.data;
|
|
136
|
-
switch (msg.type) {
|
|
137
|
-
case "DUCT_READY": {
|
|
138
|
-
setIsReady(true);
|
|
139
|
-
if (userContext && Object.keys(userContext).length > 0) {
|
|
140
|
-
postToShell({ type: "DUCT_USER_CONTEXT", context: userContext });
|
|
141
|
-
}
|
|
142
|
-
break;
|
|
143
|
-
}
|
|
144
|
-
case "DUCT_REQUEST_TOKEN": {
|
|
145
|
-
const { requestId, shellId: reqShellId } = msg;
|
|
146
|
-
if (!onTokenRequest) {
|
|
147
|
-
postToShell({ type: "DUCT_TOKEN_RESPONSE", requestId, token: "" });
|
|
148
|
-
break;
|
|
149
|
-
}
|
|
150
|
-
try {
|
|
151
|
-
const token = await onTokenRequest(reqShellId);
|
|
152
|
-
postToShell({ type: "DUCT_TOKEN_RESPONSE", requestId, token: token ?? "" });
|
|
153
|
-
}
|
|
154
|
-
catch {
|
|
155
|
-
postToShell({ type: "DUCT_TOKEN_RESPONSE", requestId, token: "" });
|
|
156
|
-
}
|
|
157
|
-
pendingTokenRequests.current.delete(requestId);
|
|
158
|
-
break;
|
|
159
|
-
}
|
|
160
|
-
case "DUCT_REQUEST_CONTEXT": {
|
|
161
|
-
// Page Context Protocol — shell is asking what the user is currently viewing.
|
|
162
|
-
const { requestId } = msg;
|
|
163
|
-
if (!onPageContext) {
|
|
164
|
-
postToShell({ type: "DUCT_PAGE_CONTEXT", requestId, context: null });
|
|
165
|
-
break;
|
|
166
|
-
}
|
|
167
|
-
try {
|
|
168
|
-
const context = await onPageContext();
|
|
169
|
-
postToShell({ type: "DUCT_PAGE_CONTEXT", requestId, context });
|
|
170
|
-
}
|
|
171
|
-
catch {
|
|
172
|
-
postToShell({ type: "DUCT_PAGE_CONTEXT", requestId, context: null });
|
|
173
|
-
}
|
|
174
|
-
break;
|
|
175
|
-
}
|
|
176
|
-
case "DUCT_DEEPLINK_NAVIGATE": {
|
|
177
|
-
const { url } = msg;
|
|
178
|
-
postToShell({ type: "DUCT_NAVIGATE_CONFIRMED", url });
|
|
179
|
-
closePanel();
|
|
180
|
-
// Delay navigation until after the close animation (220ms) so the panel
|
|
181
|
-
// slides away before the page changes — avoids a jarring simultaneous transition.
|
|
182
|
-
setTimeout(() => {
|
|
183
|
-
if (onNavigate) {
|
|
184
|
-
onNavigate(url);
|
|
185
|
-
}
|
|
186
|
-
else {
|
|
187
|
-
window.location.href = url;
|
|
188
|
-
}
|
|
189
|
-
}, 230);
|
|
190
|
-
break;
|
|
191
|
-
}
|
|
192
|
-
case "DUCT_PANEL_SIZE": {
|
|
193
|
-
if (msg.width !== undefined)
|
|
194
|
-
setDynamicWidth(msg.width);
|
|
195
|
-
if (msg.height !== undefined)
|
|
196
|
-
setDynamicHeight(msg.height);
|
|
197
|
-
break;
|
|
198
|
-
}
|
|
199
|
-
case "DUCT_OPEN": {
|
|
200
|
-
openPanel();
|
|
201
|
-
break;
|
|
202
|
-
}
|
|
203
|
-
case "DUCT_CLOSE": {
|
|
204
|
-
closePanel();
|
|
205
|
-
break;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}, [shellOrigin, userContext, onTokenRequest, onPageContext, postToShell, onNavigate, openPanel, closePanel]);
|
|
209
|
-
useEffect(() => {
|
|
210
|
-
window.addEventListener("message", handleMessage);
|
|
211
|
-
return () => window.removeEventListener("message", handleMessage);
|
|
212
|
-
}, [handleMessage]);
|
|
213
|
-
useEffect(() => {
|
|
214
|
-
if (!isReady || !userContext)
|
|
215
|
-
return;
|
|
216
|
-
postToShell({ type: "DUCT_USER_CONTEXT", context: userContext });
|
|
217
|
-
}, [isReady, userContext, postToShell]);
|
|
218
|
-
// Sync controlled open
|
|
219
|
-
useEffect(() => {
|
|
220
|
-
if (open === undefined)
|
|
221
|
-
return;
|
|
222
|
-
if (open)
|
|
223
|
-
openPanel();
|
|
224
|
-
else
|
|
225
|
-
closePanel();
|
|
226
|
-
}, [open]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
227
|
-
useEffect(() => {
|
|
228
|
-
setIconCandidateIndex(0);
|
|
229
|
-
setIconLoadFailed(false);
|
|
230
|
-
}, [launcherIconSrc, shellOrigin]);
|
|
231
|
-
const handleIconError = useCallback(() => {
|
|
232
|
-
if (iconCandidateIndex < iconCandidates.length - 1) {
|
|
233
|
-
setIconCandidateIndex((prev) => prev + 1);
|
|
234
|
-
return;
|
|
235
|
-
}
|
|
236
|
-
setIconLoadFailed(true);
|
|
237
|
-
}, [iconCandidateIndex, iconCandidates.length]);
|
|
238
|
-
if (!shellId) {
|
|
239
|
-
console.error("[DuctShell] shellId prop is required");
|
|
240
|
-
return null;
|
|
241
|
-
}
|
|
242
|
-
// Hide entirely until readyCheck confirms the shell is live
|
|
243
|
-
if (!shellLive)
|
|
244
|
-
return null;
|
|
245
|
-
const isRight = position === "bottom-right";
|
|
246
|
-
const panelVisible = panelOpen || isAnimating;
|
|
247
|
-
// Responsive: full-screen on small viewports.
|
|
248
|
-
// Dynamic size overrides prop-level size when the shell sends DUCT_PANEL_SIZE.
|
|
249
|
-
const resolvedWidth = dynamicWidth ?? panelWidth;
|
|
250
|
-
const resolvedHeight = dynamicHeight ?? panelHeight;
|
|
251
|
-
const widthVal = typeof resolvedWidth === "number" ? `${resolvedWidth}px` : resolvedWidth;
|
|
252
|
-
const heightVal = typeof resolvedHeight === "number" ? `${resolvedHeight}px` : resolvedHeight;
|
|
253
|
-
const panelStyle = {
|
|
254
|
-
position: "fixed",
|
|
255
|
-
bottom: "80px",
|
|
256
|
-
[isRight ? "right" : "left"]: "16px",
|
|
257
|
-
width: `min(${widthVal}, calc(100vw - 32px))`,
|
|
258
|
-
height: `min(${heightVal}, calc(100dvh - 96px))`,
|
|
259
|
-
zIndex: 9998,
|
|
260
|
-
borderRadius: "16px",
|
|
261
|
-
overflow: "hidden",
|
|
262
|
-
boxShadow: "0 24px 64px rgba(0,0,0,0.18), 0 8px 24px rgba(0,0,0,0.10)",
|
|
263
|
-
// Slide-in / slide-out animation
|
|
264
|
-
// Opens: slides up from below (translateY(100%) → translateY(0))
|
|
265
|
-
// Closes: slides back down
|
|
266
|
-
opacity: panelOpen && !isAnimating ? 1 : 0,
|
|
267
|
-
transform: panelOpen && !isAnimating
|
|
268
|
-
? "translateY(0)"
|
|
269
|
-
: "translateY(100%)",
|
|
270
|
-
transition: "opacity 220ms cubic-bezier(0.16,1,0.3,1), transform 0.25s cubic-bezier(0.16, 1, 0.3, 1)",
|
|
271
|
-
pointerEvents: panelOpen ? "auto" : "none",
|
|
272
|
-
visibility: panelVisible ? "visible" : "hidden",
|
|
273
|
-
...style,
|
|
274
|
-
};
|
|
275
|
-
const buttonStyle = {
|
|
276
|
-
position: "fixed",
|
|
277
|
-
bottom: "16px",
|
|
278
|
-
[isRight ? "right" : "left"]: "16px",
|
|
279
|
-
zIndex: 9999,
|
|
280
|
-
width: "56px",
|
|
281
|
-
height: "56px",
|
|
282
|
-
borderRadius: "50%",
|
|
283
|
-
border: "none",
|
|
284
|
-
cursor: "pointer",
|
|
285
|
-
display: "flex",
|
|
286
|
-
alignItems: "center",
|
|
287
|
-
justifyContent: "center",
|
|
288
|
-
backgroundColor: "#0C0B09",
|
|
289
|
-
boxShadow: "0 4px 16px rgba(0,0,0,0.40), 0 2px 8px rgba(0,0,0,0.20)",
|
|
290
|
-
padding: 0,
|
|
291
|
-
transition: "transform 150ms ease, box-shadow 150ms ease",
|
|
292
|
-
};
|
|
293
|
-
return (_jsxs(_Fragment, { children: [!panelOpen && (_jsx("button", { type: "button", style: buttonStyle, onClick: openPanel, "aria-label": "Open Duct Shell", className: className, onMouseEnter: (e) => {
|
|
294
|
-
e.currentTarget.style.transform = "scale(1.07)";
|
|
295
|
-
e.currentTarget.style.boxShadow =
|
|
296
|
-
"0 6px 20px rgba(0,0,0,0.50), 0 2px 8px rgba(0,0,0,0.20)";
|
|
297
|
-
}, onMouseLeave: (e) => {
|
|
298
|
-
e.currentTarget.style.transform = "scale(1)";
|
|
299
|
-
e.currentTarget.style.boxShadow =
|
|
300
|
-
"0 4px 16px rgba(0,0,0,0.40), 0 2px 8px rgba(0,0,0,0.20)";
|
|
301
|
-
}, 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: React.createElement("iframe", {
|
|
302
|
-
ref: iframeRef,
|
|
303
|
-
src: shellUrl,
|
|
304
|
-
title: "Duct Shell",
|
|
305
|
-
sandbox: "allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox",
|
|
306
|
-
allow: "clipboard-write",
|
|
307
|
-
referrerPolicy: "no-referrer-when-downgrade",
|
|
308
|
-
style: {
|
|
309
|
-
width: "100%",
|
|
310
|
-
height: "100%",
|
|
311
|
-
border: "none",
|
|
312
|
-
display: "block",
|
|
313
|
-
},
|
|
314
|
-
}) })] }));
|
|
315
|
-
}
|
|
316
|
-
/**
|
|
317
|
-
* Headless launch button — use when you want a custom trigger.
|
|
318
|
-
* Pair with DuctShell in controlled mode.
|
|
319
|
-
*/
|
|
320
|
-
export function DuctShellButton({ children, onToggle, className, style, open: controlledOpen, }) {
|
|
321
|
-
const [internalOpen, setInternalOpen] = useState(false);
|
|
322
|
-
const isOpen = controlledOpen !== undefined ? controlledOpen : internalOpen;
|
|
323
|
-
const handleClick = useCallback(() => {
|
|
324
|
-
const next = !isOpen;
|
|
325
|
-
if (controlledOpen === undefined)
|
|
326
|
-
setInternalOpen(next);
|
|
327
|
-
onToggle?.(next);
|
|
328
|
-
}, [isOpen, controlledOpen, onToggle]);
|
|
329
|
-
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") }));
|
|
330
|
-
}
|
|
331
|
-
// ----------------------------------------------------------------
|
|
332
|
-
// Helpers
|
|
333
|
-
// ----------------------------------------------------------------
|
|
334
|
-
function buildShellUrl(shellHost, shellId, userContext) {
|
|
335
|
-
const base = shellHost.replace(/\/$/, "");
|
|
336
|
-
const url = new URL(`${base}/${shellId}`);
|
|
337
|
-
if (userContext) {
|
|
338
|
-
for (const [key, value] of Object.entries(userContext)) {
|
|
339
|
-
if (value !== null && value !== undefined) {
|
|
340
|
-
url.searchParams.set(`ctx_${key}`, String(value));
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
return url.toString();
|
|
345
|
-
}
|
|
9
|
+
export { DuctShell, DuctShellButton, useShellStatus, } from "../react/shell.js";
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import React, { type CSSProperties } from "react";
|
|
2
|
+
import { type ShellPreset } from "../../core/types.js";
|
|
2
3
|
export interface DuctPageContext {
|
|
3
4
|
route?: string;
|
|
4
5
|
/** Only `type` is required — use whatever key names your data model has */
|
|
@@ -59,17 +60,35 @@ export interface DuctShellProps {
|
|
|
59
60
|
*/
|
|
60
61
|
shellHost?: string;
|
|
61
62
|
/**
|
|
62
|
-
*
|
|
63
|
+
* Initial position of the floating launcher. Users can drag the bubble
|
|
64
|
+
* anywhere; this only sets the starting corner.
|
|
63
65
|
* @default "bottom-right"
|
|
64
66
|
*/
|
|
65
67
|
position?: "bottom-right" | "bottom-left";
|
|
66
68
|
/**
|
|
67
|
-
*
|
|
69
|
+
* Size/shape preset for the panel. Preferred over panelWidth/panelHeight.
|
|
70
|
+
* - 'tall' — vertical rectangle (default)
|
|
71
|
+
* - 'square' — Copilot-style squarish panel
|
|
72
|
+
* - 'large' — spacious panel
|
|
73
|
+
*/
|
|
74
|
+
preset?: ShellPreset;
|
|
75
|
+
/**
|
|
76
|
+
* Theme for the launcher bubble chrome.
|
|
77
|
+
* @default "system"
|
|
78
|
+
*/
|
|
79
|
+
theme?: "light" | "dark" | "system";
|
|
80
|
+
/**
|
|
81
|
+
* Allow the user to drag the bubble anywhere on screen (persisted per shell).
|
|
82
|
+
* @default true
|
|
83
|
+
*/
|
|
84
|
+
draggable?: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Panel width in pixels. Ignored when `preset` is set.
|
|
68
87
|
* @default 420
|
|
69
88
|
*/
|
|
70
89
|
panelWidth?: number;
|
|
71
90
|
/**
|
|
72
|
-
* Panel height in pixels.
|
|
91
|
+
* Panel height in pixels. Ignored when `preset` is set.
|
|
73
92
|
* @default 640
|
|
74
93
|
*/
|
|
75
94
|
panelHeight?: number;
|
|
@@ -132,7 +151,7 @@ export declare function useShellStatus(shellId: string, shellHost?: string): She
|
|
|
132
151
|
* />
|
|
133
152
|
* ```
|
|
134
153
|
*/
|
|
135
|
-
export declare function DuctShell({ shellId, userContext, onTokenRequest, onNavigate, onPageContext, open, onOpenChange, defaultOpen, shellHost, position, panelWidth, panelHeight, style, className, launcherIconSrc, readyCheck, }: DuctShellProps): React.ReactElement | null;
|
|
154
|
+
export declare function DuctShell({ shellId, userContext, onTokenRequest, onNavigate, onPageContext, open, onOpenChange, defaultOpen, shellHost, position, preset, theme, draggable, panelWidth, panelHeight, style, className, launcherIconSrc, readyCheck, }: DuctShellProps): React.ReactElement | null;
|
|
136
155
|
export interface DuctShellButtonProps {
|
|
137
156
|
shellId: string;
|
|
138
157
|
children?: React.ReactNode;
|