@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,672 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// @duct/sdk — Shell Component Generator
|
|
3
|
+
// Generates framework-specific shell embed components
|
|
4
|
+
// ============================================================
|
|
5
|
+
// ----------------------------------------------------------------
|
|
6
|
+
// Next.js / React shell component
|
|
7
|
+
// ----------------------------------------------------------------
|
|
8
|
+
function generateReactShellComponent(config) {
|
|
9
|
+
const position = config.widget?.position ?? "bottom-right";
|
|
10
|
+
const welcomeMessage = config.widget?.welcomeMessage ?? `Hi! I'm ${config.product.name}'s AI assistant.`;
|
|
11
|
+
const width = config.widget?.width ?? 380;
|
|
12
|
+
const height = config.widget?.height ?? 620;
|
|
13
|
+
const docsBase = config.product?.baseUrl
|
|
14
|
+
? `https://docs.${new URL(config.product.baseUrl).hostname.split('.').slice(-2).join('.')}`
|
|
15
|
+
: 'https://docs.duct.sh';
|
|
16
|
+
return `/**
|
|
17
|
+
* Duct Shell Widget — auto-generated by \`npx duct init\`
|
|
18
|
+
*
|
|
19
|
+
* Mount this in your root layout. That's it.
|
|
20
|
+
* The shell is hosted by Duct. This component is a thin bridge.
|
|
21
|
+
* It renders the self-contained launcher, so do not add a second trigger.
|
|
22
|
+
*
|
|
23
|
+
* To customize: ${docsBase}/shell/customization
|
|
24
|
+
* To update: npx duct init --update
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
"use client"; // Required for Next.js App Router
|
|
28
|
+
|
|
29
|
+
import { DuctShell } from "@shoryasethia/duct-sdk/react";
|
|
30
|
+
|
|
31
|
+
export function DuctShellEmbed() {
|
|
32
|
+
const shellHost = process.env.NEXT_PUBLIC_DUCT_SHELL_HOST ?? "http://localhost:3000";
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<DuctShell
|
|
36
|
+
shellId={process.env.NEXT_PUBLIC_DUCT_SHELL_ID!}
|
|
37
|
+
shellHost={shellHost}
|
|
38
|
+
position="${position}"
|
|
39
|
+
// Optional: provide user context (not sensitive — this is metadata for the shell UI)
|
|
40
|
+
// userContext={{ theme: "light", locale: "en-US" }}
|
|
41
|
+
onTokenRequest={async (shellId) => {
|
|
42
|
+
// Called when the Duct shell needs to authenticate the current user.
|
|
43
|
+
// Your token endpoint (generated at /api/duct/token) returns a short-lived JWT.
|
|
44
|
+
const res = await fetch("/api/duct/token", {
|
|
45
|
+
method: "POST",
|
|
46
|
+
headers: { "Content-Type": "application/json" },
|
|
47
|
+
body: JSON.stringify({ shellId }),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
if (!res.ok) {
|
|
51
|
+
// Return empty string for unauthenticated/guest users
|
|
52
|
+
return "";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const data = await res.json() as { token?: string };
|
|
56
|
+
return data.token ?? "";
|
|
57
|
+
}}
|
|
58
|
+
style={{
|
|
59
|
+
width: typeof ${width} === "number" ? \`\${${width}}px\` : "${width}",
|
|
60
|
+
height: typeof ${height} === "number" ? \`\${${height}}px\` : "${height}",
|
|
61
|
+
maxWidth: "calc(100vw - 32px)",
|
|
62
|
+
maxHeight: "calc(100vh - 110px)",
|
|
63
|
+
zIndex: 9999,
|
|
64
|
+
}}
|
|
65
|
+
/>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Usage — add to your root layout:
|
|
70
|
+
//
|
|
71
|
+
// import { DuctShellEmbed } from "@/components/DuctShell";
|
|
72
|
+
//
|
|
73
|
+
// export default function RootLayout({ children }) {
|
|
74
|
+
// return (
|
|
75
|
+
// <html>
|
|
76
|
+
// <body>
|
|
77
|
+
// {children}
|
|
78
|
+
// <DuctShellEmbed />
|
|
79
|
+
// </body>
|
|
80
|
+
// </html>
|
|
81
|
+
// );
|
|
82
|
+
// }
|
|
83
|
+
//
|
|
84
|
+
// Shell ID: ${config.shellId}
|
|
85
|
+
// Welcome message: "${welcomeMessage}"
|
|
86
|
+
`;
|
|
87
|
+
}
|
|
88
|
+
// ----------------------------------------------------------------
|
|
89
|
+
// Next.js App Router layout integration snippet
|
|
90
|
+
// ----------------------------------------------------------------
|
|
91
|
+
function generateNextLayoutSnippet(config) {
|
|
92
|
+
return `// Add to your app/layout.tsx:
|
|
93
|
+
// Generated by @duct/sdk
|
|
94
|
+
|
|
95
|
+
import { DuctShellEmbed } from "@/components/DuctShell";
|
|
96
|
+
|
|
97
|
+
// In your root layout component:
|
|
98
|
+
export default function RootLayout({
|
|
99
|
+
children,
|
|
100
|
+
}: {
|
|
101
|
+
children: React.ReactNode;
|
|
102
|
+
}) {
|
|
103
|
+
return (
|
|
104
|
+
<html lang="en">
|
|
105
|
+
<body>
|
|
106
|
+
{children}
|
|
107
|
+
{/* Duct shell — ${config.product.name} AI assistant */}
|
|
108
|
+
<DuctShellEmbed />
|
|
109
|
+
</body>
|
|
110
|
+
</html>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
`;
|
|
114
|
+
}
|
|
115
|
+
// ----------------------------------------------------------------
|
|
116
|
+
// Remix shell component
|
|
117
|
+
// ----------------------------------------------------------------
|
|
118
|
+
function generateRemixShellComponent(config) {
|
|
119
|
+
const position = config.widget?.position ?? "bottom-right";
|
|
120
|
+
const docsBase = config.product?.baseUrl
|
|
121
|
+
? `https://docs.${new URL(config.product.baseUrl).hostname.split('.').slice(-2).join('.')}`
|
|
122
|
+
: 'https://docs.duct.sh';
|
|
123
|
+
return `/**
|
|
124
|
+
* Duct Shell Widget — auto-generated by \`npx duct init\`
|
|
125
|
+
*
|
|
126
|
+
* Mount this in your root layout. That's it.
|
|
127
|
+
* The shell is hosted by Duct. This component is a thin bridge.
|
|
128
|
+
* It renders the self-contained launcher, so do not add a second trigger.
|
|
129
|
+
*
|
|
130
|
+
* To customize: ${docsBase}/shell/customization
|
|
131
|
+
* To update: npx duct init --update
|
|
132
|
+
*/
|
|
133
|
+
|
|
134
|
+
import { DuctShell } from "@shoryasethia/duct-sdk/react";
|
|
135
|
+
|
|
136
|
+
export function DuctShellEmbed() {
|
|
137
|
+
const shellHost = import.meta.env.VITE_DUCT_SHELL_HOST ?? "http://localhost:3000";
|
|
138
|
+
|
|
139
|
+
return (
|
|
140
|
+
<DuctShell
|
|
141
|
+
shellId={import.meta.env.VITE_DUCT_SHELL_ID}
|
|
142
|
+
shellHost={shellHost}
|
|
143
|
+
position="${position}"
|
|
144
|
+
onTokenRequest={async (shellId) => {
|
|
145
|
+
const res = await fetch("/api/duct/token", {
|
|
146
|
+
method: "POST",
|
|
147
|
+
headers: { "Content-Type": "application/json" },
|
|
148
|
+
body: JSON.stringify({ shellId }),
|
|
149
|
+
});
|
|
150
|
+
if (!res.ok) return "";
|
|
151
|
+
const data = await res.json() as { token?: string };
|
|
152
|
+
return data.token ?? "";
|
|
153
|
+
}}
|
|
154
|
+
style={{
|
|
155
|
+
width: "380px",
|
|
156
|
+
height: "620px",
|
|
157
|
+
maxWidth: "calc(100vw - 32px)",
|
|
158
|
+
maxHeight: "calc(100vh - 110px)",
|
|
159
|
+
zIndex: 9999,
|
|
160
|
+
}}
|
|
161
|
+
/>
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Add to app/root.tsx:
|
|
166
|
+
// import { DuctShellEmbed } from "~/components/DuctShell";
|
|
167
|
+
// ... in your App component's return: <DuctShellEmbed />
|
|
168
|
+
//
|
|
169
|
+
// Shell ID: ${config.shellId}
|
|
170
|
+
`;
|
|
171
|
+
}
|
|
172
|
+
// ----------------------------------------------------------------
|
|
173
|
+
// Vite/Express React shell component
|
|
174
|
+
// ----------------------------------------------------------------
|
|
175
|
+
function generateViteShellComponent(config) {
|
|
176
|
+
const position = config.widget?.position ?? "bottom-right";
|
|
177
|
+
const docsBase = config.product?.baseUrl
|
|
178
|
+
? `https://docs.${new URL(config.product.baseUrl).hostname.split('.').slice(-2).join('.')}`
|
|
179
|
+
: 'https://docs.duct.sh';
|
|
180
|
+
return `/**
|
|
181
|
+
* Duct Shell Widget — auto-generated by \`npx duct init\`
|
|
182
|
+
*
|
|
183
|
+
* Mount this in your root layout. That's it.
|
|
184
|
+
* The shell is hosted by Duct. This component is a thin bridge.
|
|
185
|
+
* It renders the self-contained launcher, so do not add a second trigger.
|
|
186
|
+
*
|
|
187
|
+
* To customize: ${docsBase}/shell/customization
|
|
188
|
+
* To update: npx duct init --update
|
|
189
|
+
*/
|
|
190
|
+
|
|
191
|
+
import { DuctShell } from "@shoryasethia/duct-sdk/react";
|
|
192
|
+
|
|
193
|
+
export function DuctShellEmbed() {
|
|
194
|
+
const shellHost = import.meta.env.VITE_DUCT_SHELL_HOST ?? "http://localhost:3000";
|
|
195
|
+
|
|
196
|
+
return (
|
|
197
|
+
<DuctShell
|
|
198
|
+
shellId={import.meta.env.VITE_DUCT_SHELL_ID}
|
|
199
|
+
shellHost={shellHost}
|
|
200
|
+
position="${position}"
|
|
201
|
+
onTokenRequest={async (shellId) => {
|
|
202
|
+
const res = await fetch("/api/duct/token", {
|
|
203
|
+
method: "POST",
|
|
204
|
+
headers: { "Content-Type": "application/json" },
|
|
205
|
+
body: JSON.stringify({ shellId }),
|
|
206
|
+
});
|
|
207
|
+
if (!res.ok) return "";
|
|
208
|
+
const data = await res.json() as { token?: string };
|
|
209
|
+
return data.token ?? "";
|
|
210
|
+
}}
|
|
211
|
+
style={{
|
|
212
|
+
width: "380px",
|
|
213
|
+
height: "620px",
|
|
214
|
+
maxWidth: "calc(100vw - 32px)",
|
|
215
|
+
maxHeight: "calc(100vh - 110px)",
|
|
216
|
+
zIndex: 9999,
|
|
217
|
+
}}
|
|
218
|
+
/>
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Add to src/App.tsx:
|
|
223
|
+
// import { DuctShellEmbed } from "./components/DuctShell";
|
|
224
|
+
// ... in your App component's return: <DuctShellEmbed />
|
|
225
|
+
//
|
|
226
|
+
// Shell ID: ${config.shellId}
|
|
227
|
+
`;
|
|
228
|
+
}
|
|
229
|
+
// ----------------------------------------------------------------
|
|
230
|
+
// Environment variable template
|
|
231
|
+
// ----------------------------------------------------------------
|
|
232
|
+
export function generateEnvTemplate(config, framework) {
|
|
233
|
+
return generateEnvTemplateForFramework(config, framework);
|
|
234
|
+
}
|
|
235
|
+
function getPublicShellEnvKey(framework) {
|
|
236
|
+
switch (framework.name) {
|
|
237
|
+
case "vite-react":
|
|
238
|
+
case "remix":
|
|
239
|
+
case "vue3":
|
|
240
|
+
return "VITE_DUCT_SHELL_ID";
|
|
241
|
+
case "sveltekit":
|
|
242
|
+
return "PUBLIC_DUCT_SHELL_ID";
|
|
243
|
+
case "nuxt":
|
|
244
|
+
return "NUXT_PUBLIC_DUCT_SHELL_ID";
|
|
245
|
+
case "next-app":
|
|
246
|
+
case "next-pages":
|
|
247
|
+
default:
|
|
248
|
+
return "NEXT_PUBLIC_DUCT_SHELL_ID";
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
function getPublicShellHostEnvKey(framework) {
|
|
252
|
+
switch (framework.name) {
|
|
253
|
+
case "vite-react":
|
|
254
|
+
case "remix":
|
|
255
|
+
case "vue3":
|
|
256
|
+
return "VITE_DUCT_SHELL_HOST";
|
|
257
|
+
case "sveltekit":
|
|
258
|
+
return "PUBLIC_DUCT_SHELL_HOST";
|
|
259
|
+
case "nuxt":
|
|
260
|
+
return "NUXT_PUBLIC_DUCT_SHELL_HOST";
|
|
261
|
+
case "next-app":
|
|
262
|
+
case "next-pages":
|
|
263
|
+
default:
|
|
264
|
+
return "NEXT_PUBLIC_DUCT_SHELL_HOST";
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
export function generateEnvTemplateForFramework(config, framework) {
|
|
268
|
+
const shellKey = getPublicShellEnvKey(framework);
|
|
269
|
+
const hostKey = getPublicShellHostEnvKey(framework);
|
|
270
|
+
return `# Duct credentials
|
|
271
|
+
${shellKey}=${config.shellId}
|
|
272
|
+
${hostKey}=http://localhost:3000
|
|
273
|
+
DUCT_SHELL_ID=${config.shellId}
|
|
274
|
+
DUCT_SECRET_KEY=<from-dashboard>
|
|
275
|
+
`;
|
|
276
|
+
}
|
|
277
|
+
/** Returns the filename init.ts should write the env template to. */
|
|
278
|
+
export function getEnvTemplateName(framework) {
|
|
279
|
+
// Next.js conventionally uses .env.local for secrets; others use .env
|
|
280
|
+
if (framework.name === "next-app" || framework.name === "next-pages") {
|
|
281
|
+
return ".env.local.example";
|
|
282
|
+
}
|
|
283
|
+
return ".env.example";
|
|
284
|
+
}
|
|
285
|
+
// ----------------------------------------------------------------
|
|
286
|
+
// generateShell
|
|
287
|
+
// ----------------------------------------------------------------
|
|
288
|
+
// ----------------------------------------------------------------
|
|
289
|
+
// SvelteKit shell component
|
|
290
|
+
// ----------------------------------------------------------------
|
|
291
|
+
function generateSvelteKitShellComponent(config) {
|
|
292
|
+
const docsBase = config.product?.baseUrl
|
|
293
|
+
? `https://docs.${new URL(config.product.baseUrl).hostname.split('.').slice(-2).join('.')}`
|
|
294
|
+
: 'https://docs.duct.sh';
|
|
295
|
+
return `<!--
|
|
296
|
+
Duct Shell Widget — auto-generated by \`npx duct init\`
|
|
297
|
+
|
|
298
|
+
Mount this in your root layout. That's it.
|
|
299
|
+
The shell is hosted by Duct. This component is a thin bridge.
|
|
300
|
+
|
|
301
|
+
To customize: ${docsBase}/shell/customization
|
|
302
|
+
To update: npx duct init --update
|
|
303
|
+
-->
|
|
304
|
+
|
|
305
|
+
<script lang="ts">
|
|
306
|
+
import { onMount } from "svelte";
|
|
307
|
+
import { env } from "$env/dynamic/public";
|
|
308
|
+
|
|
309
|
+
let open = false;
|
|
310
|
+
|
|
311
|
+
const shellId = env.PUBLIC_DUCT_SHELL_ID ?? "";
|
|
312
|
+
const shellHost = env.PUBLIC_DUCT_SHELL_HOST ?? "http://localhost:3000";
|
|
313
|
+
const shellUrl = \`\${shellHost}/\${shellId}\`;
|
|
314
|
+
|
|
315
|
+
async function getToken(): Promise<string> {
|
|
316
|
+
const res = await fetch("/api/duct/token", {
|
|
317
|
+
method: "POST",
|
|
318
|
+
headers: { "Content-Type": "application/json" },
|
|
319
|
+
body: JSON.stringify({ shellId }),
|
|
320
|
+
});
|
|
321
|
+
if (!res.ok) return "";
|
|
322
|
+
const data = await res.json();
|
|
323
|
+
return data.token ?? "";
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Listen for postMessage events from the shell iframe
|
|
327
|
+
onMount(() => {
|
|
328
|
+
const handleMessage = async (event: MessageEvent) => {
|
|
329
|
+
if (event.data?.type === "DUCT_REQUEST_TOKEN") {
|
|
330
|
+
const token = await getToken();
|
|
331
|
+
(event.source as Window)?.postMessage(
|
|
332
|
+
{ type: "DUCT_TOKEN_RESPONSE", token, requestId: event.data.requestId },
|
|
333
|
+
event.origin
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
if (event.data?.type === "DUCT_CLOSE") {
|
|
337
|
+
open = false;
|
|
338
|
+
}
|
|
339
|
+
if (event.data?.type === "DUCT_DEEPLINK_NAVIGATE") {
|
|
340
|
+
window.location.href = event.data.url;
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
window.addEventListener("message", handleMessage);
|
|
344
|
+
return () => window.removeEventListener("message", handleMessage);
|
|
345
|
+
});
|
|
346
|
+
</script>
|
|
347
|
+
|
|
348
|
+
<!-- Trigger button -->
|
|
349
|
+
<button
|
|
350
|
+
on:click={() => (open = !open)}
|
|
351
|
+
style="position:fixed;bottom:24px;right:24px;z-index:10000;background:#f97316;color:#fff;border:none;border-radius:50%;width:52px;height:52px;cursor:pointer;font-size:22px;box-shadow:0 4px 14px rgba(0,0,0,0.18);"
|
|
352
|
+
aria-label="Open {${config.product.name}} assistant"
|
|
353
|
+
>
|
|
354
|
+
{#if open}✕{:else}💬{/if}
|
|
355
|
+
</button>
|
|
356
|
+
|
|
357
|
+
<!-- Slide-in animation for the shell panel -->
|
|
358
|
+
<style>
|
|
359
|
+
@keyframes duct-slidein {
|
|
360
|
+
from { opacity: 0; transform: translateY(100%); }
|
|
361
|
+
to { opacity: 1; transform: translateY(0); }
|
|
362
|
+
}
|
|
363
|
+
.duct-shell-panel {
|
|
364
|
+
animation: duct-slidein 0.25s cubic-bezier(0.16, 1, 0.3, 1) both;
|
|
365
|
+
}
|
|
366
|
+
</style>
|
|
367
|
+
|
|
368
|
+
<!-- Shell iframe -->
|
|
369
|
+
{#if open}
|
|
370
|
+
<div
|
|
371
|
+
class="duct-shell-panel"
|
|
372
|
+
style="position:fixed;bottom:86px;right:24px;width:380px;height:620px;max-width:calc(100vw - 32px);max-height:calc(100vh - 110px);border-radius:16px;box-shadow:0 8px 32px rgba(0,0,0,0.16);z-index:9999;overflow:hidden;"
|
|
373
|
+
>
|
|
374
|
+
<iframe
|
|
375
|
+
src={shellUrl}
|
|
376
|
+
title="${config.product.name} assistant"
|
|
377
|
+
style="width:100%;height:100%;border:none;display:block;"
|
|
378
|
+
allow="clipboard-write"
|
|
379
|
+
/>
|
|
380
|
+
</div>
|
|
381
|
+
{/if}
|
|
382
|
+
|
|
383
|
+
<!-- Usage: add to +layout.svelte
|
|
384
|
+
import DuctShell from "$lib/components/DuctShell.svelte";
|
|
385
|
+
<DuctShell />
|
|
386
|
+
Shell ID: ${config.shellId}
|
|
387
|
+
-->
|
|
388
|
+
`;
|
|
389
|
+
}
|
|
390
|
+
// ----------------------------------------------------------------
|
|
391
|
+
// Vue 3 shell component
|
|
392
|
+
// ----------------------------------------------------------------
|
|
393
|
+
function generateVue3ShellComponent(config) {
|
|
394
|
+
const position = config.widget?.position ?? "bottom-right";
|
|
395
|
+
const width = config.widget?.width ?? 380;
|
|
396
|
+
const height = config.widget?.height ?? 620;
|
|
397
|
+
const docsBase = config.product?.baseUrl
|
|
398
|
+
? `https://docs.${new URL(config.product.baseUrl).hostname.split('.').slice(-2).join('.')}`
|
|
399
|
+
: 'https://docs.duct.sh';
|
|
400
|
+
const positionStyle = position === "bottom-left"
|
|
401
|
+
? "bottom: 24px; left: 24px;"
|
|
402
|
+
: "bottom: 24px; right: 24px;";
|
|
403
|
+
const panelStyle = position === "bottom-left"
|
|
404
|
+
? "bottom: 86px; left: 24px;"
|
|
405
|
+
: "bottom: 86px; right: 24px;";
|
|
406
|
+
return `<!--
|
|
407
|
+
Duct Shell Widget — auto-generated by \`npx duct init\`
|
|
408
|
+
|
|
409
|
+
Mount this in your root App.vue (or a layout component).
|
|
410
|
+
The shell is hosted by Duct. This is a thin postMessage bridge.
|
|
411
|
+
|
|
412
|
+
To customize: ${docsBase}/shell/customization
|
|
413
|
+
To update: npx duct init --update
|
|
414
|
+
-->
|
|
415
|
+
|
|
416
|
+
<template>
|
|
417
|
+
<!-- Trigger button -->
|
|
418
|
+
<button
|
|
419
|
+
@click="open = !open"
|
|
420
|
+
:aria-label="\`Open ${config.product.name} assistant\`"
|
|
421
|
+
style="position:fixed;${positionStyle}z-index:10000;background:#f97316;color:#fff;border:none;border-radius:50%;width:52px;height:52px;cursor:pointer;font-size:22px;box-shadow:0 4px 14px rgba(0,0,0,0.18);"
|
|
422
|
+
>
|
|
423
|
+
{{ open ? '✕' : '💬' }}
|
|
424
|
+
</button>
|
|
425
|
+
|
|
426
|
+
<!-- Shell panel -->
|
|
427
|
+
<Transition name="duct-slide">
|
|
428
|
+
<div
|
|
429
|
+
v-if="open"
|
|
430
|
+
style="position:fixed;${panelStyle}width:${width}px;height:${height}px;max-width:calc(100vw - 32px);max-height:calc(100vh - 110px);border-radius:16px;box-shadow:0 8px 32px rgba(0,0,0,0.16);z-index:9999;overflow:hidden;"
|
|
431
|
+
>
|
|
432
|
+
<iframe
|
|
433
|
+
ref="iframeRef"
|
|
434
|
+
:src="shellUrl"
|
|
435
|
+
title="${config.product.name} assistant"
|
|
436
|
+
style="width:100%;height:100%;border:none;display:block;"
|
|
437
|
+
allow="clipboard-write"
|
|
438
|
+
/>
|
|
439
|
+
</div>
|
|
440
|
+
</Transition>
|
|
441
|
+
</template>
|
|
442
|
+
|
|
443
|
+
<script setup lang="ts">
|
|
444
|
+
import { ref, computed, onMounted, onUnmounted } from 'vue';
|
|
445
|
+
|
|
446
|
+
const open = ref(false);
|
|
447
|
+
const iframeRef = ref<HTMLIFrameElement | null>(null);
|
|
448
|
+
|
|
449
|
+
const shellId = import.meta.env.VITE_DUCT_SHELL_ID as string ?? '';
|
|
450
|
+
const shellHost = import.meta.env.VITE_DUCT_SHELL_HOST as string ?? 'http://localhost:3000';
|
|
451
|
+
const shellUrl = computed(() => \`\${shellHost}/\${shellId}\`);
|
|
452
|
+
|
|
453
|
+
async function getToken(): Promise<string> {
|
|
454
|
+
try {
|
|
455
|
+
const res = await fetch('/api/duct/token', {
|
|
456
|
+
method: 'POST',
|
|
457
|
+
headers: { 'Content-Type': 'application/json' },
|
|
458
|
+
body: JSON.stringify({ shellId }),
|
|
459
|
+
});
|
|
460
|
+
if (!res.ok) return '';
|
|
461
|
+
const data = await res.json() as { token?: string };
|
|
462
|
+
return data.token ?? '';
|
|
463
|
+
} catch {
|
|
464
|
+
return '';
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
async function handleMessage(event: MessageEvent) {
|
|
469
|
+
if (event.data?.type === 'DUCT_REQUEST_TOKEN') {
|
|
470
|
+
const token = await getToken();
|
|
471
|
+
iframeRef.value?.contentWindow?.postMessage(
|
|
472
|
+
{ type: 'DUCT_TOKEN_RESPONSE', token, requestId: event.data.requestId },
|
|
473
|
+
shellHost
|
|
474
|
+
);
|
|
475
|
+
} else if (event.data?.type === 'DUCT_CLOSE') {
|
|
476
|
+
open.value = false;
|
|
477
|
+
} else if (event.data?.type === 'DUCT_DEEPLINK_NAVIGATE') {
|
|
478
|
+
window.location.href = event.data.url as string;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
onMounted(() => window.addEventListener('message', handleMessage));
|
|
483
|
+
onUnmounted(() => window.removeEventListener('message', handleMessage));
|
|
484
|
+
</script>
|
|
485
|
+
|
|
486
|
+
<style scoped>
|
|
487
|
+
.duct-slide-enter-active {
|
|
488
|
+
animation: duct-slidein 0.25s cubic-bezier(0.16, 1, 0.3, 1) both;
|
|
489
|
+
}
|
|
490
|
+
.duct-slide-leave-active {
|
|
491
|
+
animation: duct-slidein 0.2s cubic-bezier(0.16, 1, 0.3, 1) reverse both;
|
|
492
|
+
}
|
|
493
|
+
@keyframes duct-slidein {
|
|
494
|
+
from { opacity: 0; transform: translateY(100%); }
|
|
495
|
+
to { opacity: 1; transform: translateY(0); }
|
|
496
|
+
}
|
|
497
|
+
</style>
|
|
498
|
+
|
|
499
|
+
<!--
|
|
500
|
+
Usage — add to src/App.vue:
|
|
501
|
+
|
|
502
|
+
import DuctShell from './components/DuctShell.vue'
|
|
503
|
+
<DuctShell />
|
|
504
|
+
|
|
505
|
+
Shell ID: ${config.shellId}
|
|
506
|
+
-->
|
|
507
|
+
`;
|
|
508
|
+
}
|
|
509
|
+
// ----------------------------------------------------------------
|
|
510
|
+
// Nuxt shell component
|
|
511
|
+
// ----------------------------------------------------------------
|
|
512
|
+
function generateNuxtShellComponent(config) {
|
|
513
|
+
const position = config.widget?.position ?? "bottom-right";
|
|
514
|
+
const width = config.widget?.width ?? 380;
|
|
515
|
+
const height = config.widget?.height ?? 620;
|
|
516
|
+
const docsBase = config.product?.baseUrl
|
|
517
|
+
? `https://docs.${new URL(config.product.baseUrl).hostname.split('.').slice(-2).join('.')}`
|
|
518
|
+
: 'https://docs.duct.sh';
|
|
519
|
+
const positionStyle = position === "bottom-left"
|
|
520
|
+
? "bottom: 24px; left: 24px;"
|
|
521
|
+
: "bottom: 24px; right: 24px;";
|
|
522
|
+
const panelStyle = position === "bottom-left"
|
|
523
|
+
? "bottom: 86px; left: 24px;"
|
|
524
|
+
: "bottom: 86px; right: 24px;";
|
|
525
|
+
return `<!--
|
|
526
|
+
Duct Shell Widget — auto-generated by \`npx duct init\`
|
|
527
|
+
|
|
528
|
+
Mount this in your app.vue or a layouts file.
|
|
529
|
+
The shell is hosted by Duct. This is a thin postMessage bridge.
|
|
530
|
+
|
|
531
|
+
To customize: ${docsBase}/shell/customization
|
|
532
|
+
To update: npx duct init --update
|
|
533
|
+
-->
|
|
534
|
+
|
|
535
|
+
<template>
|
|
536
|
+
<!-- Trigger button -->
|
|
537
|
+
<button
|
|
538
|
+
@click="open = !open"
|
|
539
|
+
:aria-label="\`Open ${config.product.name} assistant\`"
|
|
540
|
+
style="position:fixed;${positionStyle}z-index:10000;background:#f97316;color:#fff;border:none;border-radius:50%;width:52px;height:52px;cursor:pointer;font-size:22px;box-shadow:0 4px 14px rgba(0,0,0,0.18);"
|
|
541
|
+
>
|
|
542
|
+
{{ open ? '✕' : '💬' }}
|
|
543
|
+
</button>
|
|
544
|
+
|
|
545
|
+
<!-- Shell panel -->
|
|
546
|
+
<Transition name="duct-slide">
|
|
547
|
+
<div
|
|
548
|
+
v-if="open"
|
|
549
|
+
style="position:fixed;${panelStyle}width:${width}px;height:${height}px;max-width:calc(100vw - 32px);max-height:calc(100vh - 110px);border-radius:16px;box-shadow:0 8px 32px rgba(0,0,0,0.16);z-index:9999;overflow:hidden;"
|
|
550
|
+
>
|
|
551
|
+
<iframe
|
|
552
|
+
ref="iframeRef"
|
|
553
|
+
:src="shellUrl"
|
|
554
|
+
title="${config.product.name} assistant"
|
|
555
|
+
style="width:100%;height:100%;border:none;display:block;"
|
|
556
|
+
allow="clipboard-write"
|
|
557
|
+
/>
|
|
558
|
+
</div>
|
|
559
|
+
</Transition>
|
|
560
|
+
</template>
|
|
561
|
+
|
|
562
|
+
<script setup lang="ts">
|
|
563
|
+
const open = ref(false);
|
|
564
|
+
const iframeRef = ref<HTMLIFrameElement | null>(null);
|
|
565
|
+
|
|
566
|
+
// Nuxt uses NUXT_PUBLIC_ prefix in nuxt.config.ts runtimeConfig.public
|
|
567
|
+
const config = useRuntimeConfig();
|
|
568
|
+
const shellId = (config.public.ductShellId as string) ?? '';
|
|
569
|
+
const shellHost = (config.public.ductShellHost as string) ?? 'http://localhost:3000';
|
|
570
|
+
const shellUrl = computed(() => \`\${shellHost}/\${shellId}\`);
|
|
571
|
+
|
|
572
|
+
async function getToken(): Promise<string> {
|
|
573
|
+
try {
|
|
574
|
+
const data = await $fetch<{ token?: string }>('/api/duct/token', {
|
|
575
|
+
method: 'POST',
|
|
576
|
+
body: { shellId },
|
|
577
|
+
});
|
|
578
|
+
return data.token ?? '';
|
|
579
|
+
} catch {
|
|
580
|
+
return '';
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
async function handleMessage(event: MessageEvent) {
|
|
585
|
+
if (event.data?.type === 'DUCT_REQUEST_TOKEN') {
|
|
586
|
+
const token = await getToken();
|
|
587
|
+
iframeRef.value?.contentWindow?.postMessage(
|
|
588
|
+
{ type: 'DUCT_TOKEN_RESPONSE', token, requestId: event.data.requestId },
|
|
589
|
+
shellHost
|
|
590
|
+
);
|
|
591
|
+
} else if (event.data?.type === 'DUCT_CLOSE') {
|
|
592
|
+
open.value = false;
|
|
593
|
+
} else if (event.data?.type === 'DUCT_DEEPLINK_NAVIGATE') {
|
|
594
|
+
await navigateTo(event.data.url as string, { external: true });
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
onMounted(() => window.addEventListener('message', handleMessage));
|
|
599
|
+
onUnmounted(() => window.removeEventListener('message', handleMessage));
|
|
600
|
+
</script>
|
|
601
|
+
|
|
602
|
+
<style scoped>
|
|
603
|
+
.duct-slide-enter-active {
|
|
604
|
+
animation: duct-slidein 0.25s cubic-bezier(0.16, 1, 0.3, 1) both;
|
|
605
|
+
}
|
|
606
|
+
.duct-slide-leave-active {
|
|
607
|
+
animation: duct-slidein 0.2s cubic-bezier(0.16, 1, 0.3, 1) reverse both;
|
|
608
|
+
}
|
|
609
|
+
@keyframes duct-slidein {
|
|
610
|
+
from { opacity: 0; transform: translateY(100%); }
|
|
611
|
+
to { opacity: 1; transform: translateY(0); }
|
|
612
|
+
}
|
|
613
|
+
</style>
|
|
614
|
+
|
|
615
|
+
<!--
|
|
616
|
+
Usage — add to app.vue or layouts/default.vue:
|
|
617
|
+
|
|
618
|
+
import DuctShell from '~/components/DuctShell.vue'
|
|
619
|
+
<DuctShell />
|
|
620
|
+
|
|
621
|
+
Add to nuxt.config.ts:
|
|
622
|
+
runtimeConfig: {
|
|
623
|
+
public: {
|
|
624
|
+
ductShellId: process.env.NUXT_PUBLIC_DUCT_SHELL_ID,
|
|
625
|
+
ductShellHost: process.env.NUXT_PUBLIC_DUCT_SHELL_HOST ?? 'http://localhost:3000',
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
Shell ID: ${config.shellId}
|
|
630
|
+
-->
|
|
631
|
+
`;
|
|
632
|
+
}
|
|
633
|
+
// ----------------------------------------------------------------
|
|
634
|
+
// generateShell
|
|
635
|
+
// ----------------------------------------------------------------
|
|
636
|
+
/**
|
|
637
|
+
* Generate a framework-specific shell component file.
|
|
638
|
+
* Returns the file contents as a string.
|
|
639
|
+
*
|
|
640
|
+
* File extension is baked into the content header comment so the caller
|
|
641
|
+
* (init.ts getOutputPaths) knows what path to write to.
|
|
642
|
+
*/
|
|
643
|
+
export function generateShell(config, framework) {
|
|
644
|
+
switch (framework.name) {
|
|
645
|
+
case "next-app":
|
|
646
|
+
case "next-pages":
|
|
647
|
+
return generateReactShellComponent(config);
|
|
648
|
+
case "remix":
|
|
649
|
+
return generateRemixShellComponent(config);
|
|
650
|
+
case "vite-react":
|
|
651
|
+
return generateViteShellComponent(config);
|
|
652
|
+
case "sveltekit":
|
|
653
|
+
return generateSvelteKitShellComponent(config);
|
|
654
|
+
case "vue3":
|
|
655
|
+
return generateVue3ShellComponent(config);
|
|
656
|
+
case "nuxt":
|
|
657
|
+
return generateNuxtShellComponent(config);
|
|
658
|
+
default:
|
|
659
|
+
return generateReactShellComponent(config);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
/**
|
|
663
|
+
* Generate the layout integration snippet for a given framework.
|
|
664
|
+
*/
|
|
665
|
+
export function generateLayoutSnippet(config, framework) {
|
|
666
|
+
switch (framework.name) {
|
|
667
|
+
case "next-app":
|
|
668
|
+
return generateNextLayoutSnippet(config);
|
|
669
|
+
default:
|
|
670
|
+
return `// Add DuctShellEmbed to your root layout/app component.\n// Shell ID: ${config.shellId}\n`;
|
|
671
|
+
}
|
|
672
|
+
}
|