fascicle 0.12.0 → 0.12.3
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/CHANGELOG.md +148 -32
- package/README.md +25 -15
- package/dist/{adapters-hmnJOOgf.js → adapters-CutexJd3.js} +12 -12
- package/dist/adapters-CutexJd3.js.map +1 -0
- package/dist/adapters.d.ts +10 -10
- package/dist/adapters.js +1 -1
- package/dist/agents.d.ts +22 -3
- package/dist/agents.js +74 -24
- package/dist/agents.js.map +1 -1
- package/dist/bin/fascicle-viewer.js +2 -2
- package/dist/{core-BkB2l1-e.js → core-DdJEhyQc.js} +314 -501
- package/dist/core-DdJEhyQc.js.map +1 -0
- package/dist/engine-J4lIxpw5.js +7000 -0
- package/dist/engine-J4lIxpw5.js.map +1 -0
- package/dist/index.d.ts +237 -178
- package/dist/index.js +617 -7771
- package/dist/index.js.map +1 -1
- package/dist/{invoke-JkzNf7-Y.js → invoke-CH_eJZeS.js} +8 -8
- package/dist/{invoke-JkzNf7-Y.js.map → invoke-CH_eJZeS.js.map} +1 -1
- package/dist/{leading_system-BVtGMweY.js → leading_system-BUCT-PwD.js} +31 -8
- package/dist/leading_system-BUCT-PwD.js.map +1 -0
- package/dist/mcp.d.ts +5 -5
- package/dist/mcp.js +10 -9
- package/dist/mcp.js.map +1 -1
- package/dist/otel.d.ts +1 -1
- package/dist/otel.js +4 -4
- package/dist/otel.js.map +1 -1
- package/dist/static/viewer.html +2 -1
- package/dist/stdio.d.ts +2 -2
- package/dist/stdio.js +2 -2
- package/dist/testing.d.ts +66 -15
- package/dist/testing.js +200 -34
- package/dist/testing.js.map +1 -1
- package/dist/trajectory-ByoOQZ6T.d.ts +114 -0
- package/dist/trajectory-D1eq9eue.js +81 -0
- package/dist/trajectory-D1eq9eue.js.map +1 -0
- package/dist/{types-BCtKsxrX.d.ts → types-BK0UW28o.d.ts} +3 -3
- package/dist/{types-CVdTBXE_.d.ts → types-DBpU27sQ.d.ts} +20 -3
- package/dist/{types-4l54cexm.d.ts → types-DU6cvoA4.d.ts} +9 -5
- package/dist/ui.d.ts +2 -2
- package/dist/ui.js +1 -1
- package/dist/ui.js.map +1 -1
- package/dist/viewer.d.ts +117 -0
- package/dist/viewer.js +659 -0
- package/dist/viewer.js.map +1 -0
- package/package.json +26 -12
- package/dist/adapters-hmnJOOgf.js.map +0 -1
- package/dist/core-BkB2l1-e.js.map +0 -1
- package/dist/leading_system-BVtGMweY.js.map +0 -1
package/dist/ui.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ServerResponse } from "node:http";
|
|
1
|
+
import { u as TrajectoryEvent } from "./types-DBpU27sQ.js";
|
|
3
2
|
import { UIMessageChunk } from "ai";
|
|
3
|
+
import { ServerResponse } from "node:http";
|
|
4
4
|
|
|
5
5
|
//#region src/ui/to_ui_message_stream.d.ts
|
|
6
6
|
/**
|
package/dist/ui.js
CHANGED
|
@@ -6,7 +6,7 @@ import { createUIMessageStream, createUIMessageStreamResponse, pipeUIMessageStre
|
|
|
6
6
|
* `run.stream(flow, input)` yields `TrajectoryEvent`s; under streaming,
|
|
7
7
|
* `model_call` records `model_chunk` events carrying an engine `StreamChunk`.
|
|
8
8
|
* This module maps those chunks onto the AI SDK `UIMessageChunk` protocol so a
|
|
9
|
-
*
|
|
9
|
+
* Fascicle flow can back a `useChat` endpoint rendered by AI Elements /
|
|
10
10
|
* Streamdown, with zero bespoke glue.
|
|
11
11
|
*
|
|
12
12
|
* It is the outbound inverse of the engine's inbound provider->StreamChunk
|
package/dist/ui.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ui.js","names":[],"sources":["../src/ui/to_ui_message_stream.ts"],"sourcesContent":["/**\n * fascicle/ui: bridge a run's event stream to the AI SDK \"UI message stream\".\n *\n * `run.stream(flow, input)` yields `TrajectoryEvent`s; under streaming,\n * `model_call` records `model_chunk` events carrying an engine `StreamChunk`.\n * This module maps those chunks onto the AI SDK `UIMessageChunk` protocol so a\n * fascicle flow can back a `useChat` endpoint rendered by AI Elements /\n * Streamdown, with zero bespoke glue.\n *\n * It is the outbound inverse of the engine's inbound provider->StreamChunk\n * mapping. The wire framing (SSE, `data:` lines, terminal `[DONE]`, the\n * `x-vercel-ai-ui-message-stream` header) is owned by the `ai` builders; this\n * file only translates chunk shapes and manages open text/reasoning blocks.\n *\n * v7's `file`/`reasoning-file` UI parts are never emitted: the engine chunk\n * vocabulary has no generated-file kind (those provider parts drop at the\n * inbound mapping), so there is nothing here to translate them from.\n */\n\nimport {\n createUIMessageStream,\n createUIMessageStreamResponse,\n pipeUIMessageStreamToResponse,\n} from 'ai'\nimport type { UIMessageChunk } from 'ai'\nimport type { ServerResponse } from 'node:http'\nimport type { TrajectoryEvent } from '#core'\nimport type { StreamChunk } from '#engine'\n\n/**\n * The structural shape of a `run.stream(...)` handle. Typed structurally rather\n * than importing the core `StreamingRunHandle` so this module depends on no\n * core value and a caller can pass `run.stream(...)` directly.\n */\nexport type RunStreamLike = {\n readonly events: AsyncIterable<TrajectoryEvent>\n readonly result: Promise<unknown>\n}\n\nexport type ToUiStreamOptions = {\n /** Map a thrown run error to the text of an `error` part. */\n readonly on_error?: (error: unknown) => string\n}\n\n/**\n * Per-stream state: which text/reasoning blocks are open (so `*-start` fires\n * once and `*-end` closes them) and the tool name for each in-flight call (so\n * `tool-input-available` can echo it after only the id arrives at close).\n */\nexport type UiMapperState = {\n readonly open_text: Set<number>\n readonly open_reasoning: Set<number>\n readonly tool_names: Map<string, string>\n}\n\n/**\n * Create an empty `UiMapperState` for a new stream.\n */\nexport function create_ui_mapper_state(): UiMapperState {\n return { open_text: new Set(), open_reasoning: new Set(), tool_names: new Map() }\n}\n\n/**\n * Build the UI message chunk id for a step's text block.\n */\nfunction text_id(step_index: number): string {\n return `text-${step_index}`\n}\n\n/**\n * Build the UI message chunk id for a step's reasoning block.\n */\nfunction reasoning_id(step_index: number): string {\n return `reasoning-${step_index}`\n}\n\n/**\n * Close the text/reasoning blocks open for one step, returning the\n * `*-end` chunks needed and updating `state` in place.\n *\n * Called on a `step_finish` chunk, so only that step's blocks close; blocks\n * open for other steps are untouched.\n */\nfunction close_step_blocks(step_index: number, state: UiMapperState): UIMessageChunk[] {\n const parts: UIMessageChunk[] = []\n if (state.open_text.delete(step_index)) {\n parts.push({ type: 'text-end', id: text_id(step_index) })\n }\n if (state.open_reasoning.delete(step_index)) {\n parts.push({ type: 'reasoning-end', id: reasoning_id(step_index) })\n }\n return parts\n}\n\n/**\n * Close every text/reasoning block still open in `state`, returning the\n * `*-end` chunks needed.\n *\n * Used on stream `finish` and to flush any blocks still open when the\n * underlying event iterable ends.\n */\nexport function close_open_blocks(state: UiMapperState): UIMessageChunk[] {\n const parts: UIMessageChunk[] = []\n for (const step_index of state.open_text) {\n parts.push({ type: 'text-end', id: text_id(step_index) })\n }\n for (const step_index of state.open_reasoning) {\n parts.push({ type: 'reasoning-end', id: reasoning_id(step_index) })\n }\n state.open_text.clear()\n state.open_reasoning.clear()\n return parts\n}\n\n/**\n * Map a single engine `StreamChunk` to the `UIMessageChunk`s it produces,\n * opening and closing text/reasoning blocks in `state` as needed.\n */\nfunction map_chunk(chunk: StreamChunk, state: UiMapperState): UIMessageChunk[] {\n switch (chunk.kind) {\n case 'text': {\n const id = text_id(chunk.step_index)\n const parts: UIMessageChunk[] = []\n if (!state.open_text.has(chunk.step_index)) {\n state.open_text.add(chunk.step_index)\n parts.push({ type: 'text-start', id })\n }\n parts.push({ type: 'text-delta', id, delta: chunk.text })\n return parts\n }\n case 'reasoning': {\n const id = reasoning_id(chunk.step_index)\n const parts: UIMessageChunk[] = []\n if (!state.open_reasoning.has(chunk.step_index)) {\n state.open_reasoning.add(chunk.step_index)\n parts.push({ type: 'reasoning-start', id })\n }\n parts.push({ type: 'reasoning-delta', id, delta: chunk.text })\n return parts\n }\n case 'tool_call_start': {\n state.tool_names.set(chunk.id, chunk.name)\n return [{ type: 'tool-input-start', toolCallId: chunk.id, toolName: chunk.name }]\n }\n case 'tool_call_input_delta':\n return [{ type: 'tool-input-delta', toolCallId: chunk.id, inputTextDelta: chunk.delta }]\n case 'tool_call_end':\n return [\n {\n type: 'tool-input-available',\n toolCallId: chunk.id,\n toolName: state.tool_names.get(chunk.id) ?? '',\n input: chunk.input,\n },\n ]\n case 'tool_result':\n return chunk.error !== undefined\n ? [{ type: 'tool-output-error', toolCallId: chunk.id, errorText: chunk.error.message }]\n : [{ type: 'tool-output-available', toolCallId: chunk.id, output: chunk.output }]\n case 'step_finish':\n return close_step_blocks(chunk.step_index, state)\n case 'finish':\n return close_open_blocks(state)\n default:\n return []\n }\n}\n\n/**\n * Narrow an untyped `TrajectoryEvent['chunk']` payload to a `StreamChunk`\n * when it looks like one (an object with a string `kind`), otherwise\n * return `undefined`.\n */\nfunction as_stream_chunk(value: unknown): StreamChunk | undefined {\n if (typeof value !== 'object' || value === null) return undefined\n const kind = Reflect.get(value, 'kind')\n if (typeof kind !== 'string') return undefined\n // The value rode a loose TrajectoryEvent bag; model_call only records real\n // StreamChunks under `chunk`, and map_chunk narrows on `kind`, so treating it\n // as a StreamChunk is sound. Unknown kinds fall through to `[]`.\n // oxlint-disable-next-line typescript/no-unsafe-type-assertion\n return value as StreamChunk\n}\n\n/**\n * Map one run event to zero or more UI message chunks, advancing `state`.\n * Non-`model_chunk` events (spans, cost, user emits) yield nothing.\n */\nexport function to_ui_message_chunks(\n event: TrajectoryEvent,\n state: UiMapperState,\n): UIMessageChunk[] {\n if (event.kind !== 'model_chunk') return []\n const chunk = as_stream_chunk(event['chunk'])\n return chunk === undefined ? [] : map_chunk(chunk, state)\n}\n\n/**\n * Build the `ReadableStream<UIMessageChunk>` that drives a `run.stream(...)`\n * handle through `to_ui_message_chunks`, used by both the `Response` and\n * Node `ServerResponse` entry points below.\n */\nfunction build_stream(\n handle: RunStreamLike,\n options: ToUiStreamOptions,\n): ReadableStream<UIMessageChunk> {\n const on_error = options.on_error\n return createUIMessageStream({\n execute: async ({ writer }) => {\n const state = create_ui_mapper_state()\n for await (const event of handle.events) {\n for (const part of to_ui_message_chunks(event, state)) writer.write(part)\n }\n for (const part of close_open_blocks(state)) writer.write(part)\n await handle.result\n },\n ...(on_error !== undefined ? { onError: on_error } : {}),\n })\n}\n\n/**\n * Turn a `run.stream(...)` handle into an SSE `Response` a `useChat` endpoint\n * can return directly.\n */\nexport function to_ui_message_response(\n handle: RunStreamLike,\n options: ToUiStreamOptions = {},\n): Response {\n return createUIMessageStreamResponse({ stream: build_stream(handle, options) })\n}\n\n/**\n * Pipe a `run.stream(...)` handle to a Node `http.ServerResponse` as SSE, for\n * `node:http` servers that hold a `ServerResponse` rather than returning a\n * web `Response`.\n */\nexport function pipe_ui_message_stream_to_response(\n handle: RunStreamLike,\n response: ServerResponse,\n options: ToUiStreamOptions = {},\n): void {\n pipeUIMessageStreamToResponse({ response, stream: build_stream(handle, options) })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA0DA,SAAgB,yBAAwC;CACtD,OAAO;EAAE,2BAAW,IAAI,IAAI;EAAG,gCAAgB,IAAI,IAAI;EAAG,4BAAY,IAAI,IAAI;CAAE;AAClF;;;;AAKA,SAAS,QAAQ,YAA4B;CAC3C,OAAO,QAAQ;AACjB;;;;AAKA,SAAS,aAAa,YAA4B;CAChD,OAAO,aAAa;AACtB;;;;;;;;AASA,SAAS,kBAAkB,YAAoB,OAAwC;CACrF,MAAM,QAA0B,CAAC;CACjC,IAAI,MAAM,UAAU,OAAO,UAAU,GACnC,MAAM,KAAK;EAAE,MAAM;EAAY,IAAI,QAAQ,UAAU;CAAE,CAAC;CAE1D,IAAI,MAAM,eAAe,OAAO,UAAU,GACxC,MAAM,KAAK;EAAE,MAAM;EAAiB,IAAI,aAAa,UAAU;CAAE,CAAC;CAEpE,OAAO;AACT;;;;;;;;AASA,SAAgB,kBAAkB,OAAwC;CACxE,MAAM,QAA0B,CAAC;CACjC,KAAK,MAAM,cAAc,MAAM,WA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E,CAAC;AAChF;;;;;;AAOA,SAAgB,mCACd,QACA,UACA,UAA6B,CAAC,GACxB;CACN,8BAA8B;EAAE;EAAU,QAAQ,aAAa,QAAQ,OAAO;CAAE,CAAC;AACnF"}
|
|
1
|
+
{"version":3,"file":"ui.js","names":[],"sources":["../src/ui/to_ui_message_stream.ts"],"sourcesContent":["/**\n * fascicle/ui: bridge a run's event stream to the AI SDK \"UI message stream\".\n *\n * `run.stream(flow, input)` yields `TrajectoryEvent`s; under streaming,\n * `model_call` records `model_chunk` events carrying an engine `StreamChunk`.\n * This module maps those chunks onto the AI SDK `UIMessageChunk` protocol so a\n * Fascicle flow can back a `useChat` endpoint rendered by AI Elements /\n * Streamdown, with zero bespoke glue.\n *\n * It is the outbound inverse of the engine's inbound provider->StreamChunk\n * mapping. The wire framing (SSE, `data:` lines, terminal `[DONE]`, the\n * `x-vercel-ai-ui-message-stream` header) is owned by the `ai` builders; this\n * file only translates chunk shapes and manages open text/reasoning blocks.\n *\n * v7's `file`/`reasoning-file` UI parts are never emitted: the engine chunk\n * vocabulary has no generated-file kind (those provider parts drop at the\n * inbound mapping), so there is nothing here to translate them from.\n */\n\nimport {\n createUIMessageStream,\n createUIMessageStreamResponse,\n pipeUIMessageStreamToResponse,\n} from 'ai'\nimport type { UIMessageChunk } from 'ai'\nimport type { ServerResponse } from 'node:http'\nimport type { TrajectoryEvent } from '#core'\nimport type { StreamChunk } from '#engine'\n\n/**\n * The structural shape of a `run.stream(...)` handle. Typed structurally rather\n * than importing the core `StreamingRunHandle` so this module depends on no\n * core value and a caller can pass `run.stream(...)` directly.\n */\nexport type RunStreamLike = {\n readonly events: AsyncIterable<TrajectoryEvent>\n readonly result: Promise<unknown>\n}\n\nexport type ToUiStreamOptions = {\n /** Map a thrown run error to the text of an `error` part. */\n readonly on_error?: (error: unknown) => string\n}\n\n/**\n * Per-stream state: which text/reasoning blocks are open (so `*-start` fires\n * once and `*-end` closes them) and the tool name for each in-flight call (so\n * `tool-input-available` can echo it after only the id arrives at close).\n */\nexport type UiMapperState = {\n readonly open_text: Set<number>\n readonly open_reasoning: Set<number>\n readonly tool_names: Map<string, string>\n}\n\n/**\n * Create an empty `UiMapperState` for a new stream.\n */\nexport function create_ui_mapper_state(): UiMapperState {\n return { open_text: new Set(), open_reasoning: new Set(), tool_names: new Map() }\n}\n\n/**\n * Build the UI message chunk id for a step's text block.\n */\nfunction text_id(step_index: number): string {\n return `text-${step_index}`\n}\n\n/**\n * Build the UI message chunk id for a step's reasoning block.\n */\nfunction reasoning_id(step_index: number): string {\n return `reasoning-${step_index}`\n}\n\n/**\n * Close the text/reasoning blocks open for one step, returning the\n * `*-end` chunks needed and updating `state` in place.\n *\n * Called on a `step_finish` chunk, so only that step's blocks close; blocks\n * open for other steps are untouched.\n */\nfunction close_step_blocks(step_index: number, state: UiMapperState): UIMessageChunk[] {\n const parts: UIMessageChunk[] = []\n if (state.open_text.delete(step_index)) {\n parts.push({ type: 'text-end', id: text_id(step_index) })\n }\n if (state.open_reasoning.delete(step_index)) {\n parts.push({ type: 'reasoning-end', id: reasoning_id(step_index) })\n }\n return parts\n}\n\n/**\n * Close every text/reasoning block still open in `state`, returning the\n * `*-end` chunks needed.\n *\n * Used on stream `finish` and to flush any blocks still open when the\n * underlying event iterable ends.\n */\nexport function close_open_blocks(state: UiMapperState): UIMessageChunk[] {\n const parts: UIMessageChunk[] = []\n for (const step_index of state.open_text) {\n parts.push({ type: 'text-end', id: text_id(step_index) })\n }\n for (const step_index of state.open_reasoning) {\n parts.push({ type: 'reasoning-end', id: reasoning_id(step_index) })\n }\n state.open_text.clear()\n state.open_reasoning.clear()\n return parts\n}\n\n/**\n * Map a single engine `StreamChunk` to the `UIMessageChunk`s it produces,\n * opening and closing text/reasoning blocks in `state` as needed.\n */\nfunction map_chunk(chunk: StreamChunk, state: UiMapperState): UIMessageChunk[] {\n switch (chunk.kind) {\n case 'text': {\n const id = text_id(chunk.step_index)\n const parts: UIMessageChunk[] = []\n if (!state.open_text.has(chunk.step_index)) {\n state.open_text.add(chunk.step_index)\n parts.push({ type: 'text-start', id })\n }\n parts.push({ type: 'text-delta', id, delta: chunk.text })\n return parts\n }\n case 'reasoning': {\n const id = reasoning_id(chunk.step_index)\n const parts: UIMessageChunk[] = []\n if (!state.open_reasoning.has(chunk.step_index)) {\n state.open_reasoning.add(chunk.step_index)\n parts.push({ type: 'reasoning-start', id })\n }\n parts.push({ type: 'reasoning-delta', id, delta: chunk.text })\n return parts\n }\n case 'tool_call_start': {\n state.tool_names.set(chunk.id, chunk.name)\n return [{ type: 'tool-input-start', toolCallId: chunk.id, toolName: chunk.name }]\n }\n case 'tool_call_input_delta':\n return [{ type: 'tool-input-delta', toolCallId: chunk.id, inputTextDelta: chunk.delta }]\n case 'tool_call_end':\n return [\n {\n type: 'tool-input-available',\n toolCallId: chunk.id,\n toolName: state.tool_names.get(chunk.id) ?? '',\n input: chunk.input,\n },\n ]\n case 'tool_result':\n return chunk.error !== undefined\n ? [{ type: 'tool-output-error', toolCallId: chunk.id, errorText: chunk.error.message }]\n : [{ type: 'tool-output-available', toolCallId: chunk.id, output: chunk.output }]\n case 'step_finish':\n return close_step_blocks(chunk.step_index, state)\n case 'finish':\n return close_open_blocks(state)\n default:\n return []\n }\n}\n\n/**\n * Narrow an untyped `TrajectoryEvent['chunk']` payload to a `StreamChunk`\n * when it looks like one (an object with a string `kind`), otherwise\n * return `undefined`.\n */\nfunction as_stream_chunk(value: unknown): StreamChunk | undefined {\n if (typeof value !== 'object' || value === null) return undefined\n const kind = Reflect.get(value, 'kind')\n if (typeof kind !== 'string') return undefined\n // The value rode a loose TrajectoryEvent bag; model_call only records real\n // StreamChunks under `chunk`, and map_chunk narrows on `kind`, so treating it\n // as a StreamChunk is sound. Unknown kinds fall through to `[]`.\n // oxlint-disable-next-line typescript/no-unsafe-type-assertion\n return value as StreamChunk\n}\n\n/**\n * Map one run event to zero or more UI message chunks, advancing `state`.\n * Non-`model_chunk` events (spans, cost, user emits) yield nothing.\n */\nexport function to_ui_message_chunks(\n event: TrajectoryEvent,\n state: UiMapperState,\n): UIMessageChunk[] {\n if (event.kind !== 'model_chunk') return []\n const chunk = as_stream_chunk(event['chunk'])\n return chunk === undefined ? [] : map_chunk(chunk, state)\n}\n\n/**\n * Build the `ReadableStream<UIMessageChunk>` that drives a `run.stream(...)`\n * handle through `to_ui_message_chunks`, used by both the `Response` and\n * Node `ServerResponse` entry points below.\n */\nfunction build_stream(\n handle: RunStreamLike,\n options: ToUiStreamOptions,\n): ReadableStream<UIMessageChunk> {\n const on_error = options.on_error\n return createUIMessageStream({\n execute: async ({ writer }) => {\n const state = create_ui_mapper_state()\n for await (const event of handle.events) {\n for (const part of to_ui_message_chunks(event, state)) writer.write(part)\n }\n for (const part of close_open_blocks(state)) writer.write(part)\n await handle.result\n },\n ...(on_error !== undefined ? { onError: on_error } : {}),\n })\n}\n\n/**\n * Turn a `run.stream(...)` handle into an SSE `Response` a `useChat` endpoint\n * can return directly.\n */\nexport function to_ui_message_response(\n handle: RunStreamLike,\n options: ToUiStreamOptions = {},\n): Response {\n return createUIMessageStreamResponse({ stream: build_stream(handle, options) })\n}\n\n/**\n * Pipe a `run.stream(...)` handle to a Node `http.ServerResponse` as SSE, for\n * `node:http` servers that hold a `ServerResponse` rather than returning a\n * web `Response`.\n */\nexport function pipe_ui_message_stream_to_response(\n handle: RunStreamLike,\n response: ServerResponse,\n options: ToUiStreamOptions = {},\n): void {\n pipeUIMessageStreamToResponse({ response, stream: build_stream(handle, options) })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA0DA,SAAgB,yBAAwC;CACtD,OAAO;EAAE,2BAAW,IAAI,IAAI;EAAG,gCAAgB,IAAI,IAAI;EAAG,4BAAY,IAAI,IAAI;CAAE;AAClF;;;;AAKA,SAAS,QAAQ,YAA4B;CAC3C,OAAO,QAAQ;AACjB;;;;AAKA,SAAS,aAAa,YAA4B;CAChD,OAAO,aAAa;AACtB;;;;;;;;AASA,SAAS,kBAAkB,Y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,MAAM,SAAS,OAAO,QAC/B,KAAK,MAAM,QAAQ,qBAAqB,OAAO,KAAK,GAAG,OAAO,MAAM,IAAI;GAE1E,KAAK,MAAM,QAAQ,kBAAkB,KAAK,GAAG,OAAO,MAAM,IAAI;GAC9D,MAAM,OAAO;EACf;EACA,GAAI,aAAa,KAAA,IAAY,EAAE,SAAS,SAAS,IAAI,CAAC;CACxD,CAAC;AACH;;;;;AAMA,SAAgB,uBACd,QACA,UAA6B,CAAC,GACpB;CACV,OAAO,8BAA8B,EAAE,QAAQ,aAAa,QAAQ,OAAO,EAAE,CAAC;AAChF;;;;;;AAOA,SAAgB,mCACd,QACA,UACA,UAA6B,CAAC,GACxB;CACN,8BAA8B;EAAE;EAAU,QAAQ,aAAa,QAAQ,OAAO;CAAE,CAAC;AACnF"}
|
package/dist/viewer.d.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { a as ParsedTrajectoryEvent } from "./trajectory-ByoOQZ6T.js";
|
|
2
|
+
|
|
3
|
+
//#region src/viewer/start_viewer.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* `start_viewer` programmatic embed implementation.
|
|
6
|
+
*
|
|
7
|
+
* Spin up the server, optionally start tailing a JSONL file, and return a
|
|
8
|
+
* handle that can stop both. `start_viewer` only depends on core's wire-format
|
|
9
|
+
* schema; it never reaches into the engine, composites, or any provider.
|
|
10
|
+
*/
|
|
11
|
+
type StartViewerOptions = {
|
|
12
|
+
readonly path?: string;
|
|
13
|
+
readonly host?: string;
|
|
14
|
+
readonly port?: number;
|
|
15
|
+
readonly buffer?: number;
|
|
16
|
+
readonly on_parse_error?: (err: unknown, line: string) => void;
|
|
17
|
+
readonly on_io_error?: (err: unknown) => void;
|
|
18
|
+
readonly on_subscriber_error?: (err: unknown) => void;
|
|
19
|
+
};
|
|
20
|
+
type ViewerHandle = {
|
|
21
|
+
readonly url: string;
|
|
22
|
+
readonly host: string;
|
|
23
|
+
readonly port: number;
|
|
24
|
+
readonly close: () => Promise<void>;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Starts the viewer: brings up the HTTP/SSE server and, if `options.path`
|
|
28
|
+
* is set, a file tail that feeds the same broadcaster.
|
|
29
|
+
*
|
|
30
|
+
* Defaults to `127.0.0.1:4242` with a 1000-event ring buffer. HTTP ingest
|
|
31
|
+
* and SSE are always available; tailing is opt-in via `path`.
|
|
32
|
+
*/
|
|
33
|
+
declare function start_viewer(options?: StartViewerOptions): Promise<ViewerHandle>;
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/viewer/broadcast.d.ts
|
|
36
|
+
type BroadcastEvent = {
|
|
37
|
+
readonly id: number;
|
|
38
|
+
readonly event: ParsedTrajectoryEvent;
|
|
39
|
+
};
|
|
40
|
+
type Subscriber = (entry: BroadcastEvent) => void;
|
|
41
|
+
type Broadcaster = {
|
|
42
|
+
readonly emit: (event: ParsedTrajectoryEvent) => BroadcastEvent;
|
|
43
|
+
readonly subscribe: (fn: Subscriber) => () => void;
|
|
44
|
+
readonly snapshot: () => readonly BroadcastEvent[];
|
|
45
|
+
readonly snapshot_after: (last_id: number) => readonly BroadcastEvent[];
|
|
46
|
+
readonly size: () => number;
|
|
47
|
+
};
|
|
48
|
+
type BroadcasterConfig = {
|
|
49
|
+
readonly buffer: number;
|
|
50
|
+
readonly on_subscriber_error?: (err: unknown) => void;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Creates a broadcaster: a bounded ring buffer plus subscriber fan-out.
|
|
54
|
+
*
|
|
55
|
+
* Each event gets a monotonic `id` so SSE clients can replay from a known
|
|
56
|
+
* cursor on reconnect (`snapshot_after`). `emit` does not await delivery, so
|
|
57
|
+
* slow-client backpressure is the subscriber's responsibility. A subscriber
|
|
58
|
+
* that throws is unsubscribed, and the `on_subscriber_error` callback fires
|
|
59
|
+
* once with the captured error so the server can log it.
|
|
60
|
+
*/
|
|
61
|
+
declare function create_broadcaster(config: BroadcasterConfig): Broadcaster;
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/viewer/server.d.ts
|
|
64
|
+
type ServerConfig = {
|
|
65
|
+
readonly broadcaster: Broadcaster;
|
|
66
|
+
readonly host: string;
|
|
67
|
+
readonly port: number;
|
|
68
|
+
readonly on_parse_error?: (err: unknown, line: string) => void;
|
|
69
|
+
};
|
|
70
|
+
type ViewerServer = {
|
|
71
|
+
readonly url: string;
|
|
72
|
+
readonly close: () => Promise<void>;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Starts the HTTP server and resolves once it is listening.
|
|
76
|
+
*
|
|
77
|
+
* Wires the five routes (static HTML, health, snapshot, SSE, ingest) to a
|
|
78
|
+
* single request handler and resolves with the bound URL and a `close`
|
|
79
|
+
* that shuts the socket down.
|
|
80
|
+
*/
|
|
81
|
+
declare function start_server(config: ServerConfig): Promise<ViewerServer>;
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/viewer/tail.d.ts
|
|
84
|
+
type TailConfig = {
|
|
85
|
+
readonly path: string;
|
|
86
|
+
readonly on_event: (event: ParsedTrajectoryEvent) => void;
|
|
87
|
+
readonly on_parse_error?: (err: unknown, line: string) => void;
|
|
88
|
+
readonly on_io_error?: (err: unknown) => void;
|
|
89
|
+
};
|
|
90
|
+
type Tail = {
|
|
91
|
+
readonly stop: () => void;
|
|
92
|
+
readonly drain: () => Promise<void>;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Starts tailing `path` and returns a handle to stop or drain it.
|
|
96
|
+
*
|
|
97
|
+
* Watches the file with `fs.watch` and re-reads from the last known offset
|
|
98
|
+
* on every change, buffering an incomplete trailing line until the rest
|
|
99
|
+
* arrives. Overlapping change events are coalesced: a read already in
|
|
100
|
+
* flight absorbs any events that fire while it runs instead of starting a
|
|
101
|
+
* second concurrent read.
|
|
102
|
+
*/
|
|
103
|
+
declare function start_tail(config: TailConfig): Tail;
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region src/viewer/cli.d.ts
|
|
106
|
+
/**
|
|
107
|
+
* CLI entry point: parses argv, starts the viewer, and wires shutdown.
|
|
108
|
+
*
|
|
109
|
+
* Warns but proceeds when the tailed path does not exist yet or when the
|
|
110
|
+
* bind host is 0.0.0.0 (the dashboard has no auth). Optionally opens the
|
|
111
|
+
* browser, then installs SIGINT/SIGTERM handlers that close the viewer
|
|
112
|
+
* before exiting.
|
|
113
|
+
*/
|
|
114
|
+
declare function run_viewer_cli(argv: readonly string[]): Promise<void>;
|
|
115
|
+
//#endregion
|
|
116
|
+
export { type BroadcastEvent, type Broadcaster, type BroadcasterConfig, type ServerConfig, type StartViewerOptions, type Subscriber, type Tail, type TailConfig, type ViewerHandle, type ViewerServer, create_broadcaster, run_viewer_cli, start_server, start_tail, start_viewer };
|
|
117
|
+
//# sourceMappingURL=viewer.d.ts.map
|