@astralform/js 0.1.3 → 0.2.1

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.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,28 @@ interface AssetCreatedEvent {
138
253
  media_type: string;
139
254
  size_bytes: number;
140
255
  }
141
- interface ActivitySSEEvent {
142
- type: "activity";
143
- activity_id: string;
256
+ interface TimelineEntrySSEEvent {
257
+ type: "timeline_entry";
258
+ id: string;
144
259
  status: "started" | "completed";
145
- category: string;
146
- title: string;
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
- tool_name?: string;
154
- agent_name?: string;
155
- duration_ms?: number;
276
+ parent_id?: string;
277
+ structured_output?: Record<string, unknown>;
156
278
  }
157
279
  interface EditorContentStartEvent {
158
280
  type: "editor_content_start";
@@ -176,9 +298,15 @@ interface RetryEvent {
176
298
  max_attempts: number;
177
299
  delay_seconds: number;
178
300
  }
179
- type SSEEvent = MessageStartEvent | ContentBlockDeltaEvent | ToolUseStartEvent | ToolUseEndEvent | AgentStartEvent | AgentEndEvent | SubagentStartEvent | SubagentContentDeltaEvent | SubagentUpdateEvent | SubagentEndEvent | SubagentToolUseEvent | ThinkingDeltaEvent | ThinkingCompleteEvent | SourcesEvent | CapsuleOutputEvent | CapsuleOutputChunkEvent | TodoUpdateEvent | MessageStopEvent | AssetCreatedEvent | ActivitySSEEvent | EditorContentStartEvent | EditorContentDeltaEvent | EditorContentEndEvent | RetryEvent | SSEErrorEvent;
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;
180
302
  type ChatEvent = {
181
303
  type: "connected";
304
+ } | {
305
+ type: "user_message";
306
+ content: string;
307
+ } | {
308
+ type: "title_generated";
309
+ title: string;
182
310
  } | {
183
311
  type: "chunk";
184
312
  text: string;
@@ -197,6 +325,12 @@ type ChatEvent = {
197
325
  callId: string;
198
326
  toolName: string;
199
327
  result?: string;
328
+ sources?: {
329
+ title: string;
330
+ url: string;
331
+ snippet?: string;
332
+ }[];
333
+ durationMs?: number;
200
334
  } | {
201
335
  type: "agent_start";
202
336
  agentName: string;
@@ -260,6 +394,8 @@ type ChatEvent = {
260
394
  conversationId: string;
261
395
  messageId: string;
262
396
  title?: string;
397
+ metrics?: Record<string, unknown>;
398
+ job_id?: string;
263
399
  } | {
264
400
  type: "subagent_tool_use";
265
401
  agentName: string;
@@ -279,20 +415,27 @@ type ChatEvent = {
279
415
  maxAttempts: number;
280
416
  delaySeconds: number;
281
417
  } | {
282
- type: "activity";
283
- activityId: string;
418
+ type: "timeline_entry";
419
+ id: string;
284
420
  status: "started" | "completed";
285
- category: string;
286
- title: string;
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;
287
428
  detail?: string;
429
+ started_at?: number;
430
+ duration_ms?: number;
431
+ output_summary?: string;
288
432
  sources?: Array<{
289
433
  title: string;
290
434
  url: string;
291
435
  snippet?: string;
292
436
  }>;
293
- toolName?: string;
294
- agentName?: string;
295
- durationMs?: number;
437
+ parent_id?: string;
438
+ structured_output?: Record<string, unknown>;
296
439
  } | {
297
440
  type: "editor_content_start";
298
441
  callId: string;
@@ -309,6 +452,9 @@ type ChatEvent = {
309
452
  } | {
310
453
  type: "model_info";
311
454
  name: string;
455
+ } | {
456
+ type: "blocks_changed";
457
+ blocks: Block[];
312
458
  } | {
313
459
  type: "error";
314
460
  error: Error;
@@ -487,7 +633,7 @@ declare class AstralformClient {
487
633
  deleteConversation(id: string): Promise<void>;
488
634
  getAgents(): Promise<AgentInfo[]>;
489
635
  getSkills(): Promise<SkillInfo[]>;
490
- getConversationEvents(conversationId: string): Promise<ConversationEvent[]>;
636
+ getConversationEvents(conversationId: string, jobId?: string): Promise<ConversationEvent[]>;
491
637
  submitToolResult(request: ToolResultRequest): Promise<void>;
492
638
  private mapAsset;
493
639
  uploadFile(conversationId: string, file: Blob, filename?: string): Promise<ConversationAsset>;
@@ -540,6 +686,7 @@ declare class ChatSession {
540
686
  readonly client: AstralformClient;
541
687
  readonly toolRegistry: ToolRegistry;
542
688
  readonly storage: ChatStorage;
689
+ readonly blockBuilder: BlockBuilder;
543
690
  conversationId: string | null;
544
691
  conversations: Conversation[];
545
692
  messages: Message[];
@@ -560,12 +707,14 @@ declare class ChatSession {
560
707
  activeTools: Map<string, ToolState>;
561
708
  private handlers;
562
709
  private abortController;
563
- constructor(config: AstralformConfig, storage?: ChatStorage);
710
+ constructor(config: AstralformConfig, storage?: ChatStorage, blockBuilder?: BlockBuilder);
564
711
  on(handler: ChatEventHandler): () => void;
565
712
  private emit;
566
713
  connect(): Promise<void>;
567
714
  send(content: string, options?: SendOptions): Promise<void>;
568
- resendFromCheckpoint(messageId: string, newContent: string): Promise<void>;
715
+ resendFromCheckpoint(messageId: string, newContent: string, options?: {
716
+ enableSearch?: boolean;
717
+ }): Promise<void>;
569
718
  private resetStreamingState;
570
719
  private processStream;
571
720
  /** Last received sequence number for resumable reconnection */
@@ -573,6 +722,10 @@ declare class ChatSession {
573
722
  /** Current job ID for cancellation */
574
723
  private currentJobId;
575
724
  private consumeJobStream;
725
+ /**
726
+ * Shared event consumption loop used by both consumeJobStream and reconnectToJob.
727
+ */
728
+ private consumeEventStream;
576
729
  /**
577
730
  * Apply a single SSE event to session state and notify consumers.
578
731
  * Shared between live streaming and historical event replay.
@@ -580,18 +733,37 @@ declare class ChatSession {
580
733
  private applyEvent;
581
734
  private executeClientTools;
582
735
  private completeStream;
583
- disconnect(): void;
584
- createNewConversation(): Promise<string>;
585
- switchConversation(id: string): Promise<void>;
586
736
  /**
587
- * Replay a single persisted event to reconstruct session state.
588
- * Skips text deltas (final content is already in messages[]).
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.
589
745
  */
590
- private replayEvent;
746
+ reconnectToJob(jobId: string): Promise<void>;
747
+ disconnect(): void;
748
+ createNewConversation(): Promise<string>;
749
+ switchConversation(id: string, jobId?: string): Promise<void>;
591
750
  deleteConversation(id: string): Promise<void>;
592
751
  toggleClientTool(name: string): boolean;
593
752
  }
594
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
+
595
767
  declare class AstralformError extends Error {
596
768
  code: string;
597
769
  constructor(message: string, code: string);
@@ -622,4 +794,4 @@ declare function generateId(): string;
622
794
  */
623
795
  declare function streamJobSSE(options: StreamJobSSEOptions): AsyncGenerator<ChatStreamEvent>;
624
796
 
625
- 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 };