Skip to content

LayerScale Client

The LayerScale TypeScript client provides native access to sessions, streaming data ingestion, and Flash Queries. Use it for real-time streaming workloads where you need persistent sessions and continuous data processing.

For standard chat completions and tool calling, you can use the OpenAI or Anthropic SDKs instead.

Installation

Terminal window
npm install @layerscale/layerscale

Create a Session

import { LayerScale } from "@layerscale/layerscale";
const client = new LayerScale("http://localhost:8080");
const session = await client.createSession({
type: "ohlcv",
prompt: "You are a financial analyst. Analyze incoming market data.",
flash: [
{ query: "Is the trend bullish or bearish?", max_tokens: 4 },
{ query: "What is the current support level?", max_tokens: 16 },
],
});
console.log(`Session created: ${session.session_id}`);

The type parameter is required and determines the data format for the session. Supported types: ohlcv, iot, spatial, event, vitals.

Push Streaming Data

Push data points into the session. The endpoint is non-blocking: data is queued and processed in the background.

// Push OHLCV data
await client.push(session.session_id, [
{ o: 185.5, h: 186.2, l: 185.1, c: 185.8, v: 12500 },
{ o: 185.8, h: 186.5, l: 185.6, c: 186.3, v: 8700 },
]);
// Push IoT sensor data (on an "iot" session)
await client.push(session.session_id, [
{ sid: "temp-01", val: 72.5, lo: 60.0, hi: 85.0 },
]);
// Push vitals data (on a "vitals" session)
await client.push(session.session_id, [
{ hr: 72.0, bp_s: 120.0, bp_d: 80.0, spo2: 98.5, temp: 36.8 },
]);

Query the Session

Generate a response based on all the data the session has seen:

// Blocking query
const result = await client.generate(session.session_id, {
prompt: "What's the trend over the last hour?",
max_tokens: 200,
});
console.log(result.text);
// Streaming query
for await (const chunk of client.generateStream(session.session_id, {
prompt: "Describe the price action.",
max_tokens: 300,
})) {
if (chunk.token) process.stdout.write(chunk.token);
}

Receive Flash Query Results

SSE (Server-Sent Events)

for await (const event of client.events(session.session_id)) {
if (event.type === "flash_ready") {
console.log(`Query ${event.id}: ${event.value} (confidence: ${event.confidence})`);
}
}

WebSocket

The client supports WebSocket for combined push and event delivery in a single connection:

const socket = client.stream(session.session_id);
socket.on("connected", (data) => {
console.log(`Connected: ${data.session_id}`);
});
// Push data
socket.push([{ o: 185.5, h: 186.2, l: 185.1, c: 185.8, v: 12500 }]);
// Listen for flash query results
socket.on("flash_ready", (event) => {
console.log(`${event.query}: ${event.value}`);
});
socket.on("error", (err) => {
console.error(`Error: ${err.message}`);
});

Stream Status and Stats

// Check streaming processor status
const status = await client.streamStatus(session.session_id);
console.log(`Queue: ${status.streaming.queue_size}/${status.streaming.queue_capacity}`);
console.log(`Processed: ${status.statistics.items_processed} items`);
// Get computed stats for the session's data
const stats = await client.stats(session.session_id);
console.log(stats);

Delete a Session

await client.deleteSession(session.session_id);

Full Example

import { LayerScale } from "@layerscale/layerscale";
const client = new LayerScale("http://localhost:8080");
const session = await client.createSession({
type: "ohlcv",
prompt: "Monitor ETH/USD for breakout signals.",
flash: [
{ query: "Is a breakout imminent?", max_tokens: 8 },
{ query: "What's the key resistance level?", max_tokens: 16 },
],
});
// Connect via WebSocket for combined push + events
const socket = client.stream(session.session_id);
socket.on("flash_ready", (event) => {
console.log(`[FLASH] ${event.query}: ${event.value} (${event.confidence})`);
});
socket.on("open", () => {
// Push data from your feed
setInterval(() => {
const candle = getLatestCandle();
socket.push([{
o: candle.open,
h: candle.high,
l: candle.low,
c: candle.close,
v: candle.volume,
}]);
}, 1000);
});

When to Use Which Client

Use CaseRecommended Client
Chat completions, tool callingOpenAI SDK or Anthropic SDK
Streaming data ingestionLayerScale client
Flash QueriesLayerScale client
Session managementLayerScale client
WebSocket connectionsLayerScale client
Quick prototypingOpenAI SDK (most familiar to developers)