onipin-js 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/README.md +57 -0
- package/dist/index.d.ts +123 -0
- package/dist/index.js +97 -0
- package/package.json +37 -0
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# onipin-js
|
|
2
|
+
|
|
3
|
+
SDK oficial de [OniPin](https://onnivers.store) — protocolo `/v1`, chat, catálogo, reservas y pedidos.
|
|
4
|
+
|
|
5
|
+
## Instalación
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install onipin-js
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Uso rápido
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
import { OniPinClient } from "onipin-js";
|
|
15
|
+
|
|
16
|
+
const oni = new OniPinClient({
|
|
17
|
+
baseUrl: "https://onnivers.store",
|
|
18
|
+
pin: "onp_tu_pin",
|
|
19
|
+
callerType: "ai-agent",
|
|
20
|
+
callerName: "MiBot",
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const info = await oni.ping();
|
|
24
|
+
console.log(info.name, info.capabilities);
|
|
25
|
+
|
|
26
|
+
const chat = await oni.chat("Hola, ¿qué servicios ofrecen?");
|
|
27
|
+
console.log(chat.reply?.text);
|
|
28
|
+
|
|
29
|
+
const booking = await oni.createBooking({
|
|
30
|
+
service: "Consulta",
|
|
31
|
+
scheduledLabel: "2026-07-20 15:00",
|
|
32
|
+
clientName: "Ana",
|
|
33
|
+
clientContact: "ana@email.com",
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const order = await oni.createOrder({
|
|
37
|
+
productName: "Plan Pro",
|
|
38
|
+
quantity: 1,
|
|
39
|
+
clientName: "Ana",
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## API
|
|
44
|
+
|
|
45
|
+
| Método | Descripción |
|
|
46
|
+
|--------|-------------|
|
|
47
|
+
| `ping()` | Discovery del negocio |
|
|
48
|
+
| `catalog()` | Productos/servicios activos |
|
|
49
|
+
| `chat(message)` | Envía mensaje y mantiene `conversationId` |
|
|
50
|
+
| `messages({ after, limit })` | Lee el hilo |
|
|
51
|
+
| `createBooking(...)` | Reserva real en BD |
|
|
52
|
+
| `createOrder(...)` | Pedido real en BD |
|
|
53
|
+
| `reset()` | Nueva conversación |
|
|
54
|
+
|
|
55
|
+
## Licencia
|
|
56
|
+
|
|
57
|
+
MIT — Empresa Tecnológica de Colombia / OnniVers
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* onipin-js — SDK oficial del protocolo OniPin (/v1).
|
|
3
|
+
*
|
|
4
|
+
* Uso:
|
|
5
|
+
* import { OniPinClient } from "onipin-js";
|
|
6
|
+
* const oni = new OniPinClient({ baseUrl: "https://onnivers.store", pin: "onp_..." });
|
|
7
|
+
* const reply = await oni.chat("Hola");
|
|
8
|
+
*/
|
|
9
|
+
export type OniPinClientOptions = {
|
|
10
|
+
/** Origen del servidor OniPin (sin slash final). Default: https://onnivers.store */
|
|
11
|
+
baseUrl?: string;
|
|
12
|
+
/** Pin del negocio (onp_…) */
|
|
13
|
+
pin: string;
|
|
14
|
+
/** Tipo de llamador para el protocolo. Default: ai-agent */
|
|
15
|
+
callerType?: "human" | "ai-agent";
|
|
16
|
+
/** Nombre visible del llamador */
|
|
17
|
+
callerName?: string;
|
|
18
|
+
/** fetch personalizado (p. ej. undici, timeout wrappers) */
|
|
19
|
+
fetch?: typeof globalThis.fetch;
|
|
20
|
+
};
|
|
21
|
+
export type PingResult = {
|
|
22
|
+
ok: true;
|
|
23
|
+
protocol: string;
|
|
24
|
+
pin: string;
|
|
25
|
+
name: string;
|
|
26
|
+
description?: string;
|
|
27
|
+
language?: string;
|
|
28
|
+
capabilities: string[];
|
|
29
|
+
mcp?: string;
|
|
30
|
+
};
|
|
31
|
+
export type CatalogItem = {
|
|
32
|
+
id: string;
|
|
33
|
+
name: string;
|
|
34
|
+
description?: string;
|
|
35
|
+
price?: string;
|
|
36
|
+
kind?: string;
|
|
37
|
+
imageUrl?: string | null;
|
|
38
|
+
};
|
|
39
|
+
export type ChatMessage = {
|
|
40
|
+
id: string;
|
|
41
|
+
role: "visitor" | "bot" | "owner" | string;
|
|
42
|
+
author?: string;
|
|
43
|
+
text: string;
|
|
44
|
+
createdAt: string;
|
|
45
|
+
};
|
|
46
|
+
export type ChatResult = {
|
|
47
|
+
ok: true;
|
|
48
|
+
protocol: string;
|
|
49
|
+
conversationId: string;
|
|
50
|
+
reply: {
|
|
51
|
+
id?: string;
|
|
52
|
+
text: string;
|
|
53
|
+
engine?: string;
|
|
54
|
+
createdAt?: string;
|
|
55
|
+
} | null;
|
|
56
|
+
data: Record<string, unknown>;
|
|
57
|
+
};
|
|
58
|
+
export declare class OniPinError extends Error {
|
|
59
|
+
status: number;
|
|
60
|
+
body: unknown;
|
|
61
|
+
constructor(message: string, status: number, body: unknown);
|
|
62
|
+
}
|
|
63
|
+
export declare class OniPinClient {
|
|
64
|
+
readonly baseUrl: string;
|
|
65
|
+
readonly pin: string;
|
|
66
|
+
readonly callerType: "human" | "ai-agent";
|
|
67
|
+
readonly callerName?: string;
|
|
68
|
+
conversationId: string | null;
|
|
69
|
+
private readonly _fetch;
|
|
70
|
+
constructor(opts: OniPinClientOptions);
|
|
71
|
+
private request;
|
|
72
|
+
/** Discovery del negocio (GET /v1/ping/:pin). */
|
|
73
|
+
ping(): Promise<PingResult>;
|
|
74
|
+
/** Catálogo público de productos/servicios. */
|
|
75
|
+
catalog(): Promise<{
|
|
76
|
+
ok: true;
|
|
77
|
+
items: CatalogItem[];
|
|
78
|
+
business?: string;
|
|
79
|
+
}>;
|
|
80
|
+
/** Envía un mensaje y mantiene conversationId entre llamadas. */
|
|
81
|
+
chat(message: string, conversationId?: string | null): Promise<ChatResult>;
|
|
82
|
+
/** Lee mensajes del hilo (con cursor `after` y `limit` opcionales). */
|
|
83
|
+
messages(opts?: {
|
|
84
|
+
conversationId?: string;
|
|
85
|
+
after?: string;
|
|
86
|
+
limit?: number;
|
|
87
|
+
}): Promise<{
|
|
88
|
+
ok: true;
|
|
89
|
+
conversationId: string;
|
|
90
|
+
mode: string;
|
|
91
|
+
messages: ChatMessage[];
|
|
92
|
+
}>;
|
|
93
|
+
/** Crea una reserva real (tabla bookings). */
|
|
94
|
+
createBooking(input: {
|
|
95
|
+
service: string;
|
|
96
|
+
scheduledLabel?: string;
|
|
97
|
+
scheduledAt?: string;
|
|
98
|
+
clientName: string;
|
|
99
|
+
clientContact?: string;
|
|
100
|
+
notes?: string;
|
|
101
|
+
conversationId?: string;
|
|
102
|
+
}): Promise<{
|
|
103
|
+
ok: true;
|
|
104
|
+
booking: Record<string, unknown>;
|
|
105
|
+
}>;
|
|
106
|
+
/** Crea un pedido real (tabla orders). */
|
|
107
|
+
createOrder(input: {
|
|
108
|
+
productName: string;
|
|
109
|
+
quantity?: number;
|
|
110
|
+
productId?: string;
|
|
111
|
+
unitPrice?: string;
|
|
112
|
+
clientName: string;
|
|
113
|
+
clientContact?: string;
|
|
114
|
+
notes?: string;
|
|
115
|
+
conversationId?: string;
|
|
116
|
+
}): Promise<{
|
|
117
|
+
ok: true;
|
|
118
|
+
order: Record<string, unknown>;
|
|
119
|
+
}>;
|
|
120
|
+
/** Resetea el hilo local (la próxima chat() abre conversación nueva). */
|
|
121
|
+
reset(): void;
|
|
122
|
+
}
|
|
123
|
+
export default OniPinClient;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* onipin-js — SDK oficial del protocolo OniPin (/v1).
|
|
3
|
+
*
|
|
4
|
+
* Uso:
|
|
5
|
+
* import { OniPinClient } from "onipin-js";
|
|
6
|
+
* const oni = new OniPinClient({ baseUrl: "https://onnivers.store", pin: "onp_..." });
|
|
7
|
+
* const reply = await oni.chat("Hola");
|
|
8
|
+
*/
|
|
9
|
+
export class OniPinError extends Error {
|
|
10
|
+
status;
|
|
11
|
+
body;
|
|
12
|
+
constructor(message, status, body) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "OniPinError";
|
|
15
|
+
this.status = status;
|
|
16
|
+
this.body = body;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export class OniPinClient {
|
|
20
|
+
baseUrl;
|
|
21
|
+
pin;
|
|
22
|
+
callerType;
|
|
23
|
+
callerName;
|
|
24
|
+
conversationId = null;
|
|
25
|
+
_fetch;
|
|
26
|
+
constructor(opts) {
|
|
27
|
+
if (!opts.pin)
|
|
28
|
+
throw new Error("pin es obligatorio");
|
|
29
|
+
this.baseUrl = (opts.baseUrl || "https://onnivers.store").replace(/\/$/, "");
|
|
30
|
+
this.pin = opts.pin;
|
|
31
|
+
this.callerType = opts.callerType || "ai-agent";
|
|
32
|
+
this.callerName = opts.callerName;
|
|
33
|
+
this._fetch = opts.fetch || globalThis.fetch.bind(globalThis);
|
|
34
|
+
}
|
|
35
|
+
async request(path, init) {
|
|
36
|
+
const res = await this._fetch(`${this.baseUrl}${path}`, {
|
|
37
|
+
...init,
|
|
38
|
+
headers: {
|
|
39
|
+
"Content-Type": "application/json",
|
|
40
|
+
...(init?.headers || {}),
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
const body = await res.json().catch(() => ({}));
|
|
44
|
+
if (!res.ok || body.ok === false) {
|
|
45
|
+
throw new OniPinError(body.error || `OniPin HTTP ${res.status}`, res.status, body);
|
|
46
|
+
}
|
|
47
|
+
return body;
|
|
48
|
+
}
|
|
49
|
+
/** Discovery del negocio (GET /v1/ping/:pin). */
|
|
50
|
+
ping() {
|
|
51
|
+
return this.request(`/v1/ping/${encodeURIComponent(this.pin)}`);
|
|
52
|
+
}
|
|
53
|
+
/** Catálogo público de productos/servicios. */
|
|
54
|
+
async catalog() {
|
|
55
|
+
return this.request(`/v1/catalog/${encodeURIComponent(this.pin)}`);
|
|
56
|
+
}
|
|
57
|
+
/** Envía un mensaje y mantiene conversationId entre llamadas. */
|
|
58
|
+
async chat(message, conversationId) {
|
|
59
|
+
const result = await this.request(`/v1/chat/${encodeURIComponent(this.pin)}`, {
|
|
60
|
+
method: "POST",
|
|
61
|
+
headers: { "X-Caller-Type": this.callerType },
|
|
62
|
+
body: JSON.stringify({
|
|
63
|
+
message,
|
|
64
|
+
conversationId: conversationId ?? this.conversationId,
|
|
65
|
+
callerName: this.callerName,
|
|
66
|
+
}),
|
|
67
|
+
});
|
|
68
|
+
this.conversationId = result.conversationId;
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
/** Lee mensajes del hilo (con cursor `after` y `limit` opcionales). */
|
|
72
|
+
messages(opts = {}) {
|
|
73
|
+
const id = opts.conversationId || this.conversationId;
|
|
74
|
+
if (!id)
|
|
75
|
+
throw new Error("conversationId requerido — llama chat() primero");
|
|
76
|
+
const q = new URLSearchParams();
|
|
77
|
+
if (opts.after)
|
|
78
|
+
q.set("after", opts.after);
|
|
79
|
+
if (opts.limit)
|
|
80
|
+
q.set("limit", String(opts.limit));
|
|
81
|
+
const qs = q.toString() ? `?${q}` : "";
|
|
82
|
+
return this.request(`/v1/chat/${encodeURIComponent(this.pin)}/${id}/messages${qs}`);
|
|
83
|
+
}
|
|
84
|
+
/** Crea una reserva real (tabla bookings). */
|
|
85
|
+
createBooking(input) {
|
|
86
|
+
return this.request(`/v1/bookings/${encodeURIComponent(this.pin)}`, { method: "POST", body: JSON.stringify({ ...input, conversationId: input.conversationId ?? this.conversationId }) });
|
|
87
|
+
}
|
|
88
|
+
/** Crea un pedido real (tabla orders). */
|
|
89
|
+
createOrder(input) {
|
|
90
|
+
return this.request(`/v1/orders/${encodeURIComponent(this.pin)}`, { method: "POST", body: JSON.stringify({ ...input, conversationId: input.conversationId ?? this.conversationId }) });
|
|
91
|
+
}
|
|
92
|
+
/** Resetea el hilo local (la próxima chat() abre conversación nueva). */
|
|
93
|
+
reset() {
|
|
94
|
+
this.conversationId = null;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
export default OniPinClient;
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "onipin-js",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "SDK oficial de OniPin — protocolo /v1, chat, catálogo, reservas y pedidos",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": ["dist", "README.md"],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc -p tsconfig.json",
|
|
17
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
18
|
+
"prepublishOnly": "npm run build"
|
|
19
|
+
},
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=18"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"keywords": ["onipin", "mcp", "ai", "chat", "sdk", "onnivers"],
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "https://github.com/deivys1224-ctrl/oniping.git",
|
|
28
|
+
"directory": "packages/onipin-js"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://onnivers.store/docs#sdk",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/deivys1224-ctrl/oniping/issues"
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
}
|
|
37
|
+
}
|