snapshot of https://github.com/mountain-loop/yaak.git (main, 2026-09-23; blobs > 131072B omitted)
This commit is contained in:
9
crates/yaak-sse/Cargo.toml
Normal file
9
crates/yaak-sse/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "yaak-sse"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
ts-rs = { workspace = true, features = ["serde-json-impl"] }
|
||||
3
crates/yaak-sse/bindings/sse.ts
generated
Normal file
3
crates/yaak-sse/bindings/sse.ts
generated
Normal file
@@ -0,0 +1,3 @@
|
||||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
||||
|
||||
export type ServerSentEvent = { eventType: string, data: string, id: string | null, retry: bigint | null, };
|
||||
2
crates/yaak-sse/index.ts
Normal file
2
crates/yaak-sse/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./bindings/sse";
|
||||
export * from "./summary";
|
||||
12
crates/yaak-sse/package.json
Normal file
12
crates/yaak-sse/package.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "@yaakapp-internal/sse",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"jsonpath-plus": "^10.3.0"
|
||||
},
|
||||
"main": "index.ts",
|
||||
"scripts": {
|
||||
"test": "vitest run"
|
||||
}
|
||||
}
|
||||
1
crates/yaak-sse/src/lib.rs
Normal file
1
crates/yaak-sse/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod sse;
|
||||
12
crates/yaak-sse/src/sse.rs
Normal file
12
crates/yaak-sse/src/sse.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use ts_rs::TS;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default, TS)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
#[ts(export, export_to = "sse.ts")]
|
||||
pub struct ServerSentEvent {
|
||||
pub event_type: String,
|
||||
pub data: String,
|
||||
pub id: Option<String>,
|
||||
pub retry: Option<u64>,
|
||||
}
|
||||
51
crates/yaak-sse/summary.test.ts
Normal file
51
crates/yaak-sse/summary.test.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { computeSseSummary, extractSseValueAtPath } from "./summary";
|
||||
|
||||
describe("extractSseValueAtPath", () => {
|
||||
it("supports simple paths", () => {
|
||||
expect(
|
||||
extractSseValueAtPath(
|
||||
JSON.stringify({ choices: [{ delta: { content: "hello" } }] }),
|
||||
"$.choices[0].delta.content",
|
||||
),
|
||||
).toBe("hello");
|
||||
});
|
||||
|
||||
it("supports full JSONPath expressions", () => {
|
||||
expect(
|
||||
extractSseValueAtPath(
|
||||
JSON.stringify({
|
||||
choices: [
|
||||
{ delta: { role: "assistant" } },
|
||||
{ delta: { content: "hello" } },
|
||||
{ delta: { content: " world" } },
|
||||
],
|
||||
}),
|
||||
"$.choices[*].delta.content",
|
||||
),
|
||||
).toBe("hello world");
|
||||
});
|
||||
|
||||
it("returns null when a JSONPath expression has no matches", () => {
|
||||
expect(extractSseValueAtPath(JSON.stringify({ delta: {} }), "$.delta.text")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("computeSseSummary", () => {
|
||||
it("concatenates JSONPath matches across SSE messages", () => {
|
||||
expect(
|
||||
computeSseSummary(
|
||||
[
|
||||
`data: ${JSON.stringify({ choices: [{ delta: { content: "hello" } }] })}`,
|
||||
"",
|
||||
`data: ${JSON.stringify({ choices: [{ delta: { content: " world" } }] })}`,
|
||||
"",
|
||||
].join("\n"),
|
||||
"$.choices[*].delta.content",
|
||||
),
|
||||
).toEqual({
|
||||
fragmentCount: 2,
|
||||
summary: "hello world",
|
||||
});
|
||||
});
|
||||
});
|
||||
131
crates/yaak-sse/summary.ts
Normal file
131
crates/yaak-sse/summary.ts
Normal file
@@ -0,0 +1,131 @@
|
||||
import { JSONPath } from "jsonpath-plus";
|
||||
|
||||
export interface SseSummary {
|
||||
fragmentCount: number;
|
||||
summary: string;
|
||||
}
|
||||
|
||||
type JSONPathJson = null | boolean | number | string | object | unknown[];
|
||||
|
||||
const STANDARD_SSE_FIELD = /^(event|id|retry):/i;
|
||||
|
||||
export function candidateJsonPayloadsFromSseText(text: string): string[] {
|
||||
const normalized = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
||||
const blocks = normalized.split(/\n{2,}/);
|
||||
const candidates: string[] = [];
|
||||
|
||||
for (const block of blocks) {
|
||||
const lines = block.split("\n");
|
||||
const dataLines = lines
|
||||
.map((line) => {
|
||||
const match = /^data:(?: ?)(.*)$/.exec(line);
|
||||
return match?.[1];
|
||||
})
|
||||
.filter((line): line is string => line != null);
|
||||
|
||||
if (dataLines.length > 0) {
|
||||
const payload = dataLines.join("\n").trim();
|
||||
if (payload) {
|
||||
candidates.push(payload);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
const trimmedBlock = block.trim();
|
||||
if (!trimmedBlock) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isParsableJson(trimmedBlock)) {
|
||||
candidates.push(trimmedBlock);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const line of lines) {
|
||||
const trimmedLine = line.trim();
|
||||
if (
|
||||
!trimmedLine ||
|
||||
trimmedLine.startsWith(":") ||
|
||||
STANDARD_SSE_FIELD.test(trimmedLine) ||
|
||||
!isParsableJson(trimmedLine)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
candidates.push(trimmedLine);
|
||||
}
|
||||
}
|
||||
|
||||
return candidates;
|
||||
}
|
||||
|
||||
export function computeSseSummary(text: string, keyPath: string): SseSummary {
|
||||
const fragments: string[] = [];
|
||||
|
||||
for (const payload of candidateJsonPayloadsFromSseText(text)) {
|
||||
const fragment = extractSseValueAtPath(payload, keyPath);
|
||||
if (fragment != null) {
|
||||
fragments.push(fragment);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
fragmentCount: fragments.length,
|
||||
summary: fragments.join(""),
|
||||
};
|
||||
}
|
||||
|
||||
export function extractSseValueAtPath(payload: string, keyPath: string): string | null {
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = JSON.parse(payload);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
const path = keyPath.trim();
|
||||
if (!path) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let result: unknown;
|
||||
try {
|
||||
result = JSONPath({ path, json: parsed as JSONPathJson });
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Array.isArray(result)) {
|
||||
const fragments = result
|
||||
.map((item) => stringifySummaryValue(item))
|
||||
.filter((item): item is string => item != null);
|
||||
return fragments.length > 0 ? fragments.join("") : null;
|
||||
}
|
||||
|
||||
return stringifySummaryValue(result);
|
||||
}
|
||||
|
||||
function stringifySummaryValue(value: unknown): string | null {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
if (typeof value === "string") {
|
||||
return value;
|
||||
}
|
||||
if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
|
||||
return String(value);
|
||||
}
|
||||
try {
|
||||
return JSON.stringify(value);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function isParsableJson(value: string): boolean {
|
||||
try {
|
||||
JSON.parse(value);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user