base con front
This commit is contained in:
24
public/lib/api.js
Normal file
24
public/lib/api.js
Normal file
@@ -0,0 +1,24 @@
|
||||
export const api = {
|
||||
async conversations({ q = "", status = "", state = "" } = {}) {
|
||||
const u = new URL("/conversations", location.origin);
|
||||
if (q) u.searchParams.set("q", q);
|
||||
if (status) u.searchParams.set("status", status);
|
||||
if (state) u.searchParams.set("state", state);
|
||||
return fetch(u).then(r => r.json());
|
||||
},
|
||||
|
||||
async runs({ chat_id, limit = 1 } = {}) {
|
||||
const u = new URL("/runs", location.origin);
|
||||
if (chat_id) u.searchParams.set("chat_id", chat_id);
|
||||
u.searchParams.set("limit", String(limit));
|
||||
return fetch(u).then(r => r.json());
|
||||
},
|
||||
|
||||
async simSend({ chat_id, from_phone, text }) {
|
||||
return fetch("/sim/send", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ chat_id, from_phone, text }),
|
||||
}).then(r => r.json());
|
||||
},
|
||||
};
|
||||
17
public/lib/bus.js
Normal file
17
public/lib/bus.js
Normal file
@@ -0,0 +1,17 @@
|
||||
const listeners = new Map();
|
||||
|
||||
export function on(event, fn) {
|
||||
const arr = listeners.get(event) || [];
|
||||
arr.push(fn);
|
||||
listeners.set(event, arr);
|
||||
return () => off(event, fn);
|
||||
}
|
||||
|
||||
export function off(event, fn) {
|
||||
const arr = listeners.get(event) || [];
|
||||
listeners.set(event, arr.filter(x => x !== fn));
|
||||
}
|
||||
|
||||
export function emit(event, payload) {
|
||||
(listeners.get(event) || []).forEach(fn => fn(payload));
|
||||
}
|
||||
13
public/lib/sse.js
Normal file
13
public/lib/sse.js
Normal file
@@ -0,0 +1,13 @@
|
||||
import { emit } from "./bus.js";
|
||||
|
||||
export function connectSSE() {
|
||||
const es = new EventSource("/stream");
|
||||
|
||||
es.addEventListener("hello", () => emit("sse:status", { ok: true }));
|
||||
es.addEventListener("conversation.upsert", (e) => emit("conversation:upsert", JSON.parse(e.data)));
|
||||
es.addEventListener("run.created", (e) => emit("run:created", JSON.parse(e.data)));
|
||||
|
||||
es.onerror = () => emit("sse:status", { ok: false });
|
||||
|
||||
return es;
|
||||
}
|
||||
Reference in New Issue
Block a user