@auxiora/runtime 1.3.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 +191 -0
- package/dist/agent-router.d.ts +104 -0
- package/dist/agent-router.d.ts.map +1 -0
- package/dist/agent-router.js +128 -0
- package/dist/agent-router.js.map +1 -0
- package/dist/block-stream.d.ts +94 -0
- package/dist/block-stream.d.ts.map +1 -0
- package/dist/block-stream.js +120 -0
- package/dist/block-stream.js.map +1 -0
- package/dist/index.d.ts +149 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2981 -0
- package/dist/index.js.map +1 -0
- package/dist/tool-loop-detection.d.ts +48 -0
- package/dist/tool-loop-detection.d.ts.map +1 -0
- package/dist/tool-loop-detection.js +249 -0
- package/dist/tool-loop-detection.js.map +1 -0
- package/package.json +82 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding any notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
Copyright 2026 Auxiora Contributors
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { SessionManager } from '@auxiora/sessions';
|
|
2
|
+
/**
|
|
3
|
+
* Routing rule that maps incoming messages to agent instances.
|
|
4
|
+
*/
|
|
5
|
+
export interface AgentRoutingRule {
|
|
6
|
+
/** Unique ID for this rule */
|
|
7
|
+
id: string;
|
|
8
|
+
/** Match by channel type (e.g. 'discord', 'telegram', 'webchat') */
|
|
9
|
+
channelType?: string;
|
|
10
|
+
/** Match by sender ID pattern (exact or glob with *) */
|
|
11
|
+
senderId?: string;
|
|
12
|
+
/** Match by channel ID pattern (exact or glob with *) */
|
|
13
|
+
channelId?: string;
|
|
14
|
+
/** The agent instance ID to route to */
|
|
15
|
+
agentId: string;
|
|
16
|
+
/** Priority (higher = checked first). Default 0. */
|
|
17
|
+
priority?: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Configuration for an individual agent instance.
|
|
21
|
+
*/
|
|
22
|
+
export interface AgentConfig {
|
|
23
|
+
id: string;
|
|
24
|
+
name: string;
|
|
25
|
+
systemPrompt?: string;
|
|
26
|
+
workspacePath?: string;
|
|
27
|
+
/** Provider override (uses default if not set) */
|
|
28
|
+
provider?: string;
|
|
29
|
+
/** Model override */
|
|
30
|
+
model?: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Full agent routing configuration.
|
|
34
|
+
*/
|
|
35
|
+
export interface AgentRoutingConfig {
|
|
36
|
+
enabled: boolean;
|
|
37
|
+
/** Default agent ID when no rules match */
|
|
38
|
+
defaultAgentId: string;
|
|
39
|
+
agents: AgentConfig[];
|
|
40
|
+
rules: AgentRoutingRule[];
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Holds isolated state for a single agent instance.
|
|
44
|
+
*/
|
|
45
|
+
export declare class AgentInstance {
|
|
46
|
+
readonly id: string;
|
|
47
|
+
readonly name: string;
|
|
48
|
+
readonly systemPrompt: string;
|
|
49
|
+
readonly workspacePath: string;
|
|
50
|
+
readonly providerOverride?: string;
|
|
51
|
+
readonly modelOverride?: string;
|
|
52
|
+
private sessions;
|
|
53
|
+
constructor(config: AgentConfig, sessionConfig?: {
|
|
54
|
+
maxContextTokens?: number;
|
|
55
|
+
ttlMinutes?: number;
|
|
56
|
+
});
|
|
57
|
+
initialize(): Promise<void>;
|
|
58
|
+
getSessionManager(): SessionManager;
|
|
59
|
+
destroy(): void;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Routes incoming messages to the appropriate agent instance
|
|
63
|
+
* based on channel type, sender ID, and channel ID patterns.
|
|
64
|
+
*/
|
|
65
|
+
export declare class AgentRouter {
|
|
66
|
+
private agents;
|
|
67
|
+
private rules;
|
|
68
|
+
private defaultAgentId;
|
|
69
|
+
constructor(config: AgentRoutingConfig);
|
|
70
|
+
/**
|
|
71
|
+
* Register an agent instance. Call after constructing.
|
|
72
|
+
*/
|
|
73
|
+
registerAgent(agent: AgentInstance): void;
|
|
74
|
+
/**
|
|
75
|
+
* Remove an agent instance.
|
|
76
|
+
*/
|
|
77
|
+
removeAgent(id: string): void;
|
|
78
|
+
/**
|
|
79
|
+
* Route an incoming message context to an agent instance.
|
|
80
|
+
*/
|
|
81
|
+
route(context: {
|
|
82
|
+
channelType?: string;
|
|
83
|
+
senderId?: string;
|
|
84
|
+
channelId?: string;
|
|
85
|
+
}): AgentInstance | undefined;
|
|
86
|
+
/**
|
|
87
|
+
* Get an agent by ID.
|
|
88
|
+
*/
|
|
89
|
+
getAgent(id: string): AgentInstance | undefined;
|
|
90
|
+
/**
|
|
91
|
+
* List all registered agents.
|
|
92
|
+
*/
|
|
93
|
+
listAgents(): AgentInstance[];
|
|
94
|
+
/**
|
|
95
|
+
* Destroy all agents and clean up.
|
|
96
|
+
*/
|
|
97
|
+
destroy(): void;
|
|
98
|
+
private matchesRule;
|
|
99
|
+
/**
|
|
100
|
+
* Simple glob matching: supports * as wildcard for any sequence of characters.
|
|
101
|
+
*/
|
|
102
|
+
private matchPattern;
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=agent-router.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-router.d.ts","sourceRoot":"","sources":["../src/agent-router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAGnD;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,8BAA8B;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,oEAAoE;IACpE,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,wDAAwD;IACxD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,yDAAyD;IACzD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,wCAAwC;IACxC,OAAO,EAAE,MAAM,CAAC;IAChB,oDAAoD;IACpD,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE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AAM,CAAC;QACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,GAAG,aAAa,GAAG,SAAS;IAS7B;;OAEG;IACH,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS;IAI/C;;OAEG;IACH,UAAU,IAAI,aAAa,EAAE;IAI7B;;OAEG;IACH,OAAO,IAAI,IAAI;IAOf,OAAO,CAAC,WAAW;IAoBnB;;OAEG;IACH,OAAO,CAAC,YAAY;CASrB"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { SessionManager } from '@auxiora/sessions';
|
|
2
|
+
/**
|
|
3
|
+
* Holds isolated state for a single agent instance.
|
|
4
|
+
*/
|
|
5
|
+
export class AgentInstance {
|
|
6
|
+
id;
|
|
7
|
+
name;
|
|
8
|
+
systemPrompt;
|
|
9
|
+
workspacePath;
|
|
10
|
+
providerOverride;
|
|
11
|
+
modelOverride;
|
|
12
|
+
sessions;
|
|
13
|
+
constructor(config, sessionConfig) {
|
|
14
|
+
this.id = config.id;
|
|
15
|
+
this.name = config.name;
|
|
16
|
+
this.systemPrompt = config.systemPrompt ?? '';
|
|
17
|
+
this.workspacePath = config.workspacePath ?? '';
|
|
18
|
+
this.providerOverride = config.provider;
|
|
19
|
+
this.modelOverride = config.model;
|
|
20
|
+
this.sessions = new SessionManager({
|
|
21
|
+
maxContextTokens: sessionConfig?.maxContextTokens ?? 100000,
|
|
22
|
+
ttlMinutes: sessionConfig?.ttlMinutes ?? 1440,
|
|
23
|
+
autoSave: true,
|
|
24
|
+
compactionEnabled: true,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
async initialize() {
|
|
28
|
+
await this.sessions.initialize();
|
|
29
|
+
}
|
|
30
|
+
getSessionManager() {
|
|
31
|
+
return this.sessions;
|
|
32
|
+
}
|
|
33
|
+
destroy() {
|
|
34
|
+
this.sessions.destroy();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Routes incoming messages to the appropriate agent instance
|
|
39
|
+
* based on channel type, sender ID, and channel ID patterns.
|
|
40
|
+
*/
|
|
41
|
+
export class AgentRouter {
|
|
42
|
+
agents = new Map();
|
|
43
|
+
rules;
|
|
44
|
+
defaultAgentId;
|
|
45
|
+
constructor(config) {
|
|
46
|
+
this.defaultAgentId = config.defaultAgentId;
|
|
47
|
+
// Sort rules by priority (descending)
|
|
48
|
+
this.rules = [...config.rules].sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Register an agent instance. Call after constructing.
|
|
52
|
+
*/
|
|
53
|
+
registerAgent(agent) {
|
|
54
|
+
this.agents.set(agent.id, agent);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Remove an agent instance.
|
|
58
|
+
*/
|
|
59
|
+
removeAgent(id) {
|
|
60
|
+
const agent = this.agents.get(id);
|
|
61
|
+
if (agent) {
|
|
62
|
+
agent.destroy();
|
|
63
|
+
this.agents.delete(id);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Route an incoming message context to an agent instance.
|
|
68
|
+
*/
|
|
69
|
+
route(context) {
|
|
70
|
+
for (const rule of this.rules) {
|
|
71
|
+
if (this.matchesRule(rule, context)) {
|
|
72
|
+
return this.agents.get(rule.agentId);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return this.agents.get(this.defaultAgentId);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Get an agent by ID.
|
|
79
|
+
*/
|
|
80
|
+
getAgent(id) {
|
|
81
|
+
return this.agents.get(id);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* List all registered agents.
|
|
85
|
+
*/
|
|
86
|
+
listAgents() {
|
|
87
|
+
return Array.from(this.agents.values());
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Destroy all agents and clean up.
|
|
91
|
+
*/
|
|
92
|
+
destroy() {
|
|
93
|
+
for (const agent of this.agents.values()) {
|
|
94
|
+
agent.destroy();
|
|
95
|
+
}
|
|
96
|
+
this.agents.clear();
|
|
97
|
+
}
|
|
98
|
+
matchesRule(rule, context) {
|
|
99
|
+
if (rule.channelType && rule.channelType !== context.channelType) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
if (rule.senderId && !this.matchPattern(rule.senderId, context.senderId ?? '')) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
if (rule.channelId && !this.matchPattern(rule.channelId, context.channelId ?? '')) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
// At least one field must be specified in the rule
|
|
109
|
+
if (!rule.channelType && !rule.senderId && !rule.channelId) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Simple glob matching: supports * as wildcard for any sequence of characters.
|
|
116
|
+
*/
|
|
117
|
+
matchPattern(pattern, value) {
|
|
118
|
+
if (pattern === '*')
|
|
119
|
+
return true;
|
|
120
|
+
if (!pattern.includes('*'))
|
|
121
|
+
return pattern === value;
|
|
122
|
+
// Convert glob to regex
|
|
123
|
+
const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, '\\$&');
|
|
124
|
+
const regexStr = '^' + escaped.replace(/\*/g, '.*') + '$';
|
|
125
|
+
return new RegExp(regexStr).test(value);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=agent-router.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-router.js","sourceRoot":"","sources":["../src/agent-router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AA8CnD;;GAEG;AACH,MAAM,OAAO,aAAa;IACf,EAAE,CAAS;IACX,IAAI,CAAS;IACb,YAAY,CAAS;IACrB,aAA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,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1C,CAAC;CACF"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Block types for structured streaming responses.
|
|
3
|
+
*/
|
|
4
|
+
export type BlockType = 'text' | 'code' | 'tool_use' | 'tool_result' | 'thinking';
|
|
5
|
+
/**
|
|
6
|
+
* A content block within a streamed response.
|
|
7
|
+
*/
|
|
8
|
+
export interface ContentBlock {
|
|
9
|
+
id: string;
|
|
10
|
+
type: BlockType;
|
|
11
|
+
/** Incremental content accumulated so far */
|
|
12
|
+
content: string;
|
|
13
|
+
/** Whether this block is complete */
|
|
14
|
+
complete: boolean;
|
|
15
|
+
/** Additional metadata (e.g. tool name, language) */
|
|
16
|
+
metadata?: Record<string, unknown>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Events emitted by the block streamer to the WebSocket client.
|
|
20
|
+
*/
|
|
21
|
+
export type BlockStreamEvent = {
|
|
22
|
+
event: 'block_start';
|
|
23
|
+
block: ContentBlock;
|
|
24
|
+
} | {
|
|
25
|
+
event: 'block_delta';
|
|
26
|
+
blockId: string;
|
|
27
|
+
delta: string;
|
|
28
|
+
} | {
|
|
29
|
+
event: 'block_stop';
|
|
30
|
+
blockId: string;
|
|
31
|
+
} | {
|
|
32
|
+
event: 'stream_end';
|
|
33
|
+
blocks: ContentBlock[];
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Callback for sending block stream events to a client.
|
|
37
|
+
*/
|
|
38
|
+
export type BlockStreamSender = (event: BlockStreamEvent) => void;
|
|
39
|
+
/** Reset the block counter (for testing). */
|
|
40
|
+
export declare function resetBlockCounter(): void;
|
|
41
|
+
/**
|
|
42
|
+
* Manages a set of content blocks during a streaming response.
|
|
43
|
+
* Tracks incremental deltas and emits structured events.
|
|
44
|
+
*/
|
|
45
|
+
export declare class BlockStream {
|
|
46
|
+
private blocks;
|
|
47
|
+
private blockOrder;
|
|
48
|
+
private sender;
|
|
49
|
+
constructor(sender: BlockStreamSender);
|
|
50
|
+
/**
|
|
51
|
+
* Start a new content block and emit block_start.
|
|
52
|
+
*/
|
|
53
|
+
startBlock(type: BlockType, metadata?: Record<string, unknown>): string;
|
|
54
|
+
/**
|
|
55
|
+
* Append content to an existing block and emit block_delta.
|
|
56
|
+
*/
|
|
57
|
+
appendDelta(blockId: string, delta: string): void;
|
|
58
|
+
/**
|
|
59
|
+
* Mark a block as complete and emit block_stop.
|
|
60
|
+
*/
|
|
61
|
+
stopBlock(blockId: string): void;
|
|
62
|
+
/**
|
|
63
|
+
* End the entire stream and emit stream_end with all blocks.
|
|
64
|
+
*/
|
|
65
|
+
end(): ContentBlock[];
|
|
66
|
+
/**
|
|
67
|
+
* Get all blocks in order.
|
|
68
|
+
*/
|
|
69
|
+
getBlocks(): ContentBlock[];
|
|
70
|
+
/**
|
|
71
|
+
* Get a single block by ID.
|
|
72
|
+
*/
|
|
73
|
+
getBlock(blockId: string): ContentBlock | undefined;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* A tool output streamer that emits partial results as the tool runs.
|
|
77
|
+
* Wraps a callback-based pattern for progressive output.
|
|
78
|
+
*/
|
|
79
|
+
export declare class ToolOutputStream {
|
|
80
|
+
private blockStream;
|
|
81
|
+
private blockId;
|
|
82
|
+
private toolName;
|
|
83
|
+
constructor(blockStream: BlockStream, toolName: string, toolInput?: Record<string, unknown>);
|
|
84
|
+
/**
|
|
85
|
+
* Write partial output from the tool.
|
|
86
|
+
*/
|
|
87
|
+
write(data: string): void;
|
|
88
|
+
/**
|
|
89
|
+
* Complete the tool output stream.
|
|
90
|
+
*/
|
|
91
|
+
end(finalData?: string): void;
|
|
92
|
+
getBlockId(): string;
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=block-stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"block-stream.d.ts","sourceRoot":"","sources":["../src/block-stream.ts"],"names":[],"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KAAK,EAAE,MAAM,GAAG,IAAI;IAQjD;;OAEG;IACH,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAQhC;;OAEG;IACH,GAAG,IAAI,YAAY,EAAE;IAYrB;;OAEG;IACH,SAAS,IAAI,YAAY,EAAE;IAI3B;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;CAIpD;AAED;;;GAGG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,WAAW,CAAc;IACjC,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,QAAQ,CAAS;gBAEb,WAAW,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAS3F;;OAEG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAIzB;;OAEG;IACH,GAAG,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI;IAO7B,UAAU,IAAI,MAAM;CAGrB"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
let blockCounter = 0;
|
|
2
|
+
function nextBlockId() {
|
|
3
|
+
return `block_${++blockCounter}`;
|
|
4
|
+
}
|
|
5
|
+
/** Reset the block counter (for testing). */
|
|
6
|
+
export function resetBlockCounter() {
|
|
7
|
+
blockCounter = 0;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Manages a set of content blocks during a streaming response.
|
|
11
|
+
* Tracks incremental deltas and emits structured events.
|
|
12
|
+
*/
|
|
13
|
+
export class BlockStream {
|
|
14
|
+
blocks = new Map();
|
|
15
|
+
blockOrder = [];
|
|
16
|
+
sender;
|
|
17
|
+
constructor(sender) {
|
|
18
|
+
this.sender = sender;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Start a new content block and emit block_start.
|
|
22
|
+
*/
|
|
23
|
+
startBlock(type, metadata) {
|
|
24
|
+
const id = nextBlockId();
|
|
25
|
+
const block = {
|
|
26
|
+
id,
|
|
27
|
+
type,
|
|
28
|
+
content: '',
|
|
29
|
+
complete: false,
|
|
30
|
+
metadata,
|
|
31
|
+
};
|
|
32
|
+
this.blocks.set(id, block);
|
|
33
|
+
this.blockOrder.push(id);
|
|
34
|
+
this.sender({ event: 'block_start', block: { ...block } });
|
|
35
|
+
return id;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Append content to an existing block and emit block_delta.
|
|
39
|
+
*/
|
|
40
|
+
appendDelta(blockId, delta) {
|
|
41
|
+
const block = this.blocks.get(blockId);
|
|
42
|
+
if (!block || block.complete)
|
|
43
|
+
return;
|
|
44
|
+
block.content += delta;
|
|
45
|
+
this.sender({ event: 'block_delta', blockId, delta });
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Mark a block as complete and emit block_stop.
|
|
49
|
+
*/
|
|
50
|
+
stopBlock(blockId) {
|
|
51
|
+
const block = this.blocks.get(blockId);
|
|
52
|
+
if (!block || block.complete)
|
|
53
|
+
return;
|
|
54
|
+
block.complete = true;
|
|
55
|
+
this.sender({ event: 'block_stop', blockId });
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* End the entire stream and emit stream_end with all blocks.
|
|
59
|
+
*/
|
|
60
|
+
end() {
|
|
61
|
+
// Auto-complete any incomplete blocks
|
|
62
|
+
for (const block of this.blocks.values()) {
|
|
63
|
+
if (!block.complete) {
|
|
64
|
+
block.complete = true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const allBlocks = this.blockOrder.map(id => ({ ...this.blocks.get(id) }));
|
|
68
|
+
this.sender({ event: 'stream_end', blocks: allBlocks });
|
|
69
|
+
return allBlocks;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Get all blocks in order.
|
|
73
|
+
*/
|
|
74
|
+
getBlocks() {
|
|
75
|
+
return this.blockOrder.map(id => ({ ...this.blocks.get(id) }));
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Get a single block by ID.
|
|
79
|
+
*/
|
|
80
|
+
getBlock(blockId) {
|
|
81
|
+
const block = this.blocks.get(blockId);
|
|
82
|
+
return block ? { ...block } : undefined;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* A tool output streamer that emits partial results as the tool runs.
|
|
87
|
+
* Wraps a callback-based pattern for progressive output.
|
|
88
|
+
*/
|
|
89
|
+
export class ToolOutputStream {
|
|
90
|
+
blockStream;
|
|
91
|
+
blockId;
|
|
92
|
+
toolName;
|
|
93
|
+
constructor(blockStream, toolName, toolInput) {
|
|
94
|
+
this.blockStream = blockStream;
|
|
95
|
+
this.toolName = toolName;
|
|
96
|
+
this.blockId = blockStream.startBlock('tool_result', {
|
|
97
|
+
toolName,
|
|
98
|
+
toolInput,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Write partial output from the tool.
|
|
103
|
+
*/
|
|
104
|
+
write(data) {
|
|
105
|
+
this.blockStream.appendDelta(this.blockId, data);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Complete the tool output stream.
|
|
109
|
+
*/
|
|
110
|
+
end(finalData) {
|
|
111
|
+
if (finalData) {
|
|
112
|
+
this.blockStream.appendDelta(this.blockId, finalData);
|
|
113
|
+
}
|
|
114
|
+
this.blockStream.stopBlock(this.blockId);
|
|
115
|
+
}
|
|
116
|
+
getBlockId() {
|
|
117
|
+
return this.blockId;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=block-stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"block-stream.js","sourceRoot":"","sources":["../src/block-stream.ts"],"names":[],"mappings":"AAiCA,IAAI,YAAY,GAAG,CAAC,CAAC;AAErB,SAAS,WAAW;IAClB,OAAO,SAAS,EAAE,YAAY,EAAE,CAAC;AACnC,CAAC;AAED,6CAA6C;AAC7C,MA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