@uranotools/urano-social 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +6 -0
- package/LICENSE +21 -0
- package/README.md +192 -2
- package/dist/client/createUranoChat.d.ts +16 -0
- package/dist/client/createUranoChat.js +226 -0
- package/dist/client/index.d.ts +5 -0
- package/dist/client/index.js +10 -0
- package/dist/client/sse.d.ts +3 -0
- package/dist/client/sse.js +108 -0
- package/dist/client/submitContactForm.d.ts +30 -0
- package/dist/client/submitContactForm.js +65 -0
- package/dist/crypto.d.ts +5 -0
- package/dist/crypto.js +75 -0
- package/dist/flavors/UranoSocialChatElement.d.ts +13 -0
- package/dist/flavors/UranoSocialChatElement.js +178 -0
- package/dist/flavors/UranoSocialContactFormElement.d.ts +9 -0
- package/dist/flavors/UranoSocialContactFormElement.js +98 -0
- package/dist/flavors/styles.css +257 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +26 -0
- package/dist/react/UranoSocialChat.d.ts +38 -0
- package/dist/react/UranoSocialChat.js +118 -0
- package/dist/react/UranoSocialContactForm.d.ts +15 -0
- package/dist/react/UranoSocialContactForm.js +48 -0
- package/dist/react/index.d.ts +4 -0
- package/dist/react/index.js +8 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.js +13 -0
- package/dist/server/proxy.d.ts +16 -0
- package/dist/server/proxy.js +207 -0
- package/dist/server/visitorCookie.d.ts +11 -0
- package/dist/server/visitorCookie.js +43 -0
- package/dist/server/websocket.d.ts +21 -0
- package/dist/server/websocket.js +130 -0
- package/dist/types.d.ts +64 -0
- package/dist/types.js +2 -0
- package/package.json +91 -4
package/.env.example
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
# Server-only — never use NEXT_PUBLIC_ / PUBLIC_ prefixes.
|
|
2
|
+
URANO_SOCIAL_WEBHOOK_URL=http://127.0.0.1:6274/v1/webhook/mcp_SalesAgentConnector/default
|
|
3
|
+
URANO_SOCIAL_WEBHOOK_SECRET=replace-with-the-same-WEBHOOK_SECRET-from-the-plugin-vault
|
|
4
|
+
|
|
5
|
+
# Optional: separate secret for signing the visitor cookie (defaults to WEBHOOK_SECRET).
|
|
6
|
+
# URANO_SOCIAL_COOKIE_SECRET=
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Urano Open Source
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,193 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @uranotools/urano-social
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Chat de atención al cliente para el webhook MCP de **SalesAgentConnector**.
|
|
4
|
+
|
|
5
|
+
El secreto HMAC vive solo en el servidor del sitio. El navegador habla con `/api/urano-social`; el proxy firma y reenvía a Urano (Desktop o Cloud) por SSE, wait o WebSocket.
|
|
6
|
+
|
|
7
|
+
## Arranque rápido
|
|
8
|
+
|
|
9
|
+
### 1. Plugin + agente
|
|
10
|
+
|
|
11
|
+
1. Instala SalesAgentConnector **1.4.0+** en Urano Desktop / Cloud.
|
|
12
|
+
2. Vault: `WEBHOOK_SECRET` + `TARGET_AGENT` (agente lanzado con el módulo activo).
|
|
13
|
+
3. URL local:
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
http://127.0.0.1:6274/v1/webhook/mcp_SalesAgentConnector/default
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### 2. Variables de entorno (servidor)
|
|
20
|
+
|
|
21
|
+
```env
|
|
22
|
+
URANO_SOCIAL_WEBHOOK_URL=http://127.0.0.1:6274/v1/webhook/mcp_SalesAgentConnector/default
|
|
23
|
+
URANO_SOCIAL_WEBHOOK_SECRET=el-mismo-secreto-del-vault
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Nunca uses prefijos `NEXT_PUBLIC_` / `PUBLIC_` para el secreto.
|
|
27
|
+
|
|
28
|
+
### 3. Proxy en tu app
|
|
29
|
+
|
|
30
|
+
**Next.js App Router**
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
// app/api/urano-social/route.ts
|
|
34
|
+
import { socialProxyRoute } from '@uranotools/urano-social/server';
|
|
35
|
+
|
|
36
|
+
export const { GET, POST, OPTIONS } = socialProxyRoute();
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
**Astro / Node**
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { createSocialProxy } from '@uranotools/urano-social/server';
|
|
43
|
+
const proxy = createSocialProxy();
|
|
44
|
+
// en tu handler: return proxy.handle(request)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 4. Widget
|
|
48
|
+
|
|
49
|
+
**Custom element (Astro / HTML / TS)**
|
|
50
|
+
|
|
51
|
+
```html
|
|
52
|
+
<script type="module">
|
|
53
|
+
import { registerUranoSocialChat } from '@uranotools/urano-social';
|
|
54
|
+
registerUranoSocialChat();
|
|
55
|
+
</script>
|
|
56
|
+
<urano-social-chat flavor="floating" title="Soporte"></urano-social-chat>
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Slots: `header`, `messages`, `composer`, `launcher`, `empty`.
|
|
60
|
+
|
|
61
|
+
**React / Next**
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
import { UranoSocialChat } from '@uranotools/urano-social/react';
|
|
65
|
+
import '@uranotools/urano-social/flavors.css';
|
|
66
|
+
|
|
67
|
+
export function SupportWidget() {
|
|
68
|
+
return (
|
|
69
|
+
<UranoSocialChat
|
|
70
|
+
flavor="floating"
|
|
71
|
+
renderMessage={(m) => <div data-role={m.role}>{m.content}</div>}
|
|
72
|
+
/>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
**Headless**
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import { createUranoChat } from '@uranotools/urano-social/client';
|
|
81
|
+
|
|
82
|
+
const chat = createUranoChat({ endpoint: '/api/urano-social' });
|
|
83
|
+
await chat.send('Hola');
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Formularios de contacto (GET + SMTP)
|
|
87
|
+
|
|
88
|
+
Mismo proxy y secreto. El widget envía `type=contact` por **GET** `?mode=wait&payload=…`. El plugin responde con el agente y manda el correo por SMTP.
|
|
89
|
+
|
|
90
|
+
Requisito en Vault del plugin: `ENABLE_EMAIL_CONTACT=true` + SMTP.
|
|
91
|
+
|
|
92
|
+
```html
|
|
93
|
+
<script type="module">
|
|
94
|
+
import { registerUranoSocialContactForm } from '@uranotools/urano-social';
|
|
95
|
+
registerUranoSocialContactForm();
|
|
96
|
+
</script>
|
|
97
|
+
<urano-social-contact title="Escríbenos"></urano-social-contact>
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
```tsx
|
|
101
|
+
import { UranoSocialContactForm } from '@uranotools/urano-social/react';
|
|
102
|
+
<UranoSocialContactForm title="Escríbenos" />
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import { submitContactForm } from '@uranotools/urano-social/client';
|
|
107
|
+
await submitContactForm({ email: 'a@b.co', name: 'Ana', message: 'Hola' });
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Flavors
|
|
111
|
+
|
|
112
|
+
| Flavor | Uso |
|
|
113
|
+
|--------|-----|
|
|
114
|
+
| `floating` | Burbuja fija + panel |
|
|
115
|
+
| `sidebar` | Columna lateral |
|
|
116
|
+
| `headless` | Solo controlador / React con UI propia |
|
|
117
|
+
|
|
118
|
+
## Seguridad
|
|
119
|
+
|
|
120
|
+
- Cookie HttpOnly firmada con el `visitorId` (el browser no elige la identidad CRM).
|
|
121
|
+
- Firma `x-urano-signature` (HMAC-SHA256 hex) solo en el proxy.
|
|
122
|
+
- Controles `status` / `events` / `cancel` e `Idempotency-Key` para reanudar turnos.
|
|
123
|
+
|
|
124
|
+
## WebSocket
|
|
125
|
+
|
|
126
|
+
Next App Router usa SSE por defecto. En Astro Node o un `http.Server` propio:
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
import http from 'http';
|
|
130
|
+
import { attachSocialWebSocket } from '@uranotools/urano-social/server';
|
|
131
|
+
|
|
132
|
+
const server = http.createServer(app);
|
|
133
|
+
attachSocialWebSocket(server); // path /api/urano-social/ws
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Desarrollo
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
cd UranoDesktop-demos/urano-social
|
|
140
|
+
npm install
|
|
141
|
+
npm test
|
|
142
|
+
npm run build
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Ejemplos: [`examples/next`](examples/next), [`examples/astro`](examples/astro).
|
|
146
|
+
|
|
147
|
+
## Publicar en npm (`@uranotools/urano-social`)
|
|
148
|
+
|
|
149
|
+
Igual que `@uranotools/urano-guard`: scope de la org **uranotools**, acceso **public**.
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
# 1. Cuenta con permiso en la org (una sola vez)
|
|
153
|
+
npm login
|
|
154
|
+
# Si el scope es nuevo en la org: npm org invite <user> --org=uranotools
|
|
155
|
+
|
|
156
|
+
# 2. Desde esta carpeta
|
|
157
|
+
npm test
|
|
158
|
+
npm run build
|
|
159
|
+
npm publish --access public
|
|
160
|
+
# o: npm run publish:npm
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Tras publicar:
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
npm install @uranotools/urano-social
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Subidas siguientes: sube `version` en `package.json` (`0.1.1`, `0.2.0`, …) y vuelve a `npm publish`.
|
|
170
|
+
|
|
171
|
+
Dry-run sin subir nada:
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
npm publish --dry-run
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### GitHub Actions (igual que urano-guard)
|
|
178
|
+
|
|
179
|
+
1. Secret del repo: **`NPM_TOKEN`** (Settings → Secrets and variables → Actions).
|
|
180
|
+
Debe ser un [Automation token](https://www.npmjs.com/settings/~/tokens) con permiso de publish en la org `uranotools`.
|
|
181
|
+
2. Workflow: [`.github/workflows/publish.yml`](.github/workflows/publish.yml) — corre en **Release** o manualmente (`workflow_dispatch`).
|
|
182
|
+
3. Publicar una versión:
|
|
183
|
+
|
|
184
|
+
```bash
|
|
185
|
+
# sube version en package.json, commit, push
|
|
186
|
+
git tag v0.2.0
|
|
187
|
+
git push origin v0.2.0
|
|
188
|
+
# Luego en GitHub → Releases → Create release from tag v0.2.0
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
El job hace `npm ci` → typecheck → test → build → `npm publish --access public`.
|
|
192
|
+
|
|
193
|
+
Nota: `--provenance` solo funciona si el repo de GitHub es **público**. Con repo privado se publica sin provenance.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CreateUranoChatOptions, SocialChatEvent, SocialClientMessage, SocialControlOptions, SocialSendOptions } from '../types';
|
|
2
|
+
export interface UranoChat {
|
|
3
|
+
readonly sessionId: string | null;
|
|
4
|
+
readonly messages: SocialClientMessage[];
|
|
5
|
+
readonly streaming: boolean;
|
|
6
|
+
send(options: SocialSendOptions | string): Promise<SocialClientMessage>;
|
|
7
|
+
control(options: SocialControlOptions): Promise<Response | AsyncGenerator<SocialChatEvent>>;
|
|
8
|
+
cancel(turnId: string): Promise<void>;
|
|
9
|
+
setDisplayName(name: string): void;
|
|
10
|
+
destroy(): void;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Headless chat controller. Talks only to the site proxy (`/api/urano-social`).
|
|
14
|
+
* Never receives or sends the webhook secret.
|
|
15
|
+
*/
|
|
16
|
+
export declare function createUranoChat(options?: CreateUranoChatOptions): UranoChat;
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createUranoChat = createUranoChat;
|
|
4
|
+
const sse_1 = require("./sse");
|
|
5
|
+
function newMessageId(prefix) {
|
|
6
|
+
return `${prefix}_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Headless chat controller. Talks only to the site proxy (`/api/urano-social`).
|
|
10
|
+
* Never receives or sends the webhook secret.
|
|
11
|
+
*/
|
|
12
|
+
function createUranoChat(options = {}) {
|
|
13
|
+
const endpoint = (options.endpoint || '/api/urano-social').replace(/\/$/, '');
|
|
14
|
+
let sessionId = options.sessionId ?? null;
|
|
15
|
+
let displayName = options.displayName || '';
|
|
16
|
+
let streaming = false;
|
|
17
|
+
let destroyed = false;
|
|
18
|
+
const messages = [];
|
|
19
|
+
let abort = null;
|
|
20
|
+
const emitMessages = () => options.onMessages?.(messages.slice());
|
|
21
|
+
const emitStatus = () => options.onStatus?.({
|
|
22
|
+
connected: !destroyed,
|
|
23
|
+
streaming,
|
|
24
|
+
sessionId,
|
|
25
|
+
});
|
|
26
|
+
function applyEvent(evt, assistant) {
|
|
27
|
+
options.onEvent?.(evt);
|
|
28
|
+
if (evt.sessionId)
|
|
29
|
+
sessionId = String(evt.sessionId);
|
|
30
|
+
if (evt.type === 'session' && evt.sessionId)
|
|
31
|
+
sessionId = String(evt.sessionId);
|
|
32
|
+
if (evt.type === 'TokenStreamChunk' && typeof evt.chunk === 'string') {
|
|
33
|
+
assistant.content += evt.chunk;
|
|
34
|
+
assistant.streaming = true;
|
|
35
|
+
emitMessages();
|
|
36
|
+
}
|
|
37
|
+
if (evt.type === 'done') {
|
|
38
|
+
if (typeof evt.response === 'string' && evt.response && !assistant.content) {
|
|
39
|
+
assistant.content = evt.response;
|
|
40
|
+
}
|
|
41
|
+
assistant.streaming = false;
|
|
42
|
+
if (evt.turnId)
|
|
43
|
+
assistant.turnId = String(evt.turnId);
|
|
44
|
+
emitMessages();
|
|
45
|
+
}
|
|
46
|
+
if (evt.type === 'error') {
|
|
47
|
+
assistant.streaming = false;
|
|
48
|
+
if (!assistant.content)
|
|
49
|
+
assistant.content = String(evt.error || 'error');
|
|
50
|
+
options.onError?.(new Error(String(evt.error || 'stream_error')));
|
|
51
|
+
emitMessages();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async function sendViaSse(text, idempotencyKey, signal) {
|
|
55
|
+
const assistant = {
|
|
56
|
+
id: newMessageId('a'),
|
|
57
|
+
role: 'assistant',
|
|
58
|
+
content: '',
|
|
59
|
+
streaming: true,
|
|
60
|
+
createdAt: Date.now(),
|
|
61
|
+
};
|
|
62
|
+
messages.push(assistant);
|
|
63
|
+
emitMessages();
|
|
64
|
+
const body = JSON.stringify({
|
|
65
|
+
text,
|
|
66
|
+
sessionId: sessionId || undefined,
|
|
67
|
+
displayName: displayName || undefined,
|
|
68
|
+
});
|
|
69
|
+
const headers = {
|
|
70
|
+
'Content-Type': 'application/json',
|
|
71
|
+
Accept: 'text/event-stream',
|
|
72
|
+
};
|
|
73
|
+
if (idempotencyKey)
|
|
74
|
+
headers['Idempotency-Key'] = idempotencyKey;
|
|
75
|
+
const response = await fetch(`${endpoint}?mode=sse`, {
|
|
76
|
+
method: 'POST',
|
|
77
|
+
headers,
|
|
78
|
+
body,
|
|
79
|
+
credentials: 'include',
|
|
80
|
+
signal,
|
|
81
|
+
});
|
|
82
|
+
if (!response.ok) {
|
|
83
|
+
const errText = await response.text().catch(() => '');
|
|
84
|
+
throw new Error(errText || `http_${response.status}`);
|
|
85
|
+
}
|
|
86
|
+
const contentType = response.headers.get('Content-Type') || '';
|
|
87
|
+
if (contentType.includes('text/event-stream')) {
|
|
88
|
+
for await (const evt of (0, sse_1.readSseStream)(response, signal)) {
|
|
89
|
+
applyEvent(evt, assistant);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
const json = await response.json();
|
|
94
|
+
if (json.sessionId)
|
|
95
|
+
sessionId = String(json.sessionId);
|
|
96
|
+
assistant.content = String(json.text || json.response || '');
|
|
97
|
+
assistant.streaming = false;
|
|
98
|
+
assistant.turnId = json.turnId ? String(json.turnId) : undefined;
|
|
99
|
+
emitMessages();
|
|
100
|
+
}
|
|
101
|
+
assistant.streaming = false;
|
|
102
|
+
emitMessages();
|
|
103
|
+
}
|
|
104
|
+
async function sendViaWait(text, idempotencyKey, signal) {
|
|
105
|
+
const assistant = {
|
|
106
|
+
id: newMessageId('a'),
|
|
107
|
+
role: 'assistant',
|
|
108
|
+
content: '',
|
|
109
|
+
streaming: true,
|
|
110
|
+
createdAt: Date.now(),
|
|
111
|
+
};
|
|
112
|
+
messages.push(assistant);
|
|
113
|
+
emitMessages();
|
|
114
|
+
const headers = { 'Content-Type': 'application/json' };
|
|
115
|
+
if (idempotencyKey)
|
|
116
|
+
headers['Idempotency-Key'] = idempotencyKey;
|
|
117
|
+
const response = await fetch(`${endpoint}?mode=wait`, {
|
|
118
|
+
method: 'POST',
|
|
119
|
+
headers,
|
|
120
|
+
credentials: 'include',
|
|
121
|
+
signal,
|
|
122
|
+
body: JSON.stringify({
|
|
123
|
+
text,
|
|
124
|
+
sessionId: sessionId || undefined,
|
|
125
|
+
displayName: displayName || undefined,
|
|
126
|
+
}),
|
|
127
|
+
});
|
|
128
|
+
const json = await response.json().catch(() => ({}));
|
|
129
|
+
if (!response.ok) {
|
|
130
|
+
throw new Error(json.error || `http_${response.status}`);
|
|
131
|
+
}
|
|
132
|
+
if (json.sessionId)
|
|
133
|
+
sessionId = String(json.sessionId);
|
|
134
|
+
assistant.content = String(json.text || json.response || '');
|
|
135
|
+
assistant.streaming = false;
|
|
136
|
+
assistant.turnId = json.turnId ? String(json.turnId) : undefined;
|
|
137
|
+
options.onEvent?.({ type: 'done', ...json });
|
|
138
|
+
emitMessages();
|
|
139
|
+
}
|
|
140
|
+
const api = {
|
|
141
|
+
get sessionId() { return sessionId; },
|
|
142
|
+
get messages() { return messages.slice(); },
|
|
143
|
+
get streaming() { return streaming; },
|
|
144
|
+
async send(input) {
|
|
145
|
+
if (destroyed)
|
|
146
|
+
throw new Error('destroyed');
|
|
147
|
+
const opts = typeof input === 'string' ? { text: input } : input;
|
|
148
|
+
const text = String(opts.text || '').trim();
|
|
149
|
+
if (!text)
|
|
150
|
+
throw new Error('empty_text');
|
|
151
|
+
messages.push({
|
|
152
|
+
id: newMessageId('u'),
|
|
153
|
+
role: 'user',
|
|
154
|
+
content: text,
|
|
155
|
+
createdAt: Date.now(),
|
|
156
|
+
});
|
|
157
|
+
emitMessages();
|
|
158
|
+
abort?.abort();
|
|
159
|
+
abort = new AbortController();
|
|
160
|
+
const signal = opts.signal || abort.signal;
|
|
161
|
+
streaming = true;
|
|
162
|
+
emitStatus();
|
|
163
|
+
try {
|
|
164
|
+
const mode = opts.mode
|
|
165
|
+
|| (options.preferredTransport === 'wait' ? 'wait' : 'sse');
|
|
166
|
+
if (mode === 'wait') {
|
|
167
|
+
await sendViaWait(text, opts.idempotencyKey, signal);
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
await sendViaSse(text, opts.idempotencyKey, signal);
|
|
171
|
+
}
|
|
172
|
+
return messages[messages.length - 1];
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
options.onError?.(error instanceof Error ? error : new Error(String(error)));
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
finally {
|
|
179
|
+
streaming = false;
|
|
180
|
+
emitStatus();
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
async control(controlOpts) {
|
|
184
|
+
const body = {
|
|
185
|
+
webhookAction: controlOpts.action,
|
|
186
|
+
turnId: controlOpts.turnId,
|
|
187
|
+
sessionId: sessionId || undefined,
|
|
188
|
+
after: controlOpts.after,
|
|
189
|
+
};
|
|
190
|
+
if (controlOpts.action === 'events') {
|
|
191
|
+
const response = await fetch(`${endpoint}?mode=sse`, {
|
|
192
|
+
method: 'POST',
|
|
193
|
+
headers: {
|
|
194
|
+
'Content-Type': 'application/json',
|
|
195
|
+
Accept: 'text/event-stream',
|
|
196
|
+
...(controlOpts.after ? { 'Last-Event-ID': controlOpts.after } : {}),
|
|
197
|
+
},
|
|
198
|
+
credentials: 'include',
|
|
199
|
+
body: JSON.stringify(body),
|
|
200
|
+
signal: controlOpts.signal,
|
|
201
|
+
});
|
|
202
|
+
return (0, sse_1.readSseStream)(response, controlOpts.signal);
|
|
203
|
+
}
|
|
204
|
+
return fetch(`${endpoint}?mode=wait`, {
|
|
205
|
+
method: 'POST',
|
|
206
|
+
headers: { 'Content-Type': 'application/json' },
|
|
207
|
+
credentials: 'include',
|
|
208
|
+
body: JSON.stringify(body),
|
|
209
|
+
signal: controlOpts.signal,
|
|
210
|
+
});
|
|
211
|
+
},
|
|
212
|
+
async cancel(turnId) {
|
|
213
|
+
await api.control({ action: 'cancel', turnId });
|
|
214
|
+
},
|
|
215
|
+
setDisplayName(name) {
|
|
216
|
+
displayName = String(name || '');
|
|
217
|
+
},
|
|
218
|
+
destroy() {
|
|
219
|
+
destroyed = true;
|
|
220
|
+
abort?.abort();
|
|
221
|
+
emitStatus();
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
emitStatus();
|
|
225
|
+
return api;
|
|
226
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { createUranoChat } from './createUranoChat';
|
|
2
|
+
export type { UranoChat } from './createUranoChat';
|
|
3
|
+
export { readSseStream, parseSseChunk } from './sse';
|
|
4
|
+
export { submitContactForm } from './submitContactForm';
|
|
5
|
+
export type { ContactFormPayload, ContactFormResult, SubmitContactFormOptions } from './submitContactForm';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.submitContactForm = exports.parseSseChunk = exports.readSseStream = exports.createUranoChat = void 0;
|
|
4
|
+
var createUranoChat_1 = require("./createUranoChat");
|
|
5
|
+
Object.defineProperty(exports, "createUranoChat", { enumerable: true, get: function () { return createUranoChat_1.createUranoChat; } });
|
|
6
|
+
var sse_1 = require("./sse");
|
|
7
|
+
Object.defineProperty(exports, "readSseStream", { enumerable: true, get: function () { return sse_1.readSseStream; } });
|
|
8
|
+
Object.defineProperty(exports, "parseSseChunk", { enumerable: true, get: function () { return sse_1.parseSseChunk; } });
|
|
9
|
+
var submitContactForm_1 = require("./submitContactForm");
|
|
10
|
+
Object.defineProperty(exports, "submitContactForm", { enumerable: true, get: function () { return submitContactForm_1.submitContactForm; } });
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.readSseStream = readSseStream;
|
|
4
|
+
exports.parseSseChunk = parseSseChunk;
|
|
5
|
+
async function* readSseStream(response, signal) {
|
|
6
|
+
if (!response.body) {
|
|
7
|
+
throw new Error('missing_response_body');
|
|
8
|
+
}
|
|
9
|
+
const reader = response.body.getReader();
|
|
10
|
+
const decoder = new TextDecoder();
|
|
11
|
+
let buffer = '';
|
|
12
|
+
let eventType = 'message';
|
|
13
|
+
let dataLines = [];
|
|
14
|
+
const flush = () => {
|
|
15
|
+
if (!dataLines.length) {
|
|
16
|
+
eventType = 'message';
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
const raw = dataLines.join('\n');
|
|
20
|
+
dataLines = [];
|
|
21
|
+
const type = eventType;
|
|
22
|
+
eventType = 'message';
|
|
23
|
+
try {
|
|
24
|
+
const parsed = JSON.parse(raw);
|
|
25
|
+
if (parsed && typeof parsed === 'object') {
|
|
26
|
+
return { type: parsed.type || type, ...parsed };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return { type, data: raw };
|
|
31
|
+
}
|
|
32
|
+
return { type, data: raw };
|
|
33
|
+
};
|
|
34
|
+
try {
|
|
35
|
+
while (true) {
|
|
36
|
+
if (signal?.aborted)
|
|
37
|
+
break;
|
|
38
|
+
const { done, value } = await reader.read();
|
|
39
|
+
if (done)
|
|
40
|
+
break;
|
|
41
|
+
buffer += decoder.decode(value, { stream: true });
|
|
42
|
+
let nl;
|
|
43
|
+
while ((nl = buffer.indexOf('\n')) >= 0) {
|
|
44
|
+
let line = buffer.slice(0, nl);
|
|
45
|
+
buffer = buffer.slice(nl + 1);
|
|
46
|
+
if (line.endsWith('\r'))
|
|
47
|
+
line = line.slice(0, -1);
|
|
48
|
+
if (line === '') {
|
|
49
|
+
const evt = flush();
|
|
50
|
+
if (evt)
|
|
51
|
+
yield evt;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (line.startsWith(':'))
|
|
55
|
+
continue;
|
|
56
|
+
if (line.startsWith('event:')) {
|
|
57
|
+
eventType = line.slice(6).trim();
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (line.startsWith('data:')) {
|
|
61
|
+
dataLines.push(line.slice(5).trimStart());
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const trailing = flush();
|
|
66
|
+
if (trailing)
|
|
67
|
+
yield trailing;
|
|
68
|
+
}
|
|
69
|
+
finally {
|
|
70
|
+
try {
|
|
71
|
+
await reader.cancel();
|
|
72
|
+
}
|
|
73
|
+
catch { /* ignore */ }
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function parseSseChunk(chunk) {
|
|
77
|
+
const events = [];
|
|
78
|
+
let eventType = 'message';
|
|
79
|
+
let dataLines = [];
|
|
80
|
+
for (const rawLine of chunk.split(/\r?\n/)) {
|
|
81
|
+
const line = rawLine;
|
|
82
|
+
if (line === '') {
|
|
83
|
+
if (dataLines.length) {
|
|
84
|
+
const raw = dataLines.join('\n');
|
|
85
|
+
dataLines = [];
|
|
86
|
+
const type = eventType;
|
|
87
|
+
eventType = 'message';
|
|
88
|
+
try {
|
|
89
|
+
const parsed = JSON.parse(raw);
|
|
90
|
+
events.push({ type: parsed.type || type, ...parsed });
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
events.push({ type, data: raw });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (line.startsWith(':'))
|
|
99
|
+
continue;
|
|
100
|
+
if (line.startsWith('event:')) {
|
|
101
|
+
eventType = line.slice(6).trim();
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (line.startsWith('data:'))
|
|
105
|
+
dataLines.push(line.slice(5).trimStart());
|
|
106
|
+
}
|
|
107
|
+
return events;
|
|
108
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface ContactFormPayload {
|
|
2
|
+
name?: string;
|
|
3
|
+
email: string;
|
|
4
|
+
message: string;
|
|
5
|
+
subject?: string;
|
|
6
|
+
phone?: string;
|
|
7
|
+
company?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface ContactFormResult {
|
|
10
|
+
ok: boolean;
|
|
11
|
+
text?: string;
|
|
12
|
+
sessionId?: string;
|
|
13
|
+
turnId?: string;
|
|
14
|
+
delivered?: string;
|
|
15
|
+
error?: string;
|
|
16
|
+
status: number;
|
|
17
|
+
raw?: unknown;
|
|
18
|
+
}
|
|
19
|
+
export interface SubmitContactFormOptions {
|
|
20
|
+
/** Site proxy endpoint. Default `/api/urano-social`. */
|
|
21
|
+
endpoint?: string;
|
|
22
|
+
/** Prefer GET ?mode=wait&payload=… (default true). */
|
|
23
|
+
useGet?: boolean;
|
|
24
|
+
signal?: AbortSignal;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Submits a contact form through the site proxy.
|
|
28
|
+
* Default transport is GET + mode=wait (signed upstream by the server).
|
|
29
|
+
*/
|
|
30
|
+
export declare function submitContactForm(input: ContactFormPayload, options?: SubmitContactFormOptions): Promise<ContactFormResult>;
|