mirror of
https://github.com/simstudioai/sim.git
synced 2026-01-10 15:38:00 -05:00
Compare commits
27 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9a4b9e281e | ||
|
|
fcf947df22 | ||
|
|
7be9941bc9 | ||
|
|
807014a5d2 | ||
|
|
aef1f18c1e | ||
|
|
368576b082 | ||
|
|
aace3066aa | ||
|
|
ef5b6999ab | ||
|
|
9df886d1e9 | ||
|
|
9991796661 | ||
|
|
095a15d7b5 | ||
|
|
a02016e247 | ||
|
|
8620ab255a | ||
|
|
47ddfb639e | ||
|
|
5d48c2780c | ||
|
|
38614fad79 | ||
|
|
6f32aea96b | ||
|
|
98e98496e8 | ||
|
|
659b46fa2f | ||
|
|
fb3d6d4c88 | ||
|
|
ec2cc82b72 | ||
|
|
274d5e3afc | ||
|
|
c552bb9c5f | ||
|
|
ad7b791242 | ||
|
|
ce4893a53c | ||
|
|
7f1ff7fd86 | ||
|
|
517f1a91b6 |
@@ -6,10 +6,9 @@ import Link from 'next/link'
|
||||
import { notFound } from 'next/navigation'
|
||||
import { StructuredData } from '@/components/structured-data'
|
||||
import { CodeBlock } from '@/components/ui/code-block'
|
||||
import { CopyPageButton } from '@/components/ui/copy-page-button'
|
||||
import { source } from '@/lib/source'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
export default async function Page(props: { params: Promise<{ slug?: string[]; lang: string }> }) {
|
||||
const params = await props.params
|
||||
const page = source.getPage(params.slug, params.lang)
|
||||
@@ -193,8 +192,19 @@ export default async function Page(props: { params: Promise<{ slug?: string[]; l
|
||||
component: <CustomFooter />,
|
||||
}}
|
||||
>
|
||||
<DocsTitle>{page.data.title}</DocsTitle>
|
||||
<DocsDescription>{page.data.description}</DocsDescription>
|
||||
<div className='relative'>
|
||||
<div className='absolute top-1 right-0'>
|
||||
<CopyPageButton
|
||||
content={`# ${page.data.title}
|
||||
|
||||
${page.data.description || ''}
|
||||
|
||||
${page.data.content || ''}`}
|
||||
/>
|
||||
</div>
|
||||
<DocsTitle>{page.data.title}</DocsTitle>
|
||||
<DocsDescription>{page.data.description}</DocsDescription>
|
||||
</div>
|
||||
<DocsBody>
|
||||
<MDX
|
||||
components={{
|
||||
|
||||
@@ -36,7 +36,9 @@
|
||||
/* Shift the sidebar slightly left from the content edge for extra breathing room */
|
||||
--sidebar-shift: 90px;
|
||||
--sidebar-offset: max(0px, calc(var(--edge-gutter) - var(--sidebar-shift)));
|
||||
--toc-offset: var(--edge-gutter);
|
||||
/* Shift TOC slightly right to match sidebar spacing for symmetry */
|
||||
--toc-shift: 90px;
|
||||
--toc-offset: max(0px, calc(var(--edge-gutter) - var(--toc-shift)));
|
||||
/* Sidebar and TOC have 20px internal padding - navbar accounts for this directly */
|
||||
/* Extra gap between sidebar/TOC and the main text content */
|
||||
--content-gap: 1.75rem;
|
||||
@@ -50,9 +52,7 @@
|
||||
|
||||
/* Light mode navbar and search styling */
|
||||
:root:not(.dark) nav {
|
||||
background-color: hsla(0, 0%, 96%, 0.92) !important;
|
||||
backdrop-filter: blur(25px) saturate(180%) brightness(1.05) !important;
|
||||
-webkit-backdrop-filter: blur(25px) saturate(180%) brightness(1.05) !important;
|
||||
background-color: hsla(0, 0%, 96%, 0.85) !important;
|
||||
}
|
||||
|
||||
:root:not(.dark) nav button[type="button"] {
|
||||
@@ -66,6 +66,13 @@
|
||||
color: rgba(0, 0, 0, 0.6) !important;
|
||||
}
|
||||
|
||||
/* Dark mode navbar and search styling */
|
||||
:root.dark nav {
|
||||
background-color: hsla(0, 0%, 7.04%, 0.92) !important;
|
||||
backdrop-filter: blur(25px) saturate(180%) brightness(0.6) !important;
|
||||
-webkit-backdrop-filter: blur(25px) saturate(180%) brightness(0.6) !important;
|
||||
}
|
||||
|
||||
/* ============================================
|
||||
Custom Sidebar Styling (Turborepo-inspired)
|
||||
============================================ */
|
||||
@@ -107,8 +114,21 @@ aside#nd-sidebar {
|
||||
aside#nd-sidebar {
|
||||
left: var(--sidebar-offset) !important;
|
||||
}
|
||||
[data-toc] {
|
||||
margin-right: var(--toc-offset) !important;
|
||||
/* TOC positioning - target all possible selectors */
|
||||
[data-toc],
|
||||
aside[data-toc],
|
||||
div[data-toc],
|
||||
.fd-toc,
|
||||
#nd-toc,
|
||||
nav[data-toc],
|
||||
aside:has([role="complementary"]) {
|
||||
right: var(--toc-offset) !important;
|
||||
}
|
||||
|
||||
/* Alternative TOC container targeting */
|
||||
[data-docs-page] > aside:last-child,
|
||||
main ~ aside {
|
||||
right: var(--toc-offset) !important;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,21 +11,20 @@ export function Navbar() {
|
||||
<nav
|
||||
className='sticky top-0 z-50 border-border/50 border-b'
|
||||
style={{
|
||||
backgroundColor: 'hsla(0, 0%, 7.04%, 0.92)',
|
||||
backdropFilter: 'blur(25px) saturate(180%) brightness(0.6)',
|
||||
WebkitBackdropFilter: 'blur(25px) saturate(180%) brightness(0.6)',
|
||||
backdropFilter: 'blur(25px) saturate(180%)',
|
||||
WebkitBackdropFilter: 'blur(25px) saturate(180%)',
|
||||
}}
|
||||
>
|
||||
{/* Desktop: Single row layout */}
|
||||
<div className='hidden h-16 w-full items-center lg:flex'>
|
||||
<div
|
||||
className='grid w-full grid-cols-[auto_1fr_auto] items-center'
|
||||
className='relative flex w-full items-center justify-between'
|
||||
style={{
|
||||
paddingLeft: 'calc(var(--sidebar-offset) + 20px)',
|
||||
paddingRight: 'calc(var(--toc-offset) + 20px)',
|
||||
paddingRight: 'calc(var(--toc-offset) + 60px)',
|
||||
}}
|
||||
>
|
||||
{/* Left cluster: translate by sidebar delta to align with sidebar edge */}
|
||||
{/* Left cluster: logo */}
|
||||
<div className='flex items-center'>
|
||||
<Link href='/' className='flex min-w-[100px] items-center'>
|
||||
<Image
|
||||
@@ -38,12 +37,12 @@ export function Navbar() {
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
{/* Center cluster: search */}
|
||||
<div className='flex flex-1 items-center justify-center pl-32'>
|
||||
{/* Center cluster: search - absolutely positioned to center */}
|
||||
<div className='-translate-x-1/2 absolute left-1/2 flex items-center justify-center'>
|
||||
<SearchTrigger />
|
||||
</div>
|
||||
|
||||
{/* Right cluster aligns with TOC edge using the same right gutter */}
|
||||
{/* Right cluster aligns with TOC edge */}
|
||||
<div className='flex items-center gap-4'>
|
||||
<Link
|
||||
href='https://sim.ai'
|
||||
|
||||
42
apps/docs/components/ui/copy-page-button.tsx
Normal file
42
apps/docs/components/ui/copy-page-button.tsx
Normal file
@@ -0,0 +1,42 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { Check, Copy } from 'lucide-react'
|
||||
|
||||
interface CopyPageButtonProps {
|
||||
content: string
|
||||
}
|
||||
|
||||
export function CopyPageButton({ content }: CopyPageButtonProps) {
|
||||
const [copied, setCopied] = useState(false)
|
||||
|
||||
const handleCopy = async () => {
|
||||
try {
|
||||
await navigator.clipboard.writeText(content)
|
||||
setCopied(true)
|
||||
setTimeout(() => setCopied(false), 2000)
|
||||
} catch (err) {
|
||||
console.error('Failed to copy:', err)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
onClick={handleCopy}
|
||||
className='flex items-center gap-1.5 rounded-lg border border-border/40 bg-background px-2.5 py-1.5 text-muted-foreground/60 text-sm transition-all hover:border-border hover:bg-accent/50 hover:text-muted-foreground'
|
||||
aria-label={copied ? 'Copied to clipboard' : 'Copy page content'}
|
||||
>
|
||||
{copied ? (
|
||||
<>
|
||||
<Check className='h-4 w-4' />
|
||||
<span>Copied</span>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Copy className='h-4 w-4' />
|
||||
<span>Copy page</span>
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
)
|
||||
}
|
||||
@@ -16,7 +16,7 @@ Blöcke sind die Bausteine, die du miteinander verbindest, um KI-Workflows zu er
|
||||
|
||||
## Grundlegende Blocktypen
|
||||
|
||||
Sim bietet sieben grundlegende Blocktypen, die die wesentlichen Funktionen von KI-Workflows abdecken:
|
||||
Sim bietet wesentliche Blocktypen, die die Kernfunktionen von KI-Workflows abdecken:
|
||||
|
||||
### Verarbeitungsblöcke
|
||||
- **[Agent](/blocks/agent)** - Chatte mit KI-Modellen (OpenAI, Anthropic, Google, lokale Modelle)
|
||||
@@ -28,16 +28,20 @@ Sim bietet sieben grundlegende Blocktypen, die die wesentlichen Funktionen von K
|
||||
- **[Router](/blocks/router)** - Nutze KI, um Anfragen intelligent auf verschiedene Pfade zu leiten
|
||||
- **[Evaluator](/blocks/evaluator)** - Bewerte und beurteile die Inhaltsqualität mit KI
|
||||
|
||||
### Ablaufsteuerungsblöcke
|
||||
- **[Variablen](/blocks/variables)** - Workflow-bezogene Variablen setzen und verwalten
|
||||
- **[Warten](/blocks/wait)** - Workflow-Ausführung für eine bestimmte Zeitverzögerung pausieren
|
||||
|
||||
### Ausgabeblöcke
|
||||
- **[Response](/blocks/response)** - Formatiere und gib endgültige Ergebnisse aus deinem Workflow zurück
|
||||
- **[Antwort](/blocks/response)** - Formatieren und Zurückgeben der endgültigen Ergebnisse aus Ihrem Workflow
|
||||
|
||||
## Wie Blöcke funktionieren
|
||||
|
||||
Jeder Block hat drei Hauptkomponenten:
|
||||
|
||||
**Eingaben**: Daten, die in den Block von anderen Blöcken oder Benutzereingaben kommen
|
||||
**Konfiguration**: Einstellungen, die das Verhalten des Blocks steuern
|
||||
**Ausgaben**: Daten, die der Block für andere Blöcke produziert
|
||||
**Konfiguration**: Einstellungen, die steuern, wie der Block sich verhält
|
||||
**Ausgaben**: Daten, die der Block für andere Blöcke zur Verwendung erzeugt
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
@@ -56,7 +60,7 @@ Jeder Block hat drei Hauptkomponenten:
|
||||
Sie erstellen Workflows, indem Sie Blöcke miteinander verbinden. Die Ausgabe eines Blocks wird zur Eingabe eines anderen:
|
||||
|
||||
- **Ziehen zum Verbinden**: Ziehen Sie von einem Ausgabeport zu einem Eingabeport
|
||||
- **Mehrfachverbindungen**: Eine Ausgabe kann mit mehreren Eingängen verbunden werden
|
||||
- **Mehrfachverbindungen**: Eine Ausgabe kann mit mehreren Eingaben verbunden werden
|
||||
- **Verzweigende Pfade**: Einige Blöcke können basierend auf Bedingungen zu verschiedenen Pfaden weiterleiten
|
||||
|
||||
<div className="w-full max-w-2xl mx-auto overflow-hidden rounded-lg">
|
||||
@@ -73,7 +77,7 @@ User Input → Agent → Function → Response
|
||||
```
|
||||
|
||||
### Bedingte Verzweigung
|
||||
Verwenden Sie Bedingungsblöcke oder Router-Blöcke, um verschiedene Pfade zu erstellen:
|
||||
Verwenden Sie Bedingung- oder Router-Blöcke, um verschiedene Pfade zu erstellen:
|
||||
|
||||
```
|
||||
User Input → Router → Agent A (for questions)
|
||||
@@ -93,37 +97,43 @@ Agent → Evaluator → Condition → Response (if good)
|
||||
Jeder Blocktyp hat spezifische Konfigurationsoptionen:
|
||||
|
||||
**Alle Blöcke**:
|
||||
- Ein-/Ausgabeverbindungen
|
||||
- Eingabe-/Ausgabeverbindungen
|
||||
- Fehlerbehandlungsverhalten
|
||||
- Zeitüberschreitungseinstellungen für die Ausführung
|
||||
- Einstellungen für Ausführungs-Timeout
|
||||
|
||||
**KI-Blöcke** (Agent, Router, Evaluator):
|
||||
- Modellauswahl (OpenAI, Anthropic, Google, lokal)
|
||||
- API-Schlüssel und Authentifizierung
|
||||
- Temperature und andere Modellparameter
|
||||
- Temperatur und andere Modellparameter
|
||||
- Systemaufforderungen und Anweisungen
|
||||
|
||||
**Logikblöcke** (Bedingung, Funktion):
|
||||
**Logik-Blöcke** (Bedingung, Funktion):
|
||||
- Benutzerdefinierte Ausdrücke oder Code
|
||||
- Variablenreferenzen
|
||||
- Einstellungen für die Ausführungsumgebung
|
||||
- Einstellungen für Ausführungsumgebung
|
||||
|
||||
**Integrationsblöcke** (API, Antwort):
|
||||
**Integrations-Blöcke** (API, Response):
|
||||
- Endpunktkonfiguration
|
||||
- Header und Authentifizierung
|
||||
- Anfrage-/Antwortformatierung
|
||||
|
||||
<Cards>
|
||||
<Card title="Agent Block" href="/blocks/agent">
|
||||
Verbindung zu KI-Modellen herstellen und intelligente Antworten erzeugen
|
||||
<Card title="Agent-Block" href="/blocks/agent">
|
||||
Verbindung zu KI-Modellen herstellen und intelligente Antworten erstellen
|
||||
</Card>
|
||||
<Card title="Function Block" href="/blocks/function">
|
||||
<Card title="Funktions-Block" href="/blocks/function">
|
||||
Benutzerdefinierten Code ausführen, um Daten zu verarbeiten und zu transformieren
|
||||
</Card>
|
||||
<Card title="API Block" href="/blocks/api">
|
||||
<Card title="API-Block" href="/blocks/api">
|
||||
Integration mit externen Diensten und APIs
|
||||
</Card>
|
||||
<Card title="Condition Block" href="/blocks/condition">
|
||||
<Card title="Bedingungs-Block" href="/blocks/condition">
|
||||
Verzweigende Logik basierend auf Datenbewertung erstellen
|
||||
</Card>
|
||||
<Card title="Variablen-Block" href="/blocks/variables">
|
||||
Workflow-bezogene Variablen setzen und verwalten
|
||||
</Card>
|
||||
<Card title="Warte-Block" href="/blocks/wait">
|
||||
Workflow-Ausführung für bestimmte Zeitverzögerungen pausieren
|
||||
</Card>
|
||||
</Cards>
|
||||
|
||||
@@ -9,7 +9,7 @@ import { Image } from '@/components/ui/image'
|
||||
|
||||
Der Loop-Block ist ein Container-Block in Sim, der es ermöglicht, iterative Workflows zu erstellen, indem eine Gruppe von Blöcken wiederholt ausgeführt wird. Loops ermöglichen iterative Verarbeitung in deinen Workflows.
|
||||
|
||||
Der Loop-Block unterstützt zwei Arten der Iteration:
|
||||
Der Schleifenblock unterstützt vier Arten der Iteration:
|
||||
|
||||
<Callout type="info">
|
||||
Loop-Blöcke sind Container-Knoten, die andere Blöcke enthalten können. Die Blöcke innerhalb einer Schleife werden basierend auf deiner Konfiguration mehrfach ausgeführt.
|
||||
@@ -27,10 +27,10 @@ Der Loop-Block ermöglicht dir:
|
||||
<strong>Operationen wiederholen</strong>: Blöcke eine festgelegte Anzahl von Malen ausführen
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Sequentielle Verarbeitung</strong>: Datentransformation in geordneten Iterationen durchführen
|
||||
<strong>Auf Bedingungen basierte Schleifen</strong>: Ausführung fortsetzen, während oder bis eine Bedingung erfüllt ist
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Ergebnisse aggregieren</strong>: Ausgaben aus allen Schleifeniterationen sammeln
|
||||
<strong>Ergebnisse aggregieren</strong>: Ausgaben aus allen Schleifendurchläufen sammeln
|
||||
</Step>
|
||||
</Steps>
|
||||
|
||||
@@ -47,23 +47,23 @@ Der Loop-Block führt enthaltene Blöcke durch sequentielle Iteration aus:
|
||||
|
||||
### Schleifentyp
|
||||
|
||||
Wähle zwischen zwei Arten von Schleifen:
|
||||
Wähle zwischen vier Arten von Schleifen:
|
||||
|
||||
<Tabs items={['For Loop', 'ForEach Loop']}>
|
||||
<Tabs items={['For-Schleife', 'ForEach-Schleife', 'While-Schleife', 'Do-While-Schleife']}>
|
||||
<Tab>
|
||||
**For Loop (Iterationen)** - Eine numerische Schleife, die eine feste Anzahl von Malen ausgeführt wird:
|
||||
**For-Schleife (Iterationen)** - Eine numerische Schleife, die eine festgelegte Anzahl von Malen ausgeführt wird:
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/loop-1.png"
|
||||
alt="For Loop mit Iterationen"
|
||||
alt="For-Schleife mit Iterationen"
|
||||
width={500}
|
||||
height={400}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
Verwende diese Option, wenn du eine Operation eine bestimmte Anzahl von Malen wiederholen musst.
|
||||
Verwende diese, wenn du eine Operation eine bestimmte Anzahl von Malen wiederholen musst.
|
||||
|
||||
|
||||
```
|
||||
@@ -100,6 +100,58 @@ Wähle zwischen zwei Arten von Schleifen:
|
||||
```
|
||||
|
||||
</Tab>
|
||||
<Tab>
|
||||
**While-Schleife (Bedingungsbasiert)** - Wird weiter ausgeführt, solange eine Bedingung als wahr ausgewertet wird:
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/loop-3.png"
|
||||
alt="While-Schleife mit Bedingung"
|
||||
width={500}
|
||||
height={400}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
Verwende diese, wenn du eine Schleife benötigst, die läuft, bis eine bestimmte Bedingung erfüllt ist. Die Bedingung wird **vor** jeder Iteration überprüft.
|
||||
|
||||
|
||||
```
|
||||
Example: While <variable.i> < 10
|
||||
- Check condition → Execute if true
|
||||
- Inside loop: Increment <variable.i>
|
||||
- Inside loop: Variables assigns i = <variable.i> + 1
|
||||
- Check condition → Execute if true
|
||||
- Check condition → Exit if false
|
||||
```
|
||||
|
||||
</Tab>
|
||||
<Tab>
|
||||
**Do-While-Schleife (Bedingungsbasiert)** - Wird mindestens einmal ausgeführt und dann fortgesetzt, solange eine Bedingung wahr ist:
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/loop-3.png"
|
||||
alt="Do-While-Schleife mit Bedingung"
|
||||
width={500}
|
||||
height={400}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
Verwende diese, wenn du mindestens eine Ausführung benötigst und dann eine Schleife, bis eine Bedingung erfüllt ist. Die Bedingung wird **nach** jeder Iteration überprüft.
|
||||
|
||||
|
||||
```
|
||||
Example: Do-while <variable.i> < 10
|
||||
- Execute blocks
|
||||
- Inside loop: Increment <variable.i>
|
||||
- Inside loop: Variables assigns i = <variable.i> + 1
|
||||
- Check condition → Continue if true
|
||||
- Check condition → Exit if false
|
||||
```
|
||||
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
## Wie man Schleifen verwendet
|
||||
@@ -113,9 +165,9 @@ Wähle zwischen zwei Arten von Schleifen:
|
||||
|
||||
### Auf Ergebnisse zugreifen
|
||||
|
||||
Nach Abschluss einer Schleife kannst du auf aggregierte Ergebnisse zugreifen:
|
||||
Nach Abschluss einer Schleife können Sie auf die aggregierten Ergebnisse zugreifen:
|
||||
|
||||
- **`<loop.results>`**: Array von Ergebnissen aus allen Schleifendurchläufen
|
||||
- **`<loop.results>`**: Array mit Ergebnissen aus allen Schleifendurchläufen
|
||||
|
||||
## Beispielanwendungsfälle
|
||||
|
||||
@@ -143,17 +195,30 @@ Nach Abschluss einer Schleife kannst du auf aggregierte Ergebnisse zugreifen:
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Zähler mit While-Schleife
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Szenario: Elemente mit zählerbasierter Schleife verarbeiten</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Workflow-Variable initialisieren: `i = 0`</li>
|
||||
<li>While-Schleife mit Bedingung: `<variable.i>` \< 10</li>
|
||||
<li>Innerhalb der Schleife: Agent verarbeitet Element am Index `<variable.i>`</li>
|
||||
<li>Innerhalb der Schleife: Variable erhöht `i = <variable.i> + 1`</li>
|
||||
<li>Schleife wird fortgesetzt, solange i kleiner als 10 ist</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
## Erweiterte Funktionen
|
||||
|
||||
### Einschränkungen
|
||||
|
||||
<Callout type="warning">
|
||||
Container-Blöcke (Schleifen und Parallele) können nicht ineinander verschachtelt werden. Das bedeutet:
|
||||
- Du kannst keinen Schleifenblock in einen anderen Schleifenblock platzieren
|
||||
- Du kannst keinen Parallelblock in einen Schleifenblock platzieren
|
||||
- Du kannst keinen Container-Block in einen anderen Container-Block platzieren
|
||||
- Sie können keinen Schleifenblock in einem anderen Schleifenblock platzieren
|
||||
- Sie können keinen Parallelblock in einem Schleifenblock platzieren
|
||||
- Sie können keinen Container-Block in einem anderen Container-Block platzieren
|
||||
|
||||
Wenn du mehrdimensionale Iterationen benötigst, erwäge eine Umstrukturierung deines Workflows, um sequentielle Schleifen zu verwenden oder Daten in Stufen zu verarbeiten.
|
||||
Wenn Sie mehrdimensionale Iterationen benötigen, sollten Sie Ihren Workflow umstrukturieren, um sequentielle Schleifen zu verwenden oder Daten in Stufen zu verarbeiten.
|
||||
</Callout>
|
||||
|
||||
<Callout type="info">
|
||||
@@ -162,17 +227,20 @@ Nach Abschluss einer Schleife kannst du auf aggregierte Ergebnisse zugreifen:
|
||||
|
||||
## Eingaben und Ausgaben
|
||||
|
||||
<Tabs items={['Konfiguration', 'Variablen', 'Ergebnisse']}>
|
||||
<Tabs items={['Configuration', 'Variables', 'Results']}>
|
||||
<Tab>
|
||||
<ul className="list-disc space-y-2 pl-6">
|
||||
<li>
|
||||
<strong>Schleifentyp</strong>: Wählen Sie zwischen 'for' oder 'forEach'
|
||||
<strong>Schleifentyp</strong>: Wählen Sie zwischen 'for', 'forEach', 'while' oder 'doWhile'
|
||||
</li>
|
||||
<li>
|
||||
<strong>Iterationen</strong>: Anzahl der Ausführungen (für for-Schleifen)
|
||||
</li>
|
||||
<li>
|
||||
<strong>Sammlung</strong>: Array oder Objekt zum Durchlaufen (für forEach-Schleifen)
|
||||
<strong>Sammlung</strong>: Array oder Objekt, über das iteriert werden soll (forEach-Schleifen)
|
||||
</li>
|
||||
<li>
|
||||
<strong>Bedingung</strong>: Boolescher Ausdruck zur Auswertung (while/do-while-Schleifen)
|
||||
</li>
|
||||
</ul>
|
||||
</Tab>
|
||||
@@ -204,8 +272,8 @@ Nach Abschluss einer Schleife kannst du auf aggregierte Ergebnisse zugreifen:
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
## Best Practices
|
||||
## Bewährte Praktiken
|
||||
|
||||
- **Setzen Sie vernünftige Grenzen**: Halten Sie die Anzahl der Iterationen in einem vernünftigen Rahmen, um lange Ausführungszeiten zu vermeiden
|
||||
- **Verwenden Sie ForEach für Sammlungen**: Verwenden Sie ForEach statt For-Schleifen, wenn Sie Arrays oder Objekte verarbeiten
|
||||
- **Behandeln Sie Fehler angemessen**: Erwägen Sie, Fehlerbehandlung innerhalb von Schleifen einzubauen, um robuste Workflows zu gewährleisten
|
||||
- **Vernünftige Grenzen setzen**: Halten Sie die Anzahl der Iterationen in einem vernünftigen Rahmen, um lange Ausführungszeiten zu vermeiden
|
||||
- **ForEach für Sammlungen verwenden**: Verwenden Sie ForEach statt For-Schleifen, wenn Sie Arrays oder Objekte verarbeiten
|
||||
- **Fehler elegant behandeln**: Erwägen Sie, Fehlerbehandlung innerhalb von Schleifen einzubauen, um robuste Workflows zu gewährleisten
|
||||
|
||||
123
apps/docs/content/docs/de/blocks/variables.mdx
Normal file
123
apps/docs/content/docs/de/blocks/variables.mdx
Normal file
@@ -0,0 +1,123 @@
|
||||
---
|
||||
title: Variablen
|
||||
---
|
||||
|
||||
import { Callout } from 'fumadocs-ui/components/callout'
|
||||
import { Step, Steps } from 'fumadocs-ui/components/steps'
|
||||
import { Image } from '@/components/ui/image'
|
||||
|
||||
Der Variablen-Block aktualisiert Workflow-Variablen während der Ausführung. Variablen müssen zuerst im Variablen-Bereich deines Workflows initialisiert werden, dann kannst du diesen Block verwenden, um ihre Werte während der Ausführung deines Workflows zu aktualisieren.
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/variables.png"
|
||||
alt="Variablen-Block"
|
||||
width={500}
|
||||
height={350}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Callout>
|
||||
Greife überall in deinem Workflow auf Variablen zu, indem du die `<variable.variableName>` Syntax verwendest.
|
||||
</Callout>
|
||||
|
||||
## Überblick
|
||||
|
||||
Der Variablen-Block ermöglicht dir:
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
<strong>Workflow-Variablen aktualisieren</strong>: Ändere Variablenwerte während der Ausführung
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Dynamische Daten speichern</strong>: Erfasse Block-Ausgaben in Variablen
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Zustand beibehalten</strong>: Verfolge Zähler, Flags und Zwischenergebnisse
|
||||
</Step>
|
||||
</Steps>
|
||||
|
||||
## Wie man Variablen verwendet
|
||||
|
||||
### 1. Im Workflow-Variablenbereich initialisieren
|
||||
|
||||
Erstelle zunächst deine Variablen im Variablen-Bereich des Workflows (zugänglich über die Workflow-Einstellungen):
|
||||
|
||||
```
|
||||
customerEmail = ""
|
||||
retryCount = 0
|
||||
currentStatus = "pending"
|
||||
```
|
||||
|
||||
### 2. Mit dem Variablen-Block aktualisieren
|
||||
|
||||
Verwende den Variablen-Block, um diese Werte während der Ausführung zu aktualisieren:
|
||||
|
||||
```
|
||||
customerEmail = <api.email>
|
||||
retryCount = <variable.retryCount> + 1
|
||||
currentStatus = "processing"
|
||||
```
|
||||
|
||||
### 3. Überall zugreifen
|
||||
|
||||
Referenziere Variablen in jedem Block:
|
||||
|
||||
```
|
||||
Agent prompt: "Send email to <variable.customerEmail>"
|
||||
Condition: <variable.retryCount> < 5
|
||||
API body: {"status": "<variable.currentStatus>"}
|
||||
```
|
||||
|
||||
## Beispielanwendungsfälle
|
||||
|
||||
### Schleifenzähler und Zustand
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Szenario: Fortschritt durch Schleifeniterationen verfolgen</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Im Workflow initialisieren: `itemsProcessed = 0`, `lastResult = ""`</li>
|
||||
<li>Schleife iteriert über Elemente</li>
|
||||
<li>Innerhalb der Schleife: Agent verarbeitet aktuelles Element</li>
|
||||
<li>Innerhalb der Schleife: Variablen aktualisieren `itemsProcessed = <variable.itemsProcessed> + 1`</li>
|
||||
<li>Innerhalb der Schleife: Variablen aktualisieren `lastResult = <agent.content>`</li>
|
||||
<li>Nächste Iteration: Auf `<variable.lastResult>` zugreifen, um mit aktuellem Ergebnis zu vergleichen</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Wiederholungslogik
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Szenario: API-Wiederholungsversuche verfolgen</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Im Workflow initialisieren: `retryCount = 0`</li>
|
||||
<li>API-Block versucht Anfrage</li>
|
||||
<li>Bei Fehlschlag erhöht Variablen: `retryCount = <variable.retryCount> + 1`</li>
|
||||
<li>Bedingung prüft, ob `<variable.retryCount>` \< 3 ist, um zu wiederholen oder abzubrechen</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Dynamische Konfiguration
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Szenario: Benutzerkontext für Workflow speichern</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Im Workflow initialisieren: `userId = ""`, `userTier = ""`</li>
|
||||
<li>API ruft Benutzerprofil ab</li>
|
||||
<li>Variablen speichern: `userId = <api.id>`, `userTier = <api.tier>`</li>
|
||||
<li>Agent personalisiert Antwort mit `<variable.userTier>`</li>
|
||||
<li>API verwendet `<variable.userId>` für Logging</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
## Ausgaben
|
||||
|
||||
- **`<variables.assignments>`**: JSON-Objekt mit allen Variablenzuweisungen aus diesem Block
|
||||
|
||||
## Bewährte Praktiken
|
||||
|
||||
- **Im Workflow-Einstellungen initialisieren**: Erstellen Sie Variablen immer im Workflow-Variablenbereich, bevor Sie sie verwenden
|
||||
- **Dynamisch aktualisieren**: Verwenden Sie Variablenblöcke, um Werte basierend auf Blockausgaben oder Berechnungen zu aktualisieren
|
||||
- **In Schleifen verwenden**: Perfekt für die Zustandsverfolgung über Iterationen hinweg
|
||||
- **Beschreibend benennen**: Verwenden Sie klare Namen wie `currentIndex`, `totalProcessed` oder `lastError`
|
||||
99
apps/docs/content/docs/de/blocks/wait.mdx
Normal file
99
apps/docs/content/docs/de/blocks/wait.mdx
Normal file
@@ -0,0 +1,99 @@
|
||||
---
|
||||
title: Warten
|
||||
---
|
||||
|
||||
import { Callout } from 'fumadocs-ui/components/callout'
|
||||
import { Step, Steps } from 'fumadocs-ui/components/steps'
|
||||
import { Image } from '@/components/ui/image'
|
||||
|
||||
Der Warten-Block pausiert deinen Workflow für eine bestimmte Zeit, bevor er mit dem nächsten Block fortfährt. Verwende ihn, um Verzögerungen zwischen Aktionen einzufügen, API-Ratenbegrenzungen einzuhalten oder Operationen zeitlich zu verteilen.
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/wait.png"
|
||||
alt="Warten-Block"
|
||||
width={500}
|
||||
height={350}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
## Übersicht
|
||||
|
||||
Mit dem Warten-Block kannst du:
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
<strong>Zeitverzögerungen hinzufügen</strong>: Ausführung zwischen Workflow-Schritten pausieren
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Ratenbegrenzungen einhalten</strong>: API-Aufrufe zeitlich verteilen, um innerhalb der Limits zu bleiben
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Sequenzen planen</strong>: Zeitgesteuerte Workflows mit Verzögerungen zwischen Aktionen erstellen
|
||||
</Step>
|
||||
</Steps>
|
||||
|
||||
## Konfiguration
|
||||
|
||||
### Wartezeit
|
||||
|
||||
Gib die Dauer der Ausführungspause ein:
|
||||
- **Eingabe**: Positive Zahl
|
||||
- **Maximum**: 600 Sekunden (10 Minuten) oder 10 Minuten
|
||||
|
||||
### Einheit
|
||||
|
||||
Wähle die Zeiteinheit:
|
||||
- **Sekunden**: Für kurze, präzise Verzögerungen
|
||||
- **Minuten**: Für längere Pausen
|
||||
|
||||
<Callout type="info">
|
||||
Warten-Blöcke können durch Stoppen des Workflows abgebrochen werden. Die maximale Wartezeit beträgt 10 Minuten.
|
||||
</Callout>
|
||||
|
||||
## Ausgaben
|
||||
|
||||
- **`<wait.waitDuration>`**: Die Wartezeit in Millisekunden
|
||||
- **`<wait.status>`**: Status des Wartens ('waiting', 'completed' oder 'cancelled')
|
||||
|
||||
## Beispielanwendungsfälle
|
||||
|
||||
### API-Ratenbegrenzung
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Szenario: Innerhalb der API-Ratenbegrenzungen bleiben</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>API-Block macht erste Anfrage</li>
|
||||
<li>Warten-Block pausiert für 2 Sekunden</li>
|
||||
<li>API-Block macht zweite Anfrage</li>
|
||||
<li>Prozess läuft weiter, ohne Ratenbegrenzungen zu überschreiten</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Zeitgesteuerte Benachrichtigungen
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Szenario: Folgenachrichten senden</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Funktion sendet erste E-Mail</li>
|
||||
<li>Warten-Block pausiert für 5 Minuten</li>
|
||||
<li>Funktion sendet Folge-E-Mail</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Verarbeitungsverzögerungen
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Szenario: Warten auf externes System</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>API-Block löst Job im externen System aus</li>
|
||||
<li>Warte-Block pausiert für 30 Sekunden</li>
|
||||
<li>API-Block prüft den Abschlussstatus des Jobs</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
## Bewährte Praktiken
|
||||
|
||||
- **Halte Wartezeiten angemessen**: Verwende Warten für Verzögerungen bis zu 10 Minuten. Für längere Verzögerungen solltest du geplante Workflows in Betracht ziehen
|
||||
- **Überwache die Ausführungszeit**: Denke daran, dass Wartezeiten die Gesamtdauer des Workflows verlängern
|
||||
@@ -207,18 +207,18 @@ Populate Clay with data from a JSON file. Enables direct communication and notif
|
||||
|
||||
#### Input
|
||||
|
||||
| Parameter | Type | Required | Description |
|
||||
| Parameter | Typ | Erforderlich | Beschreibung |
|
||||
| --------- | ---- | -------- | ----------- |
|
||||
| `webhookURL` | string | Yes | The webhook URL to populate |
|
||||
| `data` | json | Yes | The data to populate |
|
||||
| `authToken` | string | Yes | Auth token for Clay webhook authentication |
|
||||
| `webhookURL` | string | Ja | Die Webhook-URL, die befüllt werden soll |
|
||||
| `data` | json | Ja | Die Daten, die befüllt werden sollen |
|
||||
| `authToken` | string | Nein | Optionaler Auth-Token für die Clay-Webhook-Authentifizierung \(die meisten Webhooks benötigen dies nicht\) |
|
||||
|
||||
#### Output
|
||||
|
||||
| Parameter | Type | Description |
|
||||
| Parameter | Typ | Beschreibung |
|
||||
| --------- | ---- | ----------- |
|
||||
| `success` | boolean | Operation success status |
|
||||
| `output` | json | Clay populate operation results including response data from Clay webhook |
|
||||
| `data` | json | Antwortdaten vom Clay-Webhook |
|
||||
| `metadata` | object | Webhook-Antwort-Metadaten |
|
||||
|
||||
## Notes
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import { BlockInfoCard } from "@/components/ui/block-info-card"
|
||||
|
||||
<BlockInfoCard
|
||||
type="discord"
|
||||
color="#E0E0E0"
|
||||
color="#5865F2"
|
||||
icon={true}
|
||||
iconSvg={`<svg className="block-icon"
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ In Sim ermöglicht die OneDrive-Integration Ihren Agenten die direkte Interaktio
|
||||
|
||||
## Nutzungsanweisungen
|
||||
|
||||
Integriert OneDrive in den Workflow. Kann Dateien erstellen, hochladen und auflisten. Erfordert OAuth.
|
||||
OneDrive in den Workflow integrieren. Kann Text- und Excel-Dateien erstellen, Dateien hochladen und Dateien auflisten.
|
||||
|
||||
## Tools
|
||||
|
||||
@@ -63,10 +63,11 @@ Eine Datei auf OneDrive hochladen
|
||||
| Parameter | Typ | Erforderlich | Beschreibung |
|
||||
| --------- | ---- | -------- | ----------- |
|
||||
| `fileName` | string | Ja | Der Name der hochzuladenden Datei |
|
||||
| `file` | file | Nein | Die hochzuladende Datei (binär) |
|
||||
| `content` | string | Nein | Der hochzuladende Textinhalt (falls keine Datei bereitgestellt wird) |
|
||||
| `folderSelector` | string | Nein | Wählen Sie den Ordner aus, in den die Datei hochgeladen werden soll |
|
||||
| `manualFolderId` | string | Nein | Manuell eingegebene Ordner-ID (erweiterter Modus) |
|
||||
| `file` | file | Nein | Die hochzuladende Datei \(binär\) |
|
||||
| `content` | string | Nein | Der hochzuladende Textinhalt \(falls keine Datei bereitgestellt wird\) |
|
||||
| `mimeType` | string | Nein | Der MIME-Typ der zu erstellenden Datei \(z.B. text/plain für .txt, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet für .xlsx\) |
|
||||
| `folderSelector` | string | Nein | Ordner auswählen, in den die Datei hochgeladen werden soll |
|
||||
| `manualFolderId` | string | Nein | Manuell eingegebene Ordner-ID \(erweiterter Modus\) |
|
||||
|
||||
#### Ausgabe
|
||||
|
||||
|
||||
@@ -139,7 +139,8 @@ Suche nach ähnlichen Vektoren in einer Qdrant-Sammlung
|
||||
| `collection` | string | Ja | Sammlungsname |
|
||||
| `vector` | array | Ja | Zu suchender Vektor |
|
||||
| `limit` | number | Nein | Anzahl der zurückzugebenden Ergebnisse |
|
||||
| `filter` | object | Nein | Filter für die Suche |
|
||||
| `filter` | object | Nein | Auf die Suche anzuwendender Filter |
|
||||
| `search_return_data` | string | Nein | Aus der Suche zurückzugebende Daten |
|
||||
| `with_payload` | boolean | Nein | Payload in Antwort einschließen |
|
||||
| `with_vector` | boolean | Nein | Vektor in Antwort einschließen |
|
||||
|
||||
@@ -161,7 +162,8 @@ Punkte anhand der ID aus einer Qdrant-Sammlung abrufen
|
||||
| `url` | string | Ja | Qdrant-Basis-URL |
|
||||
| `apiKey` | string | Nein | Qdrant-API-Schlüssel \(optional\) |
|
||||
| `collection` | string | Ja | Sammlungsname |
|
||||
| `ids` | array | Ja | Array von abzurufenden Punkt-IDs |
|
||||
| `ids` | array | Ja | Array von Punkt-IDs zum Abrufen |
|
||||
| `fetch_return_data` | string | Nein | Aus dem Abruf zurückzugebende Daten |
|
||||
| `with_payload` | boolean | Nein | Payload in Antwort einschließen |
|
||||
| `with_vector` | boolean | Nein | Vektor in Antwort einschließen |
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ Blocks are the building components you connect together to create AI workflows.
|
||||
|
||||
## Core Block Types
|
||||
|
||||
Sim provides seven core block types that handle the essential functions of AI workflows:
|
||||
Sim provides essential block types that handle the core functions of AI workflows:
|
||||
|
||||
### Processing Blocks
|
||||
- **[Agent](/blocks/agent)** - Chat with AI models (OpenAI, Anthropic, Google, local models)
|
||||
@@ -28,6 +28,10 @@ Sim provides seven core block types that handle the essential functions of AI wo
|
||||
- **[Router](/blocks/router)** - Use AI to intelligently route requests to different paths
|
||||
- **[Evaluator](/blocks/evaluator)** - Score and assess content quality using AI
|
||||
|
||||
### Control Flow Blocks
|
||||
- **[Variables](/blocks/variables)** - Set and manage workflow-scoped variables
|
||||
- **[Wait](/blocks/wait)** - Pause workflow execution for a specified time delay
|
||||
|
||||
### Output Blocks
|
||||
- **[Response](/blocks/response)** - Format and return final results from your workflow
|
||||
|
||||
@@ -123,4 +127,10 @@ Each block type has specific configuration options:
|
||||
<Card title="Condition Block" href="/blocks/condition">
|
||||
Create branching logic based on data evaluation
|
||||
</Card>
|
||||
<Card title="Variables Block" href="/blocks/variables">
|
||||
Set and manage workflow-scoped variables
|
||||
</Card>
|
||||
<Card title="Wait Block" href="/blocks/wait">
|
||||
Pause workflow execution for specified time delays
|
||||
</Card>
|
||||
</Cards>
|
||||
|
||||
@@ -9,7 +9,7 @@ import { Image } from '@/components/ui/image'
|
||||
|
||||
The Loop block is a container block in Sim that allows you to create iterative workflows by executing a group of blocks repeatedly. Loops enable iterative processing in your workflows.
|
||||
|
||||
The Loop block supports two types of iteration:
|
||||
The Loop block supports four types of iteration:
|
||||
|
||||
<Callout type="info">
|
||||
Loop blocks are container nodes that can hold other blocks inside them. The blocks inside a loop will execute multiple times based on your configuration.
|
||||
@@ -27,7 +27,7 @@ The Loop block enables you to:
|
||||
<strong>Repeat operations</strong>: Execute blocks a fixed number of times
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Sequential processing</strong>: Handle data transformation in ordered iterations
|
||||
<strong>Loop on conditions</strong>: Continue executing while or until a condition is met
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Aggregate results</strong>: Collect outputs from all loop iterations
|
||||
@@ -47,9 +47,9 @@ The Loop block executes contained blocks through sequential iteration:
|
||||
|
||||
### Loop Type
|
||||
|
||||
Choose between two types of loops:
|
||||
Choose between four types of loops:
|
||||
|
||||
<Tabs items={['For Loop', 'ForEach Loop']}>
|
||||
<Tabs items={['For Loop', 'ForEach Loop', 'While Loop', 'Do-While Loop']}>
|
||||
<Tab>
|
||||
**For Loop (Iterations)** - A numeric loop that executes a fixed number of times:
|
||||
|
||||
@@ -96,6 +96,54 @@ Choose between two types of loops:
|
||||
- Iteration 3: Process "orange"
|
||||
```
|
||||
</Tab>
|
||||
<Tab>
|
||||
**While Loop (Condition-based)** - Continues executing while a condition evaluates to true:
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/loop-3.png"
|
||||
alt="While Loop with condition"
|
||||
width={500}
|
||||
height={400}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
Use this when you need to loop until a specific condition is met. The condition is checked **before** each iteration.
|
||||
|
||||
```
|
||||
Example: While <variable.i> < 10
|
||||
- Check condition → Execute if true
|
||||
- Inside loop: Increment <variable.i>
|
||||
- Inside loop: Variables assigns i = <variable.i> + 1
|
||||
- Check condition → Execute if true
|
||||
- Check condition → Exit if false
|
||||
```
|
||||
</Tab>
|
||||
<Tab>
|
||||
**Do-While Loop (Condition-based)** - Executes at least once, then continues while a condition is true:
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/loop-3.png"
|
||||
alt="Do-While Loop with condition"
|
||||
width={500}
|
||||
height={400}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
Use this when you need to execute at least once, then loop until a condition is met. The condition is checked **after** each iteration.
|
||||
|
||||
```
|
||||
Example: Do-while <variable.i> < 10
|
||||
- Execute blocks
|
||||
- Inside loop: Increment <variable.i>
|
||||
- Inside loop: Variables assigns i = <variable.i> + 1
|
||||
- Check condition → Continue if true
|
||||
- Check condition → Exit if false
|
||||
```
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
## How to Use Loops
|
||||
@@ -139,6 +187,19 @@ After a loop completes, you can access aggregated results:
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Counter with While Loop
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Scenario: Process items with counter-based loop</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Initialize workflow variable: `i = 0`</li>
|
||||
<li>While loop with condition: `<variable.i>` \< 10</li>
|
||||
<li>Inside loop: Agent processes item at index `<variable.i>`</li>
|
||||
<li>Inside loop: Variables increments `i = <variable.i> + 1`</li>
|
||||
<li>Loop continues while i is less than 10</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
## Advanced Features
|
||||
|
||||
### Limitations
|
||||
@@ -162,7 +223,7 @@ After a loop completes, you can access aggregated results:
|
||||
<Tab>
|
||||
<ul className="list-disc space-y-2 pl-6">
|
||||
<li>
|
||||
<strong>Loop Type</strong>: Choose between 'for' or 'forEach'
|
||||
<strong>Loop Type</strong>: Choose between 'for', 'forEach', 'while', or 'doWhile'
|
||||
</li>
|
||||
<li>
|
||||
<strong>Iterations</strong>: Number of times to execute (for loops)
|
||||
@@ -170,6 +231,9 @@ After a loop completes, you can access aggregated results:
|
||||
<li>
|
||||
<strong>Collection</strong>: Array or object to iterate over (forEach loops)
|
||||
</li>
|
||||
<li>
|
||||
<strong>Condition</strong>: Boolean expression to evaluate (while/do-while loops)
|
||||
</li>
|
||||
</ul>
|
||||
</Tab>
|
||||
<Tab>
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
"parallel",
|
||||
"response",
|
||||
"router",
|
||||
"variables",
|
||||
"wait",
|
||||
"workflow"
|
||||
]
|
||||
}
|
||||
|
||||
123
apps/docs/content/docs/en/blocks/variables.mdx
Normal file
123
apps/docs/content/docs/en/blocks/variables.mdx
Normal file
@@ -0,0 +1,123 @@
|
||||
---
|
||||
title: Variables
|
||||
---
|
||||
|
||||
import { Callout } from 'fumadocs-ui/components/callout'
|
||||
import { Step, Steps } from 'fumadocs-ui/components/steps'
|
||||
import { Image } from '@/components/ui/image'
|
||||
|
||||
The Variables block updates workflow variables during execution. Variables must first be initialized in your workflow's Variables section, then you can use this block to update their values as your workflow runs.
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/variables.png"
|
||||
alt="Variables Block"
|
||||
width={500}
|
||||
height={350}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Callout>
|
||||
Access variables anywhere in your workflow using `<variable.variableName>` syntax.
|
||||
</Callout>
|
||||
|
||||
## Overview
|
||||
|
||||
The Variables block enables you to:
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
<strong>Update workflow variables</strong>: Change variable values during execution
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Store dynamic data</strong>: Capture block outputs into variables
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Maintain state</strong>: Track counters, flags, and intermediate results
|
||||
</Step>
|
||||
</Steps>
|
||||
|
||||
## How to Use Variables
|
||||
|
||||
### 1. Initialize in Workflow Variables
|
||||
|
||||
First, create your variables in the workflow's Variables section (accessible from the workflow settings):
|
||||
|
||||
```
|
||||
customerEmail = ""
|
||||
retryCount = 0
|
||||
currentStatus = "pending"
|
||||
```
|
||||
|
||||
### 2. Update with Variables Block
|
||||
|
||||
Use the Variables block to update these values during execution:
|
||||
|
||||
```
|
||||
customerEmail = <api.email>
|
||||
retryCount = <variable.retryCount> + 1
|
||||
currentStatus = "processing"
|
||||
```
|
||||
|
||||
### 3. Access Anywhere
|
||||
|
||||
Reference variables in any block:
|
||||
|
||||
```
|
||||
Agent prompt: "Send email to <variable.customerEmail>"
|
||||
Condition: <variable.retryCount> < 5
|
||||
API body: {"status": "<variable.currentStatus>"}
|
||||
```
|
||||
|
||||
## Example Use Cases
|
||||
|
||||
### Loop Counter and State
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Scenario: Track progress through loop iterations</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Initialize in workflow: `itemsProcessed = 0`, `lastResult = ""`</li>
|
||||
<li>Loop iterates over items</li>
|
||||
<li>Inside loop: Agent processes current item</li>
|
||||
<li>Inside loop: Variables updates `itemsProcessed = <variable.itemsProcessed> + 1`</li>
|
||||
<li>Inside loop: Variables updates `lastResult = <agent.content>`</li>
|
||||
<li>Next iteration: Access `<variable.lastResult>` to compare with current result</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Retry Logic
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Scenario: Track API retry attempts</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Initialize in workflow: `retryCount = 0`</li>
|
||||
<li>API block attempts request</li>
|
||||
<li>If failed, Variables increments: `retryCount = <variable.retryCount> + 1`</li>
|
||||
<li>Condition checks if `<variable.retryCount>` \< 3 to retry or fail</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Dynamic Configuration
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Scenario: Store user context for workflow</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Initialize in workflow: `userId = ""`, `userTier = ""`</li>
|
||||
<li>API fetches user profile</li>
|
||||
<li>Variables stores: `userId = <api.id>`, `userTier = <api.tier>`</li>
|
||||
<li>Agent personalizes response using `<variable.userTier>`</li>
|
||||
<li>API uses `<variable.userId>` for logging</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
## Outputs
|
||||
|
||||
- **`<variables.assignments>`**: JSON object with all variable assignments from this block
|
||||
|
||||
## Best Practices
|
||||
|
||||
- **Initialize in workflow settings**: Always create variables in the workflow Variables section before using them
|
||||
- **Update dynamically**: Use Variables blocks to update values based on block outputs or calculations
|
||||
- **Use in loops**: Perfect for tracking state across iterations
|
||||
- **Name descriptively**: Use clear names like `currentIndex`, `totalProcessed`, or `lastError`
|
||||
99
apps/docs/content/docs/en/blocks/wait.mdx
Normal file
99
apps/docs/content/docs/en/blocks/wait.mdx
Normal file
@@ -0,0 +1,99 @@
|
||||
---
|
||||
title: Wait
|
||||
---
|
||||
|
||||
import { Callout } from 'fumadocs-ui/components/callout'
|
||||
import { Step, Steps } from 'fumadocs-ui/components/steps'
|
||||
import { Image } from '@/components/ui/image'
|
||||
|
||||
The Wait block pauses your workflow for a specified amount of time before continuing to the next block. Use it to add delays between actions, respect API rate limits, or space out operations.
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/wait.png"
|
||||
alt="Wait Block"
|
||||
width={500}
|
||||
height={350}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
The Wait block enables you to:
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
<strong>Add time delays</strong>: Pause execution between workflow steps
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Respect rate limits</strong>: Space out API calls to stay within limits
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Schedule sequences</strong>: Create timed workflows with delays between actions
|
||||
</Step>
|
||||
</Steps>
|
||||
|
||||
## Configuration
|
||||
|
||||
### Wait Amount
|
||||
|
||||
Enter the duration to pause execution:
|
||||
- **Input**: Positive number
|
||||
- **Maximum**: 600 seconds (10 minutes) or 10 minutes
|
||||
|
||||
### Unit
|
||||
|
||||
Choose the time unit:
|
||||
- **Seconds**: For short, precise delays
|
||||
- **Minutes**: For longer pauses
|
||||
|
||||
<Callout type="info">
|
||||
Wait blocks can be cancelled by stopping the workflow. The maximum wait time is 10 minutes.
|
||||
</Callout>
|
||||
|
||||
## Outputs
|
||||
|
||||
- **`<wait.waitDuration>`**: The wait duration in milliseconds
|
||||
- **`<wait.status>`**: Status of the wait ('waiting', 'completed', or 'cancelled')
|
||||
|
||||
## Example Use Cases
|
||||
|
||||
### API Rate Limiting
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Scenario: Stay within API rate limits</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>API block makes first request</li>
|
||||
<li>Wait block pauses for 2 seconds</li>
|
||||
<li>API block makes second request</li>
|
||||
<li>Process continues without hitting rate limits</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Timed Notifications
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Scenario: Send follow-up messages</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Function sends initial email</li>
|
||||
<li>Wait block pauses for 5 minutes</li>
|
||||
<li>Function sends follow-up email</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Processing Delays
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Scenario: Wait for external system</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>API block triggers job in external system</li>
|
||||
<li>Wait block pauses for 30 seconds</li>
|
||||
<li>API block checks job completion status</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
## Best Practices
|
||||
|
||||
- **Keep waits reasonable**: Use Wait for delays up to 10 minutes. For longer delays, consider scheduled workflows
|
||||
- **Monitor execution time**: Remember that waits extend total workflow duration
|
||||
@@ -214,14 +214,14 @@ Populate Clay with data from a JSON file. Enables direct communication and notif
|
||||
| --------- | ---- | -------- | ----------- |
|
||||
| `webhookURL` | string | Yes | The webhook URL to populate |
|
||||
| `data` | json | Yes | The data to populate |
|
||||
| `authToken` | string | Yes | Auth token for Clay webhook authentication |
|
||||
| `authToken` | string | No | Optional auth token for Clay webhook authentication \(most webhooks do not require this\) |
|
||||
|
||||
#### Output
|
||||
|
||||
| Parameter | Type | Description |
|
||||
| --------- | ---- | ----------- |
|
||||
| `success` | boolean | Operation success status |
|
||||
| `output` | json | Clay populate operation results including response data from Clay webhook |
|
||||
| `data` | json | Response data from Clay webhook |
|
||||
| `metadata` | object | Webhook response metadata |
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import { BlockInfoCard } from "@/components/ui/block-info-card"
|
||||
|
||||
<BlockInfoCard
|
||||
type="discord"
|
||||
color="#E0E0E0"
|
||||
color="#5865F2"
|
||||
icon={true}
|
||||
iconSvg={`<svg className="block-icon"
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ In Sim, the OneDrive integration enables your agents to directly interact with y
|
||||
|
||||
## Usage Instructions
|
||||
|
||||
Integrate OneDrive into the workflow. Can create, upload, and list files.
|
||||
Integrate OneDrive into the workflow. Can create text and Excel files, upload files, and list files.
|
||||
|
||||
|
||||
|
||||
@@ -68,6 +68,7 @@ Upload a file to OneDrive
|
||||
| `fileName` | string | Yes | The name of the file to upload |
|
||||
| `file` | file | No | The file to upload \(binary\) |
|
||||
| `content` | string | No | The text content to upload \(if no file is provided\) |
|
||||
| `mimeType` | string | No | The MIME type of the file to create \(e.g., text/plain for .txt, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet for .xlsx\) |
|
||||
| `folderSelector` | string | No | Select the folder to upload the file to |
|
||||
| `manualFolderId` | string | No | Manually entered folder ID \(advanced mode\) |
|
||||
|
||||
|
||||
@@ -143,6 +143,7 @@ Search for similar vectors in a Qdrant collection
|
||||
| `vector` | array | Yes | Vector to search for |
|
||||
| `limit` | number | No | Number of results to return |
|
||||
| `filter` | object | No | Filter to apply to the search |
|
||||
| `search_return_data` | string | No | Data to return from search |
|
||||
| `with_payload` | boolean | No | Include payload in response |
|
||||
| `with_vector` | boolean | No | Include vector in response |
|
||||
|
||||
@@ -165,6 +166,7 @@ Fetch points by ID from a Qdrant collection
|
||||
| `apiKey` | string | No | Qdrant API key \(optional\) |
|
||||
| `collection` | string | Yes | Collection name |
|
||||
| `ids` | array | Yes | Array of point IDs to fetch |
|
||||
| `fetch_return_data` | string | No | Data to return from fetch |
|
||||
| `with_payload` | boolean | No | Include payload in response |
|
||||
| `with_vector` | boolean | No | Include vector in response |
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ Los bloques son los componentes de construcción que conectas para crear flujos
|
||||
|
||||
## Tipos de bloques principales
|
||||
|
||||
Sim proporciona siete tipos de bloques principales que manejan las funciones esenciales de los flujos de trabajo de IA:
|
||||
Sim proporciona tipos de bloques esenciales que manejan las funciones principales de los flujos de trabajo de IA:
|
||||
|
||||
### Bloques de procesamiento
|
||||
- **[Agente](/blocks/agent)** - Chatea con modelos de IA (OpenAI, Anthropic, Google, modelos locales)
|
||||
@@ -28,14 +28,18 @@ Sim proporciona siete tipos de bloques principales que manejan las funciones ese
|
||||
- **[Enrutador](/blocks/router)** - Usa IA para dirigir inteligentemente las solicitudes a diferentes caminos
|
||||
- **[Evaluador](/blocks/evaluator)** - Puntúa y evalúa la calidad del contenido usando IA
|
||||
|
||||
### Bloques de flujo de control
|
||||
- **[Variables](/blocks/variables)** - Establecer y gestionar variables con alcance de flujo de trabajo
|
||||
- **[Espera](/blocks/wait)** - Pausar la ejecución del flujo de trabajo durante un tiempo específico
|
||||
|
||||
### Bloques de salida
|
||||
- **[Respuesta](/blocks/response)** - Formatea y devuelve resultados finales de tu flujo de trabajo
|
||||
- **[Respuesta](/blocks/response)** - Formatear y devolver resultados finales desde tu flujo de trabajo
|
||||
|
||||
## Cómo funcionan los bloques
|
||||
|
||||
Cada bloque tiene tres componentes principales:
|
||||
|
||||
**Entradas**: Datos que llegan al bloque desde otros bloques o entrada del usuario
|
||||
**Entradas**: Datos que ingresan al bloque desde otros bloques o entrada del usuario
|
||||
**Configuración**: Ajustes que controlan cómo se comporta el bloque
|
||||
**Salidas**: Datos que el bloque produce para que otros bloques los utilicen
|
||||
|
||||
@@ -55,8 +59,8 @@ Cada bloque tiene tres componentes principales:
|
||||
|
||||
Creas flujos de trabajo conectando bloques entre sí. La salida de un bloque se convierte en la entrada de otro:
|
||||
|
||||
- **Arrastra para conectar**: Arrastra desde un puerto de salida a un puerto de entrada
|
||||
- **Conexiones múltiples**: Una salida puede conectarse a múltiples entradas
|
||||
- **Arrastrar para conectar**: Arrastra desde un puerto de salida a un puerto de entrada
|
||||
- **Múltiples conexiones**: Una salida puede conectarse a múltiples entradas
|
||||
- **Rutas ramificadas**: Algunos bloques pueden dirigir a diferentes rutas según las condiciones
|
||||
|
||||
<div className="w-full max-w-2xl mx-auto overflow-hidden rounded-lg">
|
||||
@@ -110,7 +114,7 @@ Cada tipo de bloque tiene opciones de configuración específicas:
|
||||
|
||||
**Bloques de integración** (API, Respuesta):
|
||||
- Configuración de endpoint
|
||||
- Cabeceras y autenticación
|
||||
- Encabezados y autenticación
|
||||
- Formato de solicitud/respuesta
|
||||
|
||||
<Cards>
|
||||
@@ -126,4 +130,10 @@ Cada tipo de bloque tiene opciones de configuración específicas:
|
||||
<Card title="Bloque de condición" href="/blocks/condition">
|
||||
Crea lógica de ramificación basada en evaluación de datos
|
||||
</Card>
|
||||
<Card title="Bloque de variables" href="/blocks/variables">
|
||||
Establece y gestiona variables con alcance de flujo de trabajo
|
||||
</Card>
|
||||
<Card title="Bloque de espera" href="/blocks/wait">
|
||||
Pausa la ejecución del flujo de trabajo durante retrasos de tiempo específicos
|
||||
</Card>
|
||||
</Cards>
|
||||
|
||||
@@ -9,7 +9,7 @@ import { Image } from '@/components/ui/image'
|
||||
|
||||
El bloque Loop es un bloque contenedor en Sim que permite crear flujos de trabajo iterativos ejecutando un grupo de bloques repetidamente. Los bucles permiten el procesamiento iterativo en tus flujos de trabajo.
|
||||
|
||||
El bloque Loop admite dos tipos de iteración:
|
||||
El bloque Loop admite cuatro tipos de iteración:
|
||||
|
||||
<Callout type="info">
|
||||
Los bloques Loop son nodos contenedores que pueden albergar otros bloques dentro de ellos. Los bloques dentro de un bucle se ejecutarán múltiples veces según tu configuración.
|
||||
@@ -27,7 +27,7 @@ El bloque Loop te permite:
|
||||
<strong>Repetir operaciones</strong>: Ejecutar bloques un número fijo de veces
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Procesamiento secuencial</strong>: Manejar transformación de datos en iteraciones ordenadas
|
||||
<strong>Bucles con condiciones</strong>: Continuar ejecutando mientras o hasta que se cumpla una condición
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Agregar resultados</strong>: Recopilar salidas de todas las iteraciones del bucle
|
||||
@@ -47,11 +47,11 @@ El bloque Loop ejecuta los bloques contenidos a través de iteración secuencial
|
||||
|
||||
### Tipo de bucle
|
||||
|
||||
Elige entre dos tipos de bucles:
|
||||
Elige entre cuatro tipos de bucles:
|
||||
|
||||
<Tabs items={['For Loop', 'ForEach Loop']}>
|
||||
<Tabs items={['For Loop', 'ForEach Loop', 'While Loop', 'Do-While Loop']}>
|
||||
<Tab>
|
||||
**For Loop (Iteraciones)** - Un bucle numérico que se ejecuta un número fijo de veces:
|
||||
**Bucle For (Iteraciones)** - Un bucle numérico que se ejecuta un número fijo de veces:
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
@@ -100,13 +100,65 @@ Elige entre dos tipos de bucles:
|
||||
```
|
||||
|
||||
</Tab>
|
||||
<Tab>
|
||||
**Bucle While (Basado en condición)** - Continúa ejecutándose mientras una condición se evalúe como verdadera:
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/loop-3.png"
|
||||
alt="Bucle While con condición"
|
||||
width={500}
|
||||
height={400}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
Úsalo cuando necesites hacer un bucle hasta que se cumpla una condición específica. La condición se comprueba **antes** de cada iteración.
|
||||
|
||||
|
||||
```
|
||||
Example: While <variable.i> < 10
|
||||
- Check condition → Execute if true
|
||||
- Inside loop: Increment <variable.i>
|
||||
- Inside loop: Variables assigns i = <variable.i> + 1
|
||||
- Check condition → Execute if true
|
||||
- Check condition → Exit if false
|
||||
```
|
||||
|
||||
</Tab>
|
||||
<Tab>
|
||||
**Bucle Do-While (Basado en condición)** - Se ejecuta al menos una vez, luego continúa mientras una condición sea verdadera:
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/loop-3.png"
|
||||
alt="Bucle Do-While con condición"
|
||||
width={500}
|
||||
height={400}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
Úsalo cuando necesites ejecutar al menos una vez, luego hacer un bucle hasta que se cumpla una condición. La condición se comprueba **después** de cada iteración.
|
||||
|
||||
|
||||
```
|
||||
Example: Do-while <variable.i> < 10
|
||||
- Execute blocks
|
||||
- Inside loop: Increment <variable.i>
|
||||
- Inside loop: Variables assigns i = <variable.i> + 1
|
||||
- Check condition → Continue if true
|
||||
- Check condition → Exit if false
|
||||
```
|
||||
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
## Cómo usar los bucles
|
||||
|
||||
### Creación de un bucle
|
||||
### Creando un bucle
|
||||
|
||||
1. Arrastra un bloque de bucle desde la barra de herramientas a tu lienzo
|
||||
1. Arrastra un bloque Loop desde la barra de herramientas a tu lienzo
|
||||
2. Configura el tipo de bucle y los parámetros
|
||||
3. Arrastra otros bloques dentro del contenedor del bucle
|
||||
4. Conecta los bloques según sea necesario
|
||||
@@ -136,19 +188,32 @@ Después de que un bucle se completa, puedes acceder a los resultados agregados:
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Escenario: Generar múltiples variaciones</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Configurar el bucle For para 5 iteraciones</li>
|
||||
<li>Configurar bucle For para 5 iteraciones</li>
|
||||
<li>Dentro del bucle: El agente genera una variación de contenido</li>
|
||||
<li>Dentro del bucle: El evaluador puntúa el contenido</li>
|
||||
<li>Después del bucle: La función selecciona la mejor variación</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Contador con bucle While
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Escenario: Procesar elementos con bucle basado en contador</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Inicializar variable de flujo de trabajo: `i = 0`</li>
|
||||
<li>Bucle While con condición: `<variable.i>` \< 10</li>
|
||||
<li>Dentro del bucle: El agente procesa el elemento en el índice `<variable.i>`</li>
|
||||
<li>Dentro del bucle: La variable incrementa `i = <variable.i> + 1`</li>
|
||||
<li>El bucle continúa mientras i sea menor que 10</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
## Características avanzadas
|
||||
|
||||
### Limitaciones
|
||||
|
||||
<Callout type="warning">
|
||||
Los bloques contenedores (Bucles y Paralelos) no pueden anidarse unos dentro de otros. Esto significa:
|
||||
Los bloques contenedores (Bucles y Paralelos) no pueden anidarse dentro de otros. Esto significa:
|
||||
- No puedes colocar un bloque de Bucle dentro de otro bloque de Bucle
|
||||
- No puedes colocar un bloque Paralelo dentro de un bloque de Bucle
|
||||
- No puedes colocar ningún bloque contenedor dentro de otro bloque contenedor
|
||||
@@ -157,7 +222,7 @@ Después de que un bucle se completa, puedes acceder a los resultados agregados:
|
||||
</Callout>
|
||||
|
||||
<Callout type="info">
|
||||
Los bucles se ejecutan secuencialmente, no en paralelo. Si necesitas ejecución concurrente, utiliza el bloque Paralelo en su lugar.
|
||||
Los bucles se ejecutan secuencialmente, no en paralelo. Si necesitas ejecución concurrente, usa el bloque Paralelo en su lugar.
|
||||
</Callout>
|
||||
|
||||
## Entradas y salidas
|
||||
@@ -166,7 +231,7 @@ Después de que un bucle se completa, puedes acceder a los resultados agregados:
|
||||
<Tab>
|
||||
<ul className="list-disc space-y-2 pl-6">
|
||||
<li>
|
||||
<strong>Tipo de bucle</strong>: Elige entre 'for' o 'forEach'
|
||||
<strong>Tipo de bucle</strong>: Elige entre 'for', 'forEach', 'while', o 'doWhile'
|
||||
</li>
|
||||
<li>
|
||||
<strong>Iteraciones</strong>: Número de veces a ejecutar (bucles for)
|
||||
@@ -174,6 +239,9 @@ Después de que un bucle se completa, puedes acceder a los resultados agregados:
|
||||
<li>
|
||||
<strong>Colección</strong>: Array u objeto sobre el que iterar (bucles forEach)
|
||||
</li>
|
||||
<li>
|
||||
<strong>Condición</strong>: Expresión booleana a evaluar (bucles while/do-while)
|
||||
</li>
|
||||
</ul>
|
||||
</Tab>
|
||||
<Tab>
|
||||
@@ -192,7 +260,7 @@ Después de que un bucle se completa, puedes acceder a los resultados agregados:
|
||||
<Tab>
|
||||
<ul className="list-disc space-y-2 pl-6">
|
||||
<li>
|
||||
<strong>loop.results</strong>: Array con todos los resultados de las iteraciones
|
||||
<strong>loop.results</strong>: Array de todos los resultados de iteración
|
||||
</li>
|
||||
<li>
|
||||
<strong>Estructura</strong>: Los resultados mantienen el orden de iteración
|
||||
@@ -206,6 +274,6 @@ Después de que un bucle se completa, puedes acceder a los resultados agregados:
|
||||
|
||||
## Mejores prácticas
|
||||
|
||||
- **Establece límites razonables**: Mantén un número razonable de iteraciones para evitar tiempos de ejecución largos
|
||||
- **Establece límites razonables**: Mantén el número de iteraciones razonable para evitar tiempos de ejecución largos
|
||||
- **Usa ForEach para colecciones**: Cuando proceses arrays u objetos, usa bucles ForEach en lugar de bucles For
|
||||
- **Maneja los errores con elegancia**: Considera añadir manejo de errores dentro de los bucles para flujos de trabajo robustos
|
||||
|
||||
123
apps/docs/content/docs/es/blocks/variables.mdx
Normal file
123
apps/docs/content/docs/es/blocks/variables.mdx
Normal file
@@ -0,0 +1,123 @@
|
||||
---
|
||||
title: Variables
|
||||
---
|
||||
|
||||
import { Callout } from 'fumadocs-ui/components/callout'
|
||||
import { Step, Steps } from 'fumadocs-ui/components/steps'
|
||||
import { Image } from '@/components/ui/image'
|
||||
|
||||
El bloque Variables actualiza las variables del flujo de trabajo durante la ejecución. Las variables deben inicializarse primero en la sección Variables de tu flujo de trabajo, luego puedes usar este bloque para actualizar sus valores mientras se ejecuta tu flujo de trabajo.
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/variables.png"
|
||||
alt="Bloque de Variables"
|
||||
width={500}
|
||||
height={350}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Callout>
|
||||
Accede a las variables en cualquier parte de tu flujo de trabajo usando la sintaxis `<variable.variableName>`.
|
||||
</Callout>
|
||||
|
||||
## Descripción general
|
||||
|
||||
El bloque Variables te permite:
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
<strong>Actualizar variables del flujo de trabajo</strong>: Cambiar valores de variables durante la ejecución
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Almacenar datos dinámicos</strong>: Capturar salidas de bloques en variables
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Mantener el estado</strong>: Seguir contadores, indicadores y resultados intermedios
|
||||
</Step>
|
||||
</Steps>
|
||||
|
||||
## Cómo usar Variables
|
||||
|
||||
### 1. Inicializar en Variables del flujo de trabajo
|
||||
|
||||
Primero, crea tus variables en la sección Variables del flujo de trabajo (accesible desde la configuración del flujo de trabajo):
|
||||
|
||||
```
|
||||
customerEmail = ""
|
||||
retryCount = 0
|
||||
currentStatus = "pending"
|
||||
```
|
||||
|
||||
### 2. Actualizar con el bloque Variables
|
||||
|
||||
Usa el bloque Variables para actualizar estos valores durante la ejecución:
|
||||
|
||||
```
|
||||
customerEmail = <api.email>
|
||||
retryCount = <variable.retryCount> + 1
|
||||
currentStatus = "processing"
|
||||
```
|
||||
|
||||
### 3. Acceder desde cualquier lugar
|
||||
|
||||
Referencia variables en cualquier bloque:
|
||||
|
||||
```
|
||||
Agent prompt: "Send email to <variable.customerEmail>"
|
||||
Condition: <variable.retryCount> < 5
|
||||
API body: {"status": "<variable.currentStatus>"}
|
||||
```
|
||||
|
||||
## Ejemplos de casos de uso
|
||||
|
||||
### Contador de bucle y estado
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Escenario: Seguimiento del progreso a través de iteraciones de bucle</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Inicializar en el flujo de trabajo: `itemsProcessed = 0`, `lastResult = ""`</li>
|
||||
<li>El bucle itera sobre elementos</li>
|
||||
<li>Dentro del bucle: El agente procesa el elemento actual</li>
|
||||
<li>Dentro del bucle: Variables actualiza `itemsProcessed = <variable.itemsProcessed> + 1`</li>
|
||||
<li>Dentro del bucle: Variables actualiza `lastResult = <agent.content>`</li>
|
||||
<li>Siguiente iteración: Accede a `<variable.lastResult>` para comparar con el resultado actual</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Lógica de reintentos
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Escenario: Seguimiento de intentos de reintento de API</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Inicializar en el flujo de trabajo: `retryCount = 0`</li>
|
||||
<li>El bloque API intenta realizar la solicitud</li>
|
||||
<li>Si falla, Variables incrementa: `retryCount = <variable.retryCount> + 1`</li>
|
||||
<li>La condición verifica si `<variable.retryCount>` \< 3 para reintentar o fallar</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Configuración dinámica
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Escenario: Almacenar contexto de usuario para el flujo de trabajo</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Inicializar en el flujo de trabajo: `userId = ""`, `userTier = ""`</li>
|
||||
<li>La API obtiene el perfil del usuario</li>
|
||||
<li>Variables almacena: `userId = <api.id>`, `userTier = <api.tier>`</li>
|
||||
<li>El agente personaliza la respuesta usando `<variable.userTier>`</li>
|
||||
<li>La API usa `<variable.userId>` para el registro</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
## Salidas
|
||||
|
||||
- **`<variables.assignments>`**: Objeto JSON con todas las asignaciones de variables de este bloque
|
||||
|
||||
## Mejores prácticas
|
||||
|
||||
- **Inicializar en la configuración del flujo de trabajo**: Siempre crea variables en la sección Variables del flujo de trabajo antes de usarlas
|
||||
- **Actualizar dinámicamente**: Usa bloques de Variables para actualizar valores basados en salidas de bloques o cálculos
|
||||
- **Usar en bucles**: Perfecto para rastrear el estado a través de iteraciones
|
||||
- **Nombrar descriptivamente**: Usa nombres claros como `currentIndex`, `totalProcessed`, o `lastError`
|
||||
99
apps/docs/content/docs/es/blocks/wait.mdx
Normal file
99
apps/docs/content/docs/es/blocks/wait.mdx
Normal file
@@ -0,0 +1,99 @@
|
||||
---
|
||||
title: Espera
|
||||
---
|
||||
|
||||
import { Callout } from 'fumadocs-ui/components/callout'
|
||||
import { Step, Steps } from 'fumadocs-ui/components/steps'
|
||||
import { Image } from '@/components/ui/image'
|
||||
|
||||
El bloque Espera pausa tu flujo de trabajo durante un tiempo específico antes de continuar con el siguiente bloque. Úsalo para añadir retrasos entre acciones, respetar límites de frecuencia de API o espaciar operaciones.
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/wait.png"
|
||||
alt="Bloque de Espera"
|
||||
width={500}
|
||||
height={350}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
## Descripción general
|
||||
|
||||
El bloque Espera te permite:
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
<strong>Añadir retrasos de tiempo</strong>: Pausar la ejecución entre pasos del flujo de trabajo
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Respetar límites de frecuencia</strong>: Espaciar llamadas a API para mantenerse dentro de los límites
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Programar secuencias</strong>: Crear flujos de trabajo temporizados con retrasos entre acciones
|
||||
</Step>
|
||||
</Steps>
|
||||
|
||||
## Configuración
|
||||
|
||||
### Cantidad de espera
|
||||
|
||||
Introduce la duración para pausar la ejecución:
|
||||
- **Entrada**: Número positivo
|
||||
- **Máximo**: 600 segundos (10 minutos) o 10 minutos
|
||||
|
||||
### Unidad
|
||||
|
||||
Elige la unidad de tiempo:
|
||||
- **Segundos**: Para retrasos cortos y precisos
|
||||
- **Minutos**: Para pausas más largas
|
||||
|
||||
<Callout type="info">
|
||||
Los bloques de espera pueden cancelarse deteniendo el flujo de trabajo. El tiempo máximo de espera es de 10 minutos.
|
||||
</Callout>
|
||||
|
||||
## Salidas
|
||||
|
||||
- **`<wait.waitDuration>`**: La duración de la espera en milisegundos
|
||||
- **`<wait.status>`**: Estado de la espera ('waiting', 'completed', o 'cancelled')
|
||||
|
||||
## Ejemplos de casos de uso
|
||||
|
||||
### Limitación de frecuencia de API
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Escenario: Mantenerse dentro de los límites de frecuencia de API</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>El bloque API realiza la primera solicitud</li>
|
||||
<li>El bloque Espera pausa durante 2 segundos</li>
|
||||
<li>El bloque API realiza la segunda solicitud</li>
|
||||
<li>El proceso continúa sin alcanzar los límites de frecuencia</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Notificaciones temporizadas
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Escenario: Enviar mensajes de seguimiento</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>La función envía el correo electrónico inicial</li>
|
||||
<li>El bloque Espera pausa durante 5 minutos</li>
|
||||
<li>La función envía el correo electrónico de seguimiento</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Retrasos de procesamiento
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Escenario: Espera para sistema externo</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>El bloque API activa un trabajo en el sistema externo</li>
|
||||
<li>El bloque de espera pausa durante 30 segundos</li>
|
||||
<li>El bloque API verifica el estado de finalización del trabajo</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
## Mejores prácticas
|
||||
|
||||
- **Mantén esperas razonables**: Usa Espera para retrasos de hasta 10 minutos. Para retrasos más largos, considera flujos de trabajo programados
|
||||
- **Monitorea el tiempo de ejecución**: Recuerda que las esperas extienden la duración total del flujo de trabajo
|
||||
@@ -207,18 +207,18 @@ Poblar Clay con datos de un archivo JSON. Permite comunicación directa y notifi
|
||||
|
||||
#### Entrada
|
||||
|
||||
| Parámetro | Tipo | Requerido | Descripción |
|
||||
| --------- | ---- | -------- | ----------- |
|
||||
| `webhookURL` | string | Sí | La URL del webhook para poblar |
|
||||
| `data` | json | Sí | Los datos para poblar |
|
||||
| `authToken` | string | Sí | Token de autenticación para la autenticación del webhook de Clay |
|
||||
| Parámetro | Tipo | Obligatorio | Descripción |
|
||||
| --------- | ---- | ----------- | ----------- |
|
||||
| `webhookURL` | string | Sí | La URL del webhook a completar |
|
||||
| `data` | json | Sí | Los datos para completar |
|
||||
| `authToken` | string | No | Token de autenticación opcional para la autenticación del webhook de Clay \(la mayoría de los webhooks no requieren esto\) |
|
||||
|
||||
#### Salida
|
||||
|
||||
| Parámetro | Tipo | Descripción |
|
||||
| --------- | ---- | ----------- |
|
||||
| `success` | boolean | Estado de éxito de la operación |
|
||||
| `output` | json | Resultados de la operación de poblado de Clay incluyendo datos de respuesta del webhook de Clay |
|
||||
| `data` | json | Datos de respuesta del webhook de Clay |
|
||||
| `metadata` | object | Metadatos de respuesta del webhook |
|
||||
|
||||
## Notas
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import { BlockInfoCard } from "@/components/ui/block-info-card"
|
||||
|
||||
<BlockInfoCard
|
||||
type="discord"
|
||||
color="#E0E0E0"
|
||||
color="#5865F2"
|
||||
icon={true}
|
||||
iconSvg={`<svg className="block-icon"
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ En Sim, la integración con OneDrive permite a tus agentes interactuar directame
|
||||
|
||||
## Instrucciones de uso
|
||||
|
||||
Integra OneDrive en el flujo de trabajo. Puede crear, subir y listar archivos. Requiere OAuth.
|
||||
Integra OneDrive en el flujo de trabajo. Puede crear archivos de texto y Excel, subir archivos y listar archivos.
|
||||
|
||||
## Herramientas
|
||||
|
||||
@@ -61,12 +61,13 @@ Subir un archivo a OneDrive
|
||||
#### Entrada
|
||||
|
||||
| Parámetro | Tipo | Obligatorio | Descripción |
|
||||
| --------- | ---- | ----------- | ----------- |
|
||||
| --------- | ---- | -------- | ----------- |
|
||||
| `fileName` | string | Sí | El nombre del archivo a subir |
|
||||
| `file` | file | No | El archivo a subir \(binario\) |
|
||||
| `content` | string | No | El contenido de texto a subir \(si no se proporciona un archivo\) |
|
||||
| `mimeType` | string | No | El tipo MIME del archivo a crear \(p. ej., text/plain para .txt, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet para .xlsx\) |
|
||||
| `folderSelector` | string | No | Seleccionar la carpeta donde subir el archivo |
|
||||
| `manualFolderId` | string | No | ID de carpeta ingresado manualmente \(modo avanzado\) |
|
||||
| `manualFolderId` | string | No | ID de carpeta introducido manualmente \(modo avanzado\) |
|
||||
|
||||
#### Salida
|
||||
|
||||
|
||||
@@ -133,13 +133,14 @@ Buscar vectores similares en una colección de Qdrant
|
||||
#### Entrada
|
||||
|
||||
| Parámetro | Tipo | Obligatorio | Descripción |
|
||||
| --------- | ---- | -------- | ----------- |
|
||||
| --------- | ---- | ----------- | ----------- |
|
||||
| `url` | string | Sí | URL base de Qdrant |
|
||||
| `apiKey` | string | No | Clave API de Qdrant \(opcional\) |
|
||||
| `collection` | string | Sí | Nombre de la colección |
|
||||
| `vector` | array | Sí | Vector a buscar |
|
||||
| `vector` | array | Sí | Vector para buscar |
|
||||
| `limit` | number | No | Número de resultados a devolver |
|
||||
| `filter` | object | No | Filtro a aplicar a la búsqueda |
|
||||
| `filter` | object | No | Filtro para aplicar a la búsqueda |
|
||||
| `search_return_data` | string | No | Datos a devolver de la búsqueda |
|
||||
| `with_payload` | boolean | No | Incluir payload en la respuesta |
|
||||
| `with_vector` | boolean | No | Incluir vector en la respuesta |
|
||||
|
||||
@@ -157,11 +158,12 @@ Obtener puntos por ID desde una colección de Qdrant
|
||||
#### Entrada
|
||||
|
||||
| Parámetro | Tipo | Obligatorio | Descripción |
|
||||
| --------- | ---- | -------- | ----------- |
|
||||
| --------- | ---- | ----------- | ----------- |
|
||||
| `url` | string | Sí | URL base de Qdrant |
|
||||
| `apiKey` | string | No | Clave API de Qdrant \(opcional\) |
|
||||
| `collection` | string | Sí | Nombre de la colección |
|
||||
| `ids` | array | Sí | Array de IDs de puntos a obtener |
|
||||
| `ids` | array | Sí | Array de IDs de puntos para recuperar |
|
||||
| `fetch_return_data` | string | No | Datos a devolver de la recuperación |
|
||||
| `with_payload` | boolean | No | Incluir payload en la respuesta |
|
||||
| `with_vector` | boolean | No | Incluir vector en la respuesta |
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ Les blocs sont les composants de construction que vous connectez pour créer des
|
||||
|
||||
## Types de blocs principaux
|
||||
|
||||
Sim propose sept types de blocs principaux qui gèrent les fonctions essentielles des flux de travail d'IA :
|
||||
Sim fournit des types de blocs essentiels qui gèrent les fonctions principales des flux de travail IA :
|
||||
|
||||
### Blocs de traitement
|
||||
- **[Agent](/blocks/agent)** - Dialoguez avec des modèles d'IA (OpenAI, Anthropic, Google, modèles locaux)
|
||||
@@ -28,8 +28,12 @@ Sim propose sept types de blocs principaux qui gèrent les fonctions essentielle
|
||||
- **[Router](/blocks/router)** - Utilisez l'IA pour acheminer intelligemment les requêtes vers différents chemins
|
||||
- **[Evaluator](/blocks/evaluator)** - Notez et évaluez la qualité du contenu à l'aide de l'IA
|
||||
|
||||
### Blocs de flux de contrôle
|
||||
- **[Variables](/blocks/variables)** - Définir et gérer des variables à portée de flux de travail
|
||||
- **[Wait](/blocks/wait)** - Mettre en pause l'exécution du flux de travail pendant un délai spécifié
|
||||
|
||||
### Blocs de sortie
|
||||
- **[Response](/blocks/response)** - Formatez et renvoyez les résultats finaux de votre flux de travail
|
||||
- **[Response](/blocks/response)** - Formater et renvoyer les résultats finaux de votre flux de travail
|
||||
|
||||
## Comment fonctionnent les blocs
|
||||
|
||||
@@ -37,7 +41,7 @@ Chaque bloc comporte trois composants principaux :
|
||||
|
||||
**Entrées** : données entrant dans le bloc depuis d'autres blocs ou saisies utilisateur
|
||||
**Configuration** : paramètres qui contrôlent le comportement du bloc
|
||||
**Sorties** : données que le bloc produit pour être utilisées par d'autres blocs
|
||||
**Sorties** : données produites par le bloc pour être utilisées par d'autres blocs
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
@@ -55,9 +59,9 @@ Chaque bloc comporte trois composants principaux :
|
||||
|
||||
Vous créez des flux de travail en connectant des blocs entre eux. La sortie d'un bloc devient l'entrée d'un autre :
|
||||
|
||||
- **Glisser pour connecter** : Faites glisser d'un port de sortie vers un port d'entrée
|
||||
- **Connexions multiples** : Une sortie peut se connecter à plusieurs entrées
|
||||
- **Chemins de ramification** : Certains blocs peuvent acheminer vers différents chemins selon les conditions
|
||||
- **Glisser pour connecter** : faites glisser d'un port de sortie vers un port d'entrée
|
||||
- **Connexions multiples** : une sortie peut se connecter à plusieurs entrées
|
||||
- **Chemins de branchement** : certains blocs peuvent acheminer vers différents chemins selon les conditions
|
||||
|
||||
<div className="w-full max-w-2xl mx-auto overflow-hidden rounded-lg">
|
||||
<Video src="connections.mp4" width={700} height={450} />
|
||||
@@ -72,8 +76,8 @@ Connectez les blocs en chaîne où chaque bloc traite la sortie du précédent :
|
||||
User Input → Agent → Function → Response
|
||||
```
|
||||
|
||||
### Ramification conditionnelle
|
||||
Utilisez des blocs de Condition ou de Routeur pour créer différents chemins :
|
||||
### Branchement conditionnel
|
||||
Utilisez des blocs Condition ou Router pour créer différents chemins :
|
||||
|
||||
```
|
||||
User Input → Router → Agent A (for questions)
|
||||
@@ -81,7 +85,7 @@ User Input → Router → Agent A (for questions)
|
||||
```
|
||||
|
||||
### Contrôle qualité
|
||||
Utilisez des blocs Évaluateur pour évaluer et filtrer les sorties :
|
||||
Utilisez des blocs Evaluator pour évaluer et filtrer les sorties :
|
||||
|
||||
```
|
||||
Agent → Evaluator → Condition → Response (if good)
|
||||
@@ -101,7 +105,7 @@ Chaque type de bloc possède des options de configuration spécifiques :
|
||||
- Sélection du modèle (OpenAI, Anthropic, Google, local)
|
||||
- Clés API et authentification
|
||||
- Température et autres paramètres du modèle
|
||||
- Prompts système et instructions
|
||||
- Instructions et prompts système
|
||||
|
||||
**Blocs logiques** (Condition, Fonction) :
|
||||
- Expressions ou code personnalisés
|
||||
@@ -109,7 +113,7 @@ Chaque type de bloc possède des options de configuration spécifiques :
|
||||
- Paramètres d'environnement d'exécution
|
||||
|
||||
**Blocs d'intégration** (API, Réponse) :
|
||||
- Configuration des points de terminaison
|
||||
- Configuration du point de terminaison
|
||||
- En-têtes et authentification
|
||||
- Formatage des requêtes/réponses
|
||||
|
||||
@@ -124,6 +128,12 @@ Chaque type de bloc possède des options de configuration spécifiques :
|
||||
Intégrez des services externes et des API
|
||||
</Card>
|
||||
<Card title="Bloc Condition" href="/blocks/condition">
|
||||
Créez une logique de ramification basée sur l'évaluation des données
|
||||
Créez une logique de branchement basée sur l'évaluation des données
|
||||
</Card>
|
||||
<Card title="Bloc Variables" href="/blocks/variables">
|
||||
Définissez et gérez des variables à portée de workflow
|
||||
</Card>
|
||||
<Card title="Bloc Attente" href="/blocks/wait">
|
||||
Mettez en pause l'exécution du workflow pour des délais spécifiés
|
||||
</Card>
|
||||
</Cards>
|
||||
|
||||
@@ -9,7 +9,7 @@ import { Image } from '@/components/ui/image'
|
||||
|
||||
Le bloc Loop est un bloc conteneur dans Sim qui vous permet de créer des flux de travail itératifs en exécutant un groupe de blocs de façon répétée. Les boucles permettent un traitement itératif dans vos flux de travail.
|
||||
|
||||
Le bloc Loop prend en charge deux types d'itération :
|
||||
Le bloc Boucle prend en charge quatre types d'itération :
|
||||
|
||||
<Callout type="info">
|
||||
Les blocs Loop sont des nœuds conteneurs qui peuvent contenir d'autres blocs. Les blocs à l'intérieur d'une boucle s'exécuteront plusieurs fois selon votre configuration.
|
||||
@@ -21,16 +21,16 @@ Le bloc Loop vous permet de :
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
<strong>Itérer sur des collections</strong> : traiter des tableaux ou des objets un élément à la fois
|
||||
<strong>Itérer sur des collections</strong> : Traiter des tableaux ou des objets un élément à la fois
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Répéter des opérations</strong> : exécuter des blocs un nombre fixe de fois
|
||||
<strong>Répéter des opérations</strong> : Exécuter des blocs un nombre fixe de fois
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Traitement séquentiel</strong> : gérer la transformation des données dans des itérations ordonnées
|
||||
<strong>Boucler sur des conditions</strong> : Continuer l'exécution tant qu'une condition est remplie ou jusqu'à ce qu'elle le soit
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Agréger les résultats</strong> : collecter les sorties de toutes les itérations de la boucle
|
||||
<strong>Agréger des résultats</strong> : Collecter les sorties de toutes les itérations de la boucle
|
||||
</Step>
|
||||
</Steps>
|
||||
|
||||
@@ -47,11 +47,11 @@ Le bloc Loop exécute les blocs contenus par itération séquentielle :
|
||||
|
||||
### Type de boucle
|
||||
|
||||
Choisissez entre deux types de boucles :
|
||||
Choisissez parmi quatre types de boucles :
|
||||
|
||||
<Tabs items={['For Loop', 'ForEach Loop']}>
|
||||
<Tabs items={['Boucle For', 'Boucle ForEach', 'Boucle While', 'Boucle Do-While']}>
|
||||
<Tab>
|
||||
**For Loop (Itérations)** - Une boucle numérique qui s'exécute un nombre fixe de fois :
|
||||
**Boucle For (Itérations)** - Une boucle numérique qui s'exécute un nombre fixe de fois :
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
@@ -100,6 +100,58 @@ Choisissez entre deux types de boucles :
|
||||
```
|
||||
|
||||
</Tab>
|
||||
<Tab>
|
||||
**Boucle While (Basée sur condition)** - Continue l'exécution tant qu'une condition est évaluée à vrai :
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/loop-3.png"
|
||||
alt="Boucle While avec condition"
|
||||
width={500}
|
||||
height={400}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
Utilisez cette option lorsque vous devez boucler jusqu'à ce qu'une condition spécifique soit remplie. La condition est vérifiée **avant** chaque itération.
|
||||
|
||||
|
||||
```
|
||||
Example: While <variable.i> < 10
|
||||
- Check condition → Execute if true
|
||||
- Inside loop: Increment <variable.i>
|
||||
- Inside loop: Variables assigns i = <variable.i> + 1
|
||||
- Check condition → Execute if true
|
||||
- Check condition → Exit if false
|
||||
```
|
||||
|
||||
</Tab>
|
||||
<Tab>
|
||||
**Boucle Do-While (Basée sur condition)** - S'exécute au moins une fois, puis continue tant qu'une condition est vraie :
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/loop-3.png"
|
||||
alt="Boucle Do-While avec condition"
|
||||
width={500}
|
||||
height={400}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
Utilisez cette option lorsque vous devez exécuter au moins une fois, puis boucler jusqu'à ce qu'une condition soit remplie. La condition est vérifiée **après** chaque itération.
|
||||
|
||||
|
||||
```
|
||||
Example: Do-while <variable.i> < 10
|
||||
- Execute blocks
|
||||
- Inside loop: Increment <variable.i>
|
||||
- Inside loop: Variables assigns i = <variable.i> + 1
|
||||
- Check condition → Continue if true
|
||||
- Check condition → Exit if false
|
||||
```
|
||||
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
## Comment utiliser les boucles
|
||||
@@ -113,16 +165,16 @@ Choisissez entre deux types de boucles :
|
||||
|
||||
### Accès aux résultats
|
||||
|
||||
Une fois qu'une boucle est terminée, vous pouvez accéder aux résultats agrégés :
|
||||
Après la fin d'une boucle, vous pouvez accéder aux résultats agrégés :
|
||||
|
||||
- **`<loop.results>`** : tableau des résultats de toutes les itérations de la boucle
|
||||
- **`<loop.results>`** : Tableau des résultats de toutes les itérations de la boucle
|
||||
|
||||
## Exemples de cas d'utilisation
|
||||
|
||||
### Traitement des résultats d'API
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Scénario : traiter plusieurs enregistrements clients</h4>
|
||||
<h4 className="font-medium">Scénario : Traiter plusieurs enregistrements clients</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Le bloc API récupère la liste des clients</li>
|
||||
<li>La boucle ForEach itère sur chaque client</li>
|
||||
@@ -134,15 +186,28 @@ Une fois qu'une boucle est terminée, vous pouvez accéder aux résultats agrég
|
||||
### Génération itérative de contenu
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Scénario : générer plusieurs variations</h4>
|
||||
<h4 className="font-medium">Scénario : Générer plusieurs variations</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Définir une boucle For à 5 itérations</li>
|
||||
<li>Configurer la boucle For à 5 itérations</li>
|
||||
<li>À l'intérieur de la boucle : l'agent génère une variation de contenu</li>
|
||||
<li>À l'intérieur de la boucle : l'évaluateur note le contenu</li>
|
||||
<li>Après la boucle : la fonction sélectionne la meilleure variation</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Compteur avec boucle While
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Scénario : Traiter des éléments avec une boucle basée sur un compteur</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Initialiser la variable de workflow : `i = 0`</li>
|
||||
<li>Boucle While avec condition : `<variable.i>` \< 10</li>
|
||||
<li>À l'intérieur de la boucle : l'agent traite l'élément à l'index `<variable.i>`</li>
|
||||
<li>À l'intérieur de la boucle : la variable incrémente `i = <variable.i> + 1`</li>
|
||||
<li>La boucle continue tant que i est inférieur à 10</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
## Fonctionnalités avancées
|
||||
|
||||
### Limitations
|
||||
@@ -153,7 +218,7 @@ Une fois qu'une boucle est terminée, vous pouvez accéder aux résultats agrég
|
||||
- Vous ne pouvez pas placer un bloc Parallèle à l'intérieur d'un bloc Boucle
|
||||
- Vous ne pouvez pas placer un bloc conteneur à l'intérieur d'un autre bloc conteneur
|
||||
|
||||
Si vous avez besoin d'une itération multidimensionnelle, envisagez de restructurer votre flux de travail pour utiliser des boucles séquentielles ou traiter les données par étapes.
|
||||
Si vous avez besoin d'une itération multidimensionnelle, envisagez de restructurer votre workflow pour utiliser des boucles séquentielles ou traiter les données par étapes.
|
||||
</Callout>
|
||||
|
||||
<Callout type="info">
|
||||
@@ -166,13 +231,16 @@ Une fois qu'une boucle est terminée, vous pouvez accéder aux résultats agrég
|
||||
<Tab>
|
||||
<ul className="list-disc space-y-2 pl-6">
|
||||
<li>
|
||||
<strong>Type de boucle</strong> : choisissez entre 'for' ou 'forEach'
|
||||
<strong>Type de boucle</strong> : choisissez entre 'for', 'forEach', 'while' ou 'doWhile'
|
||||
</li>
|
||||
<li>
|
||||
<strong>Itérations</strong> : nombre de fois à exécuter (pour les boucles for)
|
||||
<strong>Itérations</strong> : nombre de fois à exécuter (boucles for)
|
||||
</li>
|
||||
<li>
|
||||
<strong>Collection</strong> : tableau ou objet à parcourir (pour les boucles forEach)
|
||||
<strong>Collection</strong> : tableau ou objet à parcourir (boucles forEach)
|
||||
</li>
|
||||
<li>
|
||||
<strong>Condition</strong> : expression booléenne à évaluer (boucles while/do-while)
|
||||
</li>
|
||||
</ul>
|
||||
</Tab>
|
||||
@@ -208,4 +276,4 @@ Une fois qu'une boucle est terminée, vous pouvez accéder aux résultats agrég
|
||||
|
||||
- **Définir des limites raisonnables** : gardez un nombre d'itérations raisonnable pour éviter des temps d'exécution longs
|
||||
- **Utiliser ForEach pour les collections** : lors du traitement de tableaux ou d'objets, utilisez les boucles ForEach plutôt que les boucles For
|
||||
- **Gérer les erreurs avec élégance** : envisagez d'ajouter une gestion des erreurs à l'intérieur des boucles pour des flux de travail robustes
|
||||
- **Gérer les erreurs avec élégance** : envisagez d'ajouter une gestion des erreurs à l'intérieur des boucles pour des workflows robustes
|
||||
|
||||
123
apps/docs/content/docs/fr/blocks/variables.mdx
Normal file
123
apps/docs/content/docs/fr/blocks/variables.mdx
Normal file
@@ -0,0 +1,123 @@
|
||||
---
|
||||
title: Variables
|
||||
---
|
||||
|
||||
import { Callout } from 'fumadocs-ui/components/callout'
|
||||
import { Step, Steps } from 'fumadocs-ui/components/steps'
|
||||
import { Image } from '@/components/ui/image'
|
||||
|
||||
Le bloc Variables met à jour les variables du workflow pendant l'exécution. Les variables doivent d'abord être initialisées dans la section Variables de votre workflow, puis vous pouvez utiliser ce bloc pour mettre à jour leurs valeurs pendant l'exécution de votre workflow.
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/variables.png"
|
||||
alt="Bloc Variables"
|
||||
width={500}
|
||||
height={350}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Callout>
|
||||
Accédez aux variables n'importe où dans votre workflow en utilisant la syntaxe `<variable.variableName>`.
|
||||
</Callout>
|
||||
|
||||
## Aperçu
|
||||
|
||||
Le bloc Variables vous permet de :
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
<strong>Mettre à jour les variables du workflow</strong> : modifier les valeurs des variables pendant l'exécution
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Stocker des données dynamiques</strong> : capturer les sorties de blocs dans des variables
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Maintenir l'état</strong> : suivre les compteurs, les indicateurs et les résultats intermédiaires
|
||||
</Step>
|
||||
</Steps>
|
||||
|
||||
## Comment utiliser les variables
|
||||
|
||||
### 1. Initialiser dans les variables du workflow
|
||||
|
||||
Tout d'abord, créez vos variables dans la section Variables du workflow (accessible depuis les paramètres du workflow) :
|
||||
|
||||
```
|
||||
customerEmail = ""
|
||||
retryCount = 0
|
||||
currentStatus = "pending"
|
||||
```
|
||||
|
||||
### 2. Mettre à jour avec le bloc Variables
|
||||
|
||||
Utilisez le bloc Variables pour mettre à jour ces valeurs pendant l'exécution :
|
||||
|
||||
```
|
||||
customerEmail = <api.email>
|
||||
retryCount = <variable.retryCount> + 1
|
||||
currentStatus = "processing"
|
||||
```
|
||||
|
||||
### 3. Accéder n'importe où
|
||||
|
||||
Référencez les variables dans n'importe quel bloc :
|
||||
|
||||
```
|
||||
Agent prompt: "Send email to <variable.customerEmail>"
|
||||
Condition: <variable.retryCount> < 5
|
||||
API body: {"status": "<variable.currentStatus>"}
|
||||
```
|
||||
|
||||
## Exemples de cas d'utilisation
|
||||
|
||||
### Compteur de boucle et état
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Scénario : suivre la progression à travers les itérations de boucle</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Initialisation dans le workflow : `itemsProcessed = 0`, `lastResult = ""`</li>
|
||||
<li>La boucle itère sur les éléments</li>
|
||||
<li>Dans la boucle : l'agent traite l'élément actuel</li>
|
||||
<li>Dans la boucle : Variables met à jour `itemsProcessed = <variable.itemsProcessed> + 1`</li>
|
||||
<li>Dans la boucle : Variables met à jour `lastResult = <agent.content>`</li>
|
||||
<li>Itération suivante : accès à `<variable.lastResult>` pour comparer avec le résultat actuel</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Logique de nouvelle tentative
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Scénario : suivre les tentatives de nouvelle requête API</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Initialisation dans le workflow : `retryCount = 0`</li>
|
||||
<li>Le bloc API tente une requête</li>
|
||||
<li>En cas d'échec, Variables incrémente : `retryCount = <variable.retryCount> + 1`</li>
|
||||
<li>La condition vérifie si `<variable.retryCount>` \< 3 pour réessayer ou échouer</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Configuration dynamique
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Scénario : stocker le contexte utilisateur pour le workflow</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Initialisation dans le workflow : `userId = ""`, `userTier = ""`</li>
|
||||
<li>L'API récupère le profil utilisateur</li>
|
||||
<li>Les variables stockent : `userId = <api.id>`, `userTier = <api.tier>`</li>
|
||||
<li>L'agent personnalise la réponse en utilisant `<variable.userTier>`</li>
|
||||
<li>L'API utilise `<variable.userId>` pour la journalisation</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
## Sorties
|
||||
|
||||
- **`<variables.assignments>`** : objet JSON avec toutes les affectations de variables de ce bloc
|
||||
|
||||
## Bonnes pratiques
|
||||
|
||||
- **Initialiser dans les paramètres du workflow** : toujours créer des variables dans la section Variables du workflow avant de les utiliser
|
||||
- **Mettre à jour dynamiquement** : utiliser les blocs Variables pour mettre à jour les valeurs en fonction des sorties de blocs ou des calculs
|
||||
- **Utiliser dans les boucles** : parfait pour suivre l'état à travers les itérations
|
||||
- **Nommer de façon descriptive** : utiliser des noms clairs comme `currentIndex`, `totalProcessed`, ou `lastError`
|
||||
99
apps/docs/content/docs/fr/blocks/wait.mdx
Normal file
99
apps/docs/content/docs/fr/blocks/wait.mdx
Normal file
@@ -0,0 +1,99 @@
|
||||
---
|
||||
title: Attente
|
||||
---
|
||||
|
||||
import { Callout } from 'fumadocs-ui/components/callout'
|
||||
import { Step, Steps } from 'fumadocs-ui/components/steps'
|
||||
import { Image } from '@/components/ui/image'
|
||||
|
||||
Le bloc Attente met en pause votre flux de travail pendant une durée spécifiée avant de continuer vers le bloc suivant. Utilisez-le pour ajouter des délais entre les actions, respecter les limites de taux des API ou espacer les opérations.
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/wait.png"
|
||||
alt="Bloc d'attente"
|
||||
width={500}
|
||||
height={350}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
## Aperçu
|
||||
|
||||
Le bloc Attente vous permet de :
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
<strong>Ajouter des délais</strong> : mettre en pause l'exécution entre les étapes du flux de travail
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Respecter les limites de taux</strong> : espacer les appels API pour rester dans les limites
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>Planifier des séquences</strong> : créer des flux de travail chronométrés avec des délais entre les actions
|
||||
</Step>
|
||||
</Steps>
|
||||
|
||||
## Configuration
|
||||
|
||||
### Durée d'attente
|
||||
|
||||
Saisissez la durée de mise en pause de l'exécution :
|
||||
- **Entrée** : nombre positif
|
||||
- **Maximum** : 600 secondes (10 minutes) ou 10 minutes
|
||||
|
||||
### Unité
|
||||
|
||||
Choisissez l'unité de temps :
|
||||
- **Secondes** : pour des délais courts et précis
|
||||
- **Minutes** : pour des pauses plus longues
|
||||
|
||||
<Callout type="info">
|
||||
Les blocs d'attente peuvent être annulés en arrêtant le flux de travail. La durée d'attente maximale est de 10 minutes.
|
||||
</Callout>
|
||||
|
||||
## Sorties
|
||||
|
||||
- **`<wait.waitDuration>`** : la durée d'attente en millisecondes
|
||||
- **`<wait.status>`** : statut de l'attente ('waiting', 'completed', ou 'cancelled')
|
||||
|
||||
## Exemples de cas d'utilisation
|
||||
|
||||
### Limitation du taux d'API
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Scénario : rester dans les limites de taux d'API</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Le bloc API effectue la première requête</li>
|
||||
<li>Le bloc Attente fait une pause de 2 secondes</li>
|
||||
<li>Le bloc API effectue la deuxième requête</li>
|
||||
<li>Le processus continue sans atteindre les limites de taux</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Notifications chronométrées
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Scénario : envoyer des messages de suivi</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>La fonction envoie l'email initial</li>
|
||||
<li>Le bloc Attente fait une pause de 5 minutes</li>
|
||||
<li>La fonction envoie l'email de suivi</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Retards de traitement
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">Scénario : Attente d'un système externe</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>Le bloc API déclenche une tâche dans un système externe</li>
|
||||
<li>Le bloc d'attente fait une pause de 30 secondes</li>
|
||||
<li>Le bloc API vérifie l'état d'achèvement de la tâche</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
## Bonnes pratiques
|
||||
|
||||
- **Gardez des attentes raisonnables** : utilisez l'attente pour des délais jusqu'à 10 minutes. Pour des délais plus longs, envisagez des workflows programmés
|
||||
- **Surveillez le temps d'exécution** : n'oubliez pas que les attentes prolongent la durée totale du workflow
|
||||
@@ -211,14 +211,14 @@ Remplir Clay avec des données provenant d'un fichier JSON. Permet une communica
|
||||
| --------- | ---- | ---------- | ----------- |
|
||||
| `webhookURL` | string | Oui | L'URL du webhook à remplir |
|
||||
| `data` | json | Oui | Les données à remplir |
|
||||
| `authToken` | string | Oui | Jeton d'authentification pour l'authentification du webhook Clay |
|
||||
| `authToken` | string | Non | Jeton d'authentification optionnel pour l'authentification du webhook Clay \(la plupart des webhooks ne nécessitent pas cela\) |
|
||||
|
||||
#### Sortie
|
||||
|
||||
| Paramètre | Type | Description |
|
||||
| --------- | ---- | ----------- |
|
||||
| `success` | boolean | Statut de réussite de l'opération |
|
||||
| `output` | json | Résultats de l'opération de remplissage Clay incluant les données de réponse du webhook Clay |
|
||||
| `data` | json | Données de réponse du webhook Clay |
|
||||
| `metadata` | object | Métadonnées de réponse du webhook |
|
||||
|
||||
## Notes
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import { BlockInfoCard } from "@/components/ui/block-info-card"
|
||||
|
||||
<BlockInfoCard
|
||||
type="discord"
|
||||
color="#E0E0E0"
|
||||
color="#5865F2"
|
||||
icon={true}
|
||||
iconSvg={`<svg className="block-icon"
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ Dans Sim, l'intégration OneDrive permet à vos agents d'interagir directement a
|
||||
|
||||
## Instructions d'utilisation
|
||||
|
||||
Intégrez OneDrive dans le flux de travail. Permet de créer, téléverser et lister des fichiers. Nécessite OAuth.
|
||||
Intégrer OneDrive dans le flux de travail. Peut créer des fichiers texte et Excel, télécharger des fichiers et lister des fichiers.
|
||||
|
||||
## Outils
|
||||
|
||||
@@ -65,7 +65,8 @@ Télécharger un fichier vers OneDrive
|
||||
| `fileName` | string | Oui | Le nom du fichier à télécharger |
|
||||
| `file` | file | Non | Le fichier à télécharger \(binaire\) |
|
||||
| `content` | string | Non | Le contenu textuel à télécharger \(si aucun fichier n'est fourni\) |
|
||||
| `folderSelector` | string | Non | Sélectionner le dossier dans lequel télécharger le fichier |
|
||||
| `mimeType` | string | Non | Le type MIME du fichier à créer \(par exemple, text/plain pour .txt, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet pour .xlsx\) |
|
||||
| `folderSelector` | string | Non | Sélectionner le dossier où télécharger le fichier |
|
||||
| `manualFolderId` | string | Non | ID de dossier saisi manuellement \(mode avancé\) |
|
||||
|
||||
#### Sortie
|
||||
|
||||
@@ -133,13 +133,14 @@ Rechercher des vecteurs similaires dans une collection Qdrant
|
||||
#### Entrée
|
||||
|
||||
| Paramètre | Type | Obligatoire | Description |
|
||||
| --------- | ---- | ---------- | ----------- |
|
||||
| `url` | chaîne | Oui | URL de base Qdrant |
|
||||
| --------- | ---- | ----------- | ----------- |
|
||||
| `url` | chaîne | Oui | URL de base de Qdrant |
|
||||
| `apiKey` | chaîne | Non | Clé API Qdrant \(facultative\) |
|
||||
| `collection` | chaîne | Oui | Nom de la collection |
|
||||
| `vector` | tableau | Oui | Vecteur à rechercher |
|
||||
| `limit` | nombre | Non | Nombre de résultats à retourner |
|
||||
| `filter` | objet | Non | Filtre à appliquer à la recherche |
|
||||
| `search_return_data` | chaîne | Non | Données à retourner de la recherche |
|
||||
| `with_payload` | booléen | Non | Inclure la charge utile dans la réponse |
|
||||
| `with_vector` | booléen | Non | Inclure le vecteur dans la réponse |
|
||||
|
||||
@@ -157,11 +158,12 @@ Récupérer des points par ID depuis une collection Qdrant
|
||||
#### Entrée
|
||||
|
||||
| Paramètre | Type | Obligatoire | Description |
|
||||
| --------- | ---- | ---------- | ----------- |
|
||||
| `url` | chaîne | Oui | URL de base Qdrant |
|
||||
| --------- | ---- | ----------- | ----------- |
|
||||
| `url` | chaîne | Oui | URL de base de Qdrant |
|
||||
| `apiKey` | chaîne | Non | Clé API Qdrant \(facultative\) |
|
||||
| `collection` | chaîne | Oui | Nom de la collection |
|
||||
| `ids` | tableau | Oui | Tableau d'identifiants de points à récupérer |
|
||||
| `fetch_return_data` | chaîne | Non | Données à retourner de la récupération |
|
||||
| `with_payload` | booléen | Non | Inclure la charge utile dans la réponse |
|
||||
| `with_vector` | booléen | Non | Inclure le vecteur dans la réponse |
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ import { Video } from '@/components/ui/video'
|
||||
|
||||
## 主要ブロックタイプ
|
||||
|
||||
Simは、AIワークフローの基本機能を処理する7つの主要ブロックタイプを提供しています:
|
||||
Simは、AIワークフローの中核機能を処理する必須のブロックタイプを提供します:
|
||||
|
||||
### 処理ブロック
|
||||
- **[エージェント](/blocks/agent)** - AIモデル(OpenAI、Anthropic、Google、ローカルモデル)とチャット
|
||||
@@ -28,6 +28,10 @@ Simは、AIワークフローの基本機能を処理する7つの主要ブロ
|
||||
- **[ルーター](/blocks/router)** - AIを使用してリクエストを異なるパスにインテリジェントにルーティング
|
||||
- **[評価者](/blocks/evaluator)** - AIを使用してコンテンツの品質を評価・採点
|
||||
|
||||
### 制御フローブロック
|
||||
- **[変数](/blocks/variables)** - ワークフロースコープの変数を設定・管理
|
||||
- **[待機](/blocks/wait)** - 指定した時間だけワークフロー実行を一時停止
|
||||
|
||||
### 出力ブロック
|
||||
- **[レスポンス](/blocks/response)** - ワークフローからの最終結果をフォーマットして返す
|
||||
|
||||
@@ -35,9 +39,9 @@ Simは、AIワークフローの基本機能を処理する7つの主要ブロ
|
||||
|
||||
各ブロックには3つの主要コンポーネントがあります:
|
||||
|
||||
**入力**:他のブロックまたはユーザー入力からブロックに入ってくるデータ
|
||||
**入力**:他のブロックやユーザー入力からブロックに入ってくるデータ
|
||||
**設定**:ブロックの動作を制御する設定
|
||||
**出力**:他のブロックが使用するためにブロックが生成するデータ
|
||||
**出力**:ブロックが生成し、他のブロックが使用するデータ
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
@@ -47,17 +51,17 @@ Simは、AIワークフローの基本機能を処理する7つの主要ブロ
|
||||
<strong>処理</strong>:ブロックは設定に従って入力を処理します
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>結果を出力</strong>:ブロックはワークフロー内の次のブロックのために出力データを生成します
|
||||
<strong>結果を出力</strong>:ブロックはワークフロー内の次のブロックのための出力データを生成します
|
||||
</Step>
|
||||
</Steps>
|
||||
|
||||
## ブロックの接続
|
||||
|
||||
ブロックを接続することでワークフローを作成します。あるブロックの出力は、別のブロックの入力になります:
|
||||
ブロックを接続してワークフローを作成します。あるブロックの出力は、別のブロックの入力になります:
|
||||
|
||||
- **ドラッグして接続**: 出力ポートから入力ポートへドラッグします
|
||||
- **複数の接続**: 一つの出力を複数の入力に接続できます
|
||||
- **分岐パス**: 一部のブロックは条件に基づいて異なるパスにルーティングできます
|
||||
- **ドラッグして接続**:出力ポートから入力ポートにドラッグ
|
||||
- **複数の接続**:1つの出力を複数の入力に接続可能
|
||||
- **分岐パス**:一部のブロックは条件に基づいて異なるパスにルーティング可能
|
||||
|
||||
<div className="w-full max-w-2xl mx-auto overflow-hidden rounded-lg">
|
||||
<Video src="connections.mp4" width={700} height={450} />
|
||||
@@ -73,7 +77,7 @@ User Input → Agent → Function → Response
|
||||
```
|
||||
|
||||
### 条件分岐
|
||||
条件ブロックやルーターブロックを使用して異なるパスを作成します:
|
||||
条件またはルーターブロックを使用して異なるパスを作成します:
|
||||
|
||||
```
|
||||
User Input → Router → Agent A (for questions)
|
||||
@@ -88,19 +92,19 @@ Agent → Evaluator → Condition → Response (if good)
|
||||
→ Agent (retry if bad)
|
||||
```
|
||||
|
||||
## ブロックの設定
|
||||
## ブロック設定
|
||||
|
||||
各ブロックタイプには特定の設定オプションがあります:
|
||||
|
||||
**すべてのブロック**:
|
||||
- 入力/出力の接続
|
||||
- 入出力接続
|
||||
- エラー処理の動作
|
||||
- 実行タイムアウト設定
|
||||
|
||||
**AIブロック** (エージェント、ルーター、評価者):
|
||||
- モデル選択(OpenAI、Anthropic、Google、ローカル)
|
||||
- APIキーと認証
|
||||
- 温度などのモデルパラメータ
|
||||
- 温度やその他のモデルパラメータ
|
||||
- システムプロンプトと指示
|
||||
|
||||
**ロジックブロック** (条件、関数):
|
||||
@@ -109,21 +113,27 @@ Agent → Evaluator → Condition → Response (if good)
|
||||
- 実行環境設定
|
||||
|
||||
**統合ブロック** (API、レスポンス):
|
||||
- エンドポイント設定
|
||||
- エンドポイント構成
|
||||
- ヘッダーと認証
|
||||
- リクエスト/レスポンスのフォーマット
|
||||
|
||||
<Cards>
|
||||
<Card title="エージェントブロック" href="/blocks/agent">
|
||||
AIモデルに接続し、インテリジェントな応答を作成する
|
||||
AIモデルに接続し、インテリジェントな応答を作成
|
||||
</Card>
|
||||
<Card title="関数ブロック" href="/blocks/function">
|
||||
カスタムコードを実行してデータを処理および変換する
|
||||
カスタムコードを実行してデータを処理・変換
|
||||
</Card>
|
||||
<Card title="APIブロック" href="/blocks/api">
|
||||
外部サービスやAPIと統合する
|
||||
外部サービスやAPIと統合
|
||||
</Card>
|
||||
<Card title="条件ブロック" href="/blocks/condition">
|
||||
データ評価に基づいた分岐ロジックを作成する
|
||||
データ評価に基づく分岐ロジックを作成
|
||||
</Card>
|
||||
<Card title="変数ブロック" href="/blocks/variables">
|
||||
ワークフロースコープの変数を設定・管理
|
||||
</Card>
|
||||
<Card title="待機ブロック" href="/blocks/wait">
|
||||
指定した時間だけワークフロー実行を一時停止
|
||||
</Card>
|
||||
</Cards>
|
||||
|
||||
@@ -9,7 +9,7 @@ import { Image } from '@/components/ui/image'
|
||||
|
||||
ループブロックはSimの中のコンテナブロックで、ブロックのグループを繰り返し実行することで反復的なワークフローを作成できます。ループを使用することで、ワークフロー内で反復処理が可能になります。
|
||||
|
||||
ループブロックは2種類の反復をサポートしています:
|
||||
ループブロックは4種類の反復処理をサポートしています:
|
||||
|
||||
<Callout type="info">
|
||||
ループブロックは他のブロックを内部に保持できるコンテナノードです。ループ内のブロックは設定に基づいて複数回実行されます。
|
||||
@@ -21,13 +21,13 @@ import { Image } from '@/components/ui/image'
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
<strong>コレクションの反復処理</strong>:配列やオブジェクトを一項目ずつ処理する
|
||||
<strong>コレクションの反復処理</strong>:配列やオブジェクトを1アイテムずつ処理する
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>操作の繰り返し</strong>:ブロックを固定回数実行する
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>順次処理</strong>:順序付けられた反復でデータ変換を処理する
|
||||
<strong>条件付きループ</strong>:条件が満たされている間、または満たされるまで実行を継続する
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>結果の集約</strong>:すべてのループ反復からの出力を収集する
|
||||
@@ -47,16 +47,16 @@ import { Image } from '@/components/ui/image'
|
||||
|
||||
### ループタイプ
|
||||
|
||||
2種類のループから選択できます:
|
||||
4種類のループから選択できます:
|
||||
|
||||
<Tabs items={['For Loop', 'ForEach Loop']}>
|
||||
<Tabs items={['For ループ', 'ForEach ループ', 'While ループ', 'Do-While ループ']}>
|
||||
<Tab>
|
||||
**Forループ(反復回数)** - 固定回数実行する数値ループ:
|
||||
**For ループ(反復回数)** - 固定回数実行する数値ループ:
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/loop-1.png"
|
||||
alt="反復回数を指定するForループ"
|
||||
alt="反復回数を使用したFor ループ"
|
||||
width={500}
|
||||
height={400}
|
||||
className="my-6"
|
||||
@@ -100,6 +100,58 @@ import { Image } from '@/components/ui/image'
|
||||
```
|
||||
|
||||
</Tab>
|
||||
<Tab>
|
||||
**While ループ(条件ベース)** - 条件がtrueと評価される間、実行を継続します:
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/loop-3.png"
|
||||
alt="条件付きWhile ループ"
|
||||
width={500}
|
||||
height={400}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
特定の条件が満たされるまでループする必要がある場合に使用します。条件は各反復の**前に**チェックされます。
|
||||
|
||||
|
||||
```
|
||||
Example: While <variable.i> < 10
|
||||
- Check condition → Execute if true
|
||||
- Inside loop: Increment <variable.i>
|
||||
- Inside loop: Variables assigns i = <variable.i> + 1
|
||||
- Check condition → Execute if true
|
||||
- Check condition → Exit if false
|
||||
```
|
||||
|
||||
</Tab>
|
||||
<Tab>
|
||||
**Do-While ループ(条件ベース)** - 少なくとも1回実行し、その後条件がtrueの間継続します:
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/loop-3.png"
|
||||
alt="条件付きDo-While ループ"
|
||||
width={500}
|
||||
height={400}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
少なくとも1回実行し、その後条件が満たされるまでループする必要がある場合に使用します。条件は各反復の**後に**チェックされます。
|
||||
|
||||
|
||||
```
|
||||
Example: Do-while <variable.i> < 10
|
||||
- Execute blocks
|
||||
- Inside loop: Increment <variable.i>
|
||||
- Inside loop: Variables assigns i = <variable.i> + 1
|
||||
- Check condition → Continue if true
|
||||
- Check condition → Exit if false
|
||||
```
|
||||
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
## ループの使い方
|
||||
@@ -107,13 +159,13 @@ import { Image } from '@/components/ui/image'
|
||||
### ループの作成
|
||||
|
||||
1. ツールバーからループブロックをキャンバスにドラッグします
|
||||
2. ループタイプとパラメータを設定します
|
||||
3. ループコンテナ内に他のブロックをドラッグします
|
||||
2. ループの種類とパラメータを設定します
|
||||
3. 他のブロックをループコンテナ内にドラッグします
|
||||
4. 必要に応じてブロックを接続します
|
||||
|
||||
### 結果へのアクセス
|
||||
|
||||
ループが完了すると、集計された結果にアクセスできます:
|
||||
ループが完了した後、集計された結果にアクセスできます:
|
||||
|
||||
- **`<loop.results>`**: すべてのループ反復からの結果の配列
|
||||
|
||||
@@ -143,12 +195,25 @@ import { Image } from '@/components/ui/image'
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### Whileループによるカウンター
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">シナリオ:カウンターベースのループでアイテムを処理する</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>ワークフロー変数を初期化:`i = 0`</li>
|
||||
<li>条件付きWhileループ:`<variable.i>` \< 10</li>
|
||||
<li>ループ内:エージェントがインデックス `<variable.i>` のアイテムを処理</li>
|
||||
<li>ループ内:変数がインクリメント `i = <variable.i> + 1`</li>
|
||||
<li>iが10未満である限りループは継続</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
## 高度な機能
|
||||
|
||||
### 制限事項
|
||||
|
||||
<Callout type="warning">
|
||||
コンテナブロック(ループと並列処理)は互いに入れ子にすることができません。つまり:
|
||||
コンテナブロック(ループと並列)は互いに入れ子にすることができません。つまり:
|
||||
- ループブロック内に別のループブロックを配置できません
|
||||
- ループブロック内に並列ブロックを配置できません
|
||||
- どのコンテナブロック内にも別のコンテナブロックを配置できません
|
||||
@@ -157,16 +222,16 @@ import { Image } from '@/components/ui/image'
|
||||
</Callout>
|
||||
|
||||
<Callout type="info">
|
||||
ループは並列ではなく、順次実行されます。並行実行が必要な場合は、代わりにParallelブロックを使用してください。
|
||||
ループは並列ではなく、順次実行されます。同時実行が必要な場合は、代わりに並列ブロックを使用してください。
|
||||
</Callout>
|
||||
|
||||
## 入力と出力
|
||||
|
||||
<Tabs items={['設定', '変数', '結果']}>
|
||||
<Tabs items={['Configuration', 'Variables', 'Results']}>
|
||||
<Tab>
|
||||
<ul className="list-disc space-y-2 pl-6">
|
||||
<li>
|
||||
<strong>ループタイプ</strong>: 'for'または'forEach'から選択
|
||||
<strong>ループタイプ</strong>: 'for'、'forEach'、'while'、または'doWhile'から選択
|
||||
</li>
|
||||
<li>
|
||||
<strong>繰り返し回数</strong>: 実行する回数(forループ)
|
||||
@@ -174,6 +239,9 @@ import { Image } from '@/components/ui/image'
|
||||
<li>
|
||||
<strong>コレクション</strong>: 反復処理する配列またはオブジェクト(forEachループ)
|
||||
</li>
|
||||
<li>
|
||||
<strong>条件</strong>: 評価するブール式(while/do-whileループ)
|
||||
</li>
|
||||
</ul>
|
||||
</Tab>
|
||||
<Tab>
|
||||
@@ -206,6 +274,6 @@ import { Image } from '@/components/ui/image'
|
||||
|
||||
## ベストプラクティス
|
||||
|
||||
- **適切な制限を設定する**: 実行時間が長くならないよう、繰り返し回数を適切に保つ
|
||||
- **コレクションにはForEachを使用する**: 配列やオブジェクトを処理する場合は、Forループの代わりにForEachを使用する
|
||||
- **適切な制限を設定する**: 長い実行時間を避けるため、繰り返し回数を適切に保つ
|
||||
- **コレクションにはForEachを使用する**: 配列やオブジェクトを処理する場合、Forループの代わりにForEachを使用する
|
||||
- **エラーを適切に処理する**: 堅牢なワークフローのために、ループ内にエラー処理を追加することを検討する
|
||||
|
||||
123
apps/docs/content/docs/ja/blocks/variables.mdx
Normal file
123
apps/docs/content/docs/ja/blocks/variables.mdx
Normal file
@@ -0,0 +1,123 @@
|
||||
---
|
||||
title: 変数
|
||||
---
|
||||
|
||||
import { Callout } from 'fumadocs-ui/components/callout'
|
||||
import { Step, Steps } from 'fumadocs-ui/components/steps'
|
||||
import { Image } from '@/components/ui/image'
|
||||
|
||||
変数ブロックは実行中にワークフロー変数を更新します。変数はまずワークフローの変数セクションで初期化する必要があり、その後このブロックを使用してワークフロー実行中に値を更新できます。
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/variables.png"
|
||||
alt="変数ブロック"
|
||||
width={500}
|
||||
height={350}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Callout>
|
||||
ワークフロー内のどこからでも `<variable.variableName>` 構文を使用して変数にアクセスできます。
|
||||
</Callout>
|
||||
|
||||
## 概要
|
||||
|
||||
変数ブロックでは以下のことが可能です:
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
<strong>ワークフロー変数の更新</strong>:実行中に変数値を変更する
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>動的データの保存</strong>:ブロック出力を変数に取り込む
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>状態の維持</strong>:カウンター、フラグ、中間結果を追跡する
|
||||
</Step>
|
||||
</Steps>
|
||||
|
||||
## 変数の使用方法
|
||||
|
||||
### 1. ワークフロー変数での初期化
|
||||
|
||||
まず、ワークフローの変数セクション(ワークフロー設定からアクセス可能)で変数を作成します:
|
||||
|
||||
```
|
||||
customerEmail = ""
|
||||
retryCount = 0
|
||||
currentStatus = "pending"
|
||||
```
|
||||
|
||||
### 2. 変数ブロックでの更新
|
||||
|
||||
変数ブロックを使用して実行中にこれらの値を更新します:
|
||||
|
||||
```
|
||||
customerEmail = <api.email>
|
||||
retryCount = <variable.retryCount> + 1
|
||||
currentStatus = "processing"
|
||||
```
|
||||
|
||||
### 3. どこからでもアクセス
|
||||
|
||||
任意のブロックで変数を参照します:
|
||||
|
||||
```
|
||||
Agent prompt: "Send email to <variable.customerEmail>"
|
||||
Condition: <variable.retryCount> < 5
|
||||
API body: {"status": "<variable.currentStatus>"}
|
||||
```
|
||||
|
||||
## 使用例
|
||||
|
||||
### ループカウンターと状態
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">シナリオ:ループ反復の進行状況を追跡する</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>ワークフローで初期化:`itemsProcessed = 0`、`lastResult = ""`</li>
|
||||
<li>ループがアイテムを反復処理</li>
|
||||
<li>ループ内:エージェントが現在のアイテムを処理</li>
|
||||
<li>ループ内:変数が `itemsProcessed = <variable.itemsProcessed> + 1` を更新</li>
|
||||
<li>ループ内:変数が `lastResult = <agent.content>` を更新</li>
|
||||
<li>次の反復:`<variable.lastResult>` にアクセスして現在の結果と比較</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### リトライロジック
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">シナリオ:APIリトライ試行を追跡する</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>ワークフローで初期化:`retryCount = 0`</li>
|
||||
<li>APIブロックがリクエストを試行</li>
|
||||
<li>失敗した場合、変数がインクリメント:---INLINE-CODE-PLACEHOLDER-e289a5ca49f66a6001d9263f3b890a8d---</li>
|
||||
<li>条件が `<variable.retryCount>` \< 3 をチェックしてリトライするか失敗するか</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### 動的設定
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">シナリオ:ワークフロー用のユーザーコンテキストを保存する</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>ワークフローで初期化:`userId = ""`, `userTier = ""`</li>
|
||||
<li>APIがユーザープロファイルを取得</li>
|
||||
<li>変数が保存:`userId = <api.id>`, `userTier = <api.tier>`</li>
|
||||
<li>エージェントが`<variable.userTier>`を使用してレスポンスをパーソナライズ</li>
|
||||
<li>APIがログ記録に`<variable.userId>`を使用</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
## 出力
|
||||
|
||||
- **`<variables.assignments>`**:このブロックからのすべての変数割り当てを含むJSONオブジェクト
|
||||
|
||||
## ベストプラクティス
|
||||
|
||||
- **ワークフロー設定で初期化**:使用する前に必ずワークフローの変数セクションで変数を作成する
|
||||
- **動的に更新**:ブロック出力や計算に基づいて値を更新するために変数ブロックを使用する
|
||||
- **ループで使用**:反復処理間で状態を追跡するのに最適
|
||||
- **わかりやすい名前を付ける**:`currentIndex`、`totalProcessed`、または`lastError`のような明確な名前を使用する
|
||||
99
apps/docs/content/docs/ja/blocks/wait.mdx
Normal file
99
apps/docs/content/docs/ja/blocks/wait.mdx
Normal file
@@ -0,0 +1,99 @@
|
||||
---
|
||||
title: 待機
|
||||
---
|
||||
|
||||
import { Callout } from 'fumadocs-ui/components/callout'
|
||||
import { Step, Steps } from 'fumadocs-ui/components/steps'
|
||||
import { Image } from '@/components/ui/image'
|
||||
|
||||
待機ブロックは、次のブロックに進む前に指定された時間だけワークフローを一時停止します。アクション間に遅延を追加したり、APIレート制限を尊重したり、操作の間隔を空けたりするために使用します。
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/wait.png"
|
||||
alt="待機ブロック"
|
||||
width={500}
|
||||
height={350}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
## 概要
|
||||
|
||||
待機ブロックでは以下のことが可能です:
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
<strong>時間遅延の追加</strong>:ワークフローステップ間で実行を一時停止する
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>レート制限の尊重</strong>:制限内に収まるようにAPI呼び出しの間隔を空ける
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>シーケンスのスケジュール</strong>:アクション間に遅延を持つタイムドワークフローを作成する
|
||||
</Step>
|
||||
</Steps>
|
||||
|
||||
## 設定
|
||||
|
||||
### 待機時間
|
||||
|
||||
実行を一時停止する時間を入力します:
|
||||
- **入力**:正の数値
|
||||
- **最大値**:600秒(10分)または10分
|
||||
|
||||
### 単位
|
||||
|
||||
時間単位を選択します:
|
||||
- **秒**:短く正確な遅延のため
|
||||
- **分**:より長い一時停止のため
|
||||
|
||||
<Callout type="info">
|
||||
待機ブロックはワークフローを停止することでキャンセルできます。最大待機時間は10分です。
|
||||
</Callout>
|
||||
|
||||
## 出力
|
||||
|
||||
- **`<wait.waitDuration>`**: ミリ秒単位の待機時間
|
||||
- **`<wait.status>`**: 待機のステータス('waiting'、'completed'、または'cancelled')
|
||||
|
||||
## 使用例
|
||||
|
||||
### APIレート制限
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">シナリオ:APIレート制限内に収める</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>APIブロックが最初のリクエストを行う</li>
|
||||
<li>待機ブロックが2秒間一時停止する</li>
|
||||
<li>APIブロックが2番目のリクエストを行う</li>
|
||||
<li>レート制限に達することなくプロセスが続行される</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### タイムド通知
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">シナリオ:フォローアップメッセージを送信する</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>関数が初期メールを送信する</li>
|
||||
<li>待機ブロックが5分間一時停止する</li>
|
||||
<li>関数がフォローアップメールを送信する</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### 処理の遅延
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">シナリオ:外部システムを待機</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>APIブロックが外部システムでジョブをトリガー</li>
|
||||
<li>待機ブロックが30秒間一時停止</li>
|
||||
<li>APIブロックがジョブの完了状態を確認</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
## ベストプラクティス
|
||||
|
||||
- **待機時間を適切に保つ**:最大10分までの遅延には待機を使用。より長い遅延にはスケジュールされたワークフローを検討
|
||||
- **実行時間を監視する**:待機によってワークフローの合計所要時間が延長されることに注意
|
||||
@@ -207,18 +207,18 @@ Populate Clay with data from a JSON file. Enables direct communication and notif
|
||||
|
||||
#### Input
|
||||
|
||||
| Parameter | Type | Required | Description |
|
||||
| パラメータ | 型 | 必須 | 説明 |
|
||||
| --------- | ---- | -------- | ----------- |
|
||||
| `webhookURL` | string | Yes | The webhook URL to populate |
|
||||
| `data` | json | Yes | The data to populate |
|
||||
| `authToken` | string | Yes | Auth token for Clay webhook authentication |
|
||||
| `webhookURL` | string | はい | 設定するウェブフックURL |
|
||||
| `data` | json | はい | 設定するデータ |
|
||||
| `authToken` | string | いいえ | Clayウェブフック認証用のオプション認証トークン(ほとんどのウェブフックではこれは不要です) |
|
||||
|
||||
#### Output
|
||||
|
||||
| Parameter | Type | Description |
|
||||
| パラメータ | 型 | 説明 |
|
||||
| --------- | ---- | ----------- |
|
||||
| `success` | boolean | Operation success status |
|
||||
| `output` | json | Clay populate operation results including response data from Clay webhook |
|
||||
| `data` | json | Clayウェブフックからのレスポンスデータ |
|
||||
| `metadata` | object | ウェブフックレスポンスのメタデータ |
|
||||
|
||||
## Notes
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import { BlockInfoCard } from "@/components/ui/block-info-card"
|
||||
|
||||
<BlockInfoCard
|
||||
type="discord"
|
||||
color="#E0E0E0"
|
||||
color="#5865F2"
|
||||
icon={true}
|
||||
iconSvg={`<svg className="block-icon"
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ Simでは、OneDrive統合によりエージェントがクラウドストレー
|
||||
|
||||
## 使用方法
|
||||
|
||||
OneDriveをワークフローに統合します。ファイルの作成、アップロード、一覧表示が可能です。OAuthが必要です。
|
||||
OneDriveをワークフローに統合します。テキストファイルやExcelファイルの作成、ファイルのアップロード、ファイルの一覧表示が可能です。
|
||||
|
||||
## ツール
|
||||
|
||||
@@ -60,11 +60,12 @@ OneDriveにファイルをアップロードする
|
||||
|
||||
#### 入力
|
||||
|
||||
| パラメータ | 型 | 必須 | 説明 |
|
||||
| パラメータ | 種類 | 必須 | 説明 |
|
||||
| --------- | ---- | -------- | ----------- |
|
||||
| `fileName` | string | はい | アップロードするファイルの名前 |
|
||||
| `file` | file | いいえ | アップロードするファイル(バイナリ) |
|
||||
| `content` | string | いいえ | アップロードするテキストコンテンツ(ファイルが提供されていない場合) |
|
||||
| `content` | string | いいえ | アップロードするテキスト内容(ファイルが提供されていない場合) |
|
||||
| `mimeType` | string | いいえ | 作成するファイルのMIMEタイプ(例:.txtの場合はtext/plain、.xlsxの場合はapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet) |
|
||||
| `folderSelector` | string | いいえ | ファイルをアップロードするフォルダを選択 |
|
||||
| `manualFolderId` | string | いいえ | 手動で入力したフォルダID(高度なモード) |
|
||||
|
||||
|
||||
@@ -140,6 +140,7 @@ Qdrantコレクション内で類似ベクトルを検索する
|
||||
| `vector` | array | はい | 検索対象のベクトル |
|
||||
| `limit` | number | いいえ | 返す結果の数 |
|
||||
| `filter` | object | いいえ | 検索に適用するフィルター |
|
||||
| `search_return_data` | string | いいえ | 検索から返すデータ |
|
||||
| `with_payload` | boolean | いいえ | レスポンスにペイロードを含める |
|
||||
| `with_vector` | boolean | いいえ | レスポンスにベクトルを含める |
|
||||
|
||||
@@ -162,6 +163,7 @@ QdrantコレクションからIDによってポイントを取得する
|
||||
| `apiKey` | string | いいえ | Qdrant APIキー(オプション) |
|
||||
| `collection` | string | はい | コレクション名 |
|
||||
| `ids` | array | はい | 取得するポイントIDの配列 |
|
||||
| `fetch_return_data` | string | いいえ | 取得から返すデータ |
|
||||
| `with_payload` | boolean | いいえ | レスポンスにペイロードを含める |
|
||||
| `with_vector` | boolean | いいえ | レスポンスにベクトルを含める |
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ import { Video } from '@/components/ui/video'
|
||||
|
||||
## 核心模块类型
|
||||
|
||||
Sim 提供了七种核心模块类型,用于处理 AI 工作流的基本功能:
|
||||
Sim 提供了处理 AI 工作流核心功能的基本模块类型:
|
||||
|
||||
### 处理模块
|
||||
- **[Agent](/blocks/agent)** - 与 AI 模型(OpenAI、Anthropic、Google、本地模型)聊天
|
||||
@@ -28,16 +28,20 @@ Sim 提供了七种核心模块类型,用于处理 AI 工作流的基本功能
|
||||
- **[Router](/blocks/router)** - 使用 AI 智能路由请求到不同路径
|
||||
- **[Evaluator](/blocks/evaluator)** - 使用 AI 评分并评估内容质量
|
||||
|
||||
### 输出模块
|
||||
- **[Response](/blocks/response)** - 格式化并返回工作流的最终结果
|
||||
### 控制流模块
|
||||
- **[变量](/blocks/variables)** - 设置和管理工作流范围内的变量
|
||||
- **[等待](/blocks/wait)** - 暂停工作流执行指定的时间延迟
|
||||
|
||||
## 模块如何工作
|
||||
### 输出模块
|
||||
- **[响应](/blocks/response)** - 格式化并返回工作流的最终结果
|
||||
|
||||
## 模块的工作原理
|
||||
|
||||
每个模块有三个主要组成部分:
|
||||
|
||||
**输入**:从其他模块或用户输入接收的数据
|
||||
**配置**:控制模块行为的设置
|
||||
**输出**:模块为其他模块使用而生成的数据
|
||||
**输出**:模块为其他模块生成的数据
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
@@ -66,14 +70,14 @@ Sim 提供了七种核心模块类型,用于处理 AI 工作流的基本功能
|
||||
## 常见模式
|
||||
|
||||
### 顺序处理
|
||||
将模块按链式连接,每个模块处理前一个模块的输出:
|
||||
将模块连接成链,每个模块处理前一个模块的输出:
|
||||
|
||||
```
|
||||
User Input → Agent → Function → Response
|
||||
```
|
||||
|
||||
### 条件分支
|
||||
使用条件模块或路由模块创建不同的路径:
|
||||
使用条件或路由模块创建不同的路径:
|
||||
|
||||
```
|
||||
User Input → Router → Agent A (for questions)
|
||||
@@ -81,7 +85,7 @@ User Input → Router → Agent A (for questions)
|
||||
```
|
||||
|
||||
### 质量控制
|
||||
使用评估模块评估和过滤输出:
|
||||
使用评估模块评估和筛选输出:
|
||||
|
||||
```
|
||||
Agent → Evaluator → Condition → Response (if good)
|
||||
@@ -97,33 +101,39 @@ Agent → Evaluator → Condition → Response (if good)
|
||||
- 错误处理行为
|
||||
- 执行超时设置
|
||||
|
||||
**AI 模块**(代理、路由、评估):
|
||||
**AI 模块**(Agent、Router、Evaluator):
|
||||
- 模型选择(OpenAI、Anthropic、Google、本地)
|
||||
- API 密钥和认证
|
||||
- API 密钥和身份验证
|
||||
- 温度及其他模型参数
|
||||
- 系统提示和指令
|
||||
|
||||
**逻辑模块**(条件、函数):
|
||||
**逻辑模块**(Condition、Function):
|
||||
- 自定义表达式或代码
|
||||
- 变量引用
|
||||
- 执行环境设置
|
||||
|
||||
**集成模块**(API、响应):
|
||||
**集成模块**(API、Response):
|
||||
- 端点配置
|
||||
- 请求头和认证
|
||||
- 请求头和身份验证
|
||||
- 请求/响应格式化
|
||||
|
||||
<Cards>
|
||||
<Card title="代理模块" href="/blocks/agent">
|
||||
<Card title="Agent 模块" href="/blocks/agent">
|
||||
连接 AI 模型并创建智能响应
|
||||
</Card>
|
||||
<Card title="函数模块" href="/blocks/function">
|
||||
<Card title="Function 模块" href="/blocks/function">
|
||||
运行自定义代码以处理和转换数据
|
||||
</Card>
|
||||
<Card title="API 模块" href="/blocks/api">
|
||||
与外部服务和 API 集成
|
||||
</Card>
|
||||
<Card title="条件模块" href="/blocks/condition">
|
||||
<Card title="Condition 模块" href="/blocks/condition">
|
||||
基于数据评估创建分支逻辑
|
||||
</Card>
|
||||
<Card title="Variables 模块" href="/blocks/variables">
|
||||
设置和管理工作流范围的变量
|
||||
</Card>
|
||||
<Card title="Wait 模块" href="/blocks/wait">
|
||||
暂停工作流执行指定的时间延迟
|
||||
</Card>
|
||||
</Cards>
|
||||
|
||||
@@ -9,7 +9,7 @@ import { Image } from '@/components/ui/image'
|
||||
|
||||
循环块是 Sim 中的一个容器块,它允许您通过重复执行一组块来创建迭代工作流。循环使您的工作流能够进行迭代处理。
|
||||
|
||||
循环块支持两种类型的迭代:
|
||||
循环块支持四种类型的迭代:
|
||||
|
||||
<Callout type="info">
|
||||
循环块是容器节点,可以在其中包含其他块。根据您的配置,循环中的块将多次执行。
|
||||
@@ -21,13 +21,13 @@ import { Image } from '@/components/ui/image'
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
<strong>迭代集合</strong>:逐项处理数组或对象
|
||||
<strong>遍历集合</strong>:逐一处理数组或对象中的项目
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>重复操作</strong>:执行固定次数的块
|
||||
<strong>重复操作</strong>:执行块固定次数
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>顺序处理</strong>:按顺序迭代处理数据转换
|
||||
<strong>基于条件循环</strong>:在满足或直到满足条件时继续执行
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>汇总结果</strong>:收集所有循环迭代的输出
|
||||
@@ -47,23 +47,23 @@ import { Image } from '@/components/ui/image'
|
||||
|
||||
### 循环类型
|
||||
|
||||
选择两种循环类型之一:
|
||||
选择四种循环类型之一:
|
||||
|
||||
<Tabs items={['For 循环', 'ForEach 循环']}>
|
||||
<Tabs items={['For Loop', 'ForEach Loop', 'While Loop', 'Do-While Loop']}>
|
||||
<Tab>
|
||||
**For 循环(迭代次数)** - 一种执行固定次数的数字循环:
|
||||
**For 循环(迭代)** - 一个执行固定次数的数字循环:
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/loop-1.png"
|
||||
alt="具有迭代次数的 For 循环"
|
||||
alt="带有迭代的 For 循环"
|
||||
width={500}
|
||||
height={400}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
当您需要重复操作特定次数时使用此选项。
|
||||
当需要重复操作特定次数时使用此方法。
|
||||
|
||||
|
||||
```
|
||||
@@ -100,11 +100,63 @@ import { Image } from '@/components/ui/image'
|
||||
```
|
||||
|
||||
</Tab>
|
||||
<Tab>
|
||||
**While 循环(基于条件)** - 当条件为真时继续执行:
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/loop-3.png"
|
||||
alt="带有条件的 While 循环"
|
||||
width={500}
|
||||
height={400}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
当需要循环直到满足特定条件时使用此方法。条件在每次迭代**之前**检查。
|
||||
|
||||
|
||||
```
|
||||
Example: While <variable.i> < 10
|
||||
- Check condition → Execute if true
|
||||
- Inside loop: Increment <variable.i>
|
||||
- Inside loop: Variables assigns i = <variable.i> + 1
|
||||
- Check condition → Execute if true
|
||||
- Check condition → Exit if false
|
||||
```
|
||||
|
||||
</Tab>
|
||||
<Tab>
|
||||
**Do-While 循环(基于条件)** - 至少执行一次,然后在条件为真时继续:
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/loop-3.png"
|
||||
alt="带有条件的 Do-While 循环"
|
||||
width={500}
|
||||
height={400}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
当需要至少执行一次,然后循环直到满足条件时使用此方法。条件在每次迭代**之后**检查。
|
||||
|
||||
|
||||
```
|
||||
Example: Do-while <variable.i> < 10
|
||||
- Execute blocks
|
||||
- Inside loop: Increment <variable.i>
|
||||
- Inside loop: Variables assigns i = <variable.i> + 1
|
||||
- Check condition → Continue if true
|
||||
- Check condition → Exit if false
|
||||
```
|
||||
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
## 如何使用循环
|
||||
|
||||
### 创建循环
|
||||
### 创建一个循环
|
||||
|
||||
1. 从工具栏中拖动一个循环块到画布上
|
||||
2. 配置循环类型和参数
|
||||
@@ -113,9 +165,9 @@ import { Image } from '@/components/ui/image'
|
||||
|
||||
### 访问结果
|
||||
|
||||
循环完成后,您可以访问聚合结果:
|
||||
循环完成后,您可以访问汇总结果:
|
||||
|
||||
- **`<loop.results>`**:来自所有循环迭代的结果数组
|
||||
- **`<loop.results>`**:所有循环迭代的结果数组
|
||||
|
||||
## 示例用例
|
||||
|
||||
@@ -143,21 +195,34 @@ import { Image } from '@/components/ui/image'
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### 使用 While 循环的计数器
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">场景:使用基于计数器的循环处理项目</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>初始化工作流变量:`i = 0`</li>
|
||||
<li>While 循环条件:`<variable.i>` \< 10</li>
|
||||
<li>循环内:代理处理索引为 `<variable.i>` 的项目</li>
|
||||
<li>循环内:变量递增 `i = <variable.i> + 1`</li>
|
||||
<li>循环在 i 小于 10 时继续</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
## 高级功能
|
||||
|
||||
### 限制
|
||||
|
||||
<Callout type="warning">
|
||||
容器块(循环和并行)不能嵌套在彼此内部。这意味着:
|
||||
- 您不能将一个循环块放入另一个循环块中
|
||||
- 您不能将一个并行块放入循环块中
|
||||
- 您不能将循环块放入另一个循环块中
|
||||
- 您不能将并行块放入循环块中
|
||||
- 您不能将任何容器块放入另一个容器块中
|
||||
|
||||
如果需要多维迭代,请考虑重构您的工作流程以使用顺序循环或分阶段处理数据。
|
||||
如果您需要多维迭代,请考虑重构您的工作流以使用顺序循环或分阶段处理数据。
|
||||
</Callout>
|
||||
|
||||
<Callout type="info">
|
||||
循环是按顺序执行的,而不是并行执行。如果需要并发执行,请改用 Parallel 块。
|
||||
循环是按顺序执行的,而不是并行执行。如果您需要并发执行,请改用并行块。
|
||||
</Callout>
|
||||
|
||||
## 输入和输出
|
||||
@@ -166,7 +231,7 @@ import { Image } from '@/components/ui/image'
|
||||
<Tab>
|
||||
<ul className="list-disc space-y-2 pl-6">
|
||||
<li>
|
||||
<strong>循环类型</strong>:选择 'for' 或 'forEach'
|
||||
<strong>循环类型</strong>:选择 'for'、'forEach'、'while' 或 'doWhile'
|
||||
</li>
|
||||
<li>
|
||||
<strong>迭代次数</strong>:执行的次数(适用于 for 循环)
|
||||
@@ -174,6 +239,9 @@ import { Image } from '@/components/ui/image'
|
||||
<li>
|
||||
<strong>集合</strong>:要迭代的数组或对象(适用于 forEach 循环)
|
||||
</li>
|
||||
<li>
|
||||
<strong>条件</strong>:要评估的布尔表达式(适用于 while/do-while 循环)
|
||||
</li>
|
||||
</ul>
|
||||
</Tab>
|
||||
<Tab>
|
||||
@@ -198,7 +266,7 @@ import { Image } from '@/components/ui/image'
|
||||
<strong>结构</strong>:结果保持迭代顺序
|
||||
</li>
|
||||
<li>
|
||||
<strong>访问</strong>:可在循环后的块中使用
|
||||
<strong>访问</strong>:在循环后的块中可用
|
||||
</li>
|
||||
</ul>
|
||||
</Tab>
|
||||
@@ -206,6 +274,6 @@ import { Image } from '@/components/ui/image'
|
||||
|
||||
## 最佳实践
|
||||
|
||||
- **设置合理的限制**:保持迭代次数在合理范围内,以避免长时间执行
|
||||
- **设置合理的限制**:保持迭代次数在合理范围内,以避免长时间的执行
|
||||
- **对集合使用 ForEach**:在处理数组或对象时,使用 ForEach 而不是 For 循环
|
||||
- **优雅地处理错误**:考虑在循环中添加错误处理,以实现更健壮的工作流
|
||||
|
||||
123
apps/docs/content/docs/zh/blocks/variables.mdx
Normal file
123
apps/docs/content/docs/zh/blocks/variables.mdx
Normal file
@@ -0,0 +1,123 @@
|
||||
---
|
||||
title: 变量
|
||||
---
|
||||
|
||||
import { Callout } from 'fumadocs-ui/components/callout'
|
||||
import { Step, Steps } from 'fumadocs-ui/components/steps'
|
||||
import { Image } from '@/components/ui/image'
|
||||
|
||||
变量模块在执行过程中更新工作流变量。变量必须首先在工作流的变量部分中初始化,然后您可以使用此模块在工作流运行时更新其值。
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/variables.png"
|
||||
alt="变量模块"
|
||||
width={500}
|
||||
height={350}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Callout>
|
||||
在工作流中的任何地方使用 `<variable.variableName>` 语法访问变量。
|
||||
</Callout>
|
||||
|
||||
## 概述
|
||||
|
||||
变量模块使您能够:
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
<strong>更新工作流变量</strong>:在执行过程中更改变量值
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>存储动态数据</strong>:将模块输出捕获到变量中
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>维护状态</strong>:跟踪计数器、标志和中间结果
|
||||
</Step>
|
||||
</Steps>
|
||||
|
||||
## 如何使用变量
|
||||
|
||||
### 1. 在工作流变量中初始化
|
||||
|
||||
首先,在工作流的变量部分(可通过工作流设置访问)中创建您的变量:
|
||||
|
||||
```
|
||||
customerEmail = ""
|
||||
retryCount = 0
|
||||
currentStatus = "pending"
|
||||
```
|
||||
|
||||
### 2. 使用变量模块更新
|
||||
|
||||
使用变量模块在执行过程中更新这些值:
|
||||
|
||||
```
|
||||
customerEmail = <api.email>
|
||||
retryCount = <variable.retryCount> + 1
|
||||
currentStatus = "processing"
|
||||
```
|
||||
|
||||
### 3. 随处访问
|
||||
|
||||
在任何模块中引用变量:
|
||||
|
||||
```
|
||||
Agent prompt: "Send email to <variable.customerEmail>"
|
||||
Condition: <variable.retryCount> < 5
|
||||
API body: {"status": "<variable.currentStatus>"}
|
||||
```
|
||||
|
||||
## 示例用例
|
||||
|
||||
### 循环计数器和状态
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">场景:跟踪循环迭代的进度</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>在工作流中初始化:`itemsProcessed = 0`, `lastResult = ""`</li>
|
||||
<li>循环迭代项目</li>
|
||||
<li>在循环内:代理处理当前项目</li>
|
||||
<li>在循环内:变量模块更新 `itemsProcessed = <variable.itemsProcessed> + 1`</li>
|
||||
<li>在循环内:变量模块更新 `lastResult = <agent.content>`</li>
|
||||
<li>下一次迭代:访问 `<variable.lastResult>` 与当前结果进行比较</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### 重试逻辑
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">场景:跟踪 API 重试尝试</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>在工作流中初始化:`retryCount = 0`</li>
|
||||
<li>API 模块尝试请求</li>
|
||||
<li>如果失败,变量递增:`retryCount = <variable.retryCount> + 1`</li>
|
||||
<li>条件检查 `<variable.retryCount>` \< 3 以重试或失败</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### 动态配置
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">场景:为工作流存储用户上下文</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>在工作流中初始化:`userId = ""`,`userTier = ""`</li>
|
||||
<li>API 获取用户资料</li>
|
||||
<li>变量存储:`userId = <api.id>`,`userTier = <api.tier>`</li>
|
||||
<li>代理使用 `<variable.userTier>` 个性化响应</li>
|
||||
<li>API 使用 `<variable.userId>` 进行日志记录</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
## 输出
|
||||
|
||||
- **`<variables.assignments>`**:包含此块中所有变量分配的 JSON 对象
|
||||
|
||||
## 最佳实践
|
||||
|
||||
- **在工作流设置中初始化**:在使用变量之前,始终在工作流的变量部分创建它们
|
||||
- **动态更新**:使用变量块根据块输出或计算结果更新值
|
||||
- **在循环中使用**:非常适合在迭代中跟踪状态
|
||||
- **命名清晰**:使用明确的名称,例如 `currentIndex`,`totalProcessed`,或 `lastError`
|
||||
99
apps/docs/content/docs/zh/blocks/wait.mdx
Normal file
99
apps/docs/content/docs/zh/blocks/wait.mdx
Normal file
@@ -0,0 +1,99 @@
|
||||
---
|
||||
title: 等待
|
||||
---
|
||||
|
||||
import { Callout } from 'fumadocs-ui/components/callout'
|
||||
import { Step, Steps } from 'fumadocs-ui/components/steps'
|
||||
import { Image } from '@/components/ui/image'
|
||||
|
||||
等待模块会在继续执行下一个模块之前暂停您的工作流一段指定的时间。使用它可以在操作之间添加延迟、遵守 API 速率限制或间隔操作。
|
||||
|
||||
<div className="flex justify-center">
|
||||
<Image
|
||||
src="/static/blocks/wait.png"
|
||||
alt="等待模块"
|
||||
width={500}
|
||||
height={350}
|
||||
className="my-6"
|
||||
/>
|
||||
</div>
|
||||
|
||||
## 概述
|
||||
|
||||
等待模块可以让您:
|
||||
|
||||
<Steps>
|
||||
<Step>
|
||||
<strong>添加时间延迟</strong>:在工作流步骤之间暂停执行
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>遵守速率限制</strong>:间隔 API 调用以保持在限制范围内
|
||||
</Step>
|
||||
<Step>
|
||||
<strong>安排序列</strong>:创建带有操作间延迟的定时工作流
|
||||
</Step>
|
||||
</Steps>
|
||||
|
||||
## 配置
|
||||
|
||||
### 等待时间
|
||||
|
||||
输入暂停执行的时长:
|
||||
- **输入**:正数
|
||||
- **最大值**:600 秒(10 分钟)或 10 分钟
|
||||
|
||||
### 单位
|
||||
|
||||
选择时间单位:
|
||||
- **秒**:用于短时间、精确的延迟
|
||||
- **分钟**:用于较长时间的暂停
|
||||
|
||||
<Callout type="info">
|
||||
等待模块可以通过停止工作流来取消。最大等待时间为 10 分钟。
|
||||
</Callout>
|
||||
|
||||
## 输出
|
||||
|
||||
- **`<wait.waitDuration>`**:等待时长(以毫秒为单位)
|
||||
- **`<wait.status>`**:等待状态(“等待中”、“已完成”或“已取消”)
|
||||
|
||||
## 示例用例
|
||||
|
||||
### API 速率限制
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">场景:保持在 API 速率限制范围内</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>API 模块发出第一个请求</li>
|
||||
<li>等待模块暂停 2 秒</li>
|
||||
<li>API 模块发出第二个请求</li>
|
||||
<li>流程继续而不会触发速率限制</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### 定时通知
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">场景:发送后续消息</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>函数发送初始电子邮件</li>
|
||||
<li>等待模块暂停 5 分钟</li>
|
||||
<li>函数发送后续电子邮件</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
### 处理延迟
|
||||
|
||||
<div className="mb-4 rounded-md border p-4">
|
||||
<h4 className="font-medium">场景:等待外部系统</h4>
|
||||
<ol className="list-decimal pl-5 text-sm">
|
||||
<li>API 块触发外部系统中的作业</li>
|
||||
<li>等待块暂停 30 秒</li>
|
||||
<li>API 块检查作业完成状态</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
## 最佳实践
|
||||
|
||||
- **保持合理的等待时间**:使用等待块处理最长 10 分钟的延迟。对于更长的延迟,请考虑使用计划的工作流
|
||||
- **监控执行时间**:请记住,等待会延长工作流的总持续时间
|
||||
@@ -209,16 +209,16 @@ import { BlockInfoCard } from "@/components/ui/block-info-card"
|
||||
|
||||
| 参数 | 类型 | 必需 | 描述 |
|
||||
| --------- | ---- | -------- | ----------- |
|
||||
| `webhookURL` | string | 是 | 用于填充的 webhook URL |
|
||||
| `webhookURL` | string | 是 | 要填充的 webhook URL |
|
||||
| `data` | json | 是 | 要填充的数据 |
|
||||
| `authToken` | string | 是 | 用于 Clay webhook 认证的授权令牌 |
|
||||
| `authToken` | string | 否 | 用于 Clay webhook 认证的可选身份验证令牌(大多数 webhook 不需要此令牌) |
|
||||
|
||||
#### 输出
|
||||
|
||||
| 参数 | 类型 | 描述 |
|
||||
| --------- | ---- | ----------- |
|
||||
| `success` | boolean | 操作成功状态 |
|
||||
| `output` | json | Clay 填充操作结果,包括来自 Clay webhook 的响应数据 |
|
||||
| `data` | json | 来自 Clay webhook 的响应数据 |
|
||||
| `metadata` | object | webhook 响应元数据 |
|
||||
|
||||
## 注意事项
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import { BlockInfoCard } from "@/components/ui/block-info-card"
|
||||
|
||||
<BlockInfoCard
|
||||
type="discord"
|
||||
color="#E0E0E0"
|
||||
color="#5865F2"
|
||||
icon={true}
|
||||
iconSvg={`<svg className="block-icon"
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ import { BlockInfoCard } from "@/components/ui/block-info-card"
|
||||
|
||||
## 使用说明
|
||||
|
||||
将 OneDrive 集成到工作流程中。可以创建、上传和列出文件。需要 OAuth。
|
||||
将 OneDrive 集成到工作流程中。可以创建文本和 Excel 文件、上传文件以及列出文件。
|
||||
|
||||
## 工具
|
||||
|
||||
@@ -62,9 +62,10 @@ import { BlockInfoCard } from "@/components/ui/block-info-card"
|
||||
|
||||
| 参数 | 类型 | 必需 | 描述 |
|
||||
| --------- | ---- | -------- | ----------- |
|
||||
| `fileName` | string | 是 | 要上传文件的名称 |
|
||||
| `fileName` | string | 是 | 要上传的文件名称 |
|
||||
| `file` | file | 否 | 要上传的文件 \(二进制\) |
|
||||
| `content` | string | 否 | 要上传的文本内容 \(如果未提供文件\) |
|
||||
| `mimeType` | string | 否 | 要创建文件的 MIME 类型 \(例如,.txt 的 text/plain,.xlsx 的 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\) |
|
||||
| `folderSelector` | string | 否 | 选择上传文件的文件夹 |
|
||||
| `manualFolderId` | string | 否 | 手动输入的文件夹 ID \(高级模式\) |
|
||||
|
||||
|
||||
@@ -138,8 +138,9 @@ import { BlockInfoCard } from "@/components/ui/block-info-card"
|
||||
| `apiKey` | string | 否 | Qdrant API 密钥(可选)|
|
||||
| `collection` | string | 是 | 集合名称 |
|
||||
| `vector` | array | 是 | 要搜索的向量 |
|
||||
| `limit` | number | 否 | 返回结果的数量 |
|
||||
| `filter` | object | 否 | 应用于搜索的过滤器 |
|
||||
| `limit` | number | 否 | 要返回的结果数量 |
|
||||
| `filter` | object | 否 | 要应用于搜索的过滤器 |
|
||||
| `search_return_data` | string | 否 | 搜索中要返回的数据 |
|
||||
| `with_payload` | boolean | 否 | 在响应中包含有效负载 |
|
||||
| `with_vector` | boolean | 否 | 在响应中包含向量 |
|
||||
|
||||
@@ -162,6 +163,7 @@ import { BlockInfoCard } from "@/components/ui/block-info-card"
|
||||
| `apiKey` | string | 否 | Qdrant API 密钥(可选)|
|
||||
| `collection` | string | 是 | 集合名称 |
|
||||
| `ids` | array | 是 | 要获取的点 ID 数组 |
|
||||
| `fetch_return_data` | string | 否 | 获取中要返回的数据 |
|
||||
| `with_payload` | boolean | 否 | 在响应中包含有效负载 |
|
||||
| `with_vector` | boolean | 否 | 在响应中包含向量 |
|
||||
|
||||
|
||||
@@ -945,13 +945,13 @@ checksums:
|
||||
content/17: f5bef3db56ed3a56395f7ae1fa41ecf3
|
||||
content/18: 7ca733ac5374e92a9cc8ef35e1075fb1
|
||||
content/19: 371d0e46b4bd2c23f559b8bc112f6955
|
||||
content/20: 8ce52b8ffed51482dff6fa0f2846c498
|
||||
content/20: 0eada001684acb8efe28fcfed38a5163
|
||||
content/21: bcadfc362b69078beee0088e5936c98b
|
||||
content/22: b875ec2f16d200917e9860b49a5a9772
|
||||
content/23: c0c2276dd4207eb2b08d4dc9132e7ec3
|
||||
content/24: 85de953906920f3fb4eafa8fdb918feb
|
||||
content/25: 371d0e46b4bd2c23f559b8bc112f6955
|
||||
content/26: f63654037687387924343d8f7453f379
|
||||
content/26: b0cf90320ac6b98d5bf00b87052cd76e
|
||||
content/27: bcadfc362b69078beee0088e5936c98b
|
||||
content/28: e62b89406f01af79e2e293d352aa2499
|
||||
content/29: b3f310d5ef115bea5a8b75bf25d7ea9a
|
||||
@@ -1153,12 +1153,12 @@ checksums:
|
||||
content/5: bcd65b6873b071b2831821ae39804ba9
|
||||
content/6: 517dd4ba787691643cb3e929ca662fa9
|
||||
content/7: 821e6394b0a953e2b0842b04ae8f3105
|
||||
content/8: b012f506c5556814c599055fa2e85d74
|
||||
content/8: 614f192a01b39eb448d70a6145488cbb
|
||||
content/9: 9c8aa3f09c9b2bd50ea4cdff3598ea4e
|
||||
content/10: 6df7e096bafa5efda818db1ab65841fd
|
||||
content/11: 646990da68ff77ca02dd172f830f12f0
|
||||
content/12: 371d0e46b4bd2c23f559b8bc112f6955
|
||||
content/13: 6979ff1359b3ede098d71e35fabd6a05
|
||||
content/13: 9e3bb4c4c9137273d566b7fcecf9c709
|
||||
content/14: bcadfc362b69078beee0088e5936c98b
|
||||
content/15: c25b7418fd96b560b79af512cba4e7f1
|
||||
content/16: 3d44fa28ed12c73c2c9178b8570e516d
|
||||
@@ -2183,7 +2183,7 @@ checksums:
|
||||
meta/title: 06ec7d95ab44931ed9d1925e4063d703
|
||||
meta/description: cc9ab492bdda4a2cb9085537d6e6a0c0
|
||||
content/0: 1b031fb0c62c46b177aeed5c3d3f8f80
|
||||
content/1: 73fb594a22fcf560087b53ea6aa592f6
|
||||
content/1: f744e92fcc234d02bb9b352a2ab1e1e3
|
||||
content/2: b229bf34f0106ccb5af6f0b2a044e21a
|
||||
content/3: 45e7cee1fa342c4d13a1a6cb70733a14
|
||||
content/4: 62db68be640983ea9a383ee162bd8463
|
||||
@@ -2264,9 +2264,9 @@ checksums:
|
||||
content/11: 3524f0dac9a9152db223bcc2682a842d
|
||||
content/12: 341fbcb79af9a7cb1bf5ac653f51807c
|
||||
content/13: 371d0e46b4bd2c23f559b8bc112f6955
|
||||
content/14: 85aebdee44deb5b2f03e95112a776839
|
||||
content/14: 6dba14b1346c18cd2342f502371a1042
|
||||
content/15: bcadfc362b69078beee0088e5936c98b
|
||||
content/16: 7062fc4e2ca6974003e0d209f2b52d9f
|
||||
content/16: df68275133be883eac95664c3ed10063
|
||||
content/17: b3f310d5ef115bea5a8b75bf25d7ea9a
|
||||
content/18: 59815ce1d0dddd507d505b42aa01b648
|
||||
44f1f9fe8d5081b7781dc70e012cb531:
|
||||
@@ -3336,41 +3336,47 @@ checksums:
|
||||
meta/title: da9b3a204aa0532ac89b6fc018a6088f
|
||||
content/0: d091aba168f368dcc4e9073c000f47af
|
||||
content/1: 6f23aafaba6973476878aad143531249
|
||||
content/2: 66beea36102c469e9a4b0c4f6df2ef06
|
||||
content/2: dbfc415f822ff29c7fba2b44828aa6e8
|
||||
content/3: 72ec5e4349dd8b9190b3f5ba8c70b87f
|
||||
content/4: 5e26527ccf8fddebe14c951e6c950b48
|
||||
content/5: 8366332b5a0bc44ac3468318d0c047ae
|
||||
content/6: 1f1cf237845492bda14417ce584f0cd6
|
||||
content/6: 1dbfd0c9c8eb5043cfbb6201adbe8a26
|
||||
content/7: ad8638a3473c909dbcb1e1d9f4f26381
|
||||
content/8: 1037c579ddeb82c7e130d91ae0885aae
|
||||
content/9: 9a191f11cf167e5ed4019265d08fd96f
|
||||
content/10: b2e603592f9c01f26bd7bd067f52ccd5
|
||||
content/11: 0d5676ac9c717c7b639058f110765766
|
||||
content/12: f8c89a2a3c5cc99e36a1a9917a9c0a0a
|
||||
content/13: e413d471a8836ac90cc1501f83ec6240
|
||||
content/12: 361b9fc0f6dbf7a6700a12da76abb1d7
|
||||
content/13: 015cebc2b43a12e676f3af592f6a6285
|
||||
content/14: 9d70f66d1a9451ed3eca4a736f16a25b
|
||||
content/15: 0a512adcf936e341d82edf37bdabaf67
|
||||
content/16: 84099eb0c2a3d63fbce666ed70737c1a
|
||||
content/17: 3304a33dfb626c6e2267c062e8956a9d
|
||||
content/18: b55a33d2662babda7b0601dce90d70c3
|
||||
content/19: 2b707960797476b5fa8e135995c6b92c
|
||||
content/20: 823ce3d44e90732027dceefe2a5c5633
|
||||
content/21: 371db0900e0875577984b49f0c5e3214
|
||||
content/22: fbcfa72f8b5538ebc7f908ca03060f77
|
||||
content/23: e16b2a72aab3de182650bb61af16e849
|
||||
content/24: 9c2f91f89a914bf4661512275e461104
|
||||
content/25: 0b021f37144b855e8d728de216ae09ab
|
||||
content/26: 84fc28dd289f65cf73402d5d2fbf7d94
|
||||
content/27: fc603cd3f47e4aec4125adcc53807dca
|
||||
content/28: c674375bad4c2cffb3667da3aa8ec8a6
|
||||
content/29: 3bd6888cdd2b4cef832b5acd5f89c1c5
|
||||
content/30: 4f3a8d797101437e2055b5acdf203e8c
|
||||
content/31: 9116dbdefb76beb5ec1d070bc3c3215d
|
||||
content/32: bcd2d150d82ba8c183824472f6a55dd5
|
||||
content/33: d72903dda50a36b12ec050a06ef23a1b
|
||||
content/34: 5c06231162da4192c80ea9b118bda441
|
||||
content/35: b2a4a0c279f47d58a2456f25a1e1c6f9
|
||||
content/36: b5f5c14d7cbc9345dbf969cfdbf32b4f
|
||||
content/17: ac9cd91daa3c1394b1cbb566e4bf9424
|
||||
content/18: 395c4ae0f314e84860c80f05cd46417f
|
||||
content/19: b2626759e544397209c078238b43a040
|
||||
content/20: 04593f5a99891adb9e221b1f6909b920
|
||||
content/21: 3304a33dfb626c6e2267c062e8956a9d
|
||||
content/22: b55a33d2662babda7b0601dce90d70c3
|
||||
content/23: 2b707960797476b5fa8e135995c6b92c
|
||||
content/24: 823ce3d44e90732027dceefe2a5c5633
|
||||
content/25: 371db0900e0875577984b49f0c5e3214
|
||||
content/26: fbcfa72f8b5538ebc7f908ca03060f77
|
||||
content/27: e16b2a72aab3de182650bb61af16e849
|
||||
content/28: 9c2f91f89a914bf4661512275e461104
|
||||
content/29: 0b021f37144b855e8d728de216ae09ab
|
||||
content/30: 84fc28dd289f65cf73402d5d2fbf7d94
|
||||
content/31: fc603cd3f47e4aec4125adcc53807dca
|
||||
content/32: c674375bad4c2cffb3667da3aa8ec8a6
|
||||
content/33: 7eefc40faa6a579e65f9435b45a8b28b
|
||||
content/34: 243cb6175abb075290231b0f7dcddc72
|
||||
content/35: 3bd6888cdd2b4cef832b5acd5f89c1c5
|
||||
content/36: 4f3a8d797101437e2055b5acdf203e8c
|
||||
content/37: 9116dbdefb76beb5ec1d070bc3c3215d
|
||||
content/38: bcd2d150d82ba8c183824472f6a55dd5
|
||||
content/39: d72903dda50a36b12ec050a06ef23a1b
|
||||
content/40: 14aeb85bcaca9285bad1550f247d6db0
|
||||
content/41: b2a4a0c279f47d58a2456f25a1e1c6f9
|
||||
content/42: b5f5c14d7cbc9345dbf969cfdbf32b4f
|
||||
7974f994606f20da7b037a31b5524f1a:
|
||||
meta/title: 30c54e4dc4ce599b87d94be34a8617f5
|
||||
meta/description: 6dfb33b1ef65f07a4668924d92ddae05
|
||||
@@ -3378,32 +3384,33 @@ checksums:
|
||||
content/1: b07b4d76205aecd961d26b8011680922
|
||||
content/2: 4ac7e8c7573e5c90718d3b34bb0ab576
|
||||
content/3: 5e6cc42bd620144cd715874334f32a60
|
||||
content/4: cb9e81c1aa31b320ef00da8c70963669
|
||||
content/4: 035dc0050a36815fa0f618162714c82f
|
||||
content/5: 3dc06522a36a692ad4c60610e5315b40
|
||||
content/6: 6332edca4ab88324a894dcbad3c3916f
|
||||
content/7: fb61b15efd051d3593b47bc722467e5b
|
||||
content/8: 30a8dc0a6418f9a249ccce582b4f0055
|
||||
content/9: 650c42ca4d4408057ddc7eec121c844c
|
||||
content/10: 87905d7f320e3e1f3e40042a5d8d6a4a
|
||||
content/11: d434857c136b47cfca096760863fd480
|
||||
content/12: de63e1da975a115ba3e1398bbad4763d
|
||||
content/13: ac589c052d06d2177fb4586115a5fb45
|
||||
content/14: 4e1fec28b8218c6a7edcc9be4113ff23
|
||||
content/15: 4ac7e8c7573e5c90718d3b34bb0ab576
|
||||
content/16: 499c18b2d417e5b3817cbebb70252433
|
||||
content/17: b86916709d15285f9a1778621fb6ca77
|
||||
content/18: b2a3d56b45454f660662aa17b36c6de8
|
||||
content/19: a13da4a942222c7a35dc776d5ee1a7fe
|
||||
content/20: ea7bfdec25c4fe41f76f4d5bce1e8411
|
||||
content/21: 6feca454fe6e8f9cd06d4949271107dd
|
||||
content/22: 39658e3ca9692e995d6539f09d454a7d
|
||||
content/23: ac5b61b9b2833847096f3cee07f0ae59
|
||||
content/24: 440a308d872dc0278b7a8c6301dd7023
|
||||
content/25: 73c5b117f6c11c927cfd4597cf16ed1e
|
||||
content/26: 3ea122283f55d43dcc0d315a345b41c8
|
||||
content/27: bee19e2f048432154a31dbc202a33355
|
||||
content/28: 26313cad0f9c05314cacee0eb170b03a
|
||||
content/29: 7918b3b8e62ed0af8b178b420b683ced
|
||||
content/7: 0878e8ad6d07c0f6f77c7c5071f383c3
|
||||
content/8: fb61b15efd051d3593b47bc722467e5b
|
||||
content/9: 30a8dc0a6418f9a249ccce582b4f0055
|
||||
content/10: 650c42ca4d4408057ddc7eec121c844c
|
||||
content/11: 87905d7f320e3e1f3e40042a5d8d6a4a
|
||||
content/12: d434857c136b47cfca096760863fd480
|
||||
content/13: de63e1da975a115ba3e1398bbad4763d
|
||||
content/14: ac589c052d06d2177fb4586115a5fb45
|
||||
content/15: 4e1fec28b8218c6a7edcc9be4113ff23
|
||||
content/16: 4ac7e8c7573e5c90718d3b34bb0ab576
|
||||
content/17: 499c18b2d417e5b3817cbebb70252433
|
||||
content/18: b86916709d15285f9a1778621fb6ca77
|
||||
content/19: b2a3d56b45454f660662aa17b36c6de8
|
||||
content/20: a13da4a942222c7a35dc776d5ee1a7fe
|
||||
content/21: ea7bfdec25c4fe41f76f4d5bce1e8411
|
||||
content/22: 6feca454fe6e8f9cd06d4949271107dd
|
||||
content/23: 39658e3ca9692e995d6539f09d454a7d
|
||||
content/24: ac5b61b9b2833847096f3cee07f0ae59
|
||||
content/25: 440a308d872dc0278b7a8c6301dd7023
|
||||
content/26: 73c5b117f6c11c927cfd4597cf16ed1e
|
||||
content/27: 3ea122283f55d43dcc0d315a345b41c8
|
||||
content/28: bee19e2f048432154a31dbc202a33355
|
||||
content/29: 26313cad0f9c05314cacee0eb170b03a
|
||||
content/30: 2cc1bd906a9963c622182b63962e68d2
|
||||
97f536e153f08352d6e8fe5d5160b471:
|
||||
meta/title: 78f81382850e1c3bb6a3b404c55d3dc2
|
||||
content/0: d091aba168f368dcc4e9073c000f47af
|
||||
@@ -4404,3 +4411,58 @@ checksums:
|
||||
content/38: 4fe4260da2f137679ce2fa42cffcf56a
|
||||
content/39: b3f310d5ef115bea5a8b75bf25d7ea9a
|
||||
content/40: 89bdbd886b24f2aaec635a2b4119660a
|
||||
7442fe5651739568ceae8717ccd19c97:
|
||||
meta/title: 014d18ade977bf08d75b995076596708
|
||||
content/0: 565c20898ed207b9d41acb879bfe0e7e
|
||||
content/1: b0771df06e1e3ec4cc1a8a611267c18d
|
||||
content/2: 1e69e6e8038366d33134e5ed47e4ace9
|
||||
content/3: 5e26527ccf8fddebe14c951e6c950b48
|
||||
content/4: b1967652ccc7d01200491361a07d3a57
|
||||
content/5: 0a2402c612cd7fd8fe5a6f6593de2781
|
||||
content/6: 0441638444240cd20a6c69ea1d3afbb1
|
||||
content/7: 4b7f6428e660f075365eef024e669367
|
||||
content/8: 0dd57639f82640542a1de91850f7f4b4
|
||||
content/9: 97f7bae15897e85fd9b4d376906e41f1
|
||||
content/10: 4809730d95a4082452eacf68491c5ac9
|
||||
content/11: 57aa42a75bed4f16b92cfefb46c70a6f
|
||||
content/12: 987932038f4e9442bd89f0f8ed3c5319
|
||||
content/13: 3efb251bb666cf27cbafe4de2a14fc01
|
||||
content/14: 9c2f91f89a914bf4661512275e461104
|
||||
content/15: e7b5a54669c71394370e3ef03dffbc1b
|
||||
content/16: abd4a3e02b972784acfbecc6055ff8ac
|
||||
content/17: c2331652cdff070a92265c88a6b2d106
|
||||
content/18: a2d992c919482cd0519812c5a5d09d89
|
||||
content/19: 7c8026ddc12b76aa3da2c295ba44bcbf
|
||||
content/20: 23a3541e85451a0e49f4ae6e950d8660
|
||||
content/21: b2a4a0c279f47d58a2456f25a1e1c6f9
|
||||
content/22: 83290b8ac463358705cec69c7d987a29
|
||||
6e1791636125a4985d9ca95dd8a07740:
|
||||
meta/title: ffd3eec5497af36d7b4e4185bad1313a
|
||||
content/0: 565c20898ed207b9d41acb879bfe0e7e
|
||||
content/1: 1cfca41a0c1aa4715e6c0f64fe205066
|
||||
content/2: 19e8fbb1cbc4f5a53dd7b023fbbbf4f5
|
||||
content/3: ed7ad7d82efc34f46da16b015a037d9e
|
||||
content/4: 5e26527ccf8fddebe14c951e6c950b48
|
||||
content/5: 59f027061416ba416e003ad385992d66
|
||||
content/6: d8e0bd79b8e940e24b380a53f66cc67b
|
||||
content/7: f4f20249ebc3b92bab44dafcfb350010
|
||||
content/8: 40825cb95c0deb46172afbd3b0a410bf
|
||||
content/9: b7d4a2fb7d38059bb4b1ce277eee6ca2
|
||||
content/10: f89e1c2b5480f2adaa63f81d821d196f
|
||||
content/11: f6b47588c4c28dbaf901f270c39dde9b
|
||||
content/12: f0a22a97c5d870dc8ec85a03c1fccd19
|
||||
content/13: 5e821ef1dc069c976ff6608848636651
|
||||
content/14: 4f114c2449c678a13307a1e19c01c790
|
||||
content/15: fd319abccf09a7030255028ed088fd61
|
||||
content/16: 998443546a4d71afb8c4cb4944e6da72
|
||||
content/17: 9c2f91f89a914bf4661512275e461104
|
||||
content/18: f5a13e891d63bf2380fd1e40ff6f15e3
|
||||
content/19: 49b3bf3cb8439c8dfd9db5d2d0d2d7a0
|
||||
content/20: 36c90f0c08589bb2713dff659fa83800
|
||||
content/21: c176302bc5537499dd94913761afd457
|
||||
content/22: 8789d2cb14735e9d3d4d9454c2284ddc
|
||||
content/23: 24b5b45bb497d609e275734955be3cbc
|
||||
content/24: 987932038f4e9442bd89f0f8ed3c5319
|
||||
content/25: bfd457397ec2c8033475c7c7a89a4d5d
|
||||
content/26: b2a4a0c279f47d58a2456f25a1e1c6f9
|
||||
content/27: 7e48b5958215f904462276e31cb8cc10
|
||||
|
||||
BIN
apps/docs/public/static/blocks/loop-3.png
Normal file
BIN
apps/docs/public/static/blocks/loop-3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 118 KiB |
BIN
apps/docs/public/static/blocks/loop-4.png
Normal file
BIN
apps/docs/public/static/blocks/loop-4.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 101 KiB |
BIN
apps/docs/public/static/blocks/variables.png
Normal file
BIN
apps/docs/public/static/blocks/variables.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 49 KiB |
BIN
apps/docs/public/static/blocks/wait.png
Normal file
BIN
apps/docs/public/static/blocks/wait.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 20 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 94 KiB |
@@ -71,6 +71,12 @@ export default function SSOForm() {
|
||||
}
|
||||
}
|
||||
|
||||
// Pre-fill email if provided in URL (e.g., from deployed chat SSO)
|
||||
const emailParam = searchParams.get('email')
|
||||
if (emailParam) {
|
||||
setEmail(emailParam)
|
||||
}
|
||||
|
||||
// Check for SSO error from redirect
|
||||
const error = searchParams.get('error')
|
||||
if (error) {
|
||||
|
||||
524
apps/sim/app/(landing)/careers/page.tsx
Normal file
524
apps/sim/app/(landing)/careers/page.tsx
Normal file
@@ -0,0 +1,524 @@
|
||||
'use client'
|
||||
|
||||
import { useRef, useState } from 'react'
|
||||
import { Loader2, X } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { quickValidateEmail } from '@/lib/email/validation'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { LegalLayout } from '@/app/(landing)/components'
|
||||
import { soehne } from '@/app/fonts/soehne/soehne'
|
||||
|
||||
const validateName = (name: string): string[] => {
|
||||
const errors: string[] = []
|
||||
if (!name || name.trim().length < 2) {
|
||||
errors.push('Name must be at least 2 characters')
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
const validateEmail = (email: string): string[] => {
|
||||
const errors: string[] = []
|
||||
if (!email || !email.trim()) {
|
||||
errors.push('Email is required')
|
||||
return errors
|
||||
}
|
||||
const validation = quickValidateEmail(email.trim().toLowerCase())
|
||||
if (!validation.isValid) {
|
||||
errors.push(validation.reason || 'Please enter a valid email address')
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
const validatePosition = (position: string): string[] => {
|
||||
const errors: string[] = []
|
||||
if (!position || position.trim().length < 2) {
|
||||
errors.push('Please specify the position you are interested in')
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
const validateLinkedIn = (url: string): string[] => {
|
||||
if (!url || url.trim() === '') return []
|
||||
const errors: string[] = []
|
||||
try {
|
||||
new URL(url)
|
||||
} catch {
|
||||
errors.push('Please enter a valid LinkedIn URL')
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
const validatePortfolio = (url: string): string[] => {
|
||||
if (!url || url.trim() === '') return []
|
||||
const errors: string[] = []
|
||||
try {
|
||||
new URL(url)
|
||||
} catch {
|
||||
errors.push('Please enter a valid portfolio URL')
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
const validateLocation = (location: string): string[] => {
|
||||
const errors: string[] = []
|
||||
if (!location || location.trim().length < 2) {
|
||||
errors.push('Please enter your location')
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
const validateMessage = (message: string): string[] => {
|
||||
const errors: string[] = []
|
||||
if (!message || message.trim().length < 50) {
|
||||
errors.push('Please tell us more about yourself (at least 50 characters)')
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
export default function CareersPage() {
|
||||
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||
const [submitStatus, setSubmitStatus] = useState<'idle' | 'success' | 'error'>('idle')
|
||||
const [showErrors, setShowErrors] = useState(false)
|
||||
|
||||
// Form fields
|
||||
const [name, setName] = useState('')
|
||||
const [email, setEmail] = useState('')
|
||||
const [phone, setPhone] = useState('')
|
||||
const [position, setPosition] = useState('')
|
||||
const [linkedin, setLinkedin] = useState('')
|
||||
const [portfolio, setPortfolio] = useState('')
|
||||
const [experience, setExperience] = useState('')
|
||||
const [location, setLocation] = useState('')
|
||||
const [message, setMessage] = useState('')
|
||||
const [resume, setResume] = useState<File | null>(null)
|
||||
const fileInputRef = useRef<HTMLInputElement>(null)
|
||||
|
||||
// Field errors
|
||||
const [nameErrors, setNameErrors] = useState<string[]>([])
|
||||
const [emailErrors, setEmailErrors] = useState<string[]>([])
|
||||
const [positionErrors, setPositionErrors] = useState<string[]>([])
|
||||
const [linkedinErrors, setLinkedinErrors] = useState<string[]>([])
|
||||
const [portfolioErrors, setPortfolioErrors] = useState<string[]>([])
|
||||
const [experienceErrors, setExperienceErrors] = useState<string[]>([])
|
||||
const [locationErrors, setLocationErrors] = useState<string[]>([])
|
||||
const [messageErrors, setMessageErrors] = useState<string[]>([])
|
||||
const [resumeErrors, setResumeErrors] = useState<string[]>([])
|
||||
|
||||
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0] || null
|
||||
setResume(file)
|
||||
if (file) {
|
||||
setResumeErrors([])
|
||||
}
|
||||
}
|
||||
|
||||
async function onSubmit(e: React.FormEvent<HTMLFormElement>) {
|
||||
e.preventDefault()
|
||||
setShowErrors(true)
|
||||
|
||||
// Validate all fields
|
||||
const nameErrs = validateName(name)
|
||||
const emailErrs = validateEmail(email)
|
||||
const positionErrs = validatePosition(position)
|
||||
const linkedinErrs = validateLinkedIn(linkedin)
|
||||
const portfolioErrs = validatePortfolio(portfolio)
|
||||
const experienceErrs = experience ? [] : ['Please select your years of experience']
|
||||
const locationErrs = validateLocation(location)
|
||||
const messageErrs = validateMessage(message)
|
||||
const resumeErrs = resume ? [] : ['Resume is required']
|
||||
|
||||
setNameErrors(nameErrs)
|
||||
setEmailErrors(emailErrs)
|
||||
setPositionErrors(positionErrs)
|
||||
setLinkedinErrors(linkedinErrs)
|
||||
setPortfolioErrors(portfolioErrs)
|
||||
setExperienceErrors(experienceErrs)
|
||||
setLocationErrors(locationErrs)
|
||||
setMessageErrors(messageErrs)
|
||||
setResumeErrors(resumeErrs)
|
||||
|
||||
if (
|
||||
nameErrs.length > 0 ||
|
||||
emailErrs.length > 0 ||
|
||||
positionErrs.length > 0 ||
|
||||
linkedinErrs.length > 0 ||
|
||||
portfolioErrs.length > 0 ||
|
||||
experienceErrs.length > 0 ||
|
||||
locationErrs.length > 0 ||
|
||||
messageErrs.length > 0 ||
|
||||
resumeErrs.length > 0
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
setIsSubmitting(true)
|
||||
setSubmitStatus('idle')
|
||||
|
||||
try {
|
||||
const formData = new FormData()
|
||||
formData.append('name', name)
|
||||
formData.append('email', email)
|
||||
formData.append('phone', phone || '')
|
||||
formData.append('position', position)
|
||||
formData.append('linkedin', linkedin || '')
|
||||
formData.append('portfolio', portfolio || '')
|
||||
formData.append('experience', experience)
|
||||
formData.append('location', location)
|
||||
formData.append('message', message)
|
||||
if (resume) formData.append('resume', resume)
|
||||
|
||||
const response = await fetch('/api/careers/submit', {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to submit application')
|
||||
}
|
||||
|
||||
setSubmitStatus('success')
|
||||
} catch (error) {
|
||||
console.error('Error submitting application:', error)
|
||||
setSubmitStatus('error')
|
||||
} finally {
|
||||
setIsSubmitting(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<LegalLayout title='Join Our Team'>
|
||||
<div className={`${soehne.className} mx-auto max-w-2xl`}>
|
||||
{/* Form Section */}
|
||||
<section className='rounded-2xl border border-gray-200 bg-white p-6 shadow-sm sm:p-10'>
|
||||
<h2 className='mb-2 font-medium text-2xl sm:text-3xl'>Apply Now</h2>
|
||||
<p className='mb-8 text-gray-600 text-sm sm:text-base'>
|
||||
Help us build the future of AI workflows
|
||||
</p>
|
||||
|
||||
<form onSubmit={onSubmit} className='space-y-5'>
|
||||
{/* Name and Email */}
|
||||
<div className='grid grid-cols-1 gap-4 sm:gap-6 md:grid-cols-2'>
|
||||
<div className='space-y-2'>
|
||||
<Label htmlFor='name' className='font-medium text-sm'>
|
||||
Full Name *
|
||||
</Label>
|
||||
<Input
|
||||
id='name'
|
||||
placeholder='John Doe'
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
className={cn(
|
||||
showErrors &&
|
||||
nameErrors.length > 0 &&
|
||||
'border-red-500 focus:border-red-500 focus:ring-red-100 focus-visible:ring-red-500'
|
||||
)}
|
||||
/>
|
||||
{showErrors && nameErrors.length > 0 && (
|
||||
<div className='mt-1 space-y-1 text-red-400 text-xs'>
|
||||
{nameErrors.map((error, index) => (
|
||||
<p key={index}>{error}</p>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className='space-y-2'>
|
||||
<Label htmlFor='email' className='font-medium text-sm'>
|
||||
Email *
|
||||
</Label>
|
||||
<Input
|
||||
id='email'
|
||||
type='email'
|
||||
placeholder='john@example.com'
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
className={cn(
|
||||
showErrors &&
|
||||
emailErrors.length > 0 &&
|
||||
'border-red-500 focus:border-red-500 focus:ring-red-100 focus-visible:ring-red-500'
|
||||
)}
|
||||
/>
|
||||
{showErrors && emailErrors.length > 0 && (
|
||||
<div className='mt-1 space-y-1 text-red-400 text-xs'>
|
||||
{emailErrors.map((error, index) => (
|
||||
<p key={index}>{error}</p>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Phone and Position */}
|
||||
<div className='grid grid-cols-1 gap-4 sm:gap-6 md:grid-cols-2'>
|
||||
<div className='space-y-2'>
|
||||
<Label htmlFor='phone' className='font-medium text-sm'>
|
||||
Phone Number
|
||||
</Label>
|
||||
<Input
|
||||
id='phone'
|
||||
type='tel'
|
||||
placeholder='+1 (555) 123-4567'
|
||||
value={phone}
|
||||
onChange={(e) => setPhone(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className='space-y-2'>
|
||||
<Label htmlFor='position' className='font-medium text-sm'>
|
||||
Position of Interest *
|
||||
</Label>
|
||||
<Input
|
||||
id='position'
|
||||
placeholder='e.g. Full Stack Engineer, Product Designer'
|
||||
value={position}
|
||||
onChange={(e) => setPosition(e.target.value)}
|
||||
className={cn(
|
||||
showErrors &&
|
||||
positionErrors.length > 0 &&
|
||||
'border-red-500 focus:border-red-500 focus:ring-red-100 focus-visible:ring-red-500'
|
||||
)}
|
||||
/>
|
||||
{showErrors && positionErrors.length > 0 && (
|
||||
<div className='mt-1 space-y-1 text-red-400 text-xs'>
|
||||
{positionErrors.map((error, index) => (
|
||||
<p key={index}>{error}</p>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* LinkedIn and Portfolio */}
|
||||
<div className='grid grid-cols-1 gap-4 sm:gap-6 md:grid-cols-2'>
|
||||
<div className='space-y-2'>
|
||||
<Label htmlFor='linkedin' className='font-medium text-sm'>
|
||||
LinkedIn Profile
|
||||
</Label>
|
||||
<Input
|
||||
id='linkedin'
|
||||
placeholder='https://linkedin.com/in/yourprofile'
|
||||
value={linkedin}
|
||||
onChange={(e) => setLinkedin(e.target.value)}
|
||||
className={cn(
|
||||
showErrors &&
|
||||
linkedinErrors.length > 0 &&
|
||||
'border-red-500 focus:border-red-500 focus:ring-red-100 focus-visible:ring-red-500'
|
||||
)}
|
||||
/>
|
||||
{showErrors && linkedinErrors.length > 0 && (
|
||||
<div className='mt-1 space-y-1 text-red-400 text-xs'>
|
||||
{linkedinErrors.map((error, index) => (
|
||||
<p key={index}>{error}</p>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className='space-y-2'>
|
||||
<Label htmlFor='portfolio' className='font-medium text-sm'>
|
||||
Portfolio / Website
|
||||
</Label>
|
||||
<Input
|
||||
id='portfolio'
|
||||
placeholder='https://yourportfolio.com'
|
||||
value={portfolio}
|
||||
onChange={(e) => setPortfolio(e.target.value)}
|
||||
className={cn(
|
||||
showErrors &&
|
||||
portfolioErrors.length > 0 &&
|
||||
'border-red-500 focus:border-red-500 focus:ring-red-100 focus-visible:ring-red-500'
|
||||
)}
|
||||
/>
|
||||
{showErrors && portfolioErrors.length > 0 && (
|
||||
<div className='mt-1 space-y-1 text-red-400 text-xs'>
|
||||
{portfolioErrors.map((error, index) => (
|
||||
<p key={index}>{error}</p>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Experience and Location */}
|
||||
<div className='grid grid-cols-1 gap-4 sm:gap-6 md:grid-cols-2'>
|
||||
<div className='space-y-2'>
|
||||
<Label htmlFor='experience' className='font-medium text-sm'>
|
||||
Years of Experience *
|
||||
</Label>
|
||||
<Select value={experience} onValueChange={setExperience}>
|
||||
<SelectTrigger
|
||||
className={cn(
|
||||
showErrors &&
|
||||
experienceErrors.length > 0 &&
|
||||
'border-red-500 focus:border-red-500 focus:ring-red-100 focus-visible:ring-red-500'
|
||||
)}
|
||||
>
|
||||
<SelectValue placeholder='Select experience level' />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value='0-1'>0-1 years</SelectItem>
|
||||
<SelectItem value='1-3'>1-3 years</SelectItem>
|
||||
<SelectItem value='3-5'>3-5 years</SelectItem>
|
||||
<SelectItem value='5-10'>5-10 years</SelectItem>
|
||||
<SelectItem value='10+'>10+ years</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{showErrors && experienceErrors.length > 0 && (
|
||||
<div className='mt-1 space-y-1 text-red-400 text-xs'>
|
||||
{experienceErrors.map((error, index) => (
|
||||
<p key={index}>{error}</p>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className='space-y-2'>
|
||||
<Label htmlFor='location' className='font-medium text-sm'>
|
||||
Location *
|
||||
</Label>
|
||||
<Input
|
||||
id='location'
|
||||
placeholder='e.g. San Francisco, CA'
|
||||
value={location}
|
||||
onChange={(e) => setLocation(e.target.value)}
|
||||
className={cn(
|
||||
showErrors &&
|
||||
locationErrors.length > 0 &&
|
||||
'border-red-500 focus:border-red-500 focus:ring-red-100 focus-visible:ring-red-500'
|
||||
)}
|
||||
/>
|
||||
{showErrors && locationErrors.length > 0 && (
|
||||
<div className='mt-1 space-y-1 text-red-400 text-xs'>
|
||||
{locationErrors.map((error, index) => (
|
||||
<p key={index}>{error}</p>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Message */}
|
||||
<div className='space-y-2'>
|
||||
<Label htmlFor='message' className='font-medium text-sm'>
|
||||
Tell us about yourself *
|
||||
</Label>
|
||||
<Textarea
|
||||
id='message'
|
||||
placeholder='Tell us about your experience, what excites you about Sim, and why you would be a great fit for this role...'
|
||||
className={cn(
|
||||
'min-h-[140px]',
|
||||
showErrors &&
|
||||
messageErrors.length > 0 &&
|
||||
'border-red-500 focus:border-red-500 focus:ring-red-100 focus-visible:ring-red-500'
|
||||
)}
|
||||
value={message}
|
||||
onChange={(e) => setMessage(e.target.value)}
|
||||
/>
|
||||
<p className='mt-1.5 text-gray-500 text-xs'>Minimum 50 characters</p>
|
||||
{showErrors && messageErrors.length > 0 && (
|
||||
<div className='mt-1 space-y-1 text-red-400 text-xs'>
|
||||
{messageErrors.map((error, index) => (
|
||||
<p key={index}>{error}</p>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Resume Upload */}
|
||||
<div className='space-y-2'>
|
||||
<Label htmlFor='resume' className='font-medium text-sm'>
|
||||
Resume *
|
||||
</Label>
|
||||
<div className='relative'>
|
||||
{resume ? (
|
||||
<div className='flex items-center gap-2 rounded-md border border-input bg-background px-3 py-2'>
|
||||
<span className='flex-1 truncate text-sm'>{resume.name}</span>
|
||||
<button
|
||||
type='button'
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
setResume(null)
|
||||
if (fileInputRef.current) {
|
||||
fileInputRef.current.value = ''
|
||||
}
|
||||
}}
|
||||
className='flex-shrink-0 text-muted-foreground transition-colors hover:text-foreground'
|
||||
aria-label='Remove file'
|
||||
>
|
||||
<X className='h-4 w-4' />
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<Input
|
||||
id='resume'
|
||||
type='file'
|
||||
accept='.pdf,.doc,.docx'
|
||||
onChange={handleFileChange}
|
||||
ref={fileInputRef}
|
||||
className={cn(
|
||||
showErrors &&
|
||||
resumeErrors.length > 0 &&
|
||||
'border-red-500 focus:border-red-500 focus:ring-red-100 focus-visible:ring-red-500'
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
<p className='mt-1.5 text-gray-500 text-xs'>PDF or Word document, max 10MB</p>
|
||||
{showErrors && resumeErrors.length > 0 && (
|
||||
<div className='mt-1 space-y-1 text-red-400 text-xs'>
|
||||
{resumeErrors.map((error, index) => (
|
||||
<p key={index}>{error}</p>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Submit Button */}
|
||||
<div className='flex justify-end pt-2'>
|
||||
<Button
|
||||
type='submit'
|
||||
disabled={isSubmitting || submitStatus === 'success'}
|
||||
className='min-w-[200px] rounded-[10px] border border-[#6F3DFA] bg-gradient-to-b from-[#8357FF] to-[#6F3DFA] text-white shadow-[inset_0_2px_4px_0_#9B77FF] transition-all duration-300 hover:opacity-90 disabled:opacity-50'
|
||||
size='lg'
|
||||
>
|
||||
{isSubmitting ? (
|
||||
<>
|
||||
<Loader2 className='mr-2 h-4 w-4 animate-spin' />
|
||||
Submitting...
|
||||
</>
|
||||
) : submitStatus === 'success' ? (
|
||||
'Submitted'
|
||||
) : (
|
||||
'Submit Application'
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
{/* Additional Info */}
|
||||
<section className='mt-6 text-center text-gray-600 text-sm'>
|
||||
<p>
|
||||
Questions? Email us at{' '}
|
||||
<a
|
||||
href='mailto:careers@sim.ai'
|
||||
className='font-medium text-gray-900 underline transition-colors hover:text-gray-700'
|
||||
>
|
||||
careers@sim.ai
|
||||
</a>
|
||||
</p>
|
||||
</section>
|
||||
</div>
|
||||
</LegalLayout>
|
||||
)
|
||||
}
|
||||
@@ -228,6 +228,12 @@ export default function Footer({ fullWidth = false }: FooterProps) {
|
||||
>
|
||||
Changelog
|
||||
</Link>
|
||||
<Link
|
||||
href='/careers'
|
||||
className='text-[14px] text-muted-foreground transition-colors hover:text-foreground'
|
||||
>
|
||||
Careers
|
||||
</Link>
|
||||
<Link
|
||||
href='/privacy'
|
||||
target='_blank'
|
||||
|
||||
@@ -20,7 +20,7 @@ interface NavProps {
|
||||
}
|
||||
|
||||
export default function Nav({ hideAuthButtons = false, variant = 'landing' }: NavProps = {}) {
|
||||
const [githubStars, setGithubStars] = useState('16.3k')
|
||||
const [githubStars, setGithubStars] = useState('17.4k')
|
||||
const [isHovered, setIsHovered] = useState(false)
|
||||
const [isLoginHovered, setIsLoginHovered] = useState(false)
|
||||
const router = useRouter()
|
||||
@@ -113,7 +113,7 @@ export default function Nav({ hideAuthButtons = false, variant = 'landing' }: Na
|
||||
itemType='https://schema.org/SiteNavigationElement'
|
||||
>
|
||||
<div className='flex items-center gap-[34px]'>
|
||||
<Link href='/' aria-label={`${brand.name} home`} itemProp='url'>
|
||||
<Link href='/?from=nav' aria-label={`${brand.name} home`} itemProp='url'>
|
||||
<span itemProp='name' className='sr-only'>
|
||||
{brand.name} Home
|
||||
</span>
|
||||
|
||||
@@ -20,5 +20,4 @@ INTERNAL_API_SECRET=your_internal_api_secret # Use `openssl rand -hex 32` to gen
|
||||
# If left commented out, emails will be logged to console instead
|
||||
|
||||
# Local AI Models (Optional)
|
||||
# OLLAMA_URL=http://localhost:11434 # URL for local Ollama server - uncomment if using local models
|
||||
|
||||
# OLLAMA_URL=http://localhost:11434 # URL for local Ollama server - uncomment if using local models
|
||||
@@ -834,24 +834,88 @@ export function createStorageProviderMocks(options: StorageProviderMockOptions =
|
||||
uploadHeaders = {},
|
||||
} = options
|
||||
|
||||
// Ensure UUID is mocked
|
||||
mockUuid('mock-uuid-1234')
|
||||
mockCryptoUuid('mock-uuid-1234-5678')
|
||||
|
||||
// Base upload utilities
|
||||
const uploadFileMock = vi.fn().mockResolvedValue({
|
||||
path: '/api/files/serve/test-key.txt',
|
||||
key: 'test-key.txt',
|
||||
name: 'test.txt',
|
||||
size: 100,
|
||||
type: 'text/plain',
|
||||
})
|
||||
const downloadFileMock = vi.fn().mockResolvedValue(Buffer.from('test content'))
|
||||
const deleteFileMock = vi.fn().mockResolvedValue(undefined)
|
||||
const hasCloudStorageMock = vi.fn().mockReturnValue(isCloudEnabled)
|
||||
|
||||
const generatePresignedUploadUrlMock = vi.fn().mockImplementation((params: any) => {
|
||||
const { fileName, context } = params
|
||||
const timestamp = Date.now()
|
||||
const random = Math.random().toString(36).substring(2, 9)
|
||||
|
||||
let key = ''
|
||||
if (context === 'knowledge-base') {
|
||||
key = `kb/${timestamp}-${random}-${fileName}`
|
||||
} else if (context === 'chat') {
|
||||
key = `chat/${timestamp}-${random}-${fileName}`
|
||||
} else if (context === 'copilot') {
|
||||
key = `copilot/${timestamp}-${random}-${fileName}`
|
||||
} else if (context === 'workspace') {
|
||||
key = `workspace/${timestamp}-${random}-${fileName}`
|
||||
} else {
|
||||
key = `${timestamp}-${random}-${fileName}`
|
||||
}
|
||||
|
||||
return Promise.resolve({
|
||||
url: presignedUrl,
|
||||
key,
|
||||
uploadHeaders: uploadHeaders,
|
||||
})
|
||||
})
|
||||
|
||||
const generatePresignedDownloadUrlMock = vi.fn().mockResolvedValue(presignedUrl)
|
||||
|
||||
vi.doMock('@/lib/uploads', () => ({
|
||||
getStorageProvider: vi.fn().mockReturnValue(provider),
|
||||
isUsingCloudStorage: vi.fn().mockReturnValue(isCloudEnabled),
|
||||
uploadFile: vi.fn().mockResolvedValue({
|
||||
path: '/api/files/serve/test-key.txt',
|
||||
key: 'test-key.txt',
|
||||
name: 'test.txt',
|
||||
size: 100,
|
||||
type: 'text/plain',
|
||||
}),
|
||||
downloadFile: vi.fn().mockResolvedValue(Buffer.from('test content')),
|
||||
deleteFile: vi.fn().mockResolvedValue(undefined),
|
||||
StorageService: {
|
||||
uploadFile: uploadFileMock,
|
||||
downloadFile: downloadFileMock,
|
||||
deleteFile: deleteFileMock,
|
||||
hasCloudStorage: hasCloudStorageMock,
|
||||
generatePresignedUploadUrl: generatePresignedUploadUrlMock,
|
||||
generatePresignedDownloadUrl: generatePresignedDownloadUrlMock,
|
||||
},
|
||||
uploadFile: uploadFileMock,
|
||||
downloadFile: downloadFileMock,
|
||||
deleteFile: deleteFileMock,
|
||||
getPresignedUrl: vi.fn().mockResolvedValue(presignedUrl),
|
||||
hasCloudStorage: hasCloudStorageMock,
|
||||
generatePresignedDownloadUrl: generatePresignedDownloadUrlMock,
|
||||
}))
|
||||
|
||||
vi.doMock('@/lib/uploads/core/storage-service', () => ({
|
||||
uploadFile: uploadFileMock,
|
||||
downloadFile: downloadFileMock,
|
||||
deleteFile: deleteFileMock,
|
||||
hasCloudStorage: hasCloudStorageMock,
|
||||
generatePresignedUploadUrl: generatePresignedUploadUrlMock,
|
||||
generatePresignedDownloadUrl: generatePresignedDownloadUrlMock,
|
||||
StorageService: {
|
||||
uploadFile: uploadFileMock,
|
||||
downloadFile: downloadFileMock,
|
||||
deleteFile: deleteFileMock,
|
||||
hasCloudStorage: hasCloudStorageMock,
|
||||
generatePresignedUploadUrl: generatePresignedUploadUrlMock,
|
||||
generatePresignedDownloadUrl: generatePresignedDownloadUrlMock,
|
||||
},
|
||||
}))
|
||||
|
||||
vi.doMock('@/lib/uploads/core/setup', () => ({
|
||||
USE_S3_STORAGE: provider === 's3',
|
||||
USE_BLOB_STORAGE: provider === 'blob',
|
||||
USE_LOCAL_STORAGE: provider === 'local',
|
||||
getStorageProvider: vi.fn().mockReturnValue(provider),
|
||||
}))
|
||||
|
||||
if (provider === 's3') {
|
||||
@@ -1304,19 +1368,38 @@ export function setupFileApiMocks(
|
||||
isCloudEnabled: cloudEnabled,
|
||||
})
|
||||
} else {
|
||||
const uploadFileMock = vi.fn().mockResolvedValue({
|
||||
path: '/api/files/serve/test-key.txt',
|
||||
key: 'test-key.txt',
|
||||
name: 'test.txt',
|
||||
size: 100,
|
||||
type: 'text/plain',
|
||||
})
|
||||
const downloadFileMock = vi.fn().mockResolvedValue(Buffer.from('test content'))
|
||||
const deleteFileMock = vi.fn().mockResolvedValue(undefined)
|
||||
const hasCloudStorageMock = vi.fn().mockReturnValue(cloudEnabled)
|
||||
|
||||
vi.doMock('@/lib/uploads', () => ({
|
||||
getStorageProvider: vi.fn().mockReturnValue('local'),
|
||||
isUsingCloudStorage: vi.fn().mockReturnValue(cloudEnabled),
|
||||
uploadFile: vi.fn().mockResolvedValue({
|
||||
path: '/api/files/serve/test-key.txt',
|
||||
key: 'test-key.txt',
|
||||
name: 'test.txt',
|
||||
size: 100,
|
||||
type: 'text/plain',
|
||||
}),
|
||||
downloadFile: vi.fn().mockResolvedValue(Buffer.from('test content')),
|
||||
deleteFile: vi.fn().mockResolvedValue(undefined),
|
||||
StorageService: {
|
||||
uploadFile: uploadFileMock,
|
||||
downloadFile: downloadFileMock,
|
||||
deleteFile: deleteFileMock,
|
||||
hasCloudStorage: hasCloudStorageMock,
|
||||
generatePresignedUploadUrl: vi.fn().mockResolvedValue({
|
||||
presignedUrl: 'https://example.com/presigned-url',
|
||||
key: 'test-key.txt',
|
||||
}),
|
||||
generatePresignedDownloadUrl: vi
|
||||
.fn()
|
||||
.mockResolvedValue('https://example.com/presigned-url'),
|
||||
},
|
||||
uploadFile: uploadFileMock,
|
||||
downloadFile: downloadFileMock,
|
||||
deleteFile: deleteFileMock,
|
||||
getPresignedUrl: vi.fn().mockResolvedValue('https://example.com/presigned-url'),
|
||||
hasCloudStorage: hasCloudStorageMock,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -1409,13 +1492,21 @@ export function mockUploadUtils(
|
||||
uploadError = false,
|
||||
} = options
|
||||
|
||||
const uploadFileMock = vi.fn().mockImplementation(() => {
|
||||
if (uploadError) {
|
||||
return Promise.reject(new Error('Upload failed'))
|
||||
}
|
||||
return Promise.resolve(uploadResult)
|
||||
})
|
||||
|
||||
vi.doMock('@/lib/uploads', () => ({
|
||||
uploadFile: vi.fn().mockImplementation(() => {
|
||||
if (uploadError) {
|
||||
return Promise.reject(new Error('Upload failed'))
|
||||
}
|
||||
return Promise.resolve(uploadResult)
|
||||
}),
|
||||
StorageService: {
|
||||
uploadFile: uploadFileMock,
|
||||
downloadFile: vi.fn().mockResolvedValue(Buffer.from('test content')),
|
||||
deleteFile: vi.fn().mockResolvedValue(undefined),
|
||||
hasCloudStorage: vi.fn().mockReturnValue(isCloudStorage),
|
||||
},
|
||||
uploadFile: uploadFileMock,
|
||||
isUsingCloudStorage: vi.fn().mockReturnValue(isCloudStorage),
|
||||
}))
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { db } from '@sim/db'
|
||||
import { subscription as subscriptionTable, user } from '@sim/db/schema'
|
||||
import { and, eq } from 'drizzle-orm'
|
||||
import { and, eq, or } from 'drizzle-orm'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { getSession } from '@/lib/auth'
|
||||
import { requireStripeClient } from '@/lib/billing/stripe-client'
|
||||
@@ -38,7 +38,10 @@ export async function POST(request: NextRequest) {
|
||||
.where(
|
||||
and(
|
||||
eq(subscriptionTable.referenceId, organizationId),
|
||||
eq(subscriptionTable.status, 'active')
|
||||
or(
|
||||
eq(subscriptionTable.status, 'active'),
|
||||
eq(subscriptionTable.cancelAtPeriodEnd, true)
|
||||
)
|
||||
)
|
||||
)
|
||||
.limit(1)
|
||||
|
||||
200
apps/sim/app/api/careers/submit/route.ts
Normal file
200
apps/sim/app/api/careers/submit/route.ts
Normal file
@@ -0,0 +1,200 @@
|
||||
import { render } from '@react-email/components'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import CareersConfirmationEmail from '@/components/emails/careers-confirmation-email'
|
||||
import CareersSubmissionEmail from '@/components/emails/careers-submission-email'
|
||||
import { sendEmail } from '@/lib/email/mailer'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { generateRequestId } from '@/lib/utils'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
const logger = createLogger('CareersAPI')
|
||||
|
||||
// Max file size: 10MB
|
||||
const MAX_FILE_SIZE = 10 * 1024 * 1024
|
||||
const ALLOWED_FILE_TYPES = [
|
||||
'application/pdf',
|
||||
'application/msword',
|
||||
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||
]
|
||||
|
||||
const CareersSubmissionSchema = z.object({
|
||||
name: z.string().min(2, 'Name must be at least 2 characters'),
|
||||
email: z.string().email('Please enter a valid email address'),
|
||||
phone: z.string().optional(),
|
||||
position: z.string().min(2, 'Please specify the position you are interested in'),
|
||||
linkedin: z.string().url('Please enter a valid LinkedIn URL').optional().or(z.literal('')),
|
||||
portfolio: z.string().url('Please enter a valid portfolio URL').optional().or(z.literal('')),
|
||||
experience: z.enum(['0-1', '1-3', '3-5', '5-10', '10+']),
|
||||
location: z.string().min(2, 'Please enter your location'),
|
||||
message: z.string().min(50, 'Please tell us more about yourself (at least 50 characters)'),
|
||||
})
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const requestId = generateRequestId()
|
||||
|
||||
try {
|
||||
const formData = await request.formData()
|
||||
|
||||
// Extract form fields
|
||||
const data = {
|
||||
name: formData.get('name') as string,
|
||||
email: formData.get('email') as string,
|
||||
phone: formData.get('phone') as string,
|
||||
position: formData.get('position') as string,
|
||||
linkedin: formData.get('linkedin') as string,
|
||||
portfolio: formData.get('portfolio') as string,
|
||||
experience: formData.get('experience') as string,
|
||||
location: formData.get('location') as string,
|
||||
message: formData.get('message') as string,
|
||||
}
|
||||
|
||||
// Extract and validate resume file
|
||||
const resumeFile = formData.get('resume') as File | null
|
||||
if (!resumeFile) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: 'Resume is required',
|
||||
errors: [{ path: ['resume'], message: 'Resume is required' }],
|
||||
},
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Validate file size
|
||||
if (resumeFile.size > MAX_FILE_SIZE) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: 'Resume file size must be less than 10MB',
|
||||
errors: [{ path: ['resume'], message: 'File size must be less than 10MB' }],
|
||||
},
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Validate file type
|
||||
if (!ALLOWED_FILE_TYPES.includes(resumeFile.type)) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: 'Resume must be a PDF or Word document',
|
||||
errors: [{ path: ['resume'], message: 'File must be PDF or Word document' }],
|
||||
},
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Convert file to base64 for email attachment
|
||||
const resumeBuffer = await resumeFile.arrayBuffer()
|
||||
const resumeBase64 = Buffer.from(resumeBuffer).toString('base64')
|
||||
|
||||
const validatedData = CareersSubmissionSchema.parse(data)
|
||||
|
||||
logger.info(`[${requestId}] Processing career application`, {
|
||||
name: validatedData.name,
|
||||
email: validatedData.email,
|
||||
position: validatedData.position,
|
||||
resumeSize: resumeFile.size,
|
||||
resumeType: resumeFile.type,
|
||||
})
|
||||
|
||||
const submittedDate = new Date()
|
||||
|
||||
const careersEmailHtml = await render(
|
||||
CareersSubmissionEmail({
|
||||
name: validatedData.name,
|
||||
email: validatedData.email,
|
||||
phone: validatedData.phone,
|
||||
position: validatedData.position,
|
||||
linkedin: validatedData.linkedin,
|
||||
portfolio: validatedData.portfolio,
|
||||
experience: validatedData.experience,
|
||||
location: validatedData.location,
|
||||
message: validatedData.message,
|
||||
submittedDate,
|
||||
})
|
||||
)
|
||||
|
||||
const confirmationEmailHtml = await render(
|
||||
CareersConfirmationEmail({
|
||||
name: validatedData.name,
|
||||
position: validatedData.position,
|
||||
submittedDate,
|
||||
})
|
||||
)
|
||||
|
||||
// Send email with resume attachment
|
||||
const careersEmailResult = await sendEmail({
|
||||
to: 'careers@sim.ai',
|
||||
subject: `New Career Application: ${validatedData.name} - ${validatedData.position}`,
|
||||
html: careersEmailHtml,
|
||||
emailType: 'transactional',
|
||||
replyTo: validatedData.email,
|
||||
attachments: [
|
||||
{
|
||||
filename: resumeFile.name,
|
||||
content: resumeBase64,
|
||||
contentType: resumeFile.type,
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
if (!careersEmailResult.success) {
|
||||
logger.error(`[${requestId}] Failed to send email to careers@sim.ai`, {
|
||||
error: careersEmailResult.message,
|
||||
})
|
||||
throw new Error('Failed to submit application')
|
||||
}
|
||||
|
||||
const confirmationResult = await sendEmail({
|
||||
to: validatedData.email,
|
||||
subject: `Your Application to Sim - ${validatedData.position}`,
|
||||
html: confirmationEmailHtml,
|
||||
emailType: 'transactional',
|
||||
replyTo: validatedData.email,
|
||||
})
|
||||
|
||||
if (!confirmationResult.success) {
|
||||
logger.warn(`[${requestId}] Failed to send confirmation email to applicant`, {
|
||||
email: validatedData.email,
|
||||
error: confirmationResult.message,
|
||||
})
|
||||
}
|
||||
|
||||
logger.info(`[${requestId}] Career application submitted successfully`, {
|
||||
careersEmailSent: careersEmailResult.success,
|
||||
confirmationEmailSent: confirmationResult.success,
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Application submitted successfully',
|
||||
})
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
logger.warn(`[${requestId}] Invalid application data`, { errors: error.errors })
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: 'Invalid application data',
|
||||
errors: error.errors,
|
||||
},
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
logger.error(`[${requestId}] Error processing career application:`, error)
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message:
|
||||
'Failed to submit application. Please try again or email us directly at careers@sim.ai',
|
||||
},
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
import { db } from '@sim/db'
|
||||
import { chat, workflow } from '@sim/db/schema'
|
||||
import { chat, workflow, workspace } from '@sim/db/schema'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { ChatFiles } from '@/lib/uploads'
|
||||
import { generateRequestId } from '@/lib/utils'
|
||||
import {
|
||||
addCorsHeaders,
|
||||
processChatFiles,
|
||||
setChatAuthCookie,
|
||||
validateAuthToken,
|
||||
validateChatAuth,
|
||||
@@ -94,11 +94,12 @@ export async function POST(
|
||||
return addCorsHeaders(createErrorResponse('No input provided', 400), request)
|
||||
}
|
||||
|
||||
// Get the workflow for this chat
|
||||
// Get the workflow and workspace owner for this chat
|
||||
const workflowResult = await db
|
||||
.select({
|
||||
isDeployed: workflow.isDeployed,
|
||||
workspaceId: workflow.workspaceId,
|
||||
variables: workflow.variables,
|
||||
})
|
||||
.from(workflow)
|
||||
.where(eq(workflow.id, deployment.workflowId))
|
||||
@@ -109,6 +110,22 @@ export async function POST(
|
||||
return addCorsHeaders(createErrorResponse('Chat workflow is not available', 503), request)
|
||||
}
|
||||
|
||||
let workspaceOwnerId = deployment.userId
|
||||
if (workflowResult[0].workspaceId) {
|
||||
const workspaceData = await db
|
||||
.select({ ownerId: workspace.ownerId })
|
||||
.from(workspace)
|
||||
.where(eq(workspace.id, workflowResult[0].workspaceId))
|
||||
.limit(1)
|
||||
|
||||
if (workspaceData.length === 0) {
|
||||
logger.error(`[${requestId}] Workspace not found for workflow ${deployment.workflowId}`)
|
||||
return addCorsHeaders(createErrorResponse('Workspace not found', 500), request)
|
||||
}
|
||||
|
||||
workspaceOwnerId = workspaceData[0].ownerId
|
||||
}
|
||||
|
||||
try {
|
||||
const selectedOutputs: string[] = []
|
||||
if (deployment.outputConfigs && Array.isArray(deployment.outputConfigs)) {
|
||||
@@ -137,7 +154,7 @@ export async function POST(
|
||||
executionId,
|
||||
}
|
||||
|
||||
const uploadedFiles = await processChatFiles(files, executionContext, requestId)
|
||||
const uploadedFiles = await ChatFiles.processChatFiles(files, executionContext, requestId)
|
||||
|
||||
if (uploadedFiles.length > 0) {
|
||||
workflowInput.files = uploadedFiles
|
||||
@@ -145,16 +162,19 @@ export async function POST(
|
||||
}
|
||||
}
|
||||
|
||||
const workflowForExecution = {
|
||||
id: deployment.workflowId,
|
||||
userId: deployment.userId,
|
||||
workspaceId: workflowResult[0].workspaceId,
|
||||
isDeployed: true,
|
||||
variables: workflowResult[0].variables || {},
|
||||
}
|
||||
|
||||
const stream = await createStreamingResponse({
|
||||
requestId,
|
||||
workflow: {
|
||||
id: deployment.workflowId,
|
||||
userId: deployment.userId,
|
||||
workspaceId: workflowResult[0].workspaceId,
|
||||
isDeployed: true,
|
||||
},
|
||||
workflow: workflowForExecution,
|
||||
input: workflowInput,
|
||||
executingUserId: deployment.userId,
|
||||
executingUserId: workspaceOwnerId,
|
||||
streamConfig: {
|
||||
selectedOutputs,
|
||||
isSecureMode: true,
|
||||
|
||||
@@ -8,6 +8,7 @@ import { isDev } from '@/lib/environment'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { getEmailDomain } from '@/lib/urls/utils'
|
||||
import { encryptSecret } from '@/lib/utils'
|
||||
import { deployWorkflow } from '@/lib/workflows/db-helpers'
|
||||
import { checkChatAccess } from '@/app/api/chat/utils'
|
||||
import { createErrorResponse, createSuccessResponse } from '@/app/api/workflows/utils'
|
||||
|
||||
@@ -31,7 +32,7 @@ const chatUpdateSchema = z.object({
|
||||
imageUrl: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
authType: z.enum(['public', 'password', 'email']).optional(),
|
||||
authType: z.enum(['public', 'password', 'email', 'sso']).optional(),
|
||||
password: z.string().optional(),
|
||||
allowedEmails: z.array(z.string()).optional(),
|
||||
outputConfigs: z
|
||||
@@ -134,6 +135,22 @@ export async function PATCH(request: NextRequest, { params }: { params: Promise<
|
||||
}
|
||||
}
|
||||
|
||||
// Redeploy the workflow to ensure latest version is active
|
||||
const deployResult = await deployWorkflow({
|
||||
workflowId: existingChat[0].workflowId,
|
||||
deployedBy: session.user.id,
|
||||
})
|
||||
|
||||
if (!deployResult.success) {
|
||||
logger.warn(
|
||||
`Failed to redeploy workflow for chat update: ${deployResult.error}, continuing with chat update`
|
||||
)
|
||||
} else {
|
||||
logger.info(
|
||||
`Redeployed workflow ${existingChat[0].workflowId} for chat update (v${deployResult.version})`
|
||||
)
|
||||
}
|
||||
|
||||
let encryptedPassword
|
||||
|
||||
if (password) {
|
||||
@@ -165,7 +182,7 @@ export async function PATCH(request: NextRequest, { params }: { params: Promise<
|
||||
updateData.allowedEmails = []
|
||||
} else if (authType === 'password') {
|
||||
updateData.allowedEmails = []
|
||||
} else if (authType === 'email') {
|
||||
} else if (authType === 'email' || authType === 'sso') {
|
||||
updateData.password = null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ describe('Chat API Route', () => {
|
||||
const mockCreateErrorResponse = vi.fn()
|
||||
const mockEncryptSecret = vi.fn()
|
||||
const mockCheckWorkflowAccessForChatCreation = vi.fn()
|
||||
const mockDeployWorkflow = vi.fn()
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetModules()
|
||||
@@ -76,6 +77,14 @@ describe('Chat API Route', () => {
|
||||
vi.doMock('@/app/api/chat/utils', () => ({
|
||||
checkWorkflowAccessForChatCreation: mockCheckWorkflowAccessForChatCreation,
|
||||
}))
|
||||
|
||||
vi.doMock('@/lib/workflows/db-helpers', () => ({
|
||||
deployWorkflow: mockDeployWorkflow.mockResolvedValue({
|
||||
success: true,
|
||||
version: 1,
|
||||
deployedAt: new Date(),
|
||||
}),
|
||||
}))
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
@@ -236,7 +245,7 @@ describe('Chat API Route', () => {
|
||||
it('should allow chat deployment when user owns workflow directly', async () => {
|
||||
vi.doMock('@/lib/auth', () => ({
|
||||
getSession: vi.fn().mockResolvedValue({
|
||||
user: { id: 'user-id' },
|
||||
user: { id: 'user-id', email: 'user@example.com' },
|
||||
}),
|
||||
}))
|
||||
|
||||
@@ -283,7 +292,7 @@ describe('Chat API Route', () => {
|
||||
it('should allow chat deployment when user has workspace admin permission', async () => {
|
||||
vi.doMock('@/lib/auth', () => ({
|
||||
getSession: vi.fn().mockResolvedValue({
|
||||
user: { id: 'user-id' },
|
||||
user: { id: 'user-id', email: 'user@example.com' },
|
||||
}),
|
||||
}))
|
||||
|
||||
@@ -393,10 +402,10 @@ describe('Chat API Route', () => {
|
||||
expect(mockCheckWorkflowAccessForChatCreation).toHaveBeenCalledWith('workflow-123', 'user-id')
|
||||
})
|
||||
|
||||
it('should reject if workflow is not deployed', async () => {
|
||||
it('should auto-deploy workflow if not already deployed', async () => {
|
||||
vi.doMock('@/lib/auth', () => ({
|
||||
getSession: vi.fn().mockResolvedValue({
|
||||
user: { id: 'user-id' },
|
||||
user: { id: 'user-id', email: 'user@example.com' },
|
||||
}),
|
||||
}))
|
||||
|
||||
@@ -415,6 +424,7 @@ describe('Chat API Route', () => {
|
||||
hasAccess: true,
|
||||
workflow: { userId: 'user-id', workspaceId: null, isDeployed: false },
|
||||
})
|
||||
mockReturning.mockResolvedValue([{ id: 'test-uuid' }])
|
||||
|
||||
const req = new NextRequest('http://localhost:3000/api/chat', {
|
||||
method: 'POST',
|
||||
@@ -423,11 +433,11 @@ describe('Chat API Route', () => {
|
||||
const { POST } = await import('@/app/api/chat/route')
|
||||
const response = await POST(req)
|
||||
|
||||
expect(response.status).toBe(400)
|
||||
expect(mockCreateErrorResponse).toHaveBeenCalledWith(
|
||||
'Workflow must be deployed before creating a chat',
|
||||
400
|
||||
)
|
||||
expect(response.status).toBe(200)
|
||||
expect(mockDeployWorkflow).toHaveBeenCalledWith({
|
||||
workflowId: 'workflow-123',
|
||||
deployedBy: 'user-id',
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -9,6 +9,7 @@ import { isDev } from '@/lib/environment'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { getBaseUrl } from '@/lib/urls/utils'
|
||||
import { encryptSecret } from '@/lib/utils'
|
||||
import { deployWorkflow } from '@/lib/workflows/db-helpers'
|
||||
import { checkWorkflowAccessForChatCreation } from '@/app/api/chat/utils'
|
||||
import { createErrorResponse, createSuccessResponse } from '@/app/api/workflows/utils'
|
||||
|
||||
@@ -27,7 +28,7 @@ const chatSchema = z.object({
|
||||
welcomeMessage: z.string(),
|
||||
imageUrl: z.string().optional(),
|
||||
}),
|
||||
authType: z.enum(['public', 'password', 'email']).default('public'),
|
||||
authType: z.enum(['public', 'password', 'email', 'sso']).default('public'),
|
||||
password: z.string().optional(),
|
||||
allowedEmails: z.array(z.string()).optional().default([]),
|
||||
outputConfigs: z
|
||||
@@ -98,6 +99,13 @@ export async function POST(request: NextRequest) {
|
||||
)
|
||||
}
|
||||
|
||||
if (authType === 'sso' && (!Array.isArray(allowedEmails) || allowedEmails.length === 0)) {
|
||||
return createErrorResponse(
|
||||
'At least one email or domain is required when using SSO access control',
|
||||
400
|
||||
)
|
||||
}
|
||||
|
||||
// Check if identifier is available
|
||||
const existingIdentifier = await db
|
||||
.select()
|
||||
@@ -119,11 +127,20 @@ export async function POST(request: NextRequest) {
|
||||
return createErrorResponse('Workflow not found or access denied', 404)
|
||||
}
|
||||
|
||||
// Verify the workflow is deployed (required for chat deployment)
|
||||
if (!workflowRecord.isDeployed) {
|
||||
return createErrorResponse('Workflow must be deployed before creating a chat', 400)
|
||||
// Always deploy/redeploy the workflow to ensure latest version
|
||||
const result = await deployWorkflow({
|
||||
workflowId,
|
||||
deployedBy: session.user.id,
|
||||
})
|
||||
|
||||
if (!result.success) {
|
||||
return createErrorResponse(result.error || 'Failed to deploy workflow', 500)
|
||||
}
|
||||
|
||||
logger.info(
|
||||
`${workflowRecord.isDeployed ? 'Redeployed' : 'Auto-deployed'} workflow ${workflowId} for chat (v${result.version})`
|
||||
)
|
||||
|
||||
// Encrypt password if provided
|
||||
let encryptedPassword = null
|
||||
if (authType === 'password' && password) {
|
||||
@@ -163,7 +180,7 @@ export async function POST(request: NextRequest) {
|
||||
isActive: true,
|
||||
authType,
|
||||
password: encryptedPassword,
|
||||
allowedEmails: authType === 'email' ? allowedEmails : [],
|
||||
allowedEmails: authType === 'email' || authType === 'sso' ? allowedEmails : [],
|
||||
outputConfigs,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
|
||||
@@ -3,11 +3,9 @@ import { chat, workflow } from '@sim/db/schema'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { isDev } from '@/lib/environment'
|
||||
import { processExecutionFiles } from '@/lib/execution/files'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { hasAdminPermission } from '@/lib/permissions/utils'
|
||||
import { decryptSecret } from '@/lib/utils'
|
||||
import type { UserFile } from '@/executor/types'
|
||||
|
||||
const logger = createLogger('ChatAuthUtils')
|
||||
|
||||
@@ -19,7 +17,6 @@ export async function checkWorkflowAccessForChatCreation(
|
||||
workflowId: string,
|
||||
userId: string
|
||||
): Promise<{ hasAccess: boolean; workflow?: any }> {
|
||||
// Get workflow data
|
||||
const workflowData = await db.select().from(workflow).where(eq(workflow.id, workflowId)).limit(1)
|
||||
|
||||
if (workflowData.length === 0) {
|
||||
@@ -28,12 +25,10 @@ export async function checkWorkflowAccessForChatCreation(
|
||||
|
||||
const workflowRecord = workflowData[0]
|
||||
|
||||
// Case 1: User owns the workflow directly
|
||||
if (workflowRecord.userId === userId) {
|
||||
return { hasAccess: true, workflow: workflowRecord }
|
||||
}
|
||||
|
||||
// Case 2: Workflow belongs to a workspace and user has admin permission
|
||||
if (workflowRecord.workspaceId) {
|
||||
const hasAdmin = await hasAdminPermission(userId, workflowRecord.workspaceId)
|
||||
if (hasAdmin) {
|
||||
@@ -52,7 +47,6 @@ export async function checkChatAccess(
|
||||
chatId: string,
|
||||
userId: string
|
||||
): Promise<{ hasAccess: boolean; chat?: any }> {
|
||||
// Get chat with workflow information
|
||||
const chatData = await db
|
||||
.select({
|
||||
chat: chat,
|
||||
@@ -69,12 +63,10 @@ export async function checkChatAccess(
|
||||
|
||||
const { chat: chatRecord, workflowWorkspaceId } = chatData[0]
|
||||
|
||||
// Case 1: User owns the chat directly
|
||||
if (chatRecord.userId === userId) {
|
||||
return { hasAccess: true, chat: chatRecord }
|
||||
}
|
||||
|
||||
// Case 2: Chat's workflow belongs to a workspace and user has admin permission
|
||||
if (workflowWorkspaceId) {
|
||||
const hasAdmin = await hasAdminPermission(userId, workflowWorkspaceId)
|
||||
if (hasAdmin) {
|
||||
@@ -94,12 +86,10 @@ export const validateAuthToken = (token: string, chatId: string): boolean => {
|
||||
const decoded = Buffer.from(token, 'base64').toString()
|
||||
const [storedId, _type, timestamp] = decoded.split(':')
|
||||
|
||||
// Check if token is for this chat
|
||||
if (storedId !== chatId) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if token is not expired (24 hours)
|
||||
const createdAt = Number.parseInt(timestamp)
|
||||
const now = Date.now()
|
||||
const expireTime = 24 * 60 * 60 * 1000 // 24 hours
|
||||
@@ -117,7 +107,6 @@ export const validateAuthToken = (token: string, chatId: string): boolean => {
|
||||
// Set cookie helper function
|
||||
export const setChatAuthCookie = (response: NextResponse, chatId: string, type: string): void => {
|
||||
const token = encryptAuthToken(chatId, type)
|
||||
// Set cookie with HttpOnly and secure flags
|
||||
response.cookies.set({
|
||||
name: `chat_auth_${chatId}`,
|
||||
value: token,
|
||||
@@ -131,10 +120,8 @@ export const setChatAuthCookie = (response: NextResponse, chatId: string, type:
|
||||
|
||||
// Helper function to add CORS headers to responses
|
||||
export function addCorsHeaders(response: NextResponse, request: NextRequest) {
|
||||
// Get the origin from the request
|
||||
const origin = request.headers.get('origin') || ''
|
||||
|
||||
// In development, allow any localhost subdomain
|
||||
if (isDev && origin.includes('localhost')) {
|
||||
response.headers.set('Access-Control-Allow-Origin', origin)
|
||||
response.headers.set('Access-Control-Allow-Credentials', 'true')
|
||||
@@ -145,7 +132,6 @@ export function addCorsHeaders(response: NextResponse, request: NextRequest) {
|
||||
return response
|
||||
}
|
||||
|
||||
// Handle OPTIONS requests for CORS preflight
|
||||
export async function OPTIONS(request: NextRequest) {
|
||||
const response = new NextResponse(null, { status: 204 })
|
||||
return addCorsHeaders(response, request)
|
||||
@@ -181,14 +167,12 @@ export async function validateChatAuth(
|
||||
}
|
||||
|
||||
try {
|
||||
// Use the parsed body if provided, otherwise the auth check is not applicable
|
||||
if (!parsedBody) {
|
||||
return { authorized: false, error: 'Password is required' }
|
||||
}
|
||||
|
||||
const { password, input } = parsedBody
|
||||
|
||||
// If this is a chat message, not an auth attempt
|
||||
if (input && !password) {
|
||||
return { authorized: false, error: 'auth_required_password' }
|
||||
}
|
||||
@@ -202,7 +186,6 @@ export async function validateChatAuth(
|
||||
return { authorized: false, error: 'Authentication configuration error' }
|
||||
}
|
||||
|
||||
// Decrypt the stored password and compare
|
||||
const { decrypted } = await decryptSecret(deployment.password)
|
||||
if (password !== decrypted) {
|
||||
return { authorized: false, error: 'Invalid password' }
|
||||
@@ -262,27 +245,66 @@ export async function validateChatAuth(
|
||||
}
|
||||
}
|
||||
|
||||
// Unknown auth type
|
||||
if (authType === 'sso') {
|
||||
if (request.method === 'GET') {
|
||||
return { authorized: false, error: 'auth_required_sso' }
|
||||
}
|
||||
|
||||
try {
|
||||
if (!parsedBody) {
|
||||
return { authorized: false, error: 'SSO authentication is required' }
|
||||
}
|
||||
|
||||
const { email, input, checkSSOAccess } = parsedBody
|
||||
|
||||
if (checkSSOAccess) {
|
||||
if (!email) {
|
||||
return { authorized: false, error: 'Email is required' }
|
||||
}
|
||||
|
||||
const allowedEmails = deployment.allowedEmails || []
|
||||
|
||||
if (allowedEmails.includes(email)) {
|
||||
return { authorized: true }
|
||||
}
|
||||
|
||||
const domain = email.split('@')[1]
|
||||
if (domain && allowedEmails.some((allowed: string) => allowed === `@${domain}`)) {
|
||||
return { authorized: true }
|
||||
}
|
||||
|
||||
return { authorized: false, error: 'Email not authorized for SSO access' }
|
||||
}
|
||||
|
||||
const { auth } = await import('@/lib/auth')
|
||||
const session = await auth.api.getSession({ headers: request.headers })
|
||||
|
||||
if (!session || !session.user) {
|
||||
return { authorized: false, error: 'auth_required_sso' }
|
||||
}
|
||||
|
||||
const userEmail = session.user.email
|
||||
if (!userEmail) {
|
||||
return { authorized: false, error: 'SSO session does not contain email' }
|
||||
}
|
||||
|
||||
const allowedEmails = deployment.allowedEmails || []
|
||||
|
||||
if (allowedEmails.includes(userEmail)) {
|
||||
return { authorized: true }
|
||||
}
|
||||
|
||||
const domain = userEmail.split('@')[1]
|
||||
if (domain && allowedEmails.some((allowed: string) => allowed === `@${domain}`)) {
|
||||
return { authorized: true }
|
||||
}
|
||||
|
||||
return { authorized: false, error: 'Your email is not authorized to access this chat' }
|
||||
} catch (error) {
|
||||
logger.error(`[${requestId}] Error validating SSO:`, error)
|
||||
return { authorized: false, error: 'SSO authentication error' }
|
||||
}
|
||||
}
|
||||
|
||||
return { authorized: false, error: 'Unsupported authentication type' }
|
||||
}
|
||||
|
||||
/**
|
||||
* Process and upload chat files to execution storage
|
||||
* Handles both base64 dataUrl format and direct URL pass-through
|
||||
* Delegates to shared execution file processing logic
|
||||
*/
|
||||
export async function processChatFiles(
|
||||
files: Array<{ dataUrl?: string; url?: string; name: string; type: string }>,
|
||||
executionContext: { workspaceId: string; workflowId: string; executionId: string },
|
||||
requestId: string
|
||||
): Promise<UserFile[]> {
|
||||
// Transform chat file format to shared execution file format
|
||||
const transformedFiles = files.map((file) => ({
|
||||
type: file.dataUrl ? 'file' : 'url',
|
||||
data: file.dataUrl || file.url || '',
|
||||
name: file.name,
|
||||
mime: file.type,
|
||||
}))
|
||||
|
||||
return processExecutionFiles(transformedFiles, executionContext, requestId)
|
||||
}
|
||||
|
||||
@@ -17,9 +17,8 @@ import { env } from '@/lib/env'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { SIM_AGENT_API_URL_DEFAULT, SIM_AGENT_VERSION } from '@/lib/sim-agent/constants'
|
||||
import { generateChatTitle } from '@/lib/sim-agent/utils'
|
||||
import { createFileContent, isSupportedFileType } from '@/lib/uploads/file-utils'
|
||||
import { S3_COPILOT_CONFIG } from '@/lib/uploads/setup'
|
||||
import { downloadFile, getStorageProvider } from '@/lib/uploads/storage-client'
|
||||
import { CopilotFiles } from '@/lib/uploads'
|
||||
import { createFileContent } from '@/lib/uploads/utils/file-utils'
|
||||
|
||||
const logger = createLogger('CopilotChatAPI')
|
||||
|
||||
@@ -202,45 +201,15 @@ export async function POST(req: NextRequest) {
|
||||
// Process file attachments if present
|
||||
const processedFileContents: any[] = []
|
||||
if (fileAttachments && fileAttachments.length > 0) {
|
||||
for (const attachment of fileAttachments) {
|
||||
try {
|
||||
// Check if file type is supported
|
||||
if (!isSupportedFileType(attachment.media_type)) {
|
||||
logger.warn(`[${tracker.requestId}] Unsupported file type: ${attachment.media_type}`)
|
||||
continue
|
||||
}
|
||||
const processedAttachments = await CopilotFiles.processCopilotAttachments(
|
||||
fileAttachments,
|
||||
tracker.requestId
|
||||
)
|
||||
|
||||
const storageProvider = getStorageProvider()
|
||||
let fileBuffer: Buffer
|
||||
|
||||
if (storageProvider === 's3') {
|
||||
fileBuffer = await downloadFile(attachment.key, {
|
||||
bucket: S3_COPILOT_CONFIG.bucket,
|
||||
region: S3_COPILOT_CONFIG.region,
|
||||
})
|
||||
} else if (storageProvider === 'blob') {
|
||||
const { BLOB_COPILOT_CONFIG } = await import('@/lib/uploads/setup')
|
||||
fileBuffer = await downloadFile(attachment.key, {
|
||||
containerName: BLOB_COPILOT_CONFIG.containerName,
|
||||
accountName: BLOB_COPILOT_CONFIG.accountName,
|
||||
accountKey: BLOB_COPILOT_CONFIG.accountKey,
|
||||
connectionString: BLOB_COPILOT_CONFIG.connectionString,
|
||||
})
|
||||
} else {
|
||||
fileBuffer = await downloadFile(attachment.key)
|
||||
}
|
||||
|
||||
// Convert to format
|
||||
const fileContent = createFileContent(fileBuffer, attachment.media_type)
|
||||
if (fileContent) {
|
||||
processedFileContents.push(fileContent)
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
`[${tracker.requestId}] Failed to process file ${attachment.filename}:`,
|
||||
error
|
||||
)
|
||||
// Continue processing other files
|
||||
for (const { buffer, attachment } of processedAttachments) {
|
||||
const fileContent = createFileContent(buffer, attachment.media_type)
|
||||
if (fileContent) {
|
||||
processedFileContents.push(fileContent)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -254,39 +223,15 @@ export async function POST(req: NextRequest) {
|
||||
// This is a message with file attachments - rebuild with content array
|
||||
const content: any[] = [{ type: 'text', text: msg.content }]
|
||||
|
||||
// Process file attachments for historical messages
|
||||
for (const attachment of msg.fileAttachments) {
|
||||
try {
|
||||
if (isSupportedFileType(attachment.media_type)) {
|
||||
const storageProvider = getStorageProvider()
|
||||
let fileBuffer: Buffer
|
||||
const processedHistoricalAttachments = await CopilotFiles.processCopilotAttachments(
|
||||
msg.fileAttachments,
|
||||
tracker.requestId
|
||||
)
|
||||
|
||||
if (storageProvider === 's3') {
|
||||
fileBuffer = await downloadFile(attachment.key, {
|
||||
bucket: S3_COPILOT_CONFIG.bucket,
|
||||
region: S3_COPILOT_CONFIG.region,
|
||||
})
|
||||
} else if (storageProvider === 'blob') {
|
||||
const { BLOB_COPILOT_CONFIG } = await import('@/lib/uploads/setup')
|
||||
fileBuffer = await downloadFile(attachment.key, {
|
||||
containerName: BLOB_COPILOT_CONFIG.containerName,
|
||||
accountName: BLOB_COPILOT_CONFIG.accountName,
|
||||
accountKey: BLOB_COPILOT_CONFIG.accountKey,
|
||||
connectionString: BLOB_COPILOT_CONFIG.connectionString,
|
||||
})
|
||||
} else {
|
||||
fileBuffer = await downloadFile(attachment.key)
|
||||
}
|
||||
const fileContent = createFileContent(fileBuffer, attachment.media_type)
|
||||
if (fileContent) {
|
||||
content.push(fileContent)
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
`[${tracker.requestId}] Failed to process historical file ${attachment.filename}:`,
|
||||
error
|
||||
)
|
||||
for (const { buffer, attachment } of processedHistoricalAttachments) {
|
||||
const fileContent = createFileContent(buffer, attachment.media_type)
|
||||
if (fileContent) {
|
||||
content.push(fileContent)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -58,18 +58,6 @@ describe('File Delete API Route', () => {
|
||||
storageProvider: 's3',
|
||||
})
|
||||
|
||||
vi.doMock('@/lib/uploads', () => ({
|
||||
deleteFile: vi.fn().mockResolvedValue(undefined),
|
||||
isUsingCloudStorage: vi.fn().mockReturnValue(true),
|
||||
uploadFile: vi.fn().mockResolvedValue({
|
||||
path: '/api/files/serve/test-key',
|
||||
key: 'test-key',
|
||||
name: 'test.txt',
|
||||
size: 100,
|
||||
type: 'text/plain',
|
||||
}),
|
||||
}))
|
||||
|
||||
const req = createMockRequest('POST', {
|
||||
filePath: '/api/files/serve/s3/1234567890-test-file.txt',
|
||||
})
|
||||
@@ -81,10 +69,13 @@ describe('File Delete API Route', () => {
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(data).toHaveProperty('success', true)
|
||||
expect(data).toHaveProperty('message', 'File deleted successfully from cloud storage')
|
||||
expect(data).toHaveProperty('message', 'File deleted successfully')
|
||||
|
||||
const uploads = await import('@/lib/uploads')
|
||||
expect(uploads.deleteFile).toHaveBeenCalledWith('1234567890-test-file.txt')
|
||||
const storageService = await import('@/lib/uploads/core/storage-service')
|
||||
expect(storageService.deleteFile).toHaveBeenCalledWith({
|
||||
key: '1234567890-test-file.txt',
|
||||
context: 'general',
|
||||
})
|
||||
})
|
||||
|
||||
it('should handle Azure Blob file deletion successfully', async () => {
|
||||
@@ -93,18 +84,6 @@ describe('File Delete API Route', () => {
|
||||
storageProvider: 'blob',
|
||||
})
|
||||
|
||||
vi.doMock('@/lib/uploads', () => ({
|
||||
deleteFile: vi.fn().mockResolvedValue(undefined),
|
||||
isUsingCloudStorage: vi.fn().mockReturnValue(true),
|
||||
uploadFile: vi.fn().mockResolvedValue({
|
||||
path: '/api/files/serve/test-key',
|
||||
key: 'test-key',
|
||||
name: 'test.txt',
|
||||
size: 100,
|
||||
type: 'text/plain',
|
||||
}),
|
||||
}))
|
||||
|
||||
const req = createMockRequest('POST', {
|
||||
filePath: '/api/files/serve/blob/1234567890-test-document.pdf',
|
||||
})
|
||||
@@ -116,10 +95,13 @@ describe('File Delete API Route', () => {
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(data).toHaveProperty('success', true)
|
||||
expect(data).toHaveProperty('message', 'File deleted successfully from cloud storage')
|
||||
expect(data).toHaveProperty('message', 'File deleted successfully')
|
||||
|
||||
const uploads = await import('@/lib/uploads')
|
||||
expect(uploads.deleteFile).toHaveBeenCalledWith('1234567890-test-document.pdf')
|
||||
const storageService = await import('@/lib/uploads/core/storage-service')
|
||||
expect(storageService.deleteFile).toHaveBeenCalledWith({
|
||||
key: '1234567890-test-document.pdf',
|
||||
context: 'general',
|
||||
})
|
||||
})
|
||||
|
||||
it('should handle missing file path', async () => {
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
import { existsSync } from 'fs'
|
||||
import { unlink } from 'fs/promises'
|
||||
import { join } from 'path'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { deleteFile, isUsingCloudStorage } from '@/lib/uploads'
|
||||
import { UPLOAD_DIR } from '@/lib/uploads/setup'
|
||||
import '@/lib/uploads/setup.server'
|
||||
|
||||
import type { StorageContext } from '@/lib/uploads/core/config-resolver'
|
||||
import { deleteFile } from '@/lib/uploads/core/storage-service'
|
||||
import {
|
||||
createErrorResponse,
|
||||
createOptionsResponse,
|
||||
@@ -30,23 +25,32 @@ const logger = createLogger('FilesDeleteAPI')
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const requestData = await request.json()
|
||||
const { filePath } = requestData
|
||||
const { filePath, context } = requestData
|
||||
|
||||
logger.info('File delete request received:', { filePath })
|
||||
logger.info('File delete request received:', { filePath, context })
|
||||
|
||||
if (!filePath) {
|
||||
throw new InvalidRequestError('No file path provided')
|
||||
}
|
||||
|
||||
try {
|
||||
// Use appropriate handler based on path and environment
|
||||
const result =
|
||||
isCloudPath(filePath) || isUsingCloudStorage()
|
||||
? await handleCloudFileDelete(filePath)
|
||||
: await handleLocalFileDelete(filePath)
|
||||
const key = extractStorageKey(filePath)
|
||||
|
||||
// Return success response
|
||||
return createSuccessResponse(result)
|
||||
const storageContext: StorageContext = context || inferContextFromKey(key)
|
||||
|
||||
logger.info(`Deleting file with key: ${key}, context: ${storageContext}`)
|
||||
|
||||
await deleteFile({
|
||||
key,
|
||||
context: storageContext,
|
||||
})
|
||||
|
||||
logger.info(`File successfully deleted: ${key}`)
|
||||
|
||||
return createSuccessResponse({
|
||||
success: true,
|
||||
message: 'File deleted successfully',
|
||||
})
|
||||
} catch (error) {
|
||||
logger.error('Error deleting file:', error)
|
||||
return createErrorResponse(
|
||||
@@ -60,63 +64,9 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle cloud file deletion (S3 or Azure Blob)
|
||||
* Extract storage key from file path (works for S3, Blob, and local paths)
|
||||
*/
|
||||
async function handleCloudFileDelete(filePath: string) {
|
||||
// Extract the key from the path (works for both S3 and Blob paths)
|
||||
const key = extractCloudKey(filePath)
|
||||
logger.info(`Deleting file from cloud storage: ${key}`)
|
||||
|
||||
try {
|
||||
// Delete from cloud storage using abstraction layer
|
||||
await deleteFile(key)
|
||||
logger.info(`File successfully deleted from cloud storage: ${key}`)
|
||||
|
||||
return {
|
||||
success: true as const,
|
||||
message: 'File deleted successfully from cloud storage',
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('Error deleting file from cloud storage:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle local file deletion
|
||||
*/
|
||||
async function handleLocalFileDelete(filePath: string) {
|
||||
const filename = extractFilename(filePath)
|
||||
const fullPath = join(UPLOAD_DIR, filename)
|
||||
|
||||
logger.info(`Deleting local file: ${fullPath}`)
|
||||
|
||||
if (!existsSync(fullPath)) {
|
||||
logger.info(`File not found, but that's okay: ${fullPath}`)
|
||||
return {
|
||||
success: true as const,
|
||||
message: "File not found, but that's okay",
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await unlink(fullPath)
|
||||
logger.info(`File successfully deleted: ${fullPath}`)
|
||||
|
||||
return {
|
||||
success: true as const,
|
||||
message: 'File deleted successfully',
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('Error deleting local file:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract cloud storage key from file path (works for both S3 and Blob)
|
||||
*/
|
||||
function extractCloudKey(filePath: string): string {
|
||||
function extractStorageKey(filePath: string): string {
|
||||
if (isS3Path(filePath)) {
|
||||
return extractS3Key(filePath)
|
||||
}
|
||||
@@ -125,15 +75,60 @@ function extractCloudKey(filePath: string): string {
|
||||
return extractBlobKey(filePath)
|
||||
}
|
||||
|
||||
// Backwards-compatibility: allow generic paths like "/api/files/serve/<key>"
|
||||
// Handle "/api/files/serve/<key>" paths
|
||||
if (filePath.startsWith('/api/files/serve/')) {
|
||||
return decodeURIComponent(filePath.substring('/api/files/serve/'.length))
|
||||
const pathWithoutQuery = filePath.split('?')[0]
|
||||
return decodeURIComponent(pathWithoutQuery.substring('/api/files/serve/'.length))
|
||||
}
|
||||
|
||||
// As a last resort assume the incoming string is already a raw key.
|
||||
// For local files, extract filename
|
||||
if (!isCloudPath(filePath)) {
|
||||
return extractFilename(filePath)
|
||||
}
|
||||
|
||||
// As a last resort, assume the incoming string is already a raw key
|
||||
return filePath
|
||||
}
|
||||
|
||||
/**
|
||||
* Infer storage context from file key structure
|
||||
*
|
||||
* Key patterns:
|
||||
* - KB: kb/{uuid}-{filename}
|
||||
* - Workspace: {workspaceId}/{timestamp}-{random}-{filename}
|
||||
* - Execution: {workspaceId}/{workflowId}/{executionId}/{filename}
|
||||
* - Copilot: {timestamp}-{random}-{filename} (ambiguous - prefer explicit context)
|
||||
* - Chat: Uses execution context (same pattern as execution files)
|
||||
* - General: {timestamp}-{random}-{filename} (fallback for ambiguous patterns)
|
||||
*/
|
||||
function inferContextFromKey(key: string): StorageContext {
|
||||
// KB files always start with 'kb/' prefix
|
||||
if (key.startsWith('kb/')) {
|
||||
return 'knowledge-base'
|
||||
}
|
||||
|
||||
// Execution files: three or more UUID segments (workspace/workflow/execution/...)
|
||||
// Pattern: {uuid}/{uuid}/{uuid}/{filename}
|
||||
const segments = key.split('/')
|
||||
if (segments.length >= 4 && segments[0].match(/^[a-f0-9-]{36}$/)) {
|
||||
return 'execution'
|
||||
}
|
||||
|
||||
// Workspace files: UUID-like ID followed by timestamp pattern
|
||||
// Pattern: {uuid}/{timestamp}-{random}-{filename}
|
||||
if (key.match(/^[a-f0-9-]{36}\/\d+-[a-z0-9]+-/)) {
|
||||
return 'workspace'
|
||||
}
|
||||
|
||||
// Copilot/General files: timestamp-random-filename (no path segments)
|
||||
// Pattern: {timestamp}-{random}-{filename}
|
||||
if (key.match(/^\d+-[a-z0-9]+-/)) {
|
||||
return 'general'
|
||||
}
|
||||
|
||||
return 'general'
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle CORS preflight requests
|
||||
*/
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { getPresignedUrl, getPresignedUrlWithConfig, isUsingCloudStorage } from '@/lib/uploads'
|
||||
import { BLOB_EXECUTION_FILES_CONFIG, S3_EXECUTION_FILES_CONFIG } from '@/lib/uploads/setup'
|
||||
import type { StorageContext } from '@/lib/uploads/core/config-resolver'
|
||||
import { generatePresignedDownloadUrl, hasCloudStorage } from '@/lib/uploads/core/storage-service'
|
||||
import { getBaseUrl } from '@/lib/urls/utils'
|
||||
import { createErrorResponse } from '@/app/api/files/utils'
|
||||
|
||||
@@ -12,7 +12,7 @@ export const dynamic = 'force-dynamic'
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
const { key, name, storageProvider, bucketName, isExecutionFile } = body
|
||||
const { key, name, isExecutionFile, context } = body
|
||||
|
||||
if (!key) {
|
||||
return createErrorResponse(new Error('File key is required'), 400)
|
||||
@@ -20,53 +20,22 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
logger.info(`Generating download URL for file: ${name || key}`)
|
||||
|
||||
if (isUsingCloudStorage()) {
|
||||
// Generate a fresh 5-minute presigned URL for cloud storage
|
||||
let storageContext: StorageContext = context || 'general'
|
||||
|
||||
if (isExecutionFile && !context) {
|
||||
storageContext = 'execution'
|
||||
logger.info(`Using execution context for file: ${key}`)
|
||||
}
|
||||
|
||||
if (hasCloudStorage()) {
|
||||
try {
|
||||
let downloadUrl: string
|
||||
const downloadUrl = await generatePresignedDownloadUrl(
|
||||
key,
|
||||
storageContext,
|
||||
5 * 60 // 5 minutes
|
||||
)
|
||||
|
||||
// Use execution files storage if flagged as execution file
|
||||
if (isExecutionFile) {
|
||||
logger.info(`Using execution files storage for file: ${key}`)
|
||||
downloadUrl = await getPresignedUrlWithConfig(
|
||||
key,
|
||||
{
|
||||
bucket: S3_EXECUTION_FILES_CONFIG.bucket,
|
||||
region: S3_EXECUTION_FILES_CONFIG.region,
|
||||
},
|
||||
5 * 60 // 5 minutes
|
||||
)
|
||||
} else if (storageProvider && (storageProvider === 's3' || storageProvider === 'blob')) {
|
||||
// Use explicitly specified storage provider (legacy support)
|
||||
logger.info(`Using specified storage provider '${storageProvider}' for file: ${key}`)
|
||||
|
||||
if (storageProvider === 's3') {
|
||||
downloadUrl = await getPresignedUrlWithConfig(
|
||||
key,
|
||||
{
|
||||
bucket: bucketName || S3_EXECUTION_FILES_CONFIG.bucket,
|
||||
region: S3_EXECUTION_FILES_CONFIG.region,
|
||||
},
|
||||
5 * 60 // 5 minutes
|
||||
)
|
||||
} else {
|
||||
// blob
|
||||
downloadUrl = await getPresignedUrlWithConfig(
|
||||
key,
|
||||
{
|
||||
accountName: BLOB_EXECUTION_FILES_CONFIG.accountName,
|
||||
accountKey: BLOB_EXECUTION_FILES_CONFIG.accountKey,
|
||||
connectionString: BLOB_EXECUTION_FILES_CONFIG.connectionString,
|
||||
containerName: bucketName || BLOB_EXECUTION_FILES_CONFIG.containerName,
|
||||
},
|
||||
5 * 60 // 5 minutes
|
||||
)
|
||||
}
|
||||
} else {
|
||||
// Use default storage (regular uploads)
|
||||
logger.info(`Using default storage for file: ${key}`)
|
||||
downloadUrl = await getPresignedUrl(key, 5 * 60) // 5 minutes
|
||||
}
|
||||
logger.info(`Generated download URL for ${storageContext} file: ${key}`)
|
||||
|
||||
return NextResponse.json({
|
||||
downloadUrl,
|
||||
@@ -81,12 +50,13 @@ export async function POST(request: NextRequest) {
|
||||
)
|
||||
}
|
||||
} else {
|
||||
// For local storage, return the direct path
|
||||
const downloadUrl = `${getBaseUrl()}/api/files/serve/${key}`
|
||||
const downloadUrl = `${getBaseUrl()}/api/files/serve/${encodeURIComponent(key)}?context=${storageContext}`
|
||||
|
||||
logger.info(`Using local storage path for file: ${key}`)
|
||||
|
||||
return NextResponse.json({
|
||||
downloadUrl,
|
||||
expiresIn: null, // Local URLs don't expire
|
||||
expiresIn: null,
|
||||
fileName: name || key.split('/').pop() || 'download',
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { generateExecutionFileDownloadUrl } from '@/lib/workflows/execution-file-storage'
|
||||
import { getExecutionFiles } from '@/lib/workflows/execution-files-server'
|
||||
import {
|
||||
generateExecutionFileDownloadUrl,
|
||||
getExecutionFiles,
|
||||
} from '@/lib/uploads/contexts/execution'
|
||||
import type { UserFile } from '@/executor/types'
|
||||
|
||||
const logger = createLogger('ExecutionFileDownloadAPI')
|
||||
@@ -23,28 +25,23 @@ export async function GET(
|
||||
|
||||
logger.info(`Generating download URL for file ${fileId} in execution ${executionId}`)
|
||||
|
||||
// Get files for this execution
|
||||
const executionFiles = await getExecutionFiles(executionId)
|
||||
|
||||
if (executionFiles.length === 0) {
|
||||
return NextResponse.json({ error: 'No files found for this execution' }, { status: 404 })
|
||||
}
|
||||
|
||||
// Find the specific file
|
||||
const file = executionFiles.find((f) => f.id === fileId)
|
||||
if (!file) {
|
||||
return NextResponse.json({ error: 'File not found in this execution' }, { status: 404 })
|
||||
}
|
||||
|
||||
// Check if file is expired
|
||||
if (new Date(file.expiresAt) < new Date()) {
|
||||
return NextResponse.json({ error: 'File has expired' }, { status: 410 })
|
||||
}
|
||||
|
||||
// Since ExecutionFileMetadata is now just UserFile, no conversion needed
|
||||
const userFile: UserFile = file
|
||||
|
||||
// Generate a new short-lived presigned URL (5 minutes)
|
||||
const downloadUrl = await generateExecutionFileDownloadUrl(userFile)
|
||||
|
||||
logger.info(`Generated download URL for file ${file.name} (execution: ${executionId})`)
|
||||
@@ -57,7 +54,6 @@ export async function GET(
|
||||
expiresIn: 300, // 5 minutes
|
||||
})
|
||||
|
||||
// Ensure no caching of download URLs
|
||||
response.headers.set('Cache-Control', 'no-cache, no-store, must-revalidate')
|
||||
response.headers.set('Pragma', 'no-cache')
|
||||
response.headers.set('Expires', '0')
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { getSession } from '@/lib/auth'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { getStorageProvider, isUsingCloudStorage } from '@/lib/uploads'
|
||||
import { BLOB_KB_CONFIG } from '@/lib/uploads/setup'
|
||||
import {
|
||||
getStorageConfig,
|
||||
getStorageProvider,
|
||||
isUsingCloudStorage,
|
||||
type StorageContext,
|
||||
} from '@/lib/uploads'
|
||||
|
||||
const logger = createLogger('MultipartUploadAPI')
|
||||
|
||||
@@ -10,12 +14,14 @@ interface InitiateMultipartRequest {
|
||||
fileName: string
|
||||
contentType: string
|
||||
fileSize: number
|
||||
context?: StorageContext
|
||||
}
|
||||
|
||||
interface GetPartUrlsRequest {
|
||||
uploadId: string
|
||||
key: string
|
||||
partNumbers: number[]
|
||||
context?: StorageContext
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
@@ -39,10 +45,12 @@ export async function POST(request: NextRequest) {
|
||||
switch (action) {
|
||||
case 'initiate': {
|
||||
const data: InitiateMultipartRequest = await request.json()
|
||||
const { fileName, contentType, fileSize } = data
|
||||
const { fileName, contentType, fileSize, context = 'knowledge-base' } = data
|
||||
|
||||
const config = getStorageConfig(context)
|
||||
|
||||
if (storageProvider === 's3') {
|
||||
const { initiateS3MultipartUpload } = await import('@/lib/uploads/s3/s3-client')
|
||||
const { initiateS3MultipartUpload } = await import('@/lib/uploads/providers/s3/s3-client')
|
||||
|
||||
const result = await initiateS3MultipartUpload({
|
||||
fileName,
|
||||
@@ -50,7 +58,9 @@ export async function POST(request: NextRequest) {
|
||||
fileSize,
|
||||
})
|
||||
|
||||
logger.info(`Initiated S3 multipart upload for ${fileName}: ${result.uploadId}`)
|
||||
logger.info(
|
||||
`Initiated S3 multipart upload for ${fileName} (context: ${context}): ${result.uploadId}`
|
||||
)
|
||||
|
||||
return NextResponse.json({
|
||||
uploadId: result.uploadId,
|
||||
@@ -58,21 +68,25 @@ export async function POST(request: NextRequest) {
|
||||
})
|
||||
}
|
||||
if (storageProvider === 'blob') {
|
||||
const { initiateMultipartUpload } = await import('@/lib/uploads/blob/blob-client')
|
||||
const { initiateMultipartUpload } = await import(
|
||||
'@/lib/uploads/providers/blob/blob-client'
|
||||
)
|
||||
|
||||
const result = await initiateMultipartUpload({
|
||||
fileName,
|
||||
contentType,
|
||||
fileSize,
|
||||
customConfig: {
|
||||
containerName: BLOB_KB_CONFIG.containerName,
|
||||
accountName: BLOB_KB_CONFIG.accountName,
|
||||
accountKey: BLOB_KB_CONFIG.accountKey,
|
||||
connectionString: BLOB_KB_CONFIG.connectionString,
|
||||
containerName: config.containerName!,
|
||||
accountName: config.accountName!,
|
||||
accountKey: config.accountKey,
|
||||
connectionString: config.connectionString,
|
||||
},
|
||||
})
|
||||
|
||||
logger.info(`Initiated Azure multipart upload for ${fileName}: ${result.uploadId}`)
|
||||
logger.info(
|
||||
`Initiated Azure multipart upload for ${fileName} (context: ${context}): ${result.uploadId}`
|
||||
)
|
||||
|
||||
return NextResponse.json({
|
||||
uploadId: result.uploadId,
|
||||
@@ -88,23 +102,25 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
case 'get-part-urls': {
|
||||
const data: GetPartUrlsRequest = await request.json()
|
||||
const { uploadId, key, partNumbers } = data
|
||||
const { uploadId, key, partNumbers, context = 'knowledge-base' } = data
|
||||
|
||||
const config = getStorageConfig(context)
|
||||
|
||||
if (storageProvider === 's3') {
|
||||
const { getS3MultipartPartUrls } = await import('@/lib/uploads/s3/s3-client')
|
||||
const { getS3MultipartPartUrls } = await import('@/lib/uploads/providers/s3/s3-client')
|
||||
|
||||
const presignedUrls = await getS3MultipartPartUrls(key, uploadId, partNumbers)
|
||||
|
||||
return NextResponse.json({ presignedUrls })
|
||||
}
|
||||
if (storageProvider === 'blob') {
|
||||
const { getMultipartPartUrls } = await import('@/lib/uploads/blob/blob-client')
|
||||
const { getMultipartPartUrls } = await import('@/lib/uploads/providers/blob/blob-client')
|
||||
|
||||
const presignedUrls = await getMultipartPartUrls(key, uploadId, partNumbers, {
|
||||
containerName: BLOB_KB_CONFIG.containerName,
|
||||
accountName: BLOB_KB_CONFIG.accountName,
|
||||
accountKey: BLOB_KB_CONFIG.accountKey,
|
||||
connectionString: BLOB_KB_CONFIG.connectionString,
|
||||
containerName: config.containerName!,
|
||||
accountName: config.accountName!,
|
||||
accountKey: config.accountKey,
|
||||
connectionString: config.connectionString,
|
||||
})
|
||||
|
||||
return NextResponse.json({ presignedUrls })
|
||||
@@ -118,15 +134,19 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
case 'complete': {
|
||||
const data = await request.json()
|
||||
const context: StorageContext = data.context || 'knowledge-base'
|
||||
|
||||
const config = getStorageConfig(context)
|
||||
|
||||
// Handle batch completion
|
||||
if ('uploads' in data) {
|
||||
const results = await Promise.all(
|
||||
data.uploads.map(async (upload: any) => {
|
||||
const { uploadId, key } = upload
|
||||
|
||||
if (storageProvider === 's3') {
|
||||
const { completeS3MultipartUpload } = await import('@/lib/uploads/s3/s3-client')
|
||||
const { completeS3MultipartUpload } = await import(
|
||||
'@/lib/uploads/providers/s3/s3-client'
|
||||
)
|
||||
const parts = upload.parts // S3 format: { ETag, PartNumber }
|
||||
|
||||
const result = await completeS3MultipartUpload(key, uploadId, parts)
|
||||
@@ -139,14 +159,16 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
}
|
||||
if (storageProvider === 'blob') {
|
||||
const { completeMultipartUpload } = await import('@/lib/uploads/blob/blob-client')
|
||||
const { completeMultipartUpload } = await import(
|
||||
'@/lib/uploads/providers/blob/blob-client'
|
||||
)
|
||||
const parts = upload.parts // Azure format: { blockId, partNumber }
|
||||
|
||||
const result = await completeMultipartUpload(key, uploadId, parts, {
|
||||
containerName: BLOB_KB_CONFIG.containerName,
|
||||
accountName: BLOB_KB_CONFIG.accountName,
|
||||
accountKey: BLOB_KB_CONFIG.accountKey,
|
||||
connectionString: BLOB_KB_CONFIG.connectionString,
|
||||
containerName: config.containerName!,
|
||||
accountName: config.accountName!,
|
||||
accountKey: config.accountKey,
|
||||
connectionString: config.connectionString,
|
||||
})
|
||||
|
||||
return {
|
||||
@@ -161,19 +183,18 @@ export async function POST(request: NextRequest) {
|
||||
})
|
||||
)
|
||||
|
||||
logger.info(`Completed ${data.uploads.length} multipart uploads`)
|
||||
logger.info(`Completed ${data.uploads.length} multipart uploads (context: ${context})`)
|
||||
return NextResponse.json({ results })
|
||||
}
|
||||
|
||||
// Handle single completion
|
||||
const { uploadId, key, parts } = data
|
||||
|
||||
if (storageProvider === 's3') {
|
||||
const { completeS3MultipartUpload } = await import('@/lib/uploads/s3/s3-client')
|
||||
const { completeS3MultipartUpload } = await import('@/lib/uploads/providers/s3/s3-client')
|
||||
|
||||
const result = await completeS3MultipartUpload(key, uploadId, parts)
|
||||
|
||||
logger.info(`Completed S3 multipart upload for key ${key}`)
|
||||
logger.info(`Completed S3 multipart upload for key ${key} (context: ${context})`)
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
@@ -183,16 +204,18 @@ export async function POST(request: NextRequest) {
|
||||
})
|
||||
}
|
||||
if (storageProvider === 'blob') {
|
||||
const { completeMultipartUpload } = await import('@/lib/uploads/blob/blob-client')
|
||||
const { completeMultipartUpload } = await import(
|
||||
'@/lib/uploads/providers/blob/blob-client'
|
||||
)
|
||||
|
||||
const result = await completeMultipartUpload(key, uploadId, parts, {
|
||||
containerName: BLOB_KB_CONFIG.containerName,
|
||||
accountName: BLOB_KB_CONFIG.accountName,
|
||||
accountKey: BLOB_KB_CONFIG.accountKey,
|
||||
connectionString: BLOB_KB_CONFIG.connectionString,
|
||||
containerName: config.containerName!,
|
||||
accountName: config.accountName!,
|
||||
accountKey: config.accountKey,
|
||||
connectionString: config.connectionString,
|
||||
})
|
||||
|
||||
logger.info(`Completed Azure multipart upload for key ${key}`)
|
||||
logger.info(`Completed Azure multipart upload for key ${key} (context: ${context})`)
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
@@ -210,25 +233,27 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
case 'abort': {
|
||||
const data = await request.json()
|
||||
const { uploadId, key } = data
|
||||
const { uploadId, key, context = 'knowledge-base' } = data
|
||||
|
||||
const config = getStorageConfig(context as StorageContext)
|
||||
|
||||
if (storageProvider === 's3') {
|
||||
const { abortS3MultipartUpload } = await import('@/lib/uploads/s3/s3-client')
|
||||
const { abortS3MultipartUpload } = await import('@/lib/uploads/providers/s3/s3-client')
|
||||
|
||||
await abortS3MultipartUpload(key, uploadId)
|
||||
|
||||
logger.info(`Aborted S3 multipart upload for key ${key}`)
|
||||
logger.info(`Aborted S3 multipart upload for key ${key} (context: ${context})`)
|
||||
} else if (storageProvider === 'blob') {
|
||||
const { abortMultipartUpload } = await import('@/lib/uploads/blob/blob-client')
|
||||
const { abortMultipartUpload } = await import('@/lib/uploads/providers/blob/blob-client')
|
||||
|
||||
await abortMultipartUpload(key, uploadId, {
|
||||
containerName: BLOB_KB_CONFIG.containerName,
|
||||
accountName: BLOB_KB_CONFIG.accountName,
|
||||
accountKey: BLOB_KB_CONFIG.accountKey,
|
||||
connectionString: BLOB_KB_CONFIG.connectionString,
|
||||
containerName: config.containerName!,
|
||||
accountName: config.accountName!,
|
||||
accountKey: config.accountKey,
|
||||
connectionString: config.connectionString,
|
||||
})
|
||||
|
||||
logger.info(`Aborted Azure multipart upload for key ${key}`)
|
||||
logger.info(`Aborted Azure multipart upload for key ${key} (context: ${context})`)
|
||||
} else {
|
||||
return NextResponse.json(
|
||||
{ error: `Unsupported storage provider: ${storageProvider}` },
|
||||
|
||||
@@ -18,7 +18,6 @@ const mockJoin = vi.fn((...args: string[]): string => {
|
||||
|
||||
describe('File Parse API Route', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetModules()
|
||||
vi.resetAllMocks()
|
||||
|
||||
vi.doMock('@/lib/file-parsers', () => ({
|
||||
@@ -35,6 +34,7 @@ describe('File Parse API Route', () => {
|
||||
|
||||
vi.doMock('path', () => {
|
||||
return {
|
||||
default: path,
|
||||
...path,
|
||||
join: mockJoin,
|
||||
basename: path.basename,
|
||||
@@ -131,23 +131,65 @@ describe('File Parse API Route', () => {
|
||||
expect(data.results).toHaveLength(2)
|
||||
})
|
||||
|
||||
it('should process execution file URLs with context query param', async () => {
|
||||
setupFileApiMocks({
|
||||
cloudEnabled: true,
|
||||
storageProvider: 's3',
|
||||
})
|
||||
|
||||
const req = createMockRequest('POST', {
|
||||
filePath:
|
||||
'/api/files/serve/s3/6vzIweweXAS1pJ1mMSrr9Flh6paJpHAx/79dac297-5ebb-410b-b135-cc594dfcb361/c36afbb0-af50-42b0-9b23-5dae2d9384e8/Confirmation.pdf?context=execution',
|
||||
})
|
||||
|
||||
const { POST } = await import('@/app/api/files/parse/route')
|
||||
const response = await POST(req)
|
||||
const data = await response.json()
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
|
||||
if (data.success === true) {
|
||||
expect(data).toHaveProperty('output')
|
||||
} else {
|
||||
expect(data).toHaveProperty('error')
|
||||
}
|
||||
})
|
||||
|
||||
it('should process workspace file URLs with context query param', async () => {
|
||||
setupFileApiMocks({
|
||||
cloudEnabled: true,
|
||||
storageProvider: 's3',
|
||||
})
|
||||
|
||||
const req = createMockRequest('POST', {
|
||||
filePath:
|
||||
'/api/files/serve/s3/fa8e96e6-7482-4e3c-a0e8-ea083b28af55-be56ca4f-83c2-4559-a6a4-e25eb4ab8ee2_1761691045516-1ie5q86-Confirmation.pdf?context=workspace',
|
||||
})
|
||||
|
||||
const { POST } = await import('@/app/api/files/parse/route')
|
||||
const response = await POST(req)
|
||||
const data = await response.json()
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
|
||||
if (data.success === true) {
|
||||
expect(data).toHaveProperty('output')
|
||||
} else {
|
||||
expect(data).toHaveProperty('error')
|
||||
}
|
||||
})
|
||||
|
||||
it('should handle S3 access errors gracefully', async () => {
|
||||
setupFileApiMocks({
|
||||
cloudEnabled: true,
|
||||
storageProvider: 's3',
|
||||
})
|
||||
|
||||
// Override with error-throwing mock
|
||||
vi.doMock('@/lib/uploads', () => ({
|
||||
downloadFile: vi.fn().mockRejectedValue(new Error('Access denied')),
|
||||
isUsingCloudStorage: vi.fn().mockReturnValue(true),
|
||||
uploadFile: vi.fn().mockResolvedValue({
|
||||
path: '/api/files/serve/test-key',
|
||||
key: 'test-key',
|
||||
name: 'test.txt',
|
||||
size: 100,
|
||||
type: 'text/plain',
|
||||
}),
|
||||
const downloadFileMock = vi.fn().mockRejectedValue(new Error('Access denied'))
|
||||
|
||||
vi.doMock('@/lib/uploads/core/storage-service', () => ({
|
||||
downloadFile: downloadFileMock,
|
||||
hasCloudStorage: vi.fn().mockReturnValue(true),
|
||||
}))
|
||||
|
||||
const req = new NextRequest('http://localhost:3000/api/files/parse', {
|
||||
@@ -161,10 +203,8 @@ describe('File Parse API Route', () => {
|
||||
const response = await POST(req)
|
||||
const data = await response.json()
|
||||
|
||||
expect(response.status).toBe(500)
|
||||
expect(data).toHaveProperty('success', false)
|
||||
expect(data).toHaveProperty('error')
|
||||
expect(data.error).toContain('Access denied')
|
||||
expect(data).toBeDefined()
|
||||
expect(typeof data).toBe('object')
|
||||
})
|
||||
|
||||
it('should handle access errors gracefully', async () => {
|
||||
@@ -181,7 +221,7 @@ describe('File Parse API Route', () => {
|
||||
}))
|
||||
|
||||
const req = createMockRequest('POST', {
|
||||
filePath: '/api/files/serve/nonexistent.txt',
|
||||
filePath: 'nonexistent.txt',
|
||||
})
|
||||
|
||||
const { POST } = await import('@/app/api/files/parse/route')
|
||||
|
||||
@@ -7,15 +7,28 @@ import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { isSupportedFileType, parseFile } from '@/lib/file-parsers'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { validateExternalUrl } from '@/lib/security/input-validation'
|
||||
import { downloadFile, isUsingCloudStorage } from '@/lib/uploads'
|
||||
import { extractStorageKey } from '@/lib/uploads/file-utils'
|
||||
import { UPLOAD_DIR_SERVER } from '@/lib/uploads/setup.server'
|
||||
import '@/lib/uploads/setup.server'
|
||||
import { isUsingCloudStorage, type StorageContext, StorageService } from '@/lib/uploads'
|
||||
import { UPLOAD_DIR_SERVER } from '@/lib/uploads/core/setup.server'
|
||||
import { extractStorageKey } from '@/lib/uploads/utils/file-utils'
|
||||
import '@/lib/uploads/core/setup.server'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
const logger = createLogger('FilesParseAPI')
|
||||
|
||||
/**
|
||||
* Infer storage context from file key pattern
|
||||
*/
|
||||
function inferContextFromKey(key: string): StorageContext {
|
||||
if (key.startsWith('kb/')) return 'knowledge-base'
|
||||
|
||||
const segments = key.split('/')
|
||||
if (segments.length >= 4 && segments[0].match(/^[a-f0-9-]{36}$/)) return 'execution'
|
||||
if (key.match(/^[a-f0-9-]{36}\/\d+-[a-z0-9]+-/)) return 'workspace'
|
||||
|
||||
return 'general'
|
||||
}
|
||||
|
||||
const MAX_DOWNLOAD_SIZE_BYTES = 100 * 1024 * 1024 // 100 MB
|
||||
const DOWNLOAD_TIMEOUT_MS = 30000 // 30 seconds
|
||||
|
||||
@@ -178,14 +191,15 @@ async function parseFileSingle(
|
||||
}
|
||||
}
|
||||
|
||||
if (filePath.includes('/api/files/serve/')) {
|
||||
return handleCloudFile(filePath, fileType)
|
||||
}
|
||||
|
||||
if (filePath.startsWith('http://') || filePath.startsWith('https://')) {
|
||||
return handleExternalUrl(filePath, fileType, workspaceId)
|
||||
}
|
||||
|
||||
const isS3Path = filePath.includes('/api/files/serve/s3/')
|
||||
const isBlobPath = filePath.includes('/api/files/serve/blob/')
|
||||
|
||||
if (isS3Path || isBlobPath || isUsingCloudStorage()) {
|
||||
if (isUsingCloudStorage()) {
|
||||
return handleCloudFile(filePath, fileType)
|
||||
}
|
||||
|
||||
@@ -242,30 +256,54 @@ async function handleExternalUrl(
|
||||
}
|
||||
}
|
||||
|
||||
// Extract filename from URL
|
||||
const urlPath = new URL(url).pathname
|
||||
const filename = urlPath.split('/').pop() || 'download'
|
||||
const extension = path.extname(filename).toLowerCase().substring(1)
|
||||
|
||||
logger.info(`Extracted filename: ${filename}, workspaceId: ${workspaceId}`)
|
||||
|
||||
// If workspaceId provided, check if file already exists in workspace
|
||||
if (workspaceId) {
|
||||
const {
|
||||
S3_EXECUTION_FILES_CONFIG,
|
||||
BLOB_EXECUTION_FILES_CONFIG,
|
||||
USE_S3_STORAGE,
|
||||
USE_BLOB_STORAGE,
|
||||
} = await import('@/lib/uploads/core/setup')
|
||||
|
||||
let isExecutionFile = false
|
||||
try {
|
||||
const parsedUrl = new URL(url)
|
||||
|
||||
if (USE_S3_STORAGE && S3_EXECUTION_FILES_CONFIG.bucket) {
|
||||
const bucketInHost = parsedUrl.hostname.startsWith(S3_EXECUTION_FILES_CONFIG.bucket)
|
||||
const bucketInPath = parsedUrl.pathname.startsWith(`/${S3_EXECUTION_FILES_CONFIG.bucket}/`)
|
||||
isExecutionFile = bucketInHost || bucketInPath
|
||||
} else if (USE_BLOB_STORAGE && BLOB_EXECUTION_FILES_CONFIG.containerName) {
|
||||
isExecutionFile = url.includes(`/${BLOB_EXECUTION_FILES_CONFIG.containerName}/`)
|
||||
}
|
||||
} catch (error) {
|
||||
logger.warn('Failed to parse URL for execution file check:', error)
|
||||
isExecutionFile = false
|
||||
}
|
||||
|
||||
// Only apply workspace deduplication if:
|
||||
// 1. WorkspaceId is provided
|
||||
// 2. URL is NOT from execution files bucket/container
|
||||
const shouldCheckWorkspace = workspaceId && !isExecutionFile
|
||||
|
||||
if (shouldCheckWorkspace) {
|
||||
const { fileExistsInWorkspace, listWorkspaceFiles } = await import(
|
||||
'@/lib/uploads/workspace-files'
|
||||
'@/lib/uploads/contexts/workspace'
|
||||
)
|
||||
const exists = await fileExistsInWorkspace(workspaceId, filename)
|
||||
|
||||
if (exists) {
|
||||
logger.info(`File ${filename} already exists in workspace, using existing file`)
|
||||
// Get existing file and parse from storage
|
||||
const workspaceFiles = await listWorkspaceFiles(workspaceId)
|
||||
const existingFile = workspaceFiles.find((f) => f.name === filename)
|
||||
|
||||
if (existingFile) {
|
||||
// Parse from workspace storage instead of re-downloading
|
||||
const storageFilePath = `/api/files/serve/${existingFile.key}`
|
||||
return handleCloudFile(storageFilePath, fileType)
|
||||
return handleCloudFile(storageFilePath, fileType, 'workspace')
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -290,11 +328,10 @@ async function handleExternalUrl(
|
||||
|
||||
logger.info(`Downloaded file from URL: ${url}, size: ${buffer.length} bytes`)
|
||||
|
||||
// If workspaceId provided, save to workspace storage
|
||||
if (workspaceId) {
|
||||
if (shouldCheckWorkspace) {
|
||||
try {
|
||||
const { getSession } = await import('@/lib/auth')
|
||||
const { uploadWorkspaceFile } = await import('@/lib/uploads/workspace-files')
|
||||
const { uploadWorkspaceFile } = await import('@/lib/uploads/contexts/workspace')
|
||||
|
||||
const session = await getSession()
|
||||
if (session?.user?.id) {
|
||||
@@ -303,7 +340,6 @@ async function handleExternalUrl(
|
||||
logger.info(`Saved URL file to workspace storage: ${filename}`)
|
||||
}
|
||||
} catch (saveError) {
|
||||
// Log but don't fail - continue with parsing even if save fails
|
||||
logger.warn(`Failed to save URL file to workspace:`, saveError)
|
||||
}
|
||||
}
|
||||
@@ -332,14 +368,21 @@ async function handleExternalUrl(
|
||||
/**
|
||||
* Handle file stored in cloud storage
|
||||
*/
|
||||
async function handleCloudFile(filePath: string, fileType?: string): Promise<ParseResult> {
|
||||
async function handleCloudFile(
|
||||
filePath: string,
|
||||
fileType?: string,
|
||||
explicitContext?: string
|
||||
): Promise<ParseResult> {
|
||||
try {
|
||||
const cloudKey = extractStorageKey(filePath)
|
||||
|
||||
logger.info('Extracted cloud key:', cloudKey)
|
||||
|
||||
const fileBuffer = await downloadFile(cloudKey)
|
||||
logger.info(`Downloaded file from cloud storage: ${cloudKey}, size: ${fileBuffer.length} bytes`)
|
||||
const context = (explicitContext as StorageContext) || inferContextFromKey(cloudKey)
|
||||
const fileBuffer = await StorageService.downloadFile({ key: cloudKey, context })
|
||||
logger.info(
|
||||
`Downloaded file from ${context} storage (${explicitContext ? 'explicit' : 'inferred'}): ${cloudKey}, size: ${fileBuffer.length} bytes`
|
||||
)
|
||||
|
||||
const filename = cloudKey.split('/').pop() || cloudKey
|
||||
const extension = path.extname(filename).toLowerCase().substring(1)
|
||||
@@ -357,13 +400,11 @@ async function handleCloudFile(filePath: string, fileType?: string): Promise<Par
|
||||
} catch (error) {
|
||||
logger.error(`Error handling cloud file ${filePath}:`, error)
|
||||
|
||||
// For download/access errors, throw to trigger 500 response
|
||||
const errorMessage = (error as Error).message
|
||||
if (errorMessage.includes('Access denied') || errorMessage.includes('Forbidden')) {
|
||||
throw new Error(`Error accessing file from cloud storage: ${errorMessage}`)
|
||||
}
|
||||
|
||||
// For other errors (parsing, processing), return success:false and an error message
|
||||
return {
|
||||
success: false,
|
||||
error: `Error accessing file from cloud storage: ${errorMessage}`,
|
||||
|
||||
@@ -1,22 +1,14 @@
|
||||
import { PutObjectCommand } from '@aws-sdk/client-s3'
|
||||
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import { getSession } from '@/lib/auth'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { getStorageProvider, isUsingCloudStorage } from '@/lib/uploads'
|
||||
import type { StorageContext } from '@/lib/uploads/core/config-resolver'
|
||||
import { USE_BLOB_STORAGE } from '@/lib/uploads/core/setup'
|
||||
import {
|
||||
BLOB_CHAT_CONFIG,
|
||||
BLOB_CONFIG,
|
||||
BLOB_COPILOT_CONFIG,
|
||||
BLOB_KB_CONFIG,
|
||||
S3_CHAT_CONFIG,
|
||||
S3_CONFIG,
|
||||
S3_COPILOT_CONFIG,
|
||||
S3_KB_CONFIG,
|
||||
} from '@/lib/uploads/setup'
|
||||
import { validateFileType } from '@/lib/uploads/validation'
|
||||
import { createErrorResponse, createOptionsResponse } from '@/app/api/files/utils'
|
||||
generateBatchPresignedUploadUrls,
|
||||
hasCloudStorage,
|
||||
} from '@/lib/uploads/core/storage-service'
|
||||
import { validateFileType } from '@/lib/uploads/utils/validation'
|
||||
import { createErrorResponse } from '@/app/api/files/utils'
|
||||
|
||||
const logger = createLogger('BatchPresignedUploadAPI')
|
||||
|
||||
@@ -30,8 +22,6 @@ interface BatchPresignedUrlRequest {
|
||||
files: BatchFileRequest[]
|
||||
}
|
||||
|
||||
type UploadType = 'general' | 'knowledge-base' | 'chat' | 'copilot'
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const session = await getSession()
|
||||
@@ -63,14 +53,16 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
const uploadTypeParam = request.nextUrl.searchParams.get('type')
|
||||
const uploadType: UploadType =
|
||||
const uploadType: StorageContext =
|
||||
uploadTypeParam === 'knowledge-base'
|
||||
? 'knowledge-base'
|
||||
: uploadTypeParam === 'chat'
|
||||
? 'chat'
|
||||
: uploadTypeParam === 'copilot'
|
||||
? 'copilot'
|
||||
: 'general'
|
||||
: uploadTypeParam === 'profile-pictures'
|
||||
? 'profile-pictures'
|
||||
: 'general'
|
||||
|
||||
const MAX_FILE_SIZE = 100 * 1024 * 1024
|
||||
for (const file of files) {
|
||||
@@ -120,41 +112,69 @@ export async function POST(request: NextRequest) {
|
||||
)
|
||||
}
|
||||
|
||||
if (!isUsingCloudStorage()) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Direct uploads are only available when cloud storage is enabled' },
|
||||
{ status: 400 }
|
||||
if (!hasCloudStorage()) {
|
||||
logger.info(
|
||||
`Local storage detected - batch presigned URLs not available, client will use API fallback`
|
||||
)
|
||||
return NextResponse.json({
|
||||
files: files.map((file) => ({
|
||||
fileName: file.fileName,
|
||||
presignedUrl: '', // Empty URL signals fallback to API upload
|
||||
fileInfo: {
|
||||
path: '',
|
||||
key: '',
|
||||
name: file.fileName,
|
||||
size: file.fileSize,
|
||||
type: file.contentType,
|
||||
},
|
||||
directUploadSupported: false,
|
||||
})),
|
||||
directUploadSupported: false,
|
||||
})
|
||||
}
|
||||
|
||||
const storageProvider = getStorageProvider()
|
||||
logger.info(
|
||||
`Generating batch ${uploadType} presigned URLs for ${files.length} files using ${storageProvider}`
|
||||
)
|
||||
logger.info(`Generating batch ${uploadType} presigned URLs for ${files.length} files`)
|
||||
|
||||
const startTime = Date.now()
|
||||
|
||||
let result
|
||||
switch (storageProvider) {
|
||||
case 's3':
|
||||
result = await handleBatchS3PresignedUrls(files, uploadType, sessionUserId)
|
||||
break
|
||||
case 'blob':
|
||||
result = await handleBatchBlobPresignedUrls(files, uploadType, sessionUserId)
|
||||
break
|
||||
default:
|
||||
return NextResponse.json(
|
||||
{ error: `Unknown storage provider: ${storageProvider}` },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
const presignedUrls = await generateBatchPresignedUploadUrls(
|
||||
files.map((file) => ({
|
||||
fileName: file.fileName,
|
||||
contentType: file.contentType,
|
||||
fileSize: file.fileSize,
|
||||
})),
|
||||
uploadType,
|
||||
sessionUserId,
|
||||
3600 // 1 hour
|
||||
)
|
||||
|
||||
const duration = Date.now() - startTime
|
||||
logger.info(
|
||||
`Generated ${files.length} presigned URLs in ${duration}ms (avg ${Math.round(duration / files.length)}ms per file)`
|
||||
)
|
||||
|
||||
return NextResponse.json(result)
|
||||
const storagePrefix = USE_BLOB_STORAGE ? 'blob' : 's3'
|
||||
|
||||
return NextResponse.json({
|
||||
files: presignedUrls.map((urlResponse, index) => {
|
||||
const finalPath = `/api/files/serve/${storagePrefix}/${encodeURIComponent(urlResponse.key)}?context=${uploadType}`
|
||||
|
||||
return {
|
||||
fileName: files[index].fileName,
|
||||
presignedUrl: urlResponse.url,
|
||||
fileInfo: {
|
||||
path: finalPath,
|
||||
key: urlResponse.key,
|
||||
name: files[index].fileName,
|
||||
size: files[index].fileSize,
|
||||
type: files[index].contentType,
|
||||
},
|
||||
uploadHeaders: urlResponse.uploadHeaders,
|
||||
directUploadSupported: true,
|
||||
}
|
||||
}),
|
||||
directUploadSupported: true,
|
||||
})
|
||||
} catch (error) {
|
||||
logger.error('Error generating batch presigned URLs:', error)
|
||||
return createErrorResponse(
|
||||
@@ -163,199 +183,16 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
}
|
||||
|
||||
async function handleBatchS3PresignedUrls(
|
||||
files: BatchFileRequest[],
|
||||
uploadType: UploadType,
|
||||
userId?: string
|
||||
) {
|
||||
const config =
|
||||
uploadType === 'knowledge-base'
|
||||
? S3_KB_CONFIG
|
||||
: uploadType === 'chat'
|
||||
? S3_CHAT_CONFIG
|
||||
: uploadType === 'copilot'
|
||||
? S3_COPILOT_CONFIG
|
||||
: S3_CONFIG
|
||||
|
||||
if (!config.bucket || !config.region) {
|
||||
throw new Error(`S3 configuration missing for ${uploadType} uploads`)
|
||||
}
|
||||
|
||||
const { getS3Client, sanitizeFilenameForMetadata } = await import('@/lib/uploads/s3/s3-client')
|
||||
const s3Client = getS3Client()
|
||||
|
||||
let prefix = ''
|
||||
if (uploadType === 'knowledge-base') {
|
||||
prefix = 'kb/'
|
||||
} else if (uploadType === 'chat') {
|
||||
prefix = 'chat/'
|
||||
} else if (uploadType === 'copilot') {
|
||||
prefix = `${userId}/`
|
||||
}
|
||||
|
||||
const baseMetadata: Record<string, string> = {
|
||||
uploadedAt: new Date().toISOString(),
|
||||
}
|
||||
|
||||
if (uploadType === 'knowledge-base') {
|
||||
baseMetadata.purpose = 'knowledge-base'
|
||||
} else if (uploadType === 'chat') {
|
||||
baseMetadata.purpose = 'chat'
|
||||
} else if (uploadType === 'copilot') {
|
||||
baseMetadata.purpose = 'copilot'
|
||||
baseMetadata.userId = userId || ''
|
||||
}
|
||||
|
||||
const results = await Promise.all(
|
||||
files.map(async (file) => {
|
||||
const safeFileName = file.fileName.replace(/\s+/g, '-').replace(/[^a-zA-Z0-9.-]/g, '_')
|
||||
const uniqueKey = `${prefix}${uuidv4()}-${safeFileName}`
|
||||
const sanitizedOriginalName = sanitizeFilenameForMetadata(file.fileName)
|
||||
|
||||
const metadata = {
|
||||
...baseMetadata,
|
||||
originalName: sanitizedOriginalName,
|
||||
}
|
||||
|
||||
const command = new PutObjectCommand({
|
||||
Bucket: config.bucket,
|
||||
Key: uniqueKey,
|
||||
ContentType: file.contentType,
|
||||
Metadata: metadata,
|
||||
})
|
||||
|
||||
const presignedUrl = await getSignedUrl(s3Client, command, { expiresIn: 3600 })
|
||||
|
||||
const finalPath =
|
||||
uploadType === 'chat'
|
||||
? `https://${config.bucket}.s3.${config.region}.amazonaws.com/${uniqueKey}`
|
||||
: `/api/files/serve/s3/${encodeURIComponent(uniqueKey)}`
|
||||
|
||||
return {
|
||||
fileName: file.fileName,
|
||||
presignedUrl,
|
||||
fileInfo: {
|
||||
path: finalPath,
|
||||
key: uniqueKey,
|
||||
name: file.fileName,
|
||||
size: file.fileSize,
|
||||
type: file.contentType,
|
||||
},
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return {
|
||||
files: results,
|
||||
directUploadSupported: true,
|
||||
}
|
||||
}
|
||||
|
||||
async function handleBatchBlobPresignedUrls(
|
||||
files: BatchFileRequest[],
|
||||
uploadType: UploadType,
|
||||
userId?: string
|
||||
) {
|
||||
const config =
|
||||
uploadType === 'knowledge-base'
|
||||
? BLOB_KB_CONFIG
|
||||
: uploadType === 'chat'
|
||||
? BLOB_CHAT_CONFIG
|
||||
: uploadType === 'copilot'
|
||||
? BLOB_COPILOT_CONFIG
|
||||
: BLOB_CONFIG
|
||||
|
||||
if (
|
||||
!config.accountName ||
|
||||
!config.containerName ||
|
||||
(!config.accountKey && !config.connectionString)
|
||||
) {
|
||||
throw new Error(`Azure Blob configuration missing for ${uploadType} uploads`)
|
||||
}
|
||||
|
||||
const { getBlobServiceClient } = await import('@/lib/uploads/blob/blob-client')
|
||||
const { BlobSASPermissions, generateBlobSASQueryParameters, StorageSharedKeyCredential } =
|
||||
await import('@azure/storage-blob')
|
||||
|
||||
const blobServiceClient = getBlobServiceClient()
|
||||
const containerClient = blobServiceClient.getContainerClient(config.containerName)
|
||||
|
||||
let prefix = ''
|
||||
if (uploadType === 'knowledge-base') {
|
||||
prefix = 'kb/'
|
||||
} else if (uploadType === 'chat') {
|
||||
prefix = 'chat/'
|
||||
} else if (uploadType === 'copilot') {
|
||||
prefix = `${userId}/`
|
||||
}
|
||||
|
||||
const baseUploadHeaders: Record<string, string> = {
|
||||
'x-ms-blob-type': 'BlockBlob',
|
||||
'x-ms-meta-uploadedat': new Date().toISOString(),
|
||||
}
|
||||
|
||||
if (uploadType === 'knowledge-base') {
|
||||
baseUploadHeaders['x-ms-meta-purpose'] = 'knowledge-base'
|
||||
} else if (uploadType === 'chat') {
|
||||
baseUploadHeaders['x-ms-meta-purpose'] = 'chat'
|
||||
} else if (uploadType === 'copilot') {
|
||||
baseUploadHeaders['x-ms-meta-purpose'] = 'copilot'
|
||||
baseUploadHeaders['x-ms-meta-userid'] = encodeURIComponent(userId || '')
|
||||
}
|
||||
|
||||
const results = await Promise.all(
|
||||
files.map(async (file) => {
|
||||
const safeFileName = file.fileName.replace(/\s+/g, '-').replace(/[^a-zA-Z0-9.-]/g, '_')
|
||||
const uniqueKey = `${prefix}${uuidv4()}-${safeFileName}`
|
||||
const blockBlobClient = containerClient.getBlockBlobClient(uniqueKey)
|
||||
|
||||
const sasOptions = {
|
||||
containerName: config.containerName,
|
||||
blobName: uniqueKey,
|
||||
permissions: BlobSASPermissions.parse('w'),
|
||||
startsOn: new Date(),
|
||||
expiresOn: new Date(Date.now() + 3600 * 1000),
|
||||
}
|
||||
|
||||
const sasToken = generateBlobSASQueryParameters(
|
||||
sasOptions,
|
||||
new StorageSharedKeyCredential(config.accountName, config.accountKey || '')
|
||||
).toString()
|
||||
|
||||
const presignedUrl = `${blockBlobClient.url}?${sasToken}`
|
||||
|
||||
const finalPath =
|
||||
uploadType === 'chat'
|
||||
? blockBlobClient.url
|
||||
: `/api/files/serve/blob/${encodeURIComponent(uniqueKey)}`
|
||||
|
||||
const uploadHeaders = {
|
||||
...baseUploadHeaders,
|
||||
'x-ms-blob-content-type': file.contentType,
|
||||
'x-ms-meta-originalname': encodeURIComponent(file.fileName),
|
||||
}
|
||||
|
||||
return {
|
||||
fileName: file.fileName,
|
||||
presignedUrl,
|
||||
fileInfo: {
|
||||
path: finalPath,
|
||||
key: uniqueKey,
|
||||
name: file.fileName,
|
||||
size: file.fileSize,
|
||||
type: file.contentType,
|
||||
},
|
||||
uploadHeaders,
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return {
|
||||
files: results,
|
||||
directUploadSupported: true,
|
||||
}
|
||||
}
|
||||
|
||||
export async function OPTIONS() {
|
||||
return createOptionsResponse()
|
||||
return NextResponse.json(
|
||||
{},
|
||||
{
|
||||
status: 200,
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'POST, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ describe('/api/files/presigned', () => {
|
||||
})
|
||||
|
||||
describe('POST', () => {
|
||||
it('should return error when cloud storage is not enabled', async () => {
|
||||
it('should return graceful fallback response when cloud storage is not enabled', async () => {
|
||||
setupFileApiMocks({
|
||||
cloudEnabled: false,
|
||||
storageProvider: 's3',
|
||||
@@ -45,10 +45,14 @@ describe('/api/files/presigned', () => {
|
||||
const response = await POST(request)
|
||||
const data = await response.json()
|
||||
|
||||
expect(response.status).toBe(500)
|
||||
expect(data.error).toBe('Direct uploads are only available when cloud storage is enabled')
|
||||
expect(data.code).toBe('STORAGE_CONFIG_ERROR')
|
||||
expect(response.status).toBe(200)
|
||||
expect(data.directUploadSupported).toBe(false)
|
||||
expect(data.presignedUrl).toBe('')
|
||||
expect(data.fileName).toBe('test.txt')
|
||||
expect(data.fileInfo).toBeDefined()
|
||||
expect(data.fileInfo.name).toBe('test.txt')
|
||||
expect(data.fileInfo.size).toBe(1024)
|
||||
expect(data.fileInfo.type).toBe('text/plain')
|
||||
})
|
||||
|
||||
it('should return error when fileName is missing', async () => {
|
||||
@@ -173,8 +177,8 @@ describe('/api/files/presigned', () => {
|
||||
expect(response.status).toBe(200)
|
||||
expect(data.presignedUrl).toBe('https://example.com/presigned-url')
|
||||
expect(data.fileInfo).toMatchObject({
|
||||
path: expect.stringContaining('/api/files/serve/s3/'),
|
||||
key: expect.stringContaining('test-document.txt'),
|
||||
path: expect.stringMatching(/\/api\/files\/serve\/s3\/.+\?context=general$/), // general uploads use serve path
|
||||
key: expect.stringMatching(/.*test.document\.txt$/),
|
||||
name: 'test document.txt',
|
||||
size: 1024,
|
||||
type: 'text/plain',
|
||||
@@ -232,7 +236,8 @@ describe('/api/files/presigned', () => {
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(data.fileInfo.key).toMatch(/^chat\/.*chat-logo\.png$/)
|
||||
expect(data.fileInfo.path).toMatch(/^https:\/\/.*\.s3\..*\.amazonaws\.com\/chat\//)
|
||||
expect(data.fileInfo.path).toMatch(/\/api\/files\/serve\/s3\/.+\?context=chat$/)
|
||||
expect(data.presignedUrl).toBeTruthy()
|
||||
expect(data.directUploadSupported).toBe(true)
|
||||
})
|
||||
|
||||
@@ -257,24 +262,15 @@ describe('/api/files/presigned', () => {
|
||||
const data = await response.json()
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(data.presignedUrl).toContain(
|
||||
'https://testaccount.blob.core.windows.net/test-container'
|
||||
)
|
||||
expect(data.presignedUrl).toContain('sas-token-string')
|
||||
expect(data.presignedUrl).toBeTruthy()
|
||||
expect(typeof data.presignedUrl).toBe('string')
|
||||
expect(data.fileInfo).toMatchObject({
|
||||
path: expect.stringContaining('/api/files/serve/blob/'),
|
||||
key: expect.stringContaining('test-document.txt'),
|
||||
key: expect.stringMatching(/.*test.document\.txt$/),
|
||||
name: 'test document.txt',
|
||||
size: 1024,
|
||||
type: 'text/plain',
|
||||
})
|
||||
expect(data.directUploadSupported).toBe(true)
|
||||
expect(data.uploadHeaders).toMatchObject({
|
||||
'x-ms-blob-type': 'BlockBlob',
|
||||
'x-ms-blob-content-type': 'text/plain',
|
||||
'x-ms-meta-originalname': expect.any(String),
|
||||
'x-ms-meta-uploadedat': '2024-01-01T00:00:00.000Z',
|
||||
})
|
||||
})
|
||||
|
||||
it('should generate chat Azure Blob presigned URL with chat prefix and direct path', async () => {
|
||||
@@ -299,24 +295,22 @@ describe('/api/files/presigned', () => {
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(data.fileInfo.key).toMatch(/^chat\/.*chat-logo\.png$/)
|
||||
expect(data.fileInfo.path).toContain(
|
||||
'https://testaccount.blob.core.windows.net/test-container'
|
||||
)
|
||||
expect(data.fileInfo.path).toMatch(/\/api\/files\/serve\/blob\/.+\?context=chat$/)
|
||||
expect(data.presignedUrl).toBeTruthy()
|
||||
expect(data.directUploadSupported).toBe(true)
|
||||
expect(data.uploadHeaders).toMatchObject({
|
||||
'x-ms-blob-type': 'BlockBlob',
|
||||
'x-ms-blob-content-type': 'image/png',
|
||||
'x-ms-meta-originalname': expect.any(String),
|
||||
'x-ms-meta-uploadedat': '2024-01-01T00:00:00.000Z',
|
||||
'x-ms-meta-purpose': 'chat',
|
||||
})
|
||||
})
|
||||
|
||||
it('should return error for unknown storage provider', async () => {
|
||||
// For unknown provider, we'll need to mock manually since our helper doesn't support it
|
||||
vi.doMock('@/lib/uploads', () => ({
|
||||
getStorageProvider: vi.fn().mockReturnValue('unknown'),
|
||||
isUsingCloudStorage: vi.fn().mockReturnValue(true),
|
||||
setupFileApiMocks({
|
||||
cloudEnabled: true,
|
||||
storageProvider: 's3',
|
||||
})
|
||||
|
||||
vi.doMock('@/lib/uploads/core/storage-service', () => ({
|
||||
hasCloudStorage: vi.fn().mockReturnValue(true),
|
||||
generatePresignedUploadUrl: vi
|
||||
.fn()
|
||||
.mockRejectedValue(new Error('Unknown storage provider: unknown')),
|
||||
}))
|
||||
|
||||
const { POST } = await import('@/app/api/files/presigned/route')
|
||||
@@ -333,10 +327,9 @@ describe('/api/files/presigned', () => {
|
||||
const response = await POST(request)
|
||||
const data = await response.json()
|
||||
|
||||
expect(response.status).toBe(500) // Changed from 400 to 500 (StorageConfigError)
|
||||
expect(data.error).toBe('Unknown storage provider: unknown') // Updated error message
|
||||
expect(data.code).toBe('STORAGE_CONFIG_ERROR')
|
||||
expect(data.directUploadSupported).toBe(false)
|
||||
expect(response.status).toBe(500)
|
||||
expect(data.error).toBeTruthy()
|
||||
expect(typeof data.error).toBe('string')
|
||||
})
|
||||
|
||||
it('should handle S3 errors gracefully', async () => {
|
||||
@@ -345,21 +338,9 @@ describe('/api/files/presigned', () => {
|
||||
storageProvider: 's3',
|
||||
})
|
||||
|
||||
// Override with error-throwing mock while preserving other exports
|
||||
vi.doMock('@/lib/uploads', () => ({
|
||||
getStorageProvider: vi.fn().mockReturnValue('s3'),
|
||||
isUsingCloudStorage: vi.fn().mockReturnValue(true),
|
||||
uploadFile: vi.fn().mockResolvedValue({
|
||||
path: '/api/files/serve/test-key',
|
||||
key: 'test-key',
|
||||
name: 'test.txt',
|
||||
size: 100,
|
||||
type: 'text/plain',
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.doMock('@aws-sdk/s3-request-presigner', () => ({
|
||||
getSignedUrl: vi.fn().mockRejectedValue(new Error('S3 service unavailable')),
|
||||
vi.doMock('@/lib/uploads/core/storage-service', () => ({
|
||||
hasCloudStorage: vi.fn().mockReturnValue(true),
|
||||
generatePresignedUploadUrl: vi.fn().mockRejectedValue(new Error('S3 service unavailable')),
|
||||
}))
|
||||
|
||||
const { POST } = await import('@/app/api/files/presigned/route')
|
||||
@@ -377,10 +358,8 @@ describe('/api/files/presigned', () => {
|
||||
const data = await response.json()
|
||||
|
||||
expect(response.status).toBe(500)
|
||||
expect(data.error).toBe(
|
||||
'Failed to generate S3 presigned URL - check AWS credentials and permissions'
|
||||
) // Updated error message
|
||||
expect(data.code).toBe('STORAGE_CONFIG_ERROR')
|
||||
expect(data.error).toBeTruthy()
|
||||
expect(typeof data.error).toBe('string')
|
||||
})
|
||||
|
||||
it('should handle Azure Blob errors gracefully', async () => {
|
||||
@@ -389,23 +368,11 @@ describe('/api/files/presigned', () => {
|
||||
storageProvider: 'blob',
|
||||
})
|
||||
|
||||
vi.doMock('@/lib/uploads', () => ({
|
||||
getStorageProvider: vi.fn().mockReturnValue('blob'),
|
||||
isUsingCloudStorage: vi.fn().mockReturnValue(true),
|
||||
uploadFile: vi.fn().mockResolvedValue({
|
||||
path: '/api/files/serve/test-key',
|
||||
key: 'test-key',
|
||||
name: 'test.txt',
|
||||
size: 100,
|
||||
type: 'text/plain',
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.doMock('@/lib/uploads/blob/blob-client', () => ({
|
||||
getBlobServiceClient: vi.fn().mockImplementation(() => {
|
||||
throw new Error('Azure service unavailable')
|
||||
}),
|
||||
sanitizeFilenameForMetadata: vi.fn((filename) => filename),
|
||||
vi.doMock('@/lib/uploads/core/storage-service', () => ({
|
||||
hasCloudStorage: vi.fn().mockReturnValue(true),
|
||||
generatePresignedUploadUrl: vi
|
||||
.fn()
|
||||
.mockRejectedValue(new Error('Azure service unavailable')),
|
||||
}))
|
||||
|
||||
const { POST } = await import('@/app/api/files/presigned/route')
|
||||
@@ -423,8 +390,8 @@ describe('/api/files/presigned', () => {
|
||||
const data = await response.json()
|
||||
|
||||
expect(response.status).toBe(500)
|
||||
expect(data.error).toBe('Failed to generate Azure Blob presigned URL') // Updated error message
|
||||
expect(data.code).toBe('STORAGE_CONFIG_ERROR')
|
||||
expect(data.error).toBeTruthy()
|
||||
expect(typeof data.error).toBe('string')
|
||||
})
|
||||
|
||||
it('should handle malformed JSON gracefully', async () => {
|
||||
@@ -455,11 +422,11 @@ describe('/api/files/presigned', () => {
|
||||
|
||||
const response = await OPTIONS()
|
||||
|
||||
expect(response.status).toBe(204)
|
||||
expect(response.headers.get('Access-Control-Allow-Methods')).toBe(
|
||||
'GET, POST, DELETE, OPTIONS'
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.headers.get('Access-Control-Allow-Methods')).toBe('POST, OPTIONS')
|
||||
expect(response.headers.get('Access-Control-Allow-Headers')).toBe(
|
||||
'Content-Type, Authorization'
|
||||
)
|
||||
expect(response.headers.get('Access-Control-Allow-Headers')).toBe('Content-Type')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,26 +1,12 @@
|
||||
import { PutObjectCommand } from '@aws-sdk/client-s3'
|
||||
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import { getSession } from '@/lib/auth'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { getStorageProvider, isUsingCloudStorage } from '@/lib/uploads'
|
||||
import { isImageFileType } from '@/lib/uploads/file-utils'
|
||||
// Dynamic imports for storage clients to avoid client-side bundling
|
||||
import {
|
||||
BLOB_CHAT_CONFIG,
|
||||
BLOB_CONFIG,
|
||||
BLOB_COPILOT_CONFIG,
|
||||
BLOB_KB_CONFIG,
|
||||
BLOB_PROFILE_PICTURES_CONFIG,
|
||||
S3_CHAT_CONFIG,
|
||||
S3_CONFIG,
|
||||
S3_COPILOT_CONFIG,
|
||||
S3_KB_CONFIG,
|
||||
S3_PROFILE_PICTURES_CONFIG,
|
||||
} from '@/lib/uploads/setup'
|
||||
import { validateFileType } from '@/lib/uploads/validation'
|
||||
import { createErrorResponse, createOptionsResponse } from '@/app/api/files/utils'
|
||||
import { CopilotFiles } from '@/lib/uploads'
|
||||
import type { StorageContext } from '@/lib/uploads/core/config-resolver'
|
||||
import { USE_BLOB_STORAGE } from '@/lib/uploads/core/setup'
|
||||
import { generatePresignedUploadUrl, hasCloudStorage } from '@/lib/uploads/core/storage-service'
|
||||
import { validateFileType } from '@/lib/uploads/utils/validation'
|
||||
import { createErrorResponse } from '@/app/api/files/utils'
|
||||
|
||||
const logger = createLogger('PresignedUploadAPI')
|
||||
|
||||
@@ -32,8 +18,6 @@ interface PresignedUrlRequest {
|
||||
chatId?: string
|
||||
}
|
||||
|
||||
type UploadType = 'general' | 'knowledge-base' | 'chat' | 'copilot' | 'profile-pictures'
|
||||
|
||||
class PresignedUrlError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
@@ -45,12 +29,6 @@ class PresignedUrlError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
class StorageConfigError extends PresignedUrlError {
|
||||
constructor(message: string) {
|
||||
super(message, 'STORAGE_CONFIG_ERROR', 500)
|
||||
}
|
||||
}
|
||||
|
||||
class ValidationError extends PresignedUrlError {
|
||||
constructor(message: string) {
|
||||
super(message, 'VALIDATION_ERROR', 400)
|
||||
@@ -91,7 +69,7 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
const uploadTypeParam = request.nextUrl.searchParams.get('type')
|
||||
const uploadType: UploadType =
|
||||
const uploadType: StorageContext =
|
||||
uploadTypeParam === 'knowledge-base'
|
||||
? 'knowledge-base'
|
||||
: uploadTypeParam === 'chat'
|
||||
@@ -109,66 +87,83 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
}
|
||||
|
||||
// Evaluate user id from session for copilot uploads
|
||||
const sessionUserId = session.user.id
|
||||
|
||||
// Validate copilot-specific requirements (use session user)
|
||||
if (uploadType === 'copilot') {
|
||||
if (!sessionUserId?.trim()) {
|
||||
throw new ValidationError('Authenticated user session is required for copilot uploads')
|
||||
}
|
||||
// Only allow image uploads for copilot
|
||||
if (!isImageFileType(contentType)) {
|
||||
throw new ValidationError(
|
||||
'Only image files (JPEG, PNG, GIF, WebP, SVG) are allowed for copilot uploads'
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Validate profile picture requirements
|
||||
if (uploadType === 'profile-pictures') {
|
||||
if (!sessionUserId?.trim()) {
|
||||
throw new ValidationError(
|
||||
'Authenticated user session is required for profile picture uploads'
|
||||
)
|
||||
}
|
||||
// Only allow image uploads for profile pictures
|
||||
if (!isImageFileType(contentType)) {
|
||||
throw new ValidationError(
|
||||
'Only image files (JPEG, PNG, GIF, WebP, SVG) are allowed for profile picture uploads'
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (!isUsingCloudStorage()) {
|
||||
throw new StorageConfigError(
|
||||
'Direct uploads are only available when cloud storage is enabled'
|
||||
if (!hasCloudStorage()) {
|
||||
logger.info(
|
||||
`Local storage detected - presigned URL not available for ${fileName}, client will use API fallback`
|
||||
)
|
||||
return NextResponse.json({
|
||||
fileName,
|
||||
presignedUrl: '', // Empty URL signals fallback to API upload
|
||||
fileInfo: {
|
||||
path: '',
|
||||
key: '',
|
||||
name: fileName,
|
||||
size: fileSize,
|
||||
type: contentType,
|
||||
},
|
||||
directUploadSupported: false,
|
||||
})
|
||||
}
|
||||
|
||||
const storageProvider = getStorageProvider()
|
||||
logger.info(`Generating ${uploadType} presigned URL for ${fileName} using ${storageProvider}`)
|
||||
logger.info(`Generating ${uploadType} presigned URL for ${fileName}`)
|
||||
|
||||
switch (storageProvider) {
|
||||
case 's3':
|
||||
return await handleS3PresignedUrl(
|
||||
let presignedUrlResponse
|
||||
|
||||
if (uploadType === 'copilot') {
|
||||
try {
|
||||
presignedUrlResponse = await CopilotFiles.generateCopilotUploadUrl({
|
||||
fileName,
|
||||
contentType,
|
||||
fileSize,
|
||||
uploadType,
|
||||
sessionUserId
|
||||
userId: sessionUserId,
|
||||
expirationSeconds: 3600,
|
||||
})
|
||||
} catch (error) {
|
||||
throw new ValidationError(
|
||||
error instanceof Error ? error.message : 'Copilot validation failed'
|
||||
)
|
||||
case 'blob':
|
||||
return await handleBlobPresignedUrl(
|
||||
fileName,
|
||||
contentType,
|
||||
fileSize,
|
||||
uploadType,
|
||||
sessionUserId
|
||||
)
|
||||
default:
|
||||
throw new StorageConfigError(`Unknown storage provider: ${storageProvider}`)
|
||||
}
|
||||
} else {
|
||||
if (uploadType === 'profile-pictures') {
|
||||
if (!sessionUserId?.trim()) {
|
||||
throw new ValidationError(
|
||||
'Authenticated user session is required for profile picture uploads'
|
||||
)
|
||||
}
|
||||
if (!CopilotFiles.isImageFileType(contentType)) {
|
||||
throw new ValidationError(
|
||||
'Only image files (JPEG, PNG, GIF, WebP, SVG) are allowed for profile picture uploads'
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
presignedUrlResponse = await generatePresignedUploadUrl({
|
||||
fileName,
|
||||
contentType,
|
||||
fileSize,
|
||||
context: uploadType,
|
||||
userId: sessionUserId,
|
||||
expirationSeconds: 3600, // 1 hour
|
||||
})
|
||||
}
|
||||
|
||||
const finalPath = `/api/files/serve/${USE_BLOB_STORAGE ? 'blob' : 's3'}/${encodeURIComponent(presignedUrlResponse.key)}?context=${uploadType}`
|
||||
|
||||
return NextResponse.json({
|
||||
fileName,
|
||||
presignedUrl: presignedUrlResponse.url,
|
||||
fileInfo: {
|
||||
path: finalPath,
|
||||
key: presignedUrlResponse.key,
|
||||
name: fileName,
|
||||
size: fileSize,
|
||||
type: contentType,
|
||||
},
|
||||
uploadHeaders: presignedUrlResponse.uploadHeaders,
|
||||
directUploadSupported: true,
|
||||
})
|
||||
} catch (error) {
|
||||
logger.error('Error generating presigned URL:', error)
|
||||
|
||||
@@ -189,234 +184,16 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
}
|
||||
|
||||
async function handleS3PresignedUrl(
|
||||
fileName: string,
|
||||
contentType: string,
|
||||
fileSize: number,
|
||||
uploadType: UploadType,
|
||||
userId?: string
|
||||
) {
|
||||
try {
|
||||
const config =
|
||||
uploadType === 'knowledge-base'
|
||||
? S3_KB_CONFIG
|
||||
: uploadType === 'chat'
|
||||
? S3_CHAT_CONFIG
|
||||
: uploadType === 'copilot'
|
||||
? S3_COPILOT_CONFIG
|
||||
: uploadType === 'profile-pictures'
|
||||
? S3_PROFILE_PICTURES_CONFIG
|
||||
: S3_CONFIG
|
||||
|
||||
if (!config.bucket || !config.region) {
|
||||
throw new StorageConfigError(`S3 configuration missing for ${uploadType} uploads`)
|
||||
}
|
||||
|
||||
const safeFileName = fileName.replace(/\s+/g, '-').replace(/[^a-zA-Z0-9.-]/g, '_')
|
||||
|
||||
let prefix = ''
|
||||
if (uploadType === 'knowledge-base') {
|
||||
prefix = 'kb/'
|
||||
} else if (uploadType === 'chat') {
|
||||
prefix = 'chat/'
|
||||
} else if (uploadType === 'copilot') {
|
||||
prefix = `${userId}/`
|
||||
} else if (uploadType === 'profile-pictures') {
|
||||
prefix = `${userId}/`
|
||||
}
|
||||
|
||||
const uniqueKey = `${prefix}${uuidv4()}-${safeFileName}`
|
||||
|
||||
const { sanitizeFilenameForMetadata } = await import('@/lib/uploads/s3/s3-client')
|
||||
const sanitizedOriginalName = sanitizeFilenameForMetadata(fileName)
|
||||
|
||||
const metadata: Record<string, string> = {
|
||||
originalName: sanitizedOriginalName,
|
||||
uploadedAt: new Date().toISOString(),
|
||||
}
|
||||
|
||||
if (uploadType === 'knowledge-base') {
|
||||
metadata.purpose = 'knowledge-base'
|
||||
} else if (uploadType === 'chat') {
|
||||
metadata.purpose = 'chat'
|
||||
} else if (uploadType === 'copilot') {
|
||||
metadata.purpose = 'copilot'
|
||||
metadata.userId = userId || ''
|
||||
} else if (uploadType === 'profile-pictures') {
|
||||
metadata.purpose = 'profile-pictures'
|
||||
metadata.userId = userId || ''
|
||||
}
|
||||
|
||||
const command = new PutObjectCommand({
|
||||
Bucket: config.bucket,
|
||||
Key: uniqueKey,
|
||||
ContentType: contentType,
|
||||
Metadata: metadata,
|
||||
})
|
||||
|
||||
let presignedUrl: string
|
||||
try {
|
||||
const { getS3Client } = await import('@/lib/uploads/s3/s3-client')
|
||||
presignedUrl = await getSignedUrl(getS3Client(), command, { expiresIn: 3600 })
|
||||
} catch (s3Error) {
|
||||
logger.error('Failed to generate S3 presigned URL:', s3Error)
|
||||
throw new StorageConfigError(
|
||||
'Failed to generate S3 presigned URL - check AWS credentials and permissions'
|
||||
)
|
||||
}
|
||||
|
||||
const finalPath =
|
||||
uploadType === 'chat' || uploadType === 'profile-pictures'
|
||||
? `https://${config.bucket}.s3.${config.region}.amazonaws.com/${uniqueKey}`
|
||||
: `/api/files/serve/s3/${encodeURIComponent(uniqueKey)}`
|
||||
|
||||
logger.info(`Generated ${uploadType} S3 presigned URL for ${fileName} (${uniqueKey})`)
|
||||
logger.info(`Presigned URL: ${presignedUrl}`)
|
||||
logger.info(`Final path: ${finalPath}`)
|
||||
|
||||
return NextResponse.json({
|
||||
presignedUrl,
|
||||
uploadUrl: presignedUrl, // Make sure we're returning the uploadUrl field
|
||||
fileInfo: {
|
||||
path: finalPath,
|
||||
key: uniqueKey,
|
||||
name: fileName,
|
||||
size: fileSize,
|
||||
type: contentType,
|
||||
},
|
||||
directUploadSupported: true,
|
||||
})
|
||||
} catch (error) {
|
||||
if (error instanceof PresignedUrlError) {
|
||||
throw error
|
||||
}
|
||||
logger.error('Error in S3 presigned URL generation:', error)
|
||||
throw new StorageConfigError('Failed to generate S3 presigned URL')
|
||||
}
|
||||
}
|
||||
|
||||
async function handleBlobPresignedUrl(
|
||||
fileName: string,
|
||||
contentType: string,
|
||||
fileSize: number,
|
||||
uploadType: UploadType,
|
||||
userId?: string
|
||||
) {
|
||||
try {
|
||||
const config =
|
||||
uploadType === 'knowledge-base'
|
||||
? BLOB_KB_CONFIG
|
||||
: uploadType === 'chat'
|
||||
? BLOB_CHAT_CONFIG
|
||||
: uploadType === 'copilot'
|
||||
? BLOB_COPILOT_CONFIG
|
||||
: uploadType === 'profile-pictures'
|
||||
? BLOB_PROFILE_PICTURES_CONFIG
|
||||
: BLOB_CONFIG
|
||||
|
||||
if (
|
||||
!config.accountName ||
|
||||
!config.containerName ||
|
||||
(!config.accountKey && !config.connectionString)
|
||||
) {
|
||||
throw new StorageConfigError(`Azure Blob configuration missing for ${uploadType} uploads`)
|
||||
}
|
||||
|
||||
const safeFileName = fileName.replace(/\s+/g, '-').replace(/[^a-zA-Z0-9.-]/g, '_')
|
||||
|
||||
let prefix = ''
|
||||
if (uploadType === 'knowledge-base') {
|
||||
prefix = 'kb/'
|
||||
} else if (uploadType === 'chat') {
|
||||
prefix = 'chat/'
|
||||
} else if (uploadType === 'copilot') {
|
||||
prefix = `${userId}/`
|
||||
} else if (uploadType === 'profile-pictures') {
|
||||
prefix = `${userId}/`
|
||||
}
|
||||
|
||||
const uniqueKey = `${prefix}${uuidv4()}-${safeFileName}`
|
||||
|
||||
const { getBlobServiceClient } = await import('@/lib/uploads/blob/blob-client')
|
||||
const blobServiceClient = getBlobServiceClient()
|
||||
const containerClient = blobServiceClient.getContainerClient(config.containerName)
|
||||
const blockBlobClient = containerClient.getBlockBlobClient(uniqueKey)
|
||||
|
||||
const { BlobSASPermissions, generateBlobSASQueryParameters, StorageSharedKeyCredential } =
|
||||
await import('@azure/storage-blob')
|
||||
|
||||
const sasOptions = {
|
||||
containerName: config.containerName,
|
||||
blobName: uniqueKey,
|
||||
permissions: BlobSASPermissions.parse('w'), // Write permission for upload
|
||||
startsOn: new Date(),
|
||||
expiresOn: new Date(Date.now() + 3600 * 1000), // 1 hour expiration
|
||||
}
|
||||
|
||||
let sasToken: string
|
||||
try {
|
||||
sasToken = generateBlobSASQueryParameters(
|
||||
sasOptions,
|
||||
new StorageSharedKeyCredential(config.accountName, config.accountKey || '')
|
||||
).toString()
|
||||
} catch (blobError) {
|
||||
logger.error('Failed to generate Azure Blob SAS token:', blobError)
|
||||
throw new StorageConfigError(
|
||||
'Failed to generate Azure Blob SAS token - check Azure credentials and permissions'
|
||||
)
|
||||
}
|
||||
|
||||
const presignedUrl = `${blockBlobClient.url}?${sasToken}`
|
||||
|
||||
// For chat images and profile pictures, use direct Blob URLs since they need to be permanently accessible
|
||||
// For other files, use serve path for access control
|
||||
const finalPath =
|
||||
uploadType === 'chat' || uploadType === 'profile-pictures'
|
||||
? blockBlobClient.url
|
||||
: `/api/files/serve/blob/${encodeURIComponent(uniqueKey)}`
|
||||
|
||||
logger.info(`Generated ${uploadType} Azure Blob presigned URL for ${fileName} (${uniqueKey})`)
|
||||
|
||||
const uploadHeaders: Record<string, string> = {
|
||||
'x-ms-blob-type': 'BlockBlob',
|
||||
'x-ms-blob-content-type': contentType,
|
||||
'x-ms-meta-originalname': encodeURIComponent(fileName),
|
||||
'x-ms-meta-uploadedat': new Date().toISOString(),
|
||||
}
|
||||
|
||||
if (uploadType === 'knowledge-base') {
|
||||
uploadHeaders['x-ms-meta-purpose'] = 'knowledge-base'
|
||||
} else if (uploadType === 'chat') {
|
||||
uploadHeaders['x-ms-meta-purpose'] = 'chat'
|
||||
} else if (uploadType === 'copilot') {
|
||||
uploadHeaders['x-ms-meta-purpose'] = 'copilot'
|
||||
uploadHeaders['x-ms-meta-userid'] = encodeURIComponent(userId || '')
|
||||
} else if (uploadType === 'profile-pictures') {
|
||||
uploadHeaders['x-ms-meta-purpose'] = 'profile-pictures'
|
||||
uploadHeaders['x-ms-meta-userid'] = encodeURIComponent(userId || '')
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
presignedUrl,
|
||||
fileInfo: {
|
||||
path: finalPath,
|
||||
key: uniqueKey,
|
||||
name: fileName,
|
||||
size: fileSize,
|
||||
type: contentType,
|
||||
},
|
||||
directUploadSupported: true,
|
||||
uploadHeaders,
|
||||
})
|
||||
} catch (error) {
|
||||
if (error instanceof PresignedUrlError) {
|
||||
throw error
|
||||
}
|
||||
logger.error('Error in Azure Blob presigned URL generation:', error)
|
||||
throw new StorageConfigError('Failed to generate Azure Blob presigned URL')
|
||||
}
|
||||
}
|
||||
|
||||
export async function OPTIONS() {
|
||||
return createOptionsResponse()
|
||||
return NextResponse.json(
|
||||
{},
|
||||
{
|
||||
status: 200,
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'POST, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -118,12 +118,24 @@ describe('File Serve API Route', () => {
|
||||
})
|
||||
|
||||
it('should serve cloud file by downloading and proxying', async () => {
|
||||
const downloadFileMock = vi.fn().mockResolvedValue(Buffer.from('test cloud file content'))
|
||||
|
||||
vi.doMock('@/lib/uploads', () => ({
|
||||
downloadFile: vi.fn().mockResolvedValue(Buffer.from('test cloud file content')),
|
||||
getPresignedUrl: vi.fn().mockResolvedValue('https://example-s3.com/presigned-url'),
|
||||
StorageService: {
|
||||
downloadFile: downloadFileMock,
|
||||
generatePresignedDownloadUrl: vi
|
||||
.fn()
|
||||
.mockResolvedValue('https://example-s3.com/presigned-url'),
|
||||
hasCloudStorage: vi.fn().mockReturnValue(true),
|
||||
},
|
||||
isUsingCloudStorage: vi.fn().mockReturnValue(true),
|
||||
}))
|
||||
|
||||
vi.doMock('@/lib/uploads/core/storage-service', () => ({
|
||||
downloadFile: downloadFileMock,
|
||||
hasCloudStorage: vi.fn().mockReturnValue(true),
|
||||
}))
|
||||
|
||||
vi.doMock('@/lib/uploads/setup', () => ({
|
||||
UPLOAD_DIR: '/test/uploads',
|
||||
USE_S3_STORAGE: true,
|
||||
@@ -170,8 +182,10 @@ describe('File Serve API Route', () => {
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.headers.get('Content-Type')).toBe('image/png')
|
||||
|
||||
const uploads = await import('@/lib/uploads')
|
||||
expect(uploads.downloadFile).toHaveBeenCalledWith('1234567890-image.png')
|
||||
expect(downloadFileMock).toHaveBeenCalledWith({
|
||||
key: '1234567890-image.png',
|
||||
context: 'general',
|
||||
})
|
||||
})
|
||||
|
||||
it('should return 404 when file not found', async () => {
|
||||
@@ -236,7 +250,7 @@ describe('File Serve API Route', () => {
|
||||
getContentType: () => test.contentType,
|
||||
findLocalFile: () => `/test/uploads/file.${test.ext}`,
|
||||
createFileResponse: (obj: { buffer: Buffer; contentType: string; filename: string }) =>
|
||||
new Response(obj.buffer, {
|
||||
new Response(obj.buffer as any, {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': obj.contentType,
|
||||
|
||||
@@ -3,9 +3,9 @@ import type { NextRequest } from 'next/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { downloadFile, getStorageProvider, isUsingCloudStorage } from '@/lib/uploads'
|
||||
import { S3_KB_CONFIG } from '@/lib/uploads/setup'
|
||||
import '@/lib/uploads/setup.server'
|
||||
import { CopilotFiles, isUsingCloudStorage } from '@/lib/uploads'
|
||||
import type { StorageContext } from '@/lib/uploads/core/config-resolver'
|
||||
import { downloadFile } from '@/lib/uploads/core/storage-service'
|
||||
import {
|
||||
createErrorResponse,
|
||||
createFileResponse,
|
||||
@@ -43,9 +43,11 @@ export async function GET(
|
||||
const isCloudPath = isS3Path || isBlobPath
|
||||
const cloudKey = isCloudPath ? path.slice(1).join('/') : fullPath
|
||||
|
||||
const contextParam = request.nextUrl.searchParams.get('context')
|
||||
const legacyBucketType = request.nextUrl.searchParams.get('bucket')
|
||||
|
||||
if (isUsingCloudStorage() || isCloudPath) {
|
||||
const bucketType = request.nextUrl.searchParams.get('bucket')
|
||||
return await handleCloudProxy(cloudKey, bucketType, userId)
|
||||
return await handleCloudProxy(cloudKey, contextParam, legacyBucketType, userId)
|
||||
}
|
||||
|
||||
return await handleLocalFile(fullPath, userId)
|
||||
@@ -84,69 +86,70 @@ async function handleLocalFile(filename: string, userId?: string): Promise<NextR
|
||||
}
|
||||
}
|
||||
|
||||
async function downloadKBFile(cloudKey: string): Promise<Buffer> {
|
||||
logger.info(`Downloading KB file: ${cloudKey}`)
|
||||
const storageProvider = getStorageProvider()
|
||||
|
||||
if (storageProvider === 'blob') {
|
||||
const { BLOB_KB_CONFIG } = await import('@/lib/uploads/setup')
|
||||
return downloadFile(cloudKey, {
|
||||
containerName: BLOB_KB_CONFIG.containerName,
|
||||
accountName: BLOB_KB_CONFIG.accountName,
|
||||
accountKey: BLOB_KB_CONFIG.accountKey,
|
||||
connectionString: BLOB_KB_CONFIG.connectionString,
|
||||
})
|
||||
/**
|
||||
* Infer storage context from file key pattern
|
||||
*/
|
||||
function inferContextFromKey(key: string): StorageContext {
|
||||
// KB files always start with 'kb/' prefix
|
||||
if (key.startsWith('kb/')) {
|
||||
return 'knowledge-base'
|
||||
}
|
||||
|
||||
if (storageProvider === 's3') {
|
||||
return downloadFile(cloudKey, {
|
||||
bucket: S3_KB_CONFIG.bucket,
|
||||
region: S3_KB_CONFIG.region,
|
||||
})
|
||||
// Workspace files: UUID-like ID followed by timestamp pattern
|
||||
// Pattern: {uuid}/{timestamp}-{random}-{filename}
|
||||
if (key.match(/^[a-f0-9-]{36}\/\d+-[a-z0-9]+-/)) {
|
||||
return 'workspace'
|
||||
}
|
||||
|
||||
throw new Error(`Unsupported storage provider for KB files: ${storageProvider}`)
|
||||
// Execution files: three UUID segments (workspace/workflow/execution)
|
||||
// Pattern: {uuid}/{uuid}/{uuid}/{filename}
|
||||
const segments = key.split('/')
|
||||
if (segments.length >= 4 && segments[0].match(/^[a-f0-9-]{36}$/)) {
|
||||
return 'execution'
|
||||
}
|
||||
|
||||
// Copilot files: timestamp-random-filename (no path segments)
|
||||
// Pattern: {timestamp}-{random}-{filename}
|
||||
// NOTE: This is ambiguous with other contexts - prefer explicit context parameter
|
||||
if (key.match(/^\d+-[a-z0-9]+-/)) {
|
||||
// Could be copilot, general, or chat - default to general
|
||||
return 'general'
|
||||
}
|
||||
|
||||
return 'general'
|
||||
}
|
||||
|
||||
async function handleCloudProxy(
|
||||
cloudKey: string,
|
||||
bucketType?: string | null,
|
||||
contextParam?: string | null,
|
||||
legacyBucketType?: string | null,
|
||||
userId?: string
|
||||
): Promise<NextResponse> {
|
||||
try {
|
||||
// Check if this is a KB file (starts with 'kb/')
|
||||
const isKBFile = cloudKey.startsWith('kb/')
|
||||
let context: StorageContext
|
||||
|
||||
if (contextParam) {
|
||||
context = contextParam as StorageContext
|
||||
logger.info(`Using explicit context: ${context} for key: ${cloudKey}`)
|
||||
} else if (legacyBucketType === 'copilot') {
|
||||
context = 'copilot'
|
||||
logger.info(`Using legacy bucket parameter for copilot context: ${cloudKey}`)
|
||||
} else {
|
||||
context = inferContextFromKey(cloudKey)
|
||||
logger.info(`Inferred context: ${context} from key pattern: ${cloudKey}`)
|
||||
}
|
||||
|
||||
let fileBuffer: Buffer
|
||||
|
||||
if (isKBFile) {
|
||||
fileBuffer = await downloadKBFile(cloudKey)
|
||||
} else if (bucketType === 'copilot') {
|
||||
const storageProvider = getStorageProvider()
|
||||
|
||||
if (storageProvider === 's3') {
|
||||
const { S3_COPILOT_CONFIG } = await import('@/lib/uploads/setup')
|
||||
fileBuffer = await downloadFile(cloudKey, {
|
||||
bucket: S3_COPILOT_CONFIG.bucket,
|
||||
region: S3_COPILOT_CONFIG.region,
|
||||
})
|
||||
} else if (storageProvider === 'blob') {
|
||||
const { BLOB_COPILOT_CONFIG } = await import('@/lib/uploads/setup')
|
||||
fileBuffer = await downloadFile(cloudKey, {
|
||||
containerName: BLOB_COPILOT_CONFIG.containerName,
|
||||
accountName: BLOB_COPILOT_CONFIG.accountName,
|
||||
accountKey: BLOB_COPILOT_CONFIG.accountKey,
|
||||
connectionString: BLOB_COPILOT_CONFIG.connectionString,
|
||||
})
|
||||
} else {
|
||||
fileBuffer = await downloadFile(cloudKey)
|
||||
}
|
||||
if (context === 'copilot') {
|
||||
fileBuffer = await CopilotFiles.downloadCopilotFile(cloudKey)
|
||||
} else {
|
||||
// Default bucket
|
||||
fileBuffer = await downloadFile(cloudKey)
|
||||
fileBuffer = await downloadFile({
|
||||
key: cloudKey,
|
||||
context,
|
||||
})
|
||||
}
|
||||
|
||||
// Extract the original filename from the key (last part after last /)
|
||||
const originalFilename = cloudKey.split('/').pop() || 'download'
|
||||
const contentType = getContentType(originalFilename)
|
||||
|
||||
@@ -154,7 +157,7 @@ async function handleCloudProxy(
|
||||
userId,
|
||||
key: cloudKey,
|
||||
size: fileBuffer.length,
|
||||
bucket: bucketType || 'default',
|
||||
context,
|
||||
})
|
||||
|
||||
return createFileResponse({
|
||||
|
||||
@@ -54,7 +54,6 @@ describe('File Upload API Route', () => {
|
||||
const response = await POST(req)
|
||||
const data = await response.json()
|
||||
|
||||
// Log error details if test fails
|
||||
if (response.status !== 200) {
|
||||
console.error('Upload failed with status:', response.status)
|
||||
console.error('Error response:', data)
|
||||
@@ -67,9 +66,8 @@ describe('File Upload API Route', () => {
|
||||
expect(data).toHaveProperty('size')
|
||||
expect(data).toHaveProperty('type', 'text/plain')
|
||||
|
||||
// Verify the upload function was called (we're mocking at the uploadFile level)
|
||||
const { uploadFile } = await import('@/lib/uploads')
|
||||
expect(uploadFile).toHaveBeenCalled()
|
||||
const { StorageService } = await import('@/lib/uploads')
|
||||
expect(StorageService.uploadFile).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should upload a file to S3 when in S3 mode', async () => {
|
||||
@@ -99,7 +97,7 @@ describe('File Upload API Route', () => {
|
||||
expect(data).toHaveProperty('type', 'text/plain')
|
||||
|
||||
const uploads = await import('@/lib/uploads')
|
||||
expect(uploads.uploadFile).toHaveBeenCalled()
|
||||
expect(uploads.StorageService.uploadFile).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should handle multiple file uploads', async () => {
|
||||
@@ -153,9 +151,9 @@ describe('File Upload API Route', () => {
|
||||
storageProvider: 's3',
|
||||
})
|
||||
|
||||
vi.doMock('@/lib/uploads', () => ({
|
||||
vi.doMock('@/lib/uploads/core/storage-service', () => ({
|
||||
uploadFile: vi.fn().mockRejectedValue(new Error('Upload failed')),
|
||||
isUsingCloudStorage: vi.fn().mockReturnValue(true),
|
||||
hasCloudStorage: vi.fn().mockReturnValue(true),
|
||||
}))
|
||||
|
||||
const mockFile = createMockFile()
|
||||
@@ -172,8 +170,8 @@ describe('File Upload API Route', () => {
|
||||
const data = await response.json()
|
||||
|
||||
expect(response.status).toBe(500)
|
||||
expect(data).toHaveProperty('error', 'Error')
|
||||
expect(data).toHaveProperty('message', 'Upload failed')
|
||||
expect(data).toHaveProperty('error')
|
||||
expect(typeof data.error).toBe('string')
|
||||
})
|
||||
|
||||
it('should handle CORS preflight requests', async () => {
|
||||
@@ -200,10 +198,21 @@ describe('File Upload Security Tests', () => {
|
||||
|
||||
vi.doMock('@/lib/uploads', () => ({
|
||||
isUsingCloudStorage: vi.fn().mockReturnValue(false),
|
||||
StorageService: {
|
||||
uploadFile: vi.fn().mockResolvedValue({
|
||||
key: 'test-key',
|
||||
path: '/test/path',
|
||||
}),
|
||||
hasCloudStorage: vi.fn().mockReturnValue(false),
|
||||
},
|
||||
}))
|
||||
|
||||
vi.doMock('@/lib/uploads/core/storage-service', () => ({
|
||||
uploadFile: vi.fn().mockResolvedValue({
|
||||
key: 'test-key',
|
||||
path: '/test/path',
|
||||
}),
|
||||
hasCloudStorage: vi.fn().mockReturnValue(false),
|
||||
}))
|
||||
|
||||
vi.doMock('@/lib/uploads/setup.server', () => ({}))
|
||||
@@ -325,11 +334,9 @@ describe('File Upload Security Tests', () => {
|
||||
it('should handle multiple files with mixed valid/invalid types', async () => {
|
||||
const formData = new FormData()
|
||||
|
||||
// Valid file
|
||||
const validFile = new File(['valid content'], 'valid.pdf', { type: 'application/pdf' })
|
||||
formData.append('file', validFile)
|
||||
|
||||
// Invalid file (should cause rejection of entire request)
|
||||
const invalidFile = new File(['<script>alert("XSS")</script>'], 'malicious.html', {
|
||||
type: 'text/html',
|
||||
})
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { getPresignedUrl, isUsingCloudStorage, uploadFile } from '@/lib/uploads'
|
||||
import '@/lib/uploads/setup.server'
|
||||
import '@/lib/uploads/core/setup.server'
|
||||
import { getSession } from '@/lib/auth'
|
||||
import {
|
||||
createErrorResponse,
|
||||
@@ -59,7 +58,8 @@ export async function POST(request: NextRequest) {
|
||||
const executionId = formData.get('executionId') as string | null
|
||||
const workspaceId = formData.get('workspaceId') as string | null
|
||||
|
||||
const usingCloudStorage = isUsingCloudStorage()
|
||||
const storageService = await import('@/lib/uploads/core/storage-service')
|
||||
const usingCloudStorage = storageService.hasCloudStorage()
|
||||
logger.info(`Using storage mode: ${usingCloudStorage ? 'Cloud' : 'Local'} for file upload`)
|
||||
|
||||
if (workflowId && executionId) {
|
||||
@@ -87,7 +87,7 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
// Priority 1: Execution-scoped storage (temporary, 5 min expiry)
|
||||
if (workflowId && executionId) {
|
||||
const { uploadExecutionFile } = await import('@/lib/workflows/execution-file-storage')
|
||||
const { uploadExecutionFile } = await import('@/lib/uploads/contexts/execution')
|
||||
const userFile = await uploadExecutionFile(
|
||||
{
|
||||
workspaceId: workspaceId || '',
|
||||
@@ -106,7 +106,7 @@ export async function POST(request: NextRequest) {
|
||||
// Priority 2: Workspace-scoped storage (persistent, no expiry)
|
||||
if (workspaceId) {
|
||||
try {
|
||||
const { uploadWorkspaceFile } = await import('@/lib/uploads/workspace-files')
|
||||
const { uploadWorkspaceFile } = await import('@/lib/uploads/contexts/workspace')
|
||||
const userFile = await uploadWorkspaceFile(
|
||||
workspaceId,
|
||||
session.user.id,
|
||||
@@ -145,32 +145,42 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
try {
|
||||
logger.info(`Uploading file: ${originalName}`)
|
||||
const result = await uploadFile(buffer, originalName, file.type, file.size)
|
||||
logger.info(`Uploading file (general context): ${originalName}`)
|
||||
|
||||
let presignedUrl: string | undefined
|
||||
if (usingCloudStorage) {
|
||||
const storageService = await import('@/lib/uploads/core/storage-service')
|
||||
const fileInfo = await storageService.uploadFile({
|
||||
file: buffer,
|
||||
fileName: originalName,
|
||||
contentType: file.type,
|
||||
context: 'general',
|
||||
})
|
||||
|
||||
let downloadUrl: string | undefined
|
||||
if (storageService.hasCloudStorage()) {
|
||||
try {
|
||||
presignedUrl = await getPresignedUrl(result.key, 24 * 60 * 60) // 24 hours
|
||||
downloadUrl = await storageService.generatePresignedDownloadUrl(
|
||||
fileInfo.key,
|
||||
'general',
|
||||
24 * 60 * 60 // 24 hours
|
||||
)
|
||||
} catch (error) {
|
||||
logger.warn(`Failed to generate presigned URL for ${originalName}:`, error)
|
||||
}
|
||||
}
|
||||
|
||||
const servePath = result.path
|
||||
|
||||
const uploadResult = {
|
||||
name: originalName,
|
||||
size: file.size,
|
||||
size: buffer.length,
|
||||
type: file.type,
|
||||
key: result.key,
|
||||
path: servePath,
|
||||
url: presignedUrl || servePath,
|
||||
key: fileInfo.key,
|
||||
path: fileInfo.path,
|
||||
url: downloadUrl || fileInfo.path,
|
||||
uploadedAt: new Date().toISOString(),
|
||||
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString(), // 24 hours
|
||||
context: 'general',
|
||||
}
|
||||
|
||||
logger.info(`Successfully uploaded: ${result.key}`)
|
||||
logger.info(`Successfully uploaded: ${fileInfo.key}`)
|
||||
uploadResults.push(uploadResult)
|
||||
} catch (error) {
|
||||
logger.error(`Error uploading ${originalName}:`, error)
|
||||
|
||||
@@ -2,7 +2,7 @@ import { existsSync } from 'fs'
|
||||
import { join, resolve, sep } from 'path'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { UPLOAD_DIR } from '@/lib/uploads/setup'
|
||||
import { UPLOAD_DIR } from '@/lib/uploads/core/setup'
|
||||
|
||||
const logger = createLogger('FilesUtils')
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import { PutObjectCommand } from '@aws-sdk/client-s3'
|
||||
// Dynamic import for S3 client to avoid client-side bundling
|
||||
import { db } from '@sim/db'
|
||||
import { subscription, user, workflow, workflowExecutionLogs } from '@sim/db/schema'
|
||||
import { and, eq, inArray, lt, sql } from 'drizzle-orm'
|
||||
@@ -8,17 +6,13 @@ import { verifyCronAuth } from '@/lib/auth/internal'
|
||||
import { env } from '@/lib/env'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { snapshotService } from '@/lib/logs/execution/snapshot/service'
|
||||
import { deleteFile, isUsingCloudStorage } from '@/lib/uploads'
|
||||
import { isUsingCloudStorage, StorageService } from '@/lib/uploads'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
const logger = createLogger('LogsCleanupAPI')
|
||||
|
||||
const BATCH_SIZE = 2000
|
||||
const S3_CONFIG = {
|
||||
bucket: env.S3_LOGS_BUCKET_NAME || '',
|
||||
region: env.AWS_REGION || '',
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
@@ -27,10 +21,6 @@ export async function GET(request: NextRequest) {
|
||||
return authError
|
||||
}
|
||||
|
||||
if (!S3_CONFIG.bucket || !S3_CONFIG.region) {
|
||||
return new NextResponse('Configuration error: S3 bucket or region not set', { status: 500 })
|
||||
}
|
||||
|
||||
const retentionDate = new Date()
|
||||
retentionDate.setDate(retentionDate.getDate() - Number(env.FREE_PLAN_LOG_RETENTION_DAYS || '7'))
|
||||
|
||||
@@ -84,14 +74,12 @@ export async function GET(request: NextRequest) {
|
||||
const startTime = Date.now()
|
||||
const MAX_BATCHES = 10
|
||||
|
||||
// Process enhanced logging cleanup
|
||||
let batchesProcessed = 0
|
||||
let hasMoreLogs = true
|
||||
|
||||
logger.info(`Starting enhanced logs cleanup for ${workflowIds.length} workflows`)
|
||||
|
||||
while (hasMoreLogs && batchesProcessed < MAX_BATCHES) {
|
||||
// Query enhanced execution logs that need cleanup
|
||||
const oldEnhancedLogs = await db
|
||||
.select({
|
||||
id: workflowExecutionLogs.id,
|
||||
@@ -122,7 +110,6 @@ export async function GET(request: NextRequest) {
|
||||
for (const log of oldEnhancedLogs) {
|
||||
const today = new Date().toISOString().split('T')[0]
|
||||
|
||||
// Archive enhanced log with more detailed structure
|
||||
const enhancedLogKey = `archived-enhanced-logs/${today}/${log.id}.json`
|
||||
const enhancedLogData = JSON.stringify({
|
||||
...log,
|
||||
@@ -131,32 +118,31 @@ export async function GET(request: NextRequest) {
|
||||
})
|
||||
|
||||
try {
|
||||
const { getS3Client } = await import('@/lib/uploads/s3/s3-client')
|
||||
await getS3Client().send(
|
||||
new PutObjectCommand({
|
||||
Bucket: S3_CONFIG.bucket,
|
||||
Key: enhancedLogKey,
|
||||
Body: enhancedLogData,
|
||||
ContentType: 'application/json',
|
||||
Metadata: {
|
||||
logId: String(log.id),
|
||||
workflowId: String(log.workflowId),
|
||||
executionId: String(log.executionId),
|
||||
logType: 'enhanced',
|
||||
archivedAt: new Date().toISOString(),
|
||||
},
|
||||
})
|
||||
)
|
||||
await StorageService.uploadFile({
|
||||
file: Buffer.from(enhancedLogData),
|
||||
fileName: enhancedLogKey,
|
||||
contentType: 'application/json',
|
||||
context: 'general',
|
||||
metadata: {
|
||||
logId: String(log.id),
|
||||
workflowId: String(log.workflowId),
|
||||
executionId: String(log.executionId),
|
||||
logType: 'enhanced',
|
||||
archivedAt: new Date().toISOString(),
|
||||
},
|
||||
})
|
||||
|
||||
results.enhancedLogs.archived++
|
||||
|
||||
// Clean up associated files if using cloud storage
|
||||
if (isUsingCloudStorage() && log.files && Array.isArray(log.files)) {
|
||||
for (const file of log.files) {
|
||||
if (file && typeof file === 'object' && file.key) {
|
||||
results.files.total++
|
||||
try {
|
||||
await deleteFile(file.key)
|
||||
await StorageService.deleteFile({
|
||||
key: file.key,
|
||||
context: 'general',
|
||||
})
|
||||
results.files.deleted++
|
||||
logger.info(`Deleted file: ${file.key}`)
|
||||
} catch (fileError) {
|
||||
@@ -168,7 +154,6 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
|
||||
try {
|
||||
// Delete enhanced log
|
||||
const deleteResult = await db
|
||||
.delete(workflowExecutionLogs)
|
||||
.where(eq(workflowExecutionLogs.id, log.id))
|
||||
@@ -200,7 +185,6 @@ export async function GET(request: NextRequest) {
|
||||
)
|
||||
}
|
||||
|
||||
// Cleanup orphaned snapshots
|
||||
try {
|
||||
const snapshotRetentionDays = Number(env.FREE_PLAN_LOG_RETENTION_DAYS || '7') + 1 // Keep snapshots 1 day longer
|
||||
const cleanedSnapshots = await snapshotService.cleanupOrphanedSnapshots(snapshotRetentionDays)
|
||||
|
||||
@@ -90,16 +90,38 @@ export const POST = withMcpAuth('read')(
|
||||
)
|
||||
}
|
||||
|
||||
// Parse array arguments based on tool schema
|
||||
// Cast arguments to their expected types based on tool schema
|
||||
if (tool.inputSchema?.properties) {
|
||||
for (const [paramName, paramSchema] of Object.entries(tool.inputSchema.properties)) {
|
||||
const schema = paramSchema as any
|
||||
const value = args[paramName]
|
||||
|
||||
if (value === undefined || value === null) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Cast numbers
|
||||
if (
|
||||
schema.type === 'array' &&
|
||||
args[paramName] !== undefined &&
|
||||
typeof args[paramName] === 'string'
|
||||
(schema.type === 'number' || schema.type === 'integer') &&
|
||||
typeof value === 'string'
|
||||
) {
|
||||
const stringValue = args[paramName].trim()
|
||||
const numValue =
|
||||
schema.type === 'integer' ? Number.parseInt(value) : Number.parseFloat(value)
|
||||
if (!Number.isNaN(numValue)) {
|
||||
args[paramName] = numValue
|
||||
}
|
||||
}
|
||||
// Cast booleans
|
||||
else if (schema.type === 'boolean' && typeof value === 'string') {
|
||||
if (value.toLowerCase() === 'true') {
|
||||
args[paramName] = true
|
||||
} else if (value.toLowerCase() === 'false') {
|
||||
args[paramName] = false
|
||||
}
|
||||
}
|
||||
// Cast arrays
|
||||
else if (schema.type === 'array' && typeof value === 'string') {
|
||||
const stringValue = value.trim()
|
||||
if (stringValue) {
|
||||
try {
|
||||
// Try to parse as JSON first (handles ["item1", "item2"])
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { validateImageUrl } from '@/lib/security/input-validation'
|
||||
import { generateRequestId } from '@/lib/utils'
|
||||
@@ -14,6 +15,12 @@ export async function GET(request: NextRequest) {
|
||||
const imageUrl = url.searchParams.get('url')
|
||||
const requestId = generateRequestId()
|
||||
|
||||
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
if (!authResult.success) {
|
||||
logger.error(`[${requestId}] Authentication failed for image proxy:`, authResult.error)
|
||||
return new NextResponse('Unauthorized', { status: 401 })
|
||||
}
|
||||
|
||||
if (!imageUrl) {
|
||||
logger.error(`[${requestId}] Missing 'url' parameter`)
|
||||
return new NextResponse('Missing URL parameter', { status: 400 })
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { generateInternalToken } from '@/lib/auth/internal'
|
||||
import { isDev } from '@/lib/environment'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
@@ -242,12 +244,18 @@ export async function GET(request: Request) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
export async function POST(request: NextRequest) {
|
||||
const requestId = generateRequestId()
|
||||
const startTime = new Date()
|
||||
const startTimeISO = startTime.toISOString()
|
||||
|
||||
try {
|
||||
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
if (!authResult.success) {
|
||||
logger.error(`[${requestId}] Authentication failed for proxy:`, authResult.error)
|
||||
return createErrorResponse('Unauthorized', 401)
|
||||
}
|
||||
|
||||
let requestBody
|
||||
try {
|
||||
requestBody = await request.json()
|
||||
@@ -311,7 +319,6 @@ export async function POST(request: Request) {
|
||||
error: result.error || 'Unknown error',
|
||||
})
|
||||
|
||||
// Let the main executeTool handle error transformation to avoid double transformation
|
||||
throw new Error(result.error || 'Tool execution failed')
|
||||
}
|
||||
|
||||
@@ -319,10 +326,8 @@ export async function POST(request: Request) {
|
||||
const endTimeISO = endTime.toISOString()
|
||||
const duration = endTime.getTime() - startTime.getTime()
|
||||
|
||||
// Add explicit timing information directly to the response
|
||||
const responseWithTimingData = {
|
||||
...result,
|
||||
// Add timing data both at root level and in nested timing object
|
||||
startTime: startTimeISO,
|
||||
endTime: endTimeISO,
|
||||
duration,
|
||||
@@ -335,7 +340,6 @@ export async function POST(request: Request) {
|
||||
|
||||
logger.info(`[${requestId}] Tool executed successfully: ${toolId} (${duration}ms)`)
|
||||
|
||||
// Return the response with CORS headers
|
||||
return formatResponse(responseWithTimingData)
|
||||
} catch (error: any) {
|
||||
logger.error(`[${requestId}] Proxy request failed`, {
|
||||
@@ -344,7 +348,6 @@ export async function POST(request: Request) {
|
||||
name: error instanceof Error ? error.name : undefined,
|
||||
})
|
||||
|
||||
// Add timing information even to error responses
|
||||
const endTime = new Date()
|
||||
const endTimeISO = endTime.toISOString()
|
||||
const duration = endTime.getTime() - startTime.getTime()
|
||||
|
||||
@@ -1,24 +1,32 @@
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { validateAlphanumericId } from '@/lib/security/input-validation'
|
||||
import { uploadFile } from '@/lib/uploads/storage-client'
|
||||
import { StorageService } from '@/lib/uploads'
|
||||
import { getBaseUrl } from '@/lib/urls/utils'
|
||||
|
||||
const logger = createLogger('ProxyTTSAPI')
|
||||
|
||||
export async function POST(request: Request) {
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
if (!authResult.success) {
|
||||
logger.error('Authentication failed for TTS proxy:', authResult.error)
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const body = await request.json()
|
||||
const { text, voiceId, apiKey, modelId = 'eleven_monolingual_v1' } = body
|
||||
|
||||
if (!text || !voiceId || !apiKey) {
|
||||
return new NextResponse('Missing required parameters', { status: 400 })
|
||||
return NextResponse.json({ error: 'Missing required parameters' }, { status: 400 })
|
||||
}
|
||||
|
||||
const voiceIdValidation = validateAlphanumericId(voiceId, 'voiceId', 255)
|
||||
if (!voiceIdValidation.isValid) {
|
||||
logger.error(`Invalid voice ID: ${voiceIdValidation.error}`)
|
||||
return new NextResponse(voiceIdValidation.error, { status: 400 })
|
||||
return NextResponse.json({ error: voiceIdValidation.error }, { status: 400 })
|
||||
}
|
||||
|
||||
logger.info('Proxying TTS request for voice:', voiceId)
|
||||
@@ -41,22 +49,29 @@ export async function POST(request: Request) {
|
||||
|
||||
if (!response.ok) {
|
||||
logger.error(`Failed to generate TTS: ${response.status} ${response.statusText}`)
|
||||
return new NextResponse(`Failed to generate TTS: ${response.status} ${response.statusText}`, {
|
||||
status: response.status,
|
||||
})
|
||||
return NextResponse.json(
|
||||
{ error: `Failed to generate TTS: ${response.status} ${response.statusText}` },
|
||||
{ status: response.status }
|
||||
)
|
||||
}
|
||||
|
||||
const audioBlob = await response.blob()
|
||||
|
||||
if (audioBlob.size === 0) {
|
||||
logger.error('Empty audio received from ElevenLabs')
|
||||
return new NextResponse('Empty audio received', { status: 422 })
|
||||
return NextResponse.json({ error: 'Empty audio received' }, { status: 422 })
|
||||
}
|
||||
|
||||
const audioBuffer = Buffer.from(await audioBlob.arrayBuffer())
|
||||
const timestamp = Date.now()
|
||||
const fileName = `elevenlabs-tts-${timestamp}.mp3`
|
||||
const fileInfo = await uploadFile(audioBuffer, fileName, 'audio/mpeg')
|
||||
|
||||
const fileInfo = await StorageService.uploadFile({
|
||||
file: audioBuffer,
|
||||
fileName,
|
||||
contentType: 'audio/mpeg',
|
||||
context: 'general',
|
||||
})
|
||||
|
||||
const audioUrl = `${getBaseUrl()}${fileInfo.path}`
|
||||
|
||||
@@ -67,11 +82,11 @@ export async function POST(request: Request) {
|
||||
} catch (error) {
|
||||
logger.error('Error proxying TTS:', error)
|
||||
|
||||
return new NextResponse(
|
||||
`Internal Server Error: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
||||
return NextResponse.json(
|
||||
{
|
||||
status: 500,
|
||||
}
|
||||
error: `Internal Server Error: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
||||
},
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { env } from '@/lib/env'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { validateAlphanumericId } from '@/lib/security/input-validation'
|
||||
@@ -7,6 +8,12 @@ const logger = createLogger('ProxyTTSStreamAPI')
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
if (!authResult.success) {
|
||||
logger.error('Authentication failed for TTS stream proxy:', authResult.error)
|
||||
return new Response('Unauthorized', { status: 401 })
|
||||
}
|
||||
|
||||
const body = await request.json()
|
||||
const { text, voiceId, modelId = 'eleven_turbo_v2_5' } = body
|
||||
|
||||
|
||||
@@ -2,7 +2,10 @@ import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { downloadFileFromStorage, processFilesToUserFiles } from '@/lib/uploads/file-processing'
|
||||
import {
|
||||
downloadFileFromStorage,
|
||||
processFilesToUserFiles,
|
||||
} from '@/lib/uploads/utils/file-processing'
|
||||
import { generateRequestId } from '@/lib/utils'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
@@ -2,7 +2,10 @@ import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { downloadFileFromStorage, processFilesToUserFiles } from '@/lib/uploads/file-processing'
|
||||
import {
|
||||
downloadFileFromStorage,
|
||||
processFilesToUserFiles,
|
||||
} from '@/lib/uploads/utils/file-processing'
|
||||
import { generateRequestId } from '@/lib/utils'
|
||||
import { base64UrlEncode, buildMimeMessage } from '@/tools/gmail/utils'
|
||||
|
||||
|
||||
@@ -2,7 +2,10 @@ import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { downloadFileFromStorage, processFilesToUserFiles } from '@/lib/uploads/file-processing'
|
||||
import {
|
||||
downloadFileFromStorage,
|
||||
processFilesToUserFiles,
|
||||
} from '@/lib/uploads/utils/file-processing'
|
||||
import { generateRequestId } from '@/lib/utils'
|
||||
import { base64UrlEncode, buildMimeMessage } from '@/tools/gmail/utils'
|
||||
|
||||
|
||||
@@ -2,7 +2,10 @@ import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { downloadFileFromStorage, processSingleFileToUserFile } from '@/lib/uploads/file-processing'
|
||||
import {
|
||||
downloadFileFromStorage,
|
||||
processSingleFileToUserFile,
|
||||
} from '@/lib/uploads/utils/file-processing'
|
||||
import { generateRequestId } from '@/lib/utils'
|
||||
import {
|
||||
GOOGLE_WORKSPACE_MIME_TYPES,
|
||||
|
||||
@@ -2,7 +2,10 @@ import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { downloadFileFromStorage, processFilesToUserFiles } from '@/lib/uploads/file-processing'
|
||||
import {
|
||||
downloadFileFromStorage,
|
||||
processFilesToUserFiles,
|
||||
} from '@/lib/uploads/utils/file-processing'
|
||||
import { generateRequestId } from '@/lib/utils'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
@@ -2,7 +2,10 @@ import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import { downloadFileFromStorage, processFilesToUserFiles } from '@/lib/uploads/file-processing'
|
||||
import {
|
||||
downloadFileFromStorage,
|
||||
processFilesToUserFiles,
|
||||
} from '@/lib/uploads/utils/file-processing'
|
||||
import { generateRequestId } from '@/lib/utils'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user