@axiom-lattice/core 1.0.46 → 2.0.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/README.md +104 -2
- package/dist/index.d.mts +198 -2
- package/dist/index.d.ts +198 -2
- package/dist/index.js +374 -26
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +371 -32
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
- 模型格子管理 (ModelLatticeManager)
|
|
8
8
|
- 工具格子管理 (ToolLatticeManager)
|
|
9
9
|
- 代理格子管理 (AgentLatticeManager)
|
|
10
|
+
- 记忆格子管理 (MemoryLatticeManager)
|
|
11
|
+
- 流式缓冲管理 (ChunkBufferLatticeManager)
|
|
10
12
|
- DeepAgent 实现
|
|
11
13
|
- 工具类和辅助函数
|
|
12
14
|
|
|
@@ -30,11 +32,53 @@ pnpm test
|
|
|
30
32
|
|
|
31
33
|
## 使用示例
|
|
32
34
|
|
|
35
|
+
### Model Lattice
|
|
36
|
+
|
|
33
37
|
```typescript
|
|
34
|
-
import { ModelLatticeManager } from "
|
|
38
|
+
import { ModelLatticeManager } from "@axiom-lattice/core";
|
|
35
39
|
|
|
36
40
|
// 创建模型格子管理器
|
|
37
|
-
const modelLattice =
|
|
41
|
+
const modelLattice = ModelLatticeManager.getInstance();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### ChunkBuffer - Streaming Chunk Management
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import {
|
|
48
|
+
InMemoryChunkBuffer,
|
|
49
|
+
registerChunkBuffer
|
|
50
|
+
} from "@axiom-lattice/core";
|
|
51
|
+
|
|
52
|
+
// Create buffer with 30-minute TTL and optional periodic cleanup
|
|
53
|
+
const buffer = new InMemoryChunkBuffer({
|
|
54
|
+
ttl: 30 * 60 * 1000, // 30 minutes
|
|
55
|
+
cleanupInterval: 5 * 60 * 1000 // Clean every 5 minutes (optional)
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Register in Lattice system
|
|
59
|
+
registerChunkBuffer('default', buffer);
|
|
60
|
+
|
|
61
|
+
// Add chunks as they arrive
|
|
62
|
+
await buffer.addChunk('thread-123', 'msg-1', 'Hello ');
|
|
63
|
+
await buffer.addChunk('thread-123', 'msg-1', 'world!');
|
|
64
|
+
await buffer.addChunk('thread-123', 'msg-2', ' How are you?');
|
|
65
|
+
|
|
66
|
+
// Get accumulated content
|
|
67
|
+
const content = await buffer.getAccumulatedContent('thread-123');
|
|
68
|
+
// => "Hello world! How are you?"
|
|
69
|
+
|
|
70
|
+
// Check thread status
|
|
71
|
+
const isActive = await buffer.isThreadActive('thread-123'); // true
|
|
72
|
+
|
|
73
|
+
// Explicitly complete the thread
|
|
74
|
+
await buffer.completeThread('thread-123');
|
|
75
|
+
|
|
76
|
+
// Get buffer statistics
|
|
77
|
+
const stats = buffer.getStats();
|
|
78
|
+
console.log(stats);
|
|
79
|
+
|
|
80
|
+
// Cleanup
|
|
81
|
+
buffer.dispose();
|
|
38
82
|
```
|
|
39
83
|
|
|
40
84
|
## 目录结构
|
|
@@ -43,5 +87,63 @@ const modelLattice = new ModelLatticeManager();
|
|
|
43
87
|
- `src/model_lattice/`: 模型格子相关实现
|
|
44
88
|
- `src/tool_lattice/`: 工具格子相关实现
|
|
45
89
|
- `src/agent_lattice/`: 代理格子相关实现
|
|
90
|
+
- `src/memory_lattice/`: 记忆格子相关实现
|
|
91
|
+
- `src/chunk_buffer_lattice/`: 流式缓冲管理实现
|
|
46
92
|
- `src/deep_agent/`: DeepAgent 实现
|
|
47
93
|
- `src/util/`: 工具类和辅助函数
|
|
94
|
+
|
|
95
|
+
## ChunkBuffer 详细说明
|
|
96
|
+
|
|
97
|
+
ChunkBuffer 模块提供了一个高效的流式数据缓冲解决方案,用于管理按线程(thread)组织的消息块(chunk)。
|
|
98
|
+
|
|
99
|
+
### 核心特性
|
|
100
|
+
|
|
101
|
+
1. **Thread-based Organization**: 每个 thread 维护独立的 chunk 缓冲区
|
|
102
|
+
2. **Sequential Chunk Storage**: Chunks 按到达顺序存储,无需唯一 ID
|
|
103
|
+
3. **Explicit Status Management**: Thread 状态通过显式调用管理(active/completed/aborted)
|
|
104
|
+
4. **Hybrid Cleanup Strategy**:
|
|
105
|
+
- 懒清理(Lazy Cleanup): 访问时自动清除过期 thread
|
|
106
|
+
- 可选周期清理: 后台定时器定期清理过期 thread
|
|
107
|
+
5. **TTL Auto-Extension**: 有新 chunk 加入时自动延长 TTL
|
|
108
|
+
|
|
109
|
+
### API Overview
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
// Core operations
|
|
113
|
+
addChunk(threadId, messageId, content) // Add chunk to thread
|
|
114
|
+
getChunks(threadId) // Get all chunks for a thread
|
|
115
|
+
getAccumulatedContent(threadId) // Get concatenated content
|
|
116
|
+
getChunksByMessageId(threadId, messageId) // Get chunks for specific message
|
|
117
|
+
|
|
118
|
+
// Thread status management
|
|
119
|
+
completeThread(threadId) // Mark thread as completed
|
|
120
|
+
abortThread(threadId) // Mark thread as aborted
|
|
121
|
+
isThreadActive(threadId) // Check if thread is active
|
|
122
|
+
getThreadStatus(threadId) // Get thread status
|
|
123
|
+
|
|
124
|
+
// Thread lifecycle
|
|
125
|
+
clearThread(threadId) // Remove specific thread
|
|
126
|
+
cleanupExpiredThreads() // Manual cleanup of expired threads
|
|
127
|
+
extendThreadTTL(threadId, additionalMs) // Extend thread expiration time
|
|
128
|
+
|
|
129
|
+
// Query operations
|
|
130
|
+
getActiveThreads() // Get all active thread IDs
|
|
131
|
+
getAllThreads() // Get all thread IDs
|
|
132
|
+
getStats() // Get buffer statistics
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Configuration Options
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
interface ThreadBufferConfig {
|
|
139
|
+
ttl?: number; // Time-to-live in ms (default: 1 hour)
|
|
140
|
+
cleanupInterval?: number; // Optional periodic cleanup interval in ms
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Use Cases
|
|
145
|
+
|
|
146
|
+
- **Streaming AI Responses**: Buffer streaming responses from LLM models
|
|
147
|
+
- **Real-time Data Processing**: Collect and organize real-time data streams
|
|
148
|
+
- **Message Aggregation**: Aggregate fragmented messages by thread
|
|
149
|
+
- **Temporary Cache**: Short-term caching with automatic expiration
|
package/dist/index.d.mts
CHANGED
|
@@ -5,7 +5,7 @@ import { BaseChatModel, BaseChatModelCallOptions } from '@langchain/core/languag
|
|
|
5
5
|
import { BaseLanguageModelInput } from '@langchain/core/language_models/base';
|
|
6
6
|
import { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
7
7
|
import { ChatResult } from '@langchain/core/outputs';
|
|
8
|
-
import { LLMConfig, ToolConfig, ToolExecutor, AgentConfig, GraphBuildOptions } from '@axiom-lattice/protocols';
|
|
8
|
+
import { LLMConfig, ToolConfig, ToolExecutor, AgentConfig, GraphBuildOptions, MessageChunk } from '@axiom-lattice/protocols';
|
|
9
9
|
import * as protocols from '@axiom-lattice/protocols';
|
|
10
10
|
export { protocols as Protocols };
|
|
11
11
|
export { AgentConfig, AgentType, GraphBuildOptions, MemoryType } from '@axiom-lattice/protocols';
|
|
@@ -13,6 +13,7 @@ import * as _langchain_core_tools from '@langchain/core/tools';
|
|
|
13
13
|
import { StructuredTool } from '@langchain/core/tools';
|
|
14
14
|
import { CompiledStateGraph } from '@langchain/langgraph';
|
|
15
15
|
import { BaseCheckpointSaver } from '@langchain/langgraph-checkpoint';
|
|
16
|
+
import { ReplaySubject } from 'rxjs';
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* BaseLatticeManager - 抽象基类,为各种Lattice管理器提供通用功能
|
|
@@ -442,4 +443,199 @@ declare class MemoryLatticeManager extends BaseLatticeManager {
|
|
|
442
443
|
declare const getCheckpointSaver: (key: string) => BaseCheckpointSaver<number>;
|
|
443
444
|
declare const registerCheckpointSaver: (key: string, saver: BaseCheckpointSaver) => void;
|
|
444
445
|
|
|
445
|
-
|
|
446
|
+
/**
|
|
447
|
+
* ChunkBuffer Types
|
|
448
|
+
*
|
|
449
|
+
* Defines types for managing streaming chunks organized by thread
|
|
450
|
+
*/
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Represents a single chunk of data
|
|
454
|
+
* Chunks are identified by their position in the sequence (no unique ID)
|
|
455
|
+
*/
|
|
456
|
+
interface Chunk {
|
|
457
|
+
messageId: string;
|
|
458
|
+
content: MessageChunk;
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Thread status - requires explicit state transitions
|
|
462
|
+
*/
|
|
463
|
+
declare enum ThreadStatus {
|
|
464
|
+
ACTIVE = "active",
|
|
465
|
+
COMPLETED = "completed",
|
|
466
|
+
ABORTED = "aborted"
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Thread buffer configuration
|
|
470
|
+
*/
|
|
471
|
+
interface ThreadBufferConfig {
|
|
472
|
+
ttl?: number;
|
|
473
|
+
cleanupInterval?: number;
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Thread buffer state
|
|
477
|
+
*/
|
|
478
|
+
interface ThreadBuffer {
|
|
479
|
+
threadId: string;
|
|
480
|
+
chunks$: ReplaySubject<MessageChunk>;
|
|
481
|
+
status: ThreadStatus;
|
|
482
|
+
createdAt: number;
|
|
483
|
+
updatedAt: number;
|
|
484
|
+
expiresAt: number;
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Buffer statistics
|
|
488
|
+
*/
|
|
489
|
+
interface BufferStats {
|
|
490
|
+
totalThreads: number;
|
|
491
|
+
activeThreads: number;
|
|
492
|
+
completedThreads: number;
|
|
493
|
+
abortedThreads: number;
|
|
494
|
+
config: Required<ThreadBufferConfig>;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* ChunkBuffer Abstract Base Class
|
|
499
|
+
*
|
|
500
|
+
* Defines the interface for chunk buffer implementations
|
|
501
|
+
*/
|
|
502
|
+
|
|
503
|
+
declare abstract class ChunkBuffer {
|
|
504
|
+
/**
|
|
505
|
+
* Add a chunk to a thread (creates thread if not exists)
|
|
506
|
+
* Chunks are appended in order
|
|
507
|
+
*/
|
|
508
|
+
abstract addChunk(threadId: string, content: MessageChunk): Promise<void>;
|
|
509
|
+
/**
|
|
510
|
+
* Mark thread as completed (explicit call)
|
|
511
|
+
*/
|
|
512
|
+
abstract completeThread(threadId: string): Promise<void>;
|
|
513
|
+
/**
|
|
514
|
+
* Mark thread as aborted (explicit call)
|
|
515
|
+
*/
|
|
516
|
+
abstract abortThread(threadId: string): Promise<void>;
|
|
517
|
+
/**
|
|
518
|
+
* Check if thread is still active
|
|
519
|
+
*/
|
|
520
|
+
abstract isThreadActive(threadId: string): Promise<boolean>;
|
|
521
|
+
/**
|
|
522
|
+
* Get thread status (returns undefined if thread doesn't exist)
|
|
523
|
+
*/
|
|
524
|
+
abstract getThreadStatus(threadId: string): Promise<ThreadStatus | undefined>;
|
|
525
|
+
/**
|
|
526
|
+
* Get thread buffer info including metadata
|
|
527
|
+
*/
|
|
528
|
+
abstract getThreadBuffer(threadId: string): Promise<ThreadBuffer | undefined>;
|
|
529
|
+
/**
|
|
530
|
+
* Clear specific thread buffer
|
|
531
|
+
*/
|
|
532
|
+
abstract clearThread(threadId: string): Promise<void>;
|
|
533
|
+
/**
|
|
534
|
+
* Get all active thread IDs
|
|
535
|
+
*/
|
|
536
|
+
abstract getActiveThreads(): Promise<string[]>;
|
|
537
|
+
/**
|
|
538
|
+
* Get all thread IDs (regardless of status)
|
|
539
|
+
*/
|
|
540
|
+
abstract getAllThreads(): Promise<string[]>;
|
|
541
|
+
/**
|
|
542
|
+
* Manually trigger cleanup of expired threads
|
|
543
|
+
* Returns number of threads cleaned up
|
|
544
|
+
*/
|
|
545
|
+
abstract cleanupExpiredThreads(): Promise<number>;
|
|
546
|
+
/**
|
|
547
|
+
* Extend thread TTL (reset expiration time)
|
|
548
|
+
*/
|
|
549
|
+
abstract extendThreadTTL(threadId: string, additionalMs?: number): Promise<void>;
|
|
550
|
+
/**
|
|
551
|
+
* Check if a thread exists (valid or expired)
|
|
552
|
+
*/
|
|
553
|
+
abstract hasThread(threadId: string): Promise<boolean>;
|
|
554
|
+
abstract getNewChunksSinceContentIterator(threadId: string, messageId: string, knownContent: string): AsyncIterable<MessageChunk>;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* InMemoryChunkBuffer
|
|
559
|
+
*
|
|
560
|
+
* In-memory implementation of ChunkBuffer with hybrid cleanup strategy:
|
|
561
|
+
* - Lazy cleanup: expired threads are removed on access
|
|
562
|
+
* - Optional periodic cleanup: background timer to clean expired threads
|
|
563
|
+
*/
|
|
564
|
+
|
|
565
|
+
declare class InMemoryChunkBuffer extends ChunkBuffer {
|
|
566
|
+
private buffers;
|
|
567
|
+
private config;
|
|
568
|
+
private cleanupTimer?;
|
|
569
|
+
constructor(config?: ThreadBufferConfig & {
|
|
570
|
+
maxChunks?: number;
|
|
571
|
+
});
|
|
572
|
+
/**
|
|
573
|
+
* Start automatic periodic cleanup timer
|
|
574
|
+
*/
|
|
575
|
+
private startCleanupTimer;
|
|
576
|
+
/**
|
|
577
|
+
* Stop cleanup timer (for cleanup/shutdown)
|
|
578
|
+
*/
|
|
579
|
+
stopCleanupTimer(): void;
|
|
580
|
+
/**
|
|
581
|
+
* Check if a buffer is expired (lazy cleanup helper)
|
|
582
|
+
*/
|
|
583
|
+
private isExpired;
|
|
584
|
+
/**
|
|
585
|
+
* Get buffer if valid, perform lazy cleanup if expired
|
|
586
|
+
*/
|
|
587
|
+
private getBufferIfValid;
|
|
588
|
+
/**
|
|
589
|
+
* Create or get thread buffer
|
|
590
|
+
*/
|
|
591
|
+
private getOrCreateBuffer;
|
|
592
|
+
addChunk(threadId: string, content: MessageChunk): Promise<void>;
|
|
593
|
+
completeThread(threadId: string): Promise<void>;
|
|
594
|
+
abortThread(threadId: string): Promise<void>;
|
|
595
|
+
isThreadActive(threadId: string): Promise<boolean>;
|
|
596
|
+
getThreadStatus(threadId: string): Promise<ThreadStatus | undefined>;
|
|
597
|
+
getThreadBuffer(threadId: string): Promise<ThreadBuffer | undefined>;
|
|
598
|
+
clearThread(threadId: string): Promise<void>;
|
|
599
|
+
getActiveThreads(): Promise<string[]>;
|
|
600
|
+
getAllThreads(): Promise<string[]>;
|
|
601
|
+
hasThread(threadId: string): Promise<boolean>;
|
|
602
|
+
/**
|
|
603
|
+
* Cleanup expired threads based on TTL
|
|
604
|
+
* Returns number of threads cleaned up
|
|
605
|
+
*/
|
|
606
|
+
cleanupExpiredThreads(): Promise<number>;
|
|
607
|
+
extendThreadTTL(threadId: string, additionalMs?: number): Promise<void>;
|
|
608
|
+
getNewChunksSinceContentIterator(threadId: string, messageId: string, knownContent: string): AsyncIterable<MessageChunk>;
|
|
609
|
+
getStats(): BufferStats;
|
|
610
|
+
dispose(): void;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* ChunkBufferLatticeManager
|
|
615
|
+
*
|
|
616
|
+
* Manages ChunkBuffer instances following the Lattice pattern
|
|
617
|
+
*/
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* ChunkBuffer Lattice Manager
|
|
621
|
+
*/
|
|
622
|
+
declare class ChunkBufferLatticeManager extends BaseLatticeManager<ChunkBuffer> {
|
|
623
|
+
private static instance;
|
|
624
|
+
/**
|
|
625
|
+
* Private constructor for singleton pattern
|
|
626
|
+
*/
|
|
627
|
+
private constructor();
|
|
628
|
+
/**
|
|
629
|
+
* Get singleton instance
|
|
630
|
+
*/
|
|
631
|
+
static getInstance(): ChunkBufferLatticeManager;
|
|
632
|
+
/**
|
|
633
|
+
* Get Lattice type identifier
|
|
634
|
+
*/
|
|
635
|
+
protected getLatticeType(): string;
|
|
636
|
+
}
|
|
637
|
+
declare const getChunkBuffer: (key: string) => ChunkBuffer | undefined;
|
|
638
|
+
declare const registerChunkBuffer: (key: string, buffer: ChunkBuffer) => void;
|
|
639
|
+
declare const hasChunkBuffer: (key: string) => boolean;
|
|
640
|
+
|
|
641
|
+
export { type AgentClient, type AgentLattice, AgentLatticeManager, type BufferStats, type Chunk, ChunkBuffer, ChunkBufferLatticeManager, InMemoryChunkBuffer, MemoryLatticeManager, type ModelConfig, type ModelLatticeInterface, ModelLatticeManager, type ThreadBuffer, type ThreadBufferConfig, ThreadStatus, type ToolDefinition, type ToolLattice, ToolLatticeManager, agentLatticeManager, getAgentClient, getAgentConfig, getAgentLattice, getAllAgentConfigs, getAllToolDefinitions, getCheckpointSaver, getChunkBuffer, getModelLattice, getToolClient, getToolDefinition, getToolLattice, hasChunkBuffer, modelLatticeManager, registerAgentLattice, registerAgentLattices, registerCheckpointSaver, registerChunkBuffer, registerModelLattice, registerToolLattice, toolLatticeManager, validateAgentInput, validateToolInput };
|
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { BaseChatModel, BaseChatModelCallOptions } from '@langchain/core/languag
|
|
|
5
5
|
import { BaseLanguageModelInput } from '@langchain/core/language_models/base';
|
|
6
6
|
import { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
7
7
|
import { ChatResult } from '@langchain/core/outputs';
|
|
8
|
-
import { LLMConfig, ToolConfig, ToolExecutor, AgentConfig, GraphBuildOptions } from '@axiom-lattice/protocols';
|
|
8
|
+
import { LLMConfig, ToolConfig, ToolExecutor, AgentConfig, GraphBuildOptions, MessageChunk } from '@axiom-lattice/protocols';
|
|
9
9
|
import * as protocols from '@axiom-lattice/protocols';
|
|
10
10
|
export { protocols as Protocols };
|
|
11
11
|
export { AgentConfig, AgentType, GraphBuildOptions, MemoryType } from '@axiom-lattice/protocols';
|
|
@@ -13,6 +13,7 @@ import * as _langchain_core_tools from '@langchain/core/tools';
|
|
|
13
13
|
import { StructuredTool } from '@langchain/core/tools';
|
|
14
14
|
import { CompiledStateGraph } from '@langchain/langgraph';
|
|
15
15
|
import { BaseCheckpointSaver } from '@langchain/langgraph-checkpoint';
|
|
16
|
+
import { ReplaySubject } from 'rxjs';
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* BaseLatticeManager - 抽象基类,为各种Lattice管理器提供通用功能
|
|
@@ -442,4 +443,199 @@ declare class MemoryLatticeManager extends BaseLatticeManager {
|
|
|
442
443
|
declare const getCheckpointSaver: (key: string) => BaseCheckpointSaver<number>;
|
|
443
444
|
declare const registerCheckpointSaver: (key: string, saver: BaseCheckpointSaver) => void;
|
|
444
445
|
|
|
445
|
-
|
|
446
|
+
/**
|
|
447
|
+
* ChunkBuffer Types
|
|
448
|
+
*
|
|
449
|
+
* Defines types for managing streaming chunks organized by thread
|
|
450
|
+
*/
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Represents a single chunk of data
|
|
454
|
+
* Chunks are identified by their position in the sequence (no unique ID)
|
|
455
|
+
*/
|
|
456
|
+
interface Chunk {
|
|
457
|
+
messageId: string;
|
|
458
|
+
content: MessageChunk;
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Thread status - requires explicit state transitions
|
|
462
|
+
*/
|
|
463
|
+
declare enum ThreadStatus {
|
|
464
|
+
ACTIVE = "active",
|
|
465
|
+
COMPLETED = "completed",
|
|
466
|
+
ABORTED = "aborted"
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Thread buffer configuration
|
|
470
|
+
*/
|
|
471
|
+
interface ThreadBufferConfig {
|
|
472
|
+
ttl?: number;
|
|
473
|
+
cleanupInterval?: number;
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Thread buffer state
|
|
477
|
+
*/
|
|
478
|
+
interface ThreadBuffer {
|
|
479
|
+
threadId: string;
|
|
480
|
+
chunks$: ReplaySubject<MessageChunk>;
|
|
481
|
+
status: ThreadStatus;
|
|
482
|
+
createdAt: number;
|
|
483
|
+
updatedAt: number;
|
|
484
|
+
expiresAt: number;
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Buffer statistics
|
|
488
|
+
*/
|
|
489
|
+
interface BufferStats {
|
|
490
|
+
totalThreads: number;
|
|
491
|
+
activeThreads: number;
|
|
492
|
+
completedThreads: number;
|
|
493
|
+
abortedThreads: number;
|
|
494
|
+
config: Required<ThreadBufferConfig>;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* ChunkBuffer Abstract Base Class
|
|
499
|
+
*
|
|
500
|
+
* Defines the interface for chunk buffer implementations
|
|
501
|
+
*/
|
|
502
|
+
|
|
503
|
+
declare abstract class ChunkBuffer {
|
|
504
|
+
/**
|
|
505
|
+
* Add a chunk to a thread (creates thread if not exists)
|
|
506
|
+
* Chunks are appended in order
|
|
507
|
+
*/
|
|
508
|
+
abstract addChunk(threadId: string, content: MessageChunk): Promise<void>;
|
|
509
|
+
/**
|
|
510
|
+
* Mark thread as completed (explicit call)
|
|
511
|
+
*/
|
|
512
|
+
abstract completeThread(threadId: string): Promise<void>;
|
|
513
|
+
/**
|
|
514
|
+
* Mark thread as aborted (explicit call)
|
|
515
|
+
*/
|
|
516
|
+
abstract abortThread(threadId: string): Promise<void>;
|
|
517
|
+
/**
|
|
518
|
+
* Check if thread is still active
|
|
519
|
+
*/
|
|
520
|
+
abstract isThreadActive(threadId: string): Promise<boolean>;
|
|
521
|
+
/**
|
|
522
|
+
* Get thread status (returns undefined if thread doesn't exist)
|
|
523
|
+
*/
|
|
524
|
+
abstract getThreadStatus(threadId: string): Promise<ThreadStatus | undefined>;
|
|
525
|
+
/**
|
|
526
|
+
* Get thread buffer info including metadata
|
|
527
|
+
*/
|
|
528
|
+
abstract getThreadBuffer(threadId: string): Promise<ThreadBuffer | undefined>;
|
|
529
|
+
/**
|
|
530
|
+
* Clear specific thread buffer
|
|
531
|
+
*/
|
|
532
|
+
abstract clearThread(threadId: string): Promise<void>;
|
|
533
|
+
/**
|
|
534
|
+
* Get all active thread IDs
|
|
535
|
+
*/
|
|
536
|
+
abstract getActiveThreads(): Promise<string[]>;
|
|
537
|
+
/**
|
|
538
|
+
* Get all thread IDs (regardless of status)
|
|
539
|
+
*/
|
|
540
|
+
abstract getAllThreads(): Promise<string[]>;
|
|
541
|
+
/**
|
|
542
|
+
* Manually trigger cleanup of expired threads
|
|
543
|
+
* Returns number of threads cleaned up
|
|
544
|
+
*/
|
|
545
|
+
abstract cleanupExpiredThreads(): Promise<number>;
|
|
546
|
+
/**
|
|
547
|
+
* Extend thread TTL (reset expiration time)
|
|
548
|
+
*/
|
|
549
|
+
abstract extendThreadTTL(threadId: string, additionalMs?: number): Promise<void>;
|
|
550
|
+
/**
|
|
551
|
+
* Check if a thread exists (valid or expired)
|
|
552
|
+
*/
|
|
553
|
+
abstract hasThread(threadId: string): Promise<boolean>;
|
|
554
|
+
abstract getNewChunksSinceContentIterator(threadId: string, messageId: string, knownContent: string): AsyncIterable<MessageChunk>;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* InMemoryChunkBuffer
|
|
559
|
+
*
|
|
560
|
+
* In-memory implementation of ChunkBuffer with hybrid cleanup strategy:
|
|
561
|
+
* - Lazy cleanup: expired threads are removed on access
|
|
562
|
+
* - Optional periodic cleanup: background timer to clean expired threads
|
|
563
|
+
*/
|
|
564
|
+
|
|
565
|
+
declare class InMemoryChunkBuffer extends ChunkBuffer {
|
|
566
|
+
private buffers;
|
|
567
|
+
private config;
|
|
568
|
+
private cleanupTimer?;
|
|
569
|
+
constructor(config?: ThreadBufferConfig & {
|
|
570
|
+
maxChunks?: number;
|
|
571
|
+
});
|
|
572
|
+
/**
|
|
573
|
+
* Start automatic periodic cleanup timer
|
|
574
|
+
*/
|
|
575
|
+
private startCleanupTimer;
|
|
576
|
+
/**
|
|
577
|
+
* Stop cleanup timer (for cleanup/shutdown)
|
|
578
|
+
*/
|
|
579
|
+
stopCleanupTimer(): void;
|
|
580
|
+
/**
|
|
581
|
+
* Check if a buffer is expired (lazy cleanup helper)
|
|
582
|
+
*/
|
|
583
|
+
private isExpired;
|
|
584
|
+
/**
|
|
585
|
+
* Get buffer if valid, perform lazy cleanup if expired
|
|
586
|
+
*/
|
|
587
|
+
private getBufferIfValid;
|
|
588
|
+
/**
|
|
589
|
+
* Create or get thread buffer
|
|
590
|
+
*/
|
|
591
|
+
private getOrCreateBuffer;
|
|
592
|
+
addChunk(threadId: string, content: MessageChunk): Promise<void>;
|
|
593
|
+
completeThread(threadId: string): Promise<void>;
|
|
594
|
+
abortThread(threadId: string): Promise<void>;
|
|
595
|
+
isThreadActive(threadId: string): Promise<boolean>;
|
|
596
|
+
getThreadStatus(threadId: string): Promise<ThreadStatus | undefined>;
|
|
597
|
+
getThreadBuffer(threadId: string): Promise<ThreadBuffer | undefined>;
|
|
598
|
+
clearThread(threadId: string): Promise<void>;
|
|
599
|
+
getActiveThreads(): Promise<string[]>;
|
|
600
|
+
getAllThreads(): Promise<string[]>;
|
|
601
|
+
hasThread(threadId: string): Promise<boolean>;
|
|
602
|
+
/**
|
|
603
|
+
* Cleanup expired threads based on TTL
|
|
604
|
+
* Returns number of threads cleaned up
|
|
605
|
+
*/
|
|
606
|
+
cleanupExpiredThreads(): Promise<number>;
|
|
607
|
+
extendThreadTTL(threadId: string, additionalMs?: number): Promise<void>;
|
|
608
|
+
getNewChunksSinceContentIterator(threadId: string, messageId: string, knownContent: string): AsyncIterable<MessageChunk>;
|
|
609
|
+
getStats(): BufferStats;
|
|
610
|
+
dispose(): void;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* ChunkBufferLatticeManager
|
|
615
|
+
*
|
|
616
|
+
* Manages ChunkBuffer instances following the Lattice pattern
|
|
617
|
+
*/
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* ChunkBuffer Lattice Manager
|
|
621
|
+
*/
|
|
622
|
+
declare class ChunkBufferLatticeManager extends BaseLatticeManager<ChunkBuffer> {
|
|
623
|
+
private static instance;
|
|
624
|
+
/**
|
|
625
|
+
* Private constructor for singleton pattern
|
|
626
|
+
*/
|
|
627
|
+
private constructor();
|
|
628
|
+
/**
|
|
629
|
+
* Get singleton instance
|
|
630
|
+
*/
|
|
631
|
+
static getInstance(): ChunkBufferLatticeManager;
|
|
632
|
+
/**
|
|
633
|
+
* Get Lattice type identifier
|
|
634
|
+
*/
|
|
635
|
+
protected getLatticeType(): string;
|
|
636
|
+
}
|
|
637
|
+
declare const getChunkBuffer: (key: string) => ChunkBuffer | undefined;
|
|
638
|
+
declare const registerChunkBuffer: (key: string, buffer: ChunkBuffer) => void;
|
|
639
|
+
declare const hasChunkBuffer: (key: string) => boolean;
|
|
640
|
+
|
|
641
|
+
export { type AgentClient, type AgentLattice, AgentLatticeManager, type BufferStats, type Chunk, ChunkBuffer, ChunkBufferLatticeManager, InMemoryChunkBuffer, MemoryLatticeManager, type ModelConfig, type ModelLatticeInterface, ModelLatticeManager, type ThreadBuffer, type ThreadBufferConfig, ThreadStatus, type ToolDefinition, type ToolLattice, ToolLatticeManager, agentLatticeManager, getAgentClient, getAgentConfig, getAgentLattice, getAllAgentConfigs, getAllToolDefinitions, getCheckpointSaver, getChunkBuffer, getModelLattice, getToolClient, getToolDefinition, getToolLattice, hasChunkBuffer, modelLatticeManager, registerAgentLattice, registerAgentLattices, registerCheckpointSaver, registerChunkBuffer, registerModelLattice, registerToolLattice, toolLatticeManager, validateAgentInput, validateToolInput };
|