saico 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/LICENSE +21 -0
- package/README.md +398 -0
- package/context.js +1056 -0
- package/index.js +130 -0
- package/itask.js +678 -0
- package/openai.js +72 -0
- package/package.json +49 -0
- package/redis.js +123 -0
- package/sid.js +207 -0
- package/util.js +110 -0
package/index.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Saico Library - Hierarchical AI Conversation Orchestrator
|
|
5
|
+
*
|
|
6
|
+
* Combines task hierarchy (Itask) with conversation contexts (Context) to create
|
|
7
|
+
* a unified system for managing AI conversations with:
|
|
8
|
+
* - Task-based organizational structure
|
|
9
|
+
* - Optional conversation contexts attached to tasks
|
|
10
|
+
* - Hierarchical message aggregation with function collection
|
|
11
|
+
* - Full tool_calls support with depth control
|
|
12
|
+
*
|
|
13
|
+
* Main Components:
|
|
14
|
+
* - Itask: Base task class for all tasks (supports states, cancellation, promises)
|
|
15
|
+
* - Context: Conversation context with message handling and tool calls
|
|
16
|
+
* - Sid: Session root task (extends Itask, always has a context)
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const Itask = require('./itask.js');
|
|
20
|
+
const { Context, createContext } = require('./context.js');
|
|
21
|
+
const { Sid, createSid } = require('./sid.js');
|
|
22
|
+
|
|
23
|
+
// Wire up Context class reference in Itask to avoid circular dependency
|
|
24
|
+
Itask.Context = Context;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Create a new task with optional context.
|
|
28
|
+
*
|
|
29
|
+
* @param {Object|string} opt - Task options or name string
|
|
30
|
+
* @param {string} opt.name - Task name
|
|
31
|
+
* @param {string} opt.prompt - System prompt (if provided, creates a context)
|
|
32
|
+
* @param {Function} opt.tool_handler - Tool handler function
|
|
33
|
+
* @param {Array} opt.functions - Available functions for AI
|
|
34
|
+
* @param {boolean} opt.cancel - Whether task is cancelable
|
|
35
|
+
* @param {Object} opt.bind - Bind context for state functions
|
|
36
|
+
* @param {Itask} opt.spawn_parent - Parent task to spawn under
|
|
37
|
+
* @param {boolean} opt.async - If true, don't auto-run
|
|
38
|
+
* @param {Array} states - Array of state functions
|
|
39
|
+
* @returns {Itask} The created task
|
|
40
|
+
*/
|
|
41
|
+
function createTask(opt, states = []) {
|
|
42
|
+
if (typeof opt === 'string')
|
|
43
|
+
opt = { name: opt };
|
|
44
|
+
|
|
45
|
+
const task = new Itask(opt, states);
|
|
46
|
+
|
|
47
|
+
// Auto-create context if prompt is provided
|
|
48
|
+
if (opt.prompt) {
|
|
49
|
+
const context = new Context(opt.prompt, task, {
|
|
50
|
+
tag: opt.tag || task.id,
|
|
51
|
+
token_limit: opt.token_limit,
|
|
52
|
+
max_depth: opt.max_depth,
|
|
53
|
+
max_tool_repetition: opt.max_tool_repetition,
|
|
54
|
+
tool_handler: opt.tool_handler,
|
|
55
|
+
functions: opt.functions,
|
|
56
|
+
sequential_mode: opt.sequential_mode
|
|
57
|
+
});
|
|
58
|
+
task.setContext(context);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return task;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Legacy createQ function for backward compatibility.
|
|
66
|
+
* Creates a standalone Context (not attached to a task).
|
|
67
|
+
*
|
|
68
|
+
* @param {string} prompt - System prompt
|
|
69
|
+
* @param {Context} parent - Parent context (legacy, will be converted to task-based)
|
|
70
|
+
* @param {string} tag - Context tag identifier
|
|
71
|
+
* @param {number} token_limit - Token limit for summarization
|
|
72
|
+
* @param {Array} msgs - Initial messages
|
|
73
|
+
* @param {Function} tool_handler - Tool handler function
|
|
74
|
+
* @param {Object} config - Additional configuration
|
|
75
|
+
* @returns {Context} Proxied Context instance
|
|
76
|
+
*/
|
|
77
|
+
function createQ(prompt, parent, tag, token_limit, msgs, tool_handler, config = {}) {
|
|
78
|
+
// For backward compatibility, if parent is a Context, get its task
|
|
79
|
+
let task = null;
|
|
80
|
+
if (parent && parent.task) {
|
|
81
|
+
task = parent.task;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const context = createContext(prompt, task, {
|
|
85
|
+
tag,
|
|
86
|
+
token_limit,
|
|
87
|
+
msgs,
|
|
88
|
+
tool_handler,
|
|
89
|
+
...config
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// If there's a parent context, set up the relationship via tasks
|
|
93
|
+
if (parent && parent.task) {
|
|
94
|
+
// Create a child task to hold this context
|
|
95
|
+
const childTask = new Itask({
|
|
96
|
+
name: tag || 'child-context',
|
|
97
|
+
async: true,
|
|
98
|
+
spawn_parent: parent.task
|
|
99
|
+
}, []);
|
|
100
|
+
context.setTask(childTask);
|
|
101
|
+
childTask.setContext(context);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return context;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Export all components
|
|
108
|
+
module.exports = {
|
|
109
|
+
// Core classes
|
|
110
|
+
Itask,
|
|
111
|
+
Context,
|
|
112
|
+
Sid,
|
|
113
|
+
|
|
114
|
+
// Factory functions
|
|
115
|
+
createTask,
|
|
116
|
+
createSid,
|
|
117
|
+
createContext,
|
|
118
|
+
|
|
119
|
+
// Legacy compatibility
|
|
120
|
+
createQ,
|
|
121
|
+
|
|
122
|
+
// Utilities (re-export from util.js)
|
|
123
|
+
util: require('./util.js'),
|
|
124
|
+
|
|
125
|
+
// OpenAI wrapper (re-export)
|
|
126
|
+
openai: require('./openai.js'),
|
|
127
|
+
|
|
128
|
+
// Redis persistence (re-export)
|
|
129
|
+
redis: require('./redis.js')
|
|
130
|
+
};
|