@astralform/js 0.1.2 → 0.2.0
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/dist/index.cjs +475 -30
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +225 -24
- package/dist/index.d.ts +225 -24
- package/dist/index.js +473 -30
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BlockBuilder — pure rendering engine. Zero default behavior.
|
|
3
|
+
*
|
|
4
|
+
* Clients register handlers that map events to blocks.
|
|
5
|
+
* Both live streaming and restore go through the same engine.
|
|
6
|
+
*
|
|
7
|
+
* import { BlockBuilder, standardHandlers } from "@astralform/js";
|
|
8
|
+
* const builder = new BlockBuilder();
|
|
9
|
+
* builder.registerHandlers(standardHandlers);
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
interface UserBlock {
|
|
13
|
+
type: "user";
|
|
14
|
+
id: string;
|
|
15
|
+
content: string;
|
|
16
|
+
}
|
|
17
|
+
interface TextBlock {
|
|
18
|
+
type: "text";
|
|
19
|
+
id: string;
|
|
20
|
+
content: string;
|
|
21
|
+
isStreaming: boolean;
|
|
22
|
+
}
|
|
23
|
+
interface ThinkingBlock {
|
|
24
|
+
type: "thinking";
|
|
25
|
+
id: string;
|
|
26
|
+
content: string;
|
|
27
|
+
isActive: boolean;
|
|
28
|
+
durationMs?: number;
|
|
29
|
+
}
|
|
30
|
+
interface AgentBlock {
|
|
31
|
+
type: "agent";
|
|
32
|
+
id: string;
|
|
33
|
+
agentName: string;
|
|
34
|
+
displayName?: string;
|
|
35
|
+
avatarUrl?: string;
|
|
36
|
+
}
|
|
37
|
+
interface SubagentBlock {
|
|
38
|
+
type: "subagent";
|
|
39
|
+
id: string;
|
|
40
|
+
agentName: string;
|
|
41
|
+
displayName: string;
|
|
42
|
+
toolCallId: string;
|
|
43
|
+
avatarUrl?: string;
|
|
44
|
+
description?: string;
|
|
45
|
+
content: string;
|
|
46
|
+
isActive: boolean;
|
|
47
|
+
}
|
|
48
|
+
interface ToolBlock {
|
|
49
|
+
type: "tool";
|
|
50
|
+
id: string;
|
|
51
|
+
callId: string;
|
|
52
|
+
toolName: string;
|
|
53
|
+
displayName?: string;
|
|
54
|
+
description?: string;
|
|
55
|
+
arguments?: Record<string, unknown>;
|
|
56
|
+
status: string;
|
|
57
|
+
iconUrl?: string;
|
|
58
|
+
toolCategory?: string;
|
|
59
|
+
sources?: {
|
|
60
|
+
title: string;
|
|
61
|
+
url: string;
|
|
62
|
+
snippet?: string;
|
|
63
|
+
}[];
|
|
64
|
+
durationMs?: number;
|
|
65
|
+
result?: string;
|
|
66
|
+
}
|
|
67
|
+
interface ErrorBlock {
|
|
68
|
+
type: "error";
|
|
69
|
+
id: string;
|
|
70
|
+
message: string;
|
|
71
|
+
}
|
|
72
|
+
type Block = UserBlock | TextBlock | ThinkingBlock | AgentBlock | SubagentBlock | ToolBlock | ErrorBlock;
|
|
73
|
+
type EventHandler = (event: ChatEvent, builder: BlockBuilder) => void;
|
|
74
|
+
declare class BlockBuilder {
|
|
75
|
+
private _blocks;
|
|
76
|
+
private _handlers;
|
|
77
|
+
private _onChange;
|
|
78
|
+
activeTextId: string | null;
|
|
79
|
+
activeThinkingId: string | null;
|
|
80
|
+
thinkingStartMs: number | null;
|
|
81
|
+
on(eventType: string, handler: EventHandler | null): void;
|
|
82
|
+
registerHandlers(handlers: Record<string, EventHandler | null>): void;
|
|
83
|
+
processEvent(event: ChatEvent): void;
|
|
84
|
+
getBlocks(): Block[];
|
|
85
|
+
reset(): void;
|
|
86
|
+
setOnChange(fn: (() => void) | null): void;
|
|
87
|
+
addBlock(block: Block): void;
|
|
88
|
+
updateBlock<T extends Block["type"]>(id: string, updater: (block: Extract<Block, {
|
|
89
|
+
type: T;
|
|
90
|
+
}>) => Extract<Block, {
|
|
91
|
+
type: T;
|
|
92
|
+
}>): void;
|
|
93
|
+
/** Update any block by id with a partial update (type-loose for handlers). */
|
|
94
|
+
patchBlock(id: string, patch: Partial<Block>): void;
|
|
95
|
+
findBlock(predicate: (b: Block) => boolean): Block | undefined;
|
|
96
|
+
nextId(): string;
|
|
97
|
+
private _notify;
|
|
98
|
+
}
|
|
99
|
+
|
|
1
100
|
interface AstralformConfig {
|
|
2
101
|
apiKey: string;
|
|
3
102
|
baseURL?: string;
|
|
4
103
|
userId: string;
|
|
5
104
|
fetch?: typeof globalThis.fetch;
|
|
6
105
|
}
|
|
106
|
+
interface UserMessageEvent {
|
|
107
|
+
type: "user_message";
|
|
108
|
+
content: string;
|
|
109
|
+
}
|
|
110
|
+
interface TitleGeneratedEvent {
|
|
111
|
+
type: "title_generated";
|
|
112
|
+
title: string;
|
|
113
|
+
}
|
|
7
114
|
interface MessageStartEvent {
|
|
8
115
|
type: "message_start";
|
|
9
116
|
message_id: string;
|
|
@@ -37,6 +144,12 @@ interface ToolUseEndEvent {
|
|
|
37
144
|
call_id: string;
|
|
38
145
|
tool: string;
|
|
39
146
|
result?: string;
|
|
147
|
+
sources?: {
|
|
148
|
+
title: string;
|
|
149
|
+
url: string;
|
|
150
|
+
snippet?: string;
|
|
151
|
+
}[];
|
|
152
|
+
duration_ms?: number;
|
|
40
153
|
}
|
|
41
154
|
interface AgentStartEvent {
|
|
42
155
|
type: "agent_start";
|
|
@@ -117,6 +230,8 @@ interface MessageStopEvent {
|
|
|
117
230
|
type: "message_stop";
|
|
118
231
|
stop_reason: "end_turn" | "tool_use";
|
|
119
232
|
title?: string;
|
|
233
|
+
metrics?: Record<string, unknown>;
|
|
234
|
+
job_id?: string;
|
|
120
235
|
}
|
|
121
236
|
interface SSEErrorEvent {
|
|
122
237
|
type: "error";
|
|
@@ -138,21 +253,44 @@ interface AssetCreatedEvent {
|
|
|
138
253
|
media_type: string;
|
|
139
254
|
size_bytes: number;
|
|
140
255
|
}
|
|
141
|
-
interface
|
|
142
|
-
type: "
|
|
143
|
-
|
|
256
|
+
interface TimelineEntrySSEEvent {
|
|
257
|
+
type: "timeline_entry";
|
|
258
|
+
id: string;
|
|
144
259
|
status: "started" | "completed";
|
|
145
|
-
|
|
146
|
-
|
|
260
|
+
kind?: string;
|
|
261
|
+
agent_name?: string;
|
|
262
|
+
tool_name?: string;
|
|
263
|
+
display_name?: string;
|
|
264
|
+
tool_category?: string;
|
|
265
|
+
viewer?: string;
|
|
266
|
+
call_id?: string;
|
|
147
267
|
detail?: string;
|
|
268
|
+
started_at?: number;
|
|
269
|
+
duration_ms?: number;
|
|
270
|
+
output_summary?: string;
|
|
148
271
|
sources?: Array<{
|
|
149
272
|
title: string;
|
|
150
273
|
url: string;
|
|
151
274
|
snippet?: string;
|
|
152
275
|
}>;
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
276
|
+
parent_id?: string;
|
|
277
|
+
structured_output?: Record<string, unknown>;
|
|
278
|
+
}
|
|
279
|
+
interface EditorContentStartEvent {
|
|
280
|
+
type: "editor_content_start";
|
|
281
|
+
call_id: string;
|
|
282
|
+
path: string;
|
|
283
|
+
language: string;
|
|
284
|
+
}
|
|
285
|
+
interface EditorContentDeltaEvent {
|
|
286
|
+
type: "editor_content_delta";
|
|
287
|
+
call_id: string;
|
|
288
|
+
path: string;
|
|
289
|
+
delta: string;
|
|
290
|
+
}
|
|
291
|
+
interface EditorContentEndEvent {
|
|
292
|
+
type: "editor_content_end";
|
|
293
|
+
call_id: string;
|
|
156
294
|
}
|
|
157
295
|
interface RetryEvent {
|
|
158
296
|
type: "retry";
|
|
@@ -160,9 +298,15 @@ interface RetryEvent {
|
|
|
160
298
|
max_attempts: number;
|
|
161
299
|
delay_seconds: number;
|
|
162
300
|
}
|
|
163
|
-
type SSEEvent = MessageStartEvent | ContentBlockDeltaEvent | ToolUseStartEvent | ToolUseEndEvent | AgentStartEvent | AgentEndEvent | SubagentStartEvent | SubagentContentDeltaEvent | SubagentUpdateEvent | SubagentEndEvent | SubagentToolUseEvent | ThinkingDeltaEvent | ThinkingCompleteEvent | SourcesEvent | CapsuleOutputEvent | CapsuleOutputChunkEvent | TodoUpdateEvent | MessageStopEvent | AssetCreatedEvent |
|
|
301
|
+
type SSEEvent = MessageStartEvent | UserMessageEvent | TitleGeneratedEvent | ContentBlockDeltaEvent | ToolUseStartEvent | ToolUseEndEvent | AgentStartEvent | AgentEndEvent | SubagentStartEvent | SubagentContentDeltaEvent | SubagentUpdateEvent | SubagentEndEvent | SubagentToolUseEvent | ThinkingDeltaEvent | ThinkingCompleteEvent | SourcesEvent | CapsuleOutputEvent | CapsuleOutputChunkEvent | TodoUpdateEvent | MessageStopEvent | AssetCreatedEvent | TimelineEntrySSEEvent | EditorContentStartEvent | EditorContentDeltaEvent | EditorContentEndEvent | RetryEvent | SSEErrorEvent;
|
|
164
302
|
type ChatEvent = {
|
|
165
303
|
type: "connected";
|
|
304
|
+
} | {
|
|
305
|
+
type: "user_message";
|
|
306
|
+
content: string;
|
|
307
|
+
} | {
|
|
308
|
+
type: "title_generated";
|
|
309
|
+
title: string;
|
|
166
310
|
} | {
|
|
167
311
|
type: "chunk";
|
|
168
312
|
text: string;
|
|
@@ -181,6 +325,12 @@ type ChatEvent = {
|
|
|
181
325
|
callId: string;
|
|
182
326
|
toolName: string;
|
|
183
327
|
result?: string;
|
|
328
|
+
sources?: {
|
|
329
|
+
title: string;
|
|
330
|
+
url: string;
|
|
331
|
+
snippet?: string;
|
|
332
|
+
}[];
|
|
333
|
+
durationMs?: number;
|
|
184
334
|
} | {
|
|
185
335
|
type: "agent_start";
|
|
186
336
|
agentName: string;
|
|
@@ -244,6 +394,8 @@ type ChatEvent = {
|
|
|
244
394
|
conversationId: string;
|
|
245
395
|
messageId: string;
|
|
246
396
|
title?: string;
|
|
397
|
+
metrics?: Record<string, unknown>;
|
|
398
|
+
job_id?: string;
|
|
247
399
|
} | {
|
|
248
400
|
type: "subagent_tool_use";
|
|
249
401
|
agentName: string;
|
|
@@ -263,23 +415,46 @@ type ChatEvent = {
|
|
|
263
415
|
maxAttempts: number;
|
|
264
416
|
delaySeconds: number;
|
|
265
417
|
} | {
|
|
266
|
-
type: "
|
|
267
|
-
|
|
418
|
+
type: "timeline_entry";
|
|
419
|
+
id: string;
|
|
268
420
|
status: "started" | "completed";
|
|
269
|
-
|
|
270
|
-
|
|
421
|
+
kind?: string;
|
|
422
|
+
agent_name?: string;
|
|
423
|
+
tool_name?: string;
|
|
424
|
+
display_name?: string;
|
|
425
|
+
tool_category?: string;
|
|
426
|
+
viewer?: string;
|
|
427
|
+
call_id?: string;
|
|
271
428
|
detail?: string;
|
|
429
|
+
started_at?: number;
|
|
430
|
+
duration_ms?: number;
|
|
431
|
+
output_summary?: string;
|
|
272
432
|
sources?: Array<{
|
|
273
433
|
title: string;
|
|
274
434
|
url: string;
|
|
275
435
|
snippet?: string;
|
|
276
436
|
}>;
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
437
|
+
parent_id?: string;
|
|
438
|
+
structured_output?: Record<string, unknown>;
|
|
439
|
+
} | {
|
|
440
|
+
type: "editor_content_start";
|
|
441
|
+
callId: string;
|
|
442
|
+
path: string;
|
|
443
|
+
language: string;
|
|
444
|
+
} | {
|
|
445
|
+
type: "editor_content_delta";
|
|
446
|
+
callId: string;
|
|
447
|
+
path: string;
|
|
448
|
+
delta: string;
|
|
449
|
+
} | {
|
|
450
|
+
type: "editor_content_end";
|
|
451
|
+
callId: string;
|
|
280
452
|
} | {
|
|
281
453
|
type: "model_info";
|
|
282
454
|
name: string;
|
|
455
|
+
} | {
|
|
456
|
+
type: "blocks_changed";
|
|
457
|
+
blocks: Block[];
|
|
283
458
|
} | {
|
|
284
459
|
type: "error";
|
|
285
460
|
error: Error;
|
|
@@ -511,6 +686,7 @@ declare class ChatSession {
|
|
|
511
686
|
readonly client: AstralformClient;
|
|
512
687
|
readonly toolRegistry: ToolRegistry;
|
|
513
688
|
readonly storage: ChatStorage;
|
|
689
|
+
readonly blockBuilder: BlockBuilder;
|
|
514
690
|
conversationId: string | null;
|
|
515
691
|
conversations: Conversation[];
|
|
516
692
|
messages: Message[];
|
|
@@ -531,12 +707,14 @@ declare class ChatSession {
|
|
|
531
707
|
activeTools: Map<string, ToolState>;
|
|
532
708
|
private handlers;
|
|
533
709
|
private abortController;
|
|
534
|
-
constructor(config: AstralformConfig, storage?: ChatStorage);
|
|
710
|
+
constructor(config: AstralformConfig, storage?: ChatStorage, blockBuilder?: BlockBuilder);
|
|
535
711
|
on(handler: ChatEventHandler): () => void;
|
|
536
712
|
private emit;
|
|
537
713
|
connect(): Promise<void>;
|
|
538
714
|
send(content: string, options?: SendOptions): Promise<void>;
|
|
539
|
-
resendFromCheckpoint(messageId: string, newContent: string
|
|
715
|
+
resendFromCheckpoint(messageId: string, newContent: string, options?: {
|
|
716
|
+
enableSearch?: boolean;
|
|
717
|
+
}): Promise<void>;
|
|
540
718
|
private resetStreamingState;
|
|
541
719
|
private processStream;
|
|
542
720
|
/** Last received sequence number for resumable reconnection */
|
|
@@ -544,6 +722,10 @@ declare class ChatSession {
|
|
|
544
722
|
/** Current job ID for cancellation */
|
|
545
723
|
private currentJobId;
|
|
546
724
|
private consumeJobStream;
|
|
725
|
+
/**
|
|
726
|
+
* Shared event consumption loop used by both consumeJobStream and reconnectToJob.
|
|
727
|
+
*/
|
|
728
|
+
private consumeEventStream;
|
|
547
729
|
/**
|
|
548
730
|
* Apply a single SSE event to session state and notify consumers.
|
|
549
731
|
* Shared between live streaming and historical event replay.
|
|
@@ -551,18 +733,37 @@ declare class ChatSession {
|
|
|
551
733
|
private applyEvent;
|
|
552
734
|
private executeClientTools;
|
|
553
735
|
private completeStream;
|
|
736
|
+
/**
|
|
737
|
+
* Load conversation context (messages) without replaying events.
|
|
738
|
+
* Used before reconnectToJob — SSE replay handles event replay.
|
|
739
|
+
*/
|
|
740
|
+
loadConversation(id: string): Promise<void>;
|
|
741
|
+
/**
|
|
742
|
+
* Reconnect to a running job's SSE stream (e.g. after page reload).
|
|
743
|
+
* Replays all events from the beginning and continues live.
|
|
744
|
+
* Does NOT reset BlockBuilder — caller controls reset.
|
|
745
|
+
*/
|
|
746
|
+
reconnectToJob(jobId: string): Promise<void>;
|
|
554
747
|
disconnect(): void;
|
|
555
748
|
createNewConversation(): Promise<string>;
|
|
556
749
|
switchConversation(id: string): Promise<void>;
|
|
557
|
-
/**
|
|
558
|
-
* Replay a single persisted event to reconstruct session state.
|
|
559
|
-
* Skips text deltas (final content is already in messages[]).
|
|
560
|
-
*/
|
|
561
|
-
private replayEvent;
|
|
562
750
|
deleteConversation(id: string): Promise<void>;
|
|
563
751
|
toggleClientTool(name: string): boolean;
|
|
564
752
|
}
|
|
565
753
|
|
|
754
|
+
/**
|
|
755
|
+
* Standard event handlers for BlockBuilder.
|
|
756
|
+
*
|
|
757
|
+
* These define the default event→block mapping rules. Clients import
|
|
758
|
+
* and register them explicitly:
|
|
759
|
+
*
|
|
760
|
+
* import { BlockBuilder, standardHandlers } from "@astralform/js";
|
|
761
|
+
* const builder = new BlockBuilder();
|
|
762
|
+
* builder.registerHandlers(standardHandlers);
|
|
763
|
+
*/
|
|
764
|
+
|
|
765
|
+
declare const standardHandlers: Record<string, EventHandler>;
|
|
766
|
+
|
|
566
767
|
declare class AstralformError extends Error {
|
|
567
768
|
code: string;
|
|
568
769
|
constructor(message: string, code: string);
|
|
@@ -593,4 +794,4 @@ declare function generateId(): string;
|
|
|
593
794
|
*/
|
|
594
795
|
declare function streamJobSSE(options: StreamJobSSEOptions): AsyncGenerator<ChatStreamEvent>;
|
|
595
796
|
|
|
596
|
-
export { type AgentEndEvent, type AgentInfo, type AgentStartEvent, AstralformClient, type AstralformConfig, AstralformError, AuthenticationError, type CapsuleOutput, type CapsuleOutputChunkEvent, type CapsuleOutputEvent, type ChatEvent, ChatSession, type ChatStorage, type ChatStreamEvent, type ChatStreamRequest, ConnectionError, type ContentBlockDeltaEvent, type Conversation, type ConversationAsset, type ConversationEvent, InMemoryStorage, type JobCreateResponse, LLMNotConfiguredError, type Message, type MessageStartEvent, type MessageStopEvent, type ProjectStatus, RateLimitError, type RetryEvent, type SSEErrorEvent, type SSEEvent, type SendOptions, ServerError, type SkillInfo, type Source, type SourcesEvent, StreamAbortedError, type StreamJobSSEOptions, type SubagentContentDeltaEvent, type SubagentEndEvent, type SubagentStartEvent, type SubagentState, type SubagentToolUseEvent, type ThinkingCompleteEvent, type ThinkingDeltaEvent, type TodoItem, type TodoUpdateEvent, type ToolCallRequest, type ToolDefinition, type ToolHandler, ToolRegistry, type ToolResult, type ToolResultRequest, type ToolState, type ToolUseEndEvent, type ToolUseStartEvent, generateId, streamJobSSE };
|
|
797
|
+
export { type AgentBlock, type AgentEndEvent, type AgentInfo, type AgentStartEvent, AstralformClient, type AstralformConfig, AstralformError, AuthenticationError, type Block, BlockBuilder, type CapsuleOutput, type CapsuleOutputChunkEvent, type CapsuleOutputEvent, type ChatEvent, ChatSession, type ChatStorage, type ChatStreamEvent, type ChatStreamRequest, ConnectionError, type ContentBlockDeltaEvent, type Conversation, type ConversationAsset, type ConversationEvent, type ErrorBlock, type EventHandler, InMemoryStorage, type JobCreateResponse, LLMNotConfiguredError, type Message, type MessageStartEvent, type MessageStopEvent, type ProjectStatus, RateLimitError, type RetryEvent, type SSEErrorEvent, type SSEEvent, type SendOptions, ServerError, type SkillInfo, type Source, type SourcesEvent, StreamAbortedError, type StreamJobSSEOptions, type SubagentBlock, type SubagentContentDeltaEvent, type SubagentEndEvent, type SubagentStartEvent, type SubagentState, type SubagentToolUseEvent, type TextBlock, type ThinkingBlock, type ThinkingCompleteEvent, type ThinkingDeltaEvent, type TodoItem, type TodoUpdateEvent, type ToolBlock, type ToolCallRequest, type ToolDefinition, type ToolHandler, ToolRegistry, type ToolResult, type ToolResultRequest, type ToolState, type ToolUseEndEvent, type ToolUseStartEvent, type UserBlock, generateId, standardHandlers, streamJobSSE };
|