@alveus-ai/std 0.1.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/agents/basic-chat.d.ts +80 -0
- package/dist/agents/basic-chat.d.ts.map +1 -0
- package/dist/agents/basic-chat.js +91 -0
- package/dist/agents/basic-chat.js.map +1 -0
- package/dist/agents/index.d.ts +8 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/index.js +14 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/react.d.ts +153 -0
- package/dist/agents/react.d.ts.map +1 -0
- package/dist/agents/react.js +298 -0
- package/dist/agents/react.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/memory/history.d.ts +109 -0
- package/dist/memory/history.d.ts.map +1 -0
- package/dist/memory/history.js +140 -0
- package/dist/memory/history.js.map +1 -0
- package/dist/memory/index.d.ts +7 -0
- package/dist/memory/index.d.ts.map +1 -0
- package/dist/memory/index.js +7 -0
- package/dist/memory/index.js.map +1 -0
- package/dist/patterns/approval.d.ts +151 -0
- package/dist/patterns/approval.d.ts.map +1 -0
- package/dist/patterns/approval.js +183 -0
- package/dist/patterns/approval.js.map +1 -0
- package/dist/patterns/index.d.ts +7 -0
- package/dist/patterns/index.d.ts.map +1 -0
- package/dist/patterns/index.js +7 -0
- package/dist/patterns/index.js.map +1 -0
- package/package.json +45 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.js","sourceRoot":"","sources":["../../src/agents/react.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AA2EzC;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAK9B,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,GAA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AAC;gBACb,MAAM,IAAI,KAAK,CACb,0BAA0B,GAAG,gBAAgB,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CACrF,CAAC;YACJ,CAAC;YACD,KAAK,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC;QACxB,CAAC;aAAM,CAAC;YACN,oBAAoB;YACpB,MAAM,SAAS,GAAG,OAAO,GAAG,CAAC,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC;YAExF,KAAK,CAAC,IAAI,CAAC,GAAG;gBACZ,IAAI,EAAE,SAAS;gBACf,WAAW,EAAE,GAAG,CAAC,WAAW;gBAC5B,UAAU,EAAE,GAAG,CAAC,UAAU;aAC3B,CAAC;QACJ,CAAC;IACH,CAAC;IAED,OAAO;QACL,UAAU,EAAE,UAAU;QACtB,YAAY,EAAE;YACZ,YAAY,EAAE,OAAO,CAAC,YAAY;YAClC,KAAK;YACL,aAAa,EAAE,OAAO,CAAC,aAAa,IAAI,EAAE;SAC3C;KACF,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @alveus-ai/std - Standard Library for Alveus
|
|
3
|
+
*
|
|
4
|
+
* Provides reusable agents and utilities for common patterns.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { createChatAgent } from '@alveus-ai/std/agents';
|
|
9
|
+
* import { saveHistory, loadHistory } from '@alveus-ai/std/memory';
|
|
10
|
+
*
|
|
11
|
+
* // Create a chat agent
|
|
12
|
+
* const assistant = createChatAgent('You are a helpful assistant.');
|
|
13
|
+
*
|
|
14
|
+
* // In agent handler, manage history
|
|
15
|
+
* const history = await loadHistory(ctx, event.historyRef);
|
|
16
|
+
* // ... use history
|
|
17
|
+
* const ref = await saveHistory(ctx, history);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export * from './agents/index.js';
|
|
21
|
+
export * from './memory/index.js';
|
|
22
|
+
export * from './patterns/index.js';
|
|
23
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,cAAc,mBAAmB,CAAC;AAGlC,cAAc,mBAAmB,CAAC;AAGlC,cAAc,qBAAqB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @alveus-ai/std - Standard Library for Alveus
|
|
3
|
+
*
|
|
4
|
+
* Provides reusable agents and utilities for common patterns.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { createChatAgent } from '@alveus-ai/std/agents';
|
|
9
|
+
* import { saveHistory, loadHistory } from '@alveus-ai/std/memory';
|
|
10
|
+
*
|
|
11
|
+
* // Create a chat agent
|
|
12
|
+
* const assistant = createChatAgent('You are a helpful assistant.');
|
|
13
|
+
*
|
|
14
|
+
* // In agent handler, manage history
|
|
15
|
+
* const history = await loadHistory(ctx, event.historyRef);
|
|
16
|
+
* // ... use history
|
|
17
|
+
* const ref = await saveHistory(ctx, history);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
// Re-export all agents
|
|
21
|
+
export * from './agents/index.js';
|
|
22
|
+
// Re-export all memory utilities
|
|
23
|
+
export * from './memory/index.js';
|
|
24
|
+
// Re-export all patterns
|
|
25
|
+
export * from './patterns/index.js';
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,uBAAuB;AACvB,cAAc,mBAAmB,CAAC;AAElC,iCAAiC;AACjC,cAAc,mBAAmB,CAAC;AAElC,yBAAyB;AACzB,cAAc,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* History Manager - Intelligent Context Window Management
|
|
3
|
+
*
|
|
4
|
+
* Handles large conversation histories by offloading to blob storage.
|
|
5
|
+
* This solves the "Infinite Context" problem where conversations can
|
|
6
|
+
* grow beyond message size limits.
|
|
7
|
+
*
|
|
8
|
+
* Key features:
|
|
9
|
+
* - Automatic offloading when history exceeds threshold
|
|
10
|
+
* - Content-addressed storage for deduplication
|
|
11
|
+
* - Transparent loading/saving API
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* // In an agent handler:
|
|
16
|
+
* const history = await loadHistory(ctx, event.historyRef);
|
|
17
|
+
* history.push({ role: 'user', content: event.message });
|
|
18
|
+
* history.push({ role: 'assistant', content: reply });
|
|
19
|
+
*
|
|
20
|
+
* // Save and potentially offload
|
|
21
|
+
* const ref = await saveHistory(ctx, history);
|
|
22
|
+
*
|
|
23
|
+
* // Pass ref to child agent (small payload even if history is huge)
|
|
24
|
+
* await ctx.call(childAgent, { historyRef: ref });
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
import type { ExecutionContext, ChatMessage } from '@alveus-ai/core';
|
|
28
|
+
/**
|
|
29
|
+
* Reference to history stored in blob storage
|
|
30
|
+
*/
|
|
31
|
+
export interface HistoryRef {
|
|
32
|
+
type: 'blob_ref';
|
|
33
|
+
key: string;
|
|
34
|
+
summary?: string;
|
|
35
|
+
messageCount?: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Union type for history that may be inline or referenced
|
|
39
|
+
*/
|
|
40
|
+
export type HistoryData = ChatMessage[] | HistoryRef;
|
|
41
|
+
/**
|
|
42
|
+
* Options for history management
|
|
43
|
+
*/
|
|
44
|
+
export interface HistoryOptions {
|
|
45
|
+
/**
|
|
46
|
+
* Size threshold in bytes before offloading to blob storage
|
|
47
|
+
* Default: 10KB
|
|
48
|
+
*/
|
|
49
|
+
offloadThreshold?: number;
|
|
50
|
+
/**
|
|
51
|
+
* Include a summary in the reference for debugging
|
|
52
|
+
* Default: true
|
|
53
|
+
*/
|
|
54
|
+
includeSummary?: boolean;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Check if data is a blob reference
|
|
58
|
+
*/
|
|
59
|
+
export declare function isHistoryRef(data: HistoryData): data is HistoryRef;
|
|
60
|
+
/**
|
|
61
|
+
* Save conversation history, offloading to blob storage if large
|
|
62
|
+
*
|
|
63
|
+
* @param ctx - The execution context with storage capability
|
|
64
|
+
* @param messages - The conversation history to save
|
|
65
|
+
* @param options - Configuration options
|
|
66
|
+
* @returns Inline messages if small, or a HistoryRef if offloaded
|
|
67
|
+
*/
|
|
68
|
+
export declare function saveHistory(ctx: ExecutionContext, messages: ChatMessage[], options?: HistoryOptions): Promise<HistoryData>;
|
|
69
|
+
/**
|
|
70
|
+
* Load conversation history, retrieving from blob storage if necessary
|
|
71
|
+
*
|
|
72
|
+
* @param ctx - The execution context with storage capability
|
|
73
|
+
* @param ref - The history data (inline messages or blob reference)
|
|
74
|
+
* @returns The conversation history as an array of messages
|
|
75
|
+
*/
|
|
76
|
+
export declare function loadHistory(ctx: ExecutionContext, ref: HistoryData | undefined | null): Promise<ChatMessage[]>;
|
|
77
|
+
/**
|
|
78
|
+
* Append a message to history and save
|
|
79
|
+
*
|
|
80
|
+
* Convenience function for common pattern of adding a message and saving.
|
|
81
|
+
*
|
|
82
|
+
* @param ctx - The execution context
|
|
83
|
+
* @param history - Current history (may be ref or inline)
|
|
84
|
+
* @param message - Message to append
|
|
85
|
+
* @param options - Save options
|
|
86
|
+
* @returns Updated history reference
|
|
87
|
+
*/
|
|
88
|
+
export declare function appendAndSave(ctx: ExecutionContext, history: HistoryData | undefined | null, message: ChatMessage, options?: HistoryOptions): Promise<HistoryData>;
|
|
89
|
+
/**
|
|
90
|
+
* Truncate history to a maximum number of messages
|
|
91
|
+
*
|
|
92
|
+
* Keeps the most recent messages while preserving the system prompt.
|
|
93
|
+
*
|
|
94
|
+
* @param messages - The full history
|
|
95
|
+
* @param maxMessages - Maximum messages to keep (excluding system)
|
|
96
|
+
* @returns Truncated history
|
|
97
|
+
*/
|
|
98
|
+
export declare function truncateHistory(messages: ChatMessage[], maxMessages: number): ChatMessage[];
|
|
99
|
+
/**
|
|
100
|
+
* Calculate approximate token count for history
|
|
101
|
+
*
|
|
102
|
+
* Uses a simple heuristic (4 chars per token) for estimation.
|
|
103
|
+
* For precise counts, use a tokenizer like tiktoken.
|
|
104
|
+
*
|
|
105
|
+
* @param messages - The conversation history
|
|
106
|
+
* @returns Estimated token count
|
|
107
|
+
*/
|
|
108
|
+
export declare function estimateTokens(messages: ChatMessage[]): number;
|
|
109
|
+
//# sourceMappingURL=history.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"history.d.ts","sourceRoot":"","sources":["../../src/memory/history.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAErE;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,UAAU,CAAC;IACjB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,WAAW,EAAE,GAAG,UAAU,CAAC;AAErD;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAE1B;;;OAGG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAOD;;GAEG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,WAAW,GAAG,IAAI,IAAI,UAAU,CAIlE;AAED;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAC/B,GAAG,EAAE,gBAAgB,EACrB,QAAQ,EAAE,WAAW,EAAE,EACvB,OAAO,GAAE,cAAmB,GAC3B,OAAO,CAAC,WAAW,CAAC,CAuBtB;AAED;;;;;;GAMG;AACH,wBAAsB,WAAW,CAC/B,GAAG,EAAE,gBAAgB,EACrB,GAAG,EAAE,WAAW,GAAG,SAAS,GAAG,IAAI,GAClC,OAAO,CAAC,WAAW,EAAE,CAAC,CAsBxB;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,aAAa,CACjC,GAAG,EAAE,gBAAgB,EACrB,OAAO,EAAE,WAAW,GAAG,SAAS,GAAG,IAAI,EACvC,OAAO,EAAE,WAAW,EACpB,OAAO,GAAE,cAAmB,GAC3B,OAAO,CAAC,WAAW,CAAC,CAItB;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,WAAW,EAAE,EAAE,WAAW,EAAE,MAAM,GAAG,WAAW,EAAE,CAY3F;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,WAAW,EAAE,GAAG,MAAM,CAG9D"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* History Manager - Intelligent Context Window Management
|
|
3
|
+
*
|
|
4
|
+
* Handles large conversation histories by offloading to blob storage.
|
|
5
|
+
* This solves the "Infinite Context" problem where conversations can
|
|
6
|
+
* grow beyond message size limits.
|
|
7
|
+
*
|
|
8
|
+
* Key features:
|
|
9
|
+
* - Automatic offloading when history exceeds threshold
|
|
10
|
+
* - Content-addressed storage for deduplication
|
|
11
|
+
* - Transparent loading/saving API
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* // In an agent handler:
|
|
16
|
+
* const history = await loadHistory(ctx, event.historyRef);
|
|
17
|
+
* history.push({ role: 'user', content: event.message });
|
|
18
|
+
* history.push({ role: 'assistant', content: reply });
|
|
19
|
+
*
|
|
20
|
+
* // Save and potentially offload
|
|
21
|
+
* const ref = await saveHistory(ctx, history);
|
|
22
|
+
*
|
|
23
|
+
* // Pass ref to child agent (small payload even if history is huge)
|
|
24
|
+
* await ctx.call(childAgent, { historyRef: ref });
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
const DEFAULT_OPTIONS = {
|
|
28
|
+
offloadThreshold: 1024 * 10, // 10KB
|
|
29
|
+
includeSummary: true,
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Check if data is a blob reference
|
|
33
|
+
*/
|
|
34
|
+
export function isHistoryRef(data) {
|
|
35
|
+
return (typeof data === 'object' && !Array.isArray(data) && data !== null && data.type === 'blob_ref');
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Save conversation history, offloading to blob storage if large
|
|
39
|
+
*
|
|
40
|
+
* @param ctx - The execution context with storage capability
|
|
41
|
+
* @param messages - The conversation history to save
|
|
42
|
+
* @param options - Configuration options
|
|
43
|
+
* @returns Inline messages if small, or a HistoryRef if offloaded
|
|
44
|
+
*/
|
|
45
|
+
export async function saveHistory(ctx, messages, options = {}) {
|
|
46
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
47
|
+
const payload = JSON.stringify(messages);
|
|
48
|
+
// If under threshold, return inline
|
|
49
|
+
if (payload.length <= opts.offloadThreshold) {
|
|
50
|
+
return messages;
|
|
51
|
+
}
|
|
52
|
+
// Offload to blob storage
|
|
53
|
+
const key = await ctx.storage.put(payload);
|
|
54
|
+
const ref = {
|
|
55
|
+
type: 'blob_ref',
|
|
56
|
+
key,
|
|
57
|
+
messageCount: messages.length,
|
|
58
|
+
};
|
|
59
|
+
if (opts.includeSummary) {
|
|
60
|
+
ref.summary = `History of ${messages.length} messages (${(payload.length / 1024).toFixed(1)}KB)`;
|
|
61
|
+
}
|
|
62
|
+
return ref;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Load conversation history, retrieving from blob storage if necessary
|
|
66
|
+
*
|
|
67
|
+
* @param ctx - The execution context with storage capability
|
|
68
|
+
* @param ref - The history data (inline messages or blob reference)
|
|
69
|
+
* @returns The conversation history as an array of messages
|
|
70
|
+
*/
|
|
71
|
+
export async function loadHistory(ctx, ref) {
|
|
72
|
+
// Handle undefined/null
|
|
73
|
+
if (!ref) {
|
|
74
|
+
return [];
|
|
75
|
+
}
|
|
76
|
+
// If inline array, return directly
|
|
77
|
+
if (Array.isArray(ref)) {
|
|
78
|
+
return ref;
|
|
79
|
+
}
|
|
80
|
+
// Load from blob storage
|
|
81
|
+
if (isHistoryRef(ref)) {
|
|
82
|
+
const raw = await ctx.storage.get(ref.key);
|
|
83
|
+
if (!raw) {
|
|
84
|
+
console.warn(`History not found for key: ${ref.key}`);
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
87
|
+
return JSON.parse(raw);
|
|
88
|
+
}
|
|
89
|
+
return [];
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Append a message to history and save
|
|
93
|
+
*
|
|
94
|
+
* Convenience function for common pattern of adding a message and saving.
|
|
95
|
+
*
|
|
96
|
+
* @param ctx - The execution context
|
|
97
|
+
* @param history - Current history (may be ref or inline)
|
|
98
|
+
* @param message - Message to append
|
|
99
|
+
* @param options - Save options
|
|
100
|
+
* @returns Updated history reference
|
|
101
|
+
*/
|
|
102
|
+
export async function appendAndSave(ctx, history, message, options = {}) {
|
|
103
|
+
const messages = await loadHistory(ctx, history);
|
|
104
|
+
messages.push(message);
|
|
105
|
+
return saveHistory(ctx, messages, options);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Truncate history to a maximum number of messages
|
|
109
|
+
*
|
|
110
|
+
* Keeps the most recent messages while preserving the system prompt.
|
|
111
|
+
*
|
|
112
|
+
* @param messages - The full history
|
|
113
|
+
* @param maxMessages - Maximum messages to keep (excluding system)
|
|
114
|
+
* @returns Truncated history
|
|
115
|
+
*/
|
|
116
|
+
export function truncateHistory(messages, maxMessages) {
|
|
117
|
+
// Find system messages (usually first)
|
|
118
|
+
const systemMessages = messages.filter((m) => m.role === 'system');
|
|
119
|
+
const nonSystemMessages = messages.filter((m) => m.role !== 'system');
|
|
120
|
+
// Keep system messages + most recent conversation
|
|
121
|
+
if (nonSystemMessages.length <= maxMessages) {
|
|
122
|
+
return messages;
|
|
123
|
+
}
|
|
124
|
+
const recentMessages = nonSystemMessages.slice(-maxMessages);
|
|
125
|
+
return [...systemMessages, ...recentMessages];
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Calculate approximate token count for history
|
|
129
|
+
*
|
|
130
|
+
* Uses a simple heuristic (4 chars per token) for estimation.
|
|
131
|
+
* For precise counts, use a tokenizer like tiktoken.
|
|
132
|
+
*
|
|
133
|
+
* @param messages - The conversation history
|
|
134
|
+
* @returns Estimated token count
|
|
135
|
+
*/
|
|
136
|
+
export function estimateTokens(messages) {
|
|
137
|
+
const totalChars = messages.reduce((sum, m) => sum + m.content.length, 0);
|
|
138
|
+
return Math.ceil(totalChars / 4);
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=history.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"history.js","sourceRoot":"","sources":["../../src/memory/history.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAoCH,MAAM,eAAe,GAA6B;IAChD,gBAAgB,EAAE,IAAI,GAAG,EAAE,EAAE,OAAO;IACpC,cAAc,EAAE,IAAI;CACrB,CAAC;AAEF;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,IAAiB;IAC5C,OAAO,CACL,OAAO,IAAI,KAAK,QAAQ,IAAI,CA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AC,GAAG,cAAc,EAAE,GAAG,cAAc,CAAC,CAAC;AAChD,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,cAAc,CAAC,QAAuB;IACpD,MAAM,UAAU,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IAC1E,OAAO,IAAI,CAAC,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC,CAAC;AACnC,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory Management Utilities
|
|
3
|
+
*
|
|
4
|
+
* Tools for handling conversation history and large payloads.
|
|
5
|
+
*/
|
|
6
|
+
export { saveHistory, loadHistory, appendAndSave, truncateHistory, estimateTokens, isHistoryRef, type HistoryRef, type HistoryData, type HistoryOptions, } from './history.js';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/memory/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,WAAW,EACX,WAAW,EACX,aAAa,EACb,eAAe,EACf,cAAc,EACd,YAAY,EACZ,KAAK,UAAU,EACf,KAAK,WAAW,EAChB,KAAK,cAAc,GACpB,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/memory/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,WAAW,EACX,WAAW,EACX,aAAa,EACb,eAAe,EACf,cAAc,EACd,YAAY,GAIb,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Approval Pattern - Human-in-the-Loop Workflows
|
|
3
|
+
*
|
|
4
|
+
* Provides utilities for implementing approval workflows where
|
|
5
|
+
* agents can pause and wait for human decisions.
|
|
6
|
+
*
|
|
7
|
+
* Uses the Event capability for durable waiting:
|
|
8
|
+
* 1. Emit an "approval-needed" event with request details
|
|
9
|
+
* 2. Wait for an "approval-response" event
|
|
10
|
+
* 3. Continue based on the approval decision
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { waitForApproval, type ApprovalRequest } from '@alveus-ai/std/patterns';
|
|
15
|
+
*
|
|
16
|
+
* const myAgent = agent$(async (state, event, ctx) => {
|
|
17
|
+
* // Do some work...
|
|
18
|
+
* const result = await performRiskyOperation();
|
|
19
|
+
*
|
|
20
|
+
* // Request human approval before continuing
|
|
21
|
+
* const approval = await waitForApproval(ctx, {
|
|
22
|
+
* action: 'deploy-to-production',
|
|
23
|
+
* details: { version: '1.2.3', environment: 'prod' },
|
|
24
|
+
* message: 'Please approve deployment to production',
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* if (!approval.approved) {
|
|
28
|
+
* return { status: 'rejected', reason: approval.reason };
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* // Continue with deployment...
|
|
32
|
+
* return { status: 'deployed', approvedBy: approval.approvedBy };
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
import type { ExecutionContext } from '@alveus-ai/core';
|
|
37
|
+
/**
|
|
38
|
+
* Request for approval
|
|
39
|
+
*/
|
|
40
|
+
export interface ApprovalRequest {
|
|
41
|
+
/** The action requiring approval */
|
|
42
|
+
action: string;
|
|
43
|
+
/** Additional details about the action */
|
|
44
|
+
details?: Record<string, unknown>;
|
|
45
|
+
/** Human-readable message for the approver */
|
|
46
|
+
message?: string;
|
|
47
|
+
/** Timeout in milliseconds (default: 24 hours) */
|
|
48
|
+
timeoutMs?: number;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Response from an approval request
|
|
52
|
+
*/
|
|
53
|
+
export interface ApprovalResponse {
|
|
54
|
+
/** Whether the action was approved */
|
|
55
|
+
approved: boolean;
|
|
56
|
+
/** Who approved/rejected (if known) */
|
|
57
|
+
approvedBy?: string;
|
|
58
|
+
/** Reason for rejection (if rejected) */
|
|
59
|
+
reason?: string;
|
|
60
|
+
/** Timestamp of the decision */
|
|
61
|
+
timestamp: number;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Topics used for approval events
|
|
65
|
+
*/
|
|
66
|
+
export declare const ApprovalTopics: {
|
|
67
|
+
/** Topic for approval requests */
|
|
68
|
+
readonly NEEDED: "approval.needed";
|
|
69
|
+
/** Topic for approval responses */
|
|
70
|
+
readonly RESPONSE: "approval.response";
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Wait for human approval
|
|
74
|
+
*
|
|
75
|
+
* Emits an approval request event and durably waits for a response.
|
|
76
|
+
* The workflow will pause until an approval response is received.
|
|
77
|
+
*
|
|
78
|
+
* @param ctx - The execution context
|
|
79
|
+
* @param request - The approval request details
|
|
80
|
+
* @returns The approval response
|
|
81
|
+
* @throws Error if timeout is reached
|
|
82
|
+
*/
|
|
83
|
+
export declare function waitForApproval(ctx: ExecutionContext, request: ApprovalRequest): Promise<ApprovalResponse>;
|
|
84
|
+
/**
|
|
85
|
+
* Send an approval decision
|
|
86
|
+
*
|
|
87
|
+
* Use this from an approval UI or API to respond to an approval request.
|
|
88
|
+
* The response will be delivered to the waiting workflow via the Event Bridge.
|
|
89
|
+
*
|
|
90
|
+
* @param ctx - The execution context
|
|
91
|
+
* @param workflowId - The ID of the workflow waiting for approval
|
|
92
|
+
* @param approved - Whether to approve or reject
|
|
93
|
+
* @param options - Additional response options
|
|
94
|
+
*/
|
|
95
|
+
export declare function sendApproval(ctx: ExecutionContext, workflowId: string, approved: boolean, options?: {
|
|
96
|
+
approvedBy?: string;
|
|
97
|
+
reason?: string;
|
|
98
|
+
}): Promise<void>;
|
|
99
|
+
/**
|
|
100
|
+
* Create an approval-gated operation wrapper
|
|
101
|
+
*
|
|
102
|
+
* Wraps an async operation with an approval requirement.
|
|
103
|
+
* The operation will only execute if approval is granted.
|
|
104
|
+
*
|
|
105
|
+
* @param operation - The operation requiring approval
|
|
106
|
+
* @param getRequest - Function to build the approval request from the input
|
|
107
|
+
* @returns A function that requests approval before executing
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* const safeDelete = requireApproval(
|
|
112
|
+
* async (ctx, userId: string) => {
|
|
113
|
+
* await database.deleteUser(userId);
|
|
114
|
+
* return { deleted: userId };
|
|
115
|
+
* },
|
|
116
|
+
* (userId) => ({
|
|
117
|
+
* action: 'delete-user',
|
|
118
|
+
* details: { userId },
|
|
119
|
+
* message: `Delete user ${userId}?`,
|
|
120
|
+
* })
|
|
121
|
+
* );
|
|
122
|
+
*
|
|
123
|
+
* // In agent handler:
|
|
124
|
+
* const result = await safeDelete(ctx, 'user-123');
|
|
125
|
+
* if (!result.approved) {
|
|
126
|
+
* return { error: 'Deletion rejected' };
|
|
127
|
+
* }
|
|
128
|
+
* return result.result;
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
export declare function requireApproval<TInput, TResult>(operation: (ctx: ExecutionContext, input: TInput) => Promise<TResult>, getRequest: (input: TInput) => ApprovalRequest): (ctx: ExecutionContext, input: TInput) => Promise<{
|
|
132
|
+
approved: true;
|
|
133
|
+
result: TResult;
|
|
134
|
+
approvedBy?: string;
|
|
135
|
+
} | {
|
|
136
|
+
approved: false;
|
|
137
|
+
reason?: string;
|
|
138
|
+
}>;
|
|
139
|
+
/**
|
|
140
|
+
* Multi-step approval chain
|
|
141
|
+
*
|
|
142
|
+
* Requires multiple approvals before proceeding.
|
|
143
|
+
* Useful for critical operations requiring sign-off from multiple parties.
|
|
144
|
+
*
|
|
145
|
+
* @param ctx - The execution context
|
|
146
|
+
* @param request - The base approval request
|
|
147
|
+
* @param approvers - List of required approver identifiers
|
|
148
|
+
* @returns Array of approval responses
|
|
149
|
+
*/
|
|
150
|
+
export declare function waitForMultipleApprovals(ctx: ExecutionContext, request: ApprovalRequest, approvers: string[]): Promise<ApprovalResponse[]>;
|
|
151
|
+
//# sourceMappingURL=approval.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"approval.d.ts","sourceRoot":"","sources":["../../src/patterns/approval.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,oCAAoC;IACpC,MAAM,EAAE,MAAM,CAAC;IAEf,0CAA0C;IAC1C,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAElC,8CAA8C;IAC9C,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB,kDAAkD;IAClD,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,sCAAsC;IACtC,QAAQ,EAAE,OAAO,CAAC;IAElB,uCAAuC;IACvC,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB,yCAAyC;IACzC,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,gCAAgC;IAChC,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,eAAO,MAAM,cAAc;IACzB,kCAAkC;;IAGlC,mCAAmC;;CAE3B,CAAC;AAEX;;;;;;;;;;GAUG;AACH,wBAAsB,eAAe,CACnC,GAAG,EAAE,gBAAgB,EACrB,OAAO,EAAE,eAAe,GACvB,OAAO,CAAC,gBAAgB,CAAC,CAwB3B;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,YAAY,CAChC,GAAG,EAAE,gBAAgB,EACrB,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,OAAO,EACjB,OAAO,GAAE;IACP,UA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