Archived
Add paged chat history loading
This commit is contained in:
@@ -2,9 +2,9 @@ import { homedir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
export function sessiondSocketPath(): string {
|
||||
return process.env.PI_WEB_SESSIOND_SOCKET ?? join(homedir(), ".pi-web", "sessiond.sock");
|
||||
return process.env["PI_WEB_SESSIOND_SOCKET"] ?? join(homedir(), ".pi-web", "sessiond.sock");
|
||||
}
|
||||
|
||||
export function sessiondHttpUrl(): string | undefined {
|
||||
return process.env.PI_WEB_SESSIOND_URL;
|
||||
return process.env["PI_WEB_SESSIOND_URL"];
|
||||
}
|
||||
|
||||
@@ -8,12 +8,12 @@ export class SessionDaemonClient {
|
||||
|
||||
async request(method: string, path: string, body?: unknown): Promise<{ statusCode: number; headers: Record<string, string>; body: string }> {
|
||||
const payload = body === undefined ? undefined : JSON.stringify(body);
|
||||
if (this.baseUrl) return this.requestUrl(method, path, payload);
|
||||
if (this.baseUrl !== undefined && this.baseUrl !== "") return this.requestUrl(method, path, payload);
|
||||
return this.requestSocket(method, path, payload);
|
||||
}
|
||||
|
||||
connectWebSocket(path: string): WebSocket {
|
||||
if (this.baseUrl) {
|
||||
if (this.baseUrl !== undefined && this.baseUrl !== "") {
|
||||
const url = new URL(path, this.baseUrl);
|
||||
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
||||
return new WebSocket(url);
|
||||
@@ -22,11 +22,12 @@ export class SessionDaemonClient {
|
||||
}
|
||||
|
||||
private async requestUrl(method: string, path: string, payload?: string) {
|
||||
const response = await fetch(new URL(path, this.baseUrl), {
|
||||
method,
|
||||
headers: payload ? { "content-type": "application/json" } : undefined,
|
||||
body: payload,
|
||||
});
|
||||
const init: RequestInit = { method };
|
||||
if (payload !== undefined && payload !== "") {
|
||||
init.headers = { "content-type": "application/json" };
|
||||
init.body = payload;
|
||||
}
|
||||
const response = await fetch(new URL(path, this.baseUrl), init);
|
||||
return {
|
||||
statusCode: response.status,
|
||||
headers: Object.fromEntries(response.headers.entries()),
|
||||
@@ -41,13 +42,15 @@ export class SessionDaemonClient {
|
||||
socketPath: this.socketPath,
|
||||
path,
|
||||
method,
|
||||
headers: payload
|
||||
headers: payload !== undefined && payload !== ""
|
||||
? { "content-type": "application/json", "content-length": Buffer.byteLength(payload) }
|
||||
: undefined,
|
||||
},
|
||||
(response) => {
|
||||
const chunks: Buffer[] = [];
|
||||
response.on("data", (chunk) => chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)));
|
||||
const chunks: Uint8Array[] = [];
|
||||
response.on("data", (chunk: Buffer | string) => {
|
||||
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
||||
});
|
||||
response.on("end", () => {
|
||||
resolve({
|
||||
statusCode: response.statusCode ?? 500,
|
||||
@@ -58,7 +61,7 @@ export class SessionDaemonClient {
|
||||
},
|
||||
);
|
||||
request.on("error", reject);
|
||||
if (payload) request.write(payload);
|
||||
if (payload !== undefined && payload !== "") request.write(payload);
|
||||
request.end();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,15 +2,17 @@ import type { FastifyInstance, FastifyReply } from "fastify";
|
||||
import { WebSocket, type RawData } from "ws";
|
||||
import { SessionDaemonClient } from "./sessionDaemonClient.js";
|
||||
|
||||
export async function registerSessionProxyRoutes(app: FastifyInstance, daemon = new SessionDaemonClient()): Promise<void> {
|
||||
export function registerSessionProxyRoutes(app: FastifyInstance, daemon = new SessionDaemonClient()): void {
|
||||
const proxy = async (request: { method: string; url: string; body?: unknown }, reply: FastifyReply) => {
|
||||
try {
|
||||
const upstream = await daemon.request(request.method, stripApiPrefix(request.url), request.body);
|
||||
reply.code(upstream.statusCode);
|
||||
if (upstream.headers["content-type"]) reply.header("content-type", upstream.headers["content-type"]);
|
||||
return upstream.body ? JSON.parse(upstream.body) : undefined;
|
||||
const contentType = upstream.headers["content-type"];
|
||||
if (contentType !== undefined && contentType !== "") reply.header("content-type", contentType);
|
||||
return upstream.body !== "" ? parseJson(upstream.body) : undefined;
|
||||
} catch (error) {
|
||||
requestFailed(reply, error);
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -36,22 +38,30 @@ export async function registerSessionProxyRoutes(app: FastifyInstance, daemon =
|
||||
}
|
||||
|
||||
function stripApiPrefix(url: string): string {
|
||||
return url.startsWith("/api") ? url.slice(4) || "/" : url;
|
||||
const stripped = url.startsWith("/api") ? url.slice(4) : url;
|
||||
return stripped === "" ? "/" : stripped;
|
||||
}
|
||||
|
||||
function requestFailed(reply: FastifyReply, error: unknown) {
|
||||
function parseJson(text: string): unknown {
|
||||
const value: unknown = JSON.parse(text);
|
||||
return value;
|
||||
}
|
||||
|
||||
function requestFailed(reply: FastifyReply, error: unknown): void {
|
||||
reply.code(502).send({ error: `Session daemon unavailable: ${error instanceof Error ? error.message : String(error)}` });
|
||||
}
|
||||
|
||||
function bridgeSockets(client: WebSocket, upstream: WebSocket): void {
|
||||
client.on("message", (data) => sendIfOpen(upstream, data));
|
||||
upstream.on("message", (data) => sendIfOpen(client, data));
|
||||
client.on("close", () => upstream.close());
|
||||
upstream.on("close", () => client.close());
|
||||
upstream.on("error", () => client.close());
|
||||
client.on("error", () => upstream.close());
|
||||
client.on("message", (data) => { sendIfOpen(upstream, data); });
|
||||
upstream.on("message", (data) => { sendIfOpen(client, data); });
|
||||
client.on("close", () => { upstream.close(); });
|
||||
upstream.on("close", () => { client.close(); });
|
||||
upstream.on("error", () => { client.close(); });
|
||||
client.on("error", () => { upstream.close(); });
|
||||
}
|
||||
|
||||
function sendIfOpen(socket: WebSocket, data: RawData): void {
|
||||
if (socket.readyState === WebSocket.OPEN) socket.send(data);
|
||||
if (socket.readyState === WebSocket.OPEN) {
|
||||
socket.send(data);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user